commit c58dbdb7
Allow zooming in fullscreen mode
Changed files
| M | imageviewwidget.cpp before |
| M | imageviewwidget.h before |
diff --git a/imageviewwidget.cpp b/imageviewwidget.cpp
index 98b3538..ef7238c 100644
--- a/imageviewwidget.cpp
+++ b/imageviewwidget.cpp
@@ -3,6 +3,7 @@
#include <QPainter>
#include <QKeyEvent>
#include <QImageReader>
+#include <algorithm>
ImageViewWidget::ImageViewWidget(QWidget *parent)
: QWidget(parent)
@@ -22,6 +23,8 @@ void ImageViewWidget::setImage(const QString &path)
else
m_pixmap = QPixmap();
+ m_zoomMode = FitToScreen;
+ m_zoomFactor = 1.0;
update();
}
@@ -31,10 +34,26 @@ void ImageViewWidget::paintEvent(QPaintEvent *)
return;
QPainter painter(this);
- QPixmap scaled = m_pixmap.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
- int x = (width() - scaled.width()) / 2;
- int y = (height() - scaled.height()) / 2;
- painter.drawPixmap(x, y, scaled);
+ QPixmap drawn;
+
+ switch (m_zoomMode) {
+ case FitToScreen:
+ drawn = m_pixmap.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
+ break;
+ case OriginalSize:
+ drawn = m_pixmap;
+ break;
+ case CustomZoom: {
+ QSize target(static_cast<int>(m_pixmap.width() * m_zoomFactor),
+ static_cast<int>(m_pixmap.height() * m_zoomFactor));
+ drawn = m_pixmap.scaled(target, Qt::KeepAspectRatio, Qt::SmoothTransformation);
+ break;
+ }
+ }
+
+ int x = (width() - drawn.width()) / 2;
+ int y = (height() - drawn.height()) / 2;
+ painter.drawPixmap(x, y, drawn);
}
void ImageViewWidget::keyPressEvent(QKeyEvent *event)
@@ -60,5 +79,73 @@ void ImageViewWidget::keyPressEvent(QKeyEvent *event)
return;
}
+ if (event->key() == Qt::Key_Slash)
+ {
+ zoomOriginal();
+ event->accept();
+ return;
+ }
+
+ if (event->key() == Qt::Key_Asterisk)
+ {
+ zoomFitToScreen();
+ event->accept();
+ return;
+ }
+
+ if (event->key() == Qt::Key_Plus)
+ {
+ zoomIn();
+ event->accept();
+ return;
+ }
+
+ if (event->key() == Qt::Key_Minus)
+ {
+ zoomOut();
+ event->accept();
+ return;
+ }
+
QWidget::keyPressEvent(event);
}
+
+void ImageViewWidget::zoomIn()
+{
+ if (m_zoomMode == FitToScreen) {
+ // Calculate the current effective scale so zooming feels continuous
+ double sx = static_cast<double>(width()) / m_pixmap.width();
+ double sy = static_cast<double>(height()) / m_pixmap.height();
+ m_zoomFactor = std::min(sx, sy);
+ }
+ m_zoomFactor *= 1.25;
+ m_zoomMode = CustomZoom;
+ update();
+}
+
+void ImageViewWidget::zoomOut()
+{
+ if (m_zoomMode == FitToScreen) {
+ double sx = static_cast<double>(width()) / m_pixmap.width();
+ double sy = static_cast<double>(height()) / m_pixmap.height();
+ m_zoomFactor = std::min(sx, sy);
+ }
+ m_zoomFactor /= 1.25;
+ if (m_zoomFactor < 0.01)
+ m_zoomFactor = 0.01;
+ m_zoomMode = CustomZoom;
+ update();
+}
+
+void ImageViewWidget::zoomOriginal()
+{
+ m_zoomMode = OriginalSize;
+ m_zoomFactor = 1.0;
+ update();
+}
+
+void ImageViewWidget::zoomFitToScreen()
+{
+ m_zoomMode = FitToScreen;
+ update();
+}
diff --git a/imageviewwidget.h b/imageviewwidget.h
index c6f5d3c..af2935a 100644
--- a/imageviewwidget.h
+++ b/imageviewwidget.h
@@ -10,9 +10,15 @@ class ImageViewWidget : public QWidget
Q_OBJECT
public:
+ enum ZoomMode { FitToScreen, OriginalSize, CustomZoom };
+
explicit ImageViewWidget(QWidget *parent = nullptr);
void setImage(const QString &path);
+ void zoomIn();
+ void zoomOut();
+ void zoomOriginal();
+ void zoomFitToScreen();
signals:
void closeRequested();
@@ -25,4 +31,6 @@ protected:
private:
QPixmap m_pixmap;
+ ZoomMode m_zoomMode = FitToScreen;
+ double m_zoomFactor = 1.0;
};