Skip to content

Commit

Permalink
feat: Zoomies
Browse files Browse the repository at this point in the history
  • Loading branch information
JJL772 committed Jul 20, 2022
1 parent e2a7ce8 commit af2a7fd
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ if (BUILD_GUI)

set(VIEWER_SRC
src/gui/main.cpp
src/gui/viewer.cpp)
src/gui/viewer.cpp
res/resource.qrc)

add_executable(vtfview ${VIEWER_SRC})
endif()
Expand All @@ -71,7 +72,7 @@ if (BUILD_GUI)
target_link_libraries(vtfview PRIVATE vtflib libcom fmt::fmt)
target_include_directories(vtfview PRIVATE src external)

find_package(Qt5 REQUIRED COMPONENTS Widgets Core Gui)
target_link_libraries(vtfview PRIVATE Qt5::Widgets Qt5::Core Qt5::Gui)
find_package(Qt5 REQUIRED COMPONENTS Widgets Core Gui Svg)
target_link_libraries(vtfview PRIVATE Qt5::Widgets Qt5::Core Qt5::Gui Qt5::Svg)
target_include_directories(vtfview PRIVATE ${QT_INCLUDE} ${QT_INCLUDE}/QtWidgets ${QT_INCLUDE}/QtGui ${QT_INCLUDE}/QtCore)
endif()
4 changes: 3 additions & 1 deletion res/resource.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="resource">
<qresource prefix="/">
<file>chaos.png</file>
<file>zoom-minus.svg</file>
<file>zoom-plus.svg</file>
</qresource>
</RCC>
1 change: 1 addition & 0 deletions res/zoom-minus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions res/zoom-plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 41 additions & 12 deletions src/gui/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <QStyle>

#include <iostream>
#include <cfloat>

#include "fmt/format.h"

Expand Down Expand Up @@ -109,17 +110,23 @@ void ViewerMainWindow::setup_ui() {
addDockWidget(Qt::RightDockWidgetArea, resDock);

// Main image viewer
auto* imageView = new ImageViewWidget(this);
auto* scroller = new QScrollArea(this);
viewer_ = new ImageViewWidget(this);
scroller->setAlignment(Qt::AlignCenter);

connect(this, &ViewerMainWindow::vtfFileChanged, [imageView](VTFLib::CVTFFile* file) {
imageView->set_vtf(file);
connect(this, &ViewerMainWindow::vtfFileChanged, [this](VTFLib::CVTFFile* file) {
viewer_->set_vtf(file);
});
setCentralWidget(imageView);

scroller->setVisible(true);
scroller->setWidget(viewer_);

setCentralWidget(scroller);

// Viewer settings
auto* viewerDock = new QDockWidget(tr("Viewer Settings"), this);

auto* viewSettings = new ImageSettingsWidget(imageView, this);
auto* viewSettings = new ImageSettingsWidget(viewer_, this);
connect(this, &ViewerMainWindow::vtfFileChanged, viewSettings, &ImageSettingsWidget::set_vtf);
connect(viewSettings, &ImageSettingsWidget::fileModified, this, &ViewerMainWindow::mark_modified);

Expand Down Expand Up @@ -149,8 +156,13 @@ void ViewerMainWindow::setup_menubar() {
toolBar->addAction(style()->standardIcon(QStyle::SP_BrowserReload), "Reload File", [this]() {
this->reload_file();
});

toolBar->addSeparator();
toolBar->addAction(QIcon::fromTheme("zoom-in", QIcon(":/zoom-plus.svg")), "Zoom In", [this]() {
viewer_->zoom(.1f);
});
toolBar->addAction(QIcon::fromTheme("zoom-out", QIcon(":/zoom-minus.svg")), "Zoom Out", [this]() {
viewer_->zoom(-.1f);
});

// File menu
auto* fileMenu = menuBar()->addMenu(tr("File"));
Expand Down Expand Up @@ -431,10 +443,7 @@ void ImageViewWidget::set_vtf(VTFLib::CVTFFile* file) {
if (!file)
return;

// Make the image fit if it doesn't
QSize sz(file->GetWidth(), file->GetHeight());
if (size().width() < sz.width() || size().height() < sz.height())
resize(sz);
update_size();
}

void ImageViewWidget::paintEvent(QPaintEvent* event) {
Expand Down Expand Up @@ -473,8 +482,28 @@ void ImageViewWidget::paintEvent(QPaintEvent* event) {
currentMip_ = mip_;
}

QPoint center = QPoint(width()/2, height()/2) - QPoint(imageWidth/2, imageHeight/2);
painter.drawImage(center + pos_, image_);
QPoint destpt = QPoint(width()/2, height()/2) - QPoint((imageWidth*zoom_)/2, (imageHeight*zoom_)/2) + pos_;
QRect target = QRect(destpt.x(), destpt.y(), image_.width() * zoom_, image_.height() * zoom_);

painter.drawImage(target, image_, QRect(0, 0, image_.width(), image_.height()));
}

void ImageViewWidget::zoom(float amount) {
if (amount == 0)
return; // Skip expensive repaint
zoom_ += amount;
if (zoom_ < 0.1f)
zoom_ = 0.1f;
update_size();
}

void ImageViewWidget::update_size() {
if (!file_)
return;

// Resize widget to be the same size as the image
QSize sz(file_->GetWidth() * zoom_, file_->GetHeight() * zoom_);
resize(sz);
}

//////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions src/gui/viewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class QSpinBox;
class QCheckBox;

namespace vtfview {
class ImageViewWidget;

/**
* Main window container for the VTF viewer
Expand Down Expand Up @@ -60,6 +61,7 @@ namespace vtfview {
VTFLib::CVTFFile* file_ = nullptr;
bool dirty_ = false;
std::string path_;
ImageViewWidget* viewer_ = nullptr;
};

/**
Expand Down Expand Up @@ -102,7 +104,11 @@ namespace vtfview {
void set_face(int f) { face_ = f; repaint(); }
void set_mip(int f) { mip_ = f; repaint(); }

void zoom(float amount);

private:
void update_size();

QImage image_;
void* imgBuf_ = nullptr;
VTFLib::CVTFFile* file_ = nullptr;
Expand Down

0 comments on commit af2a7fd

Please sign in to comment.