From 038d04b3da8238cc71914d5851f2586857b8445d Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Fri, 14 May 2021 00:00:03 +0800 Subject: [PATCH] feat: fit by width Signed-off-by: Gary Wang --- app/actionmanager.cpp | 2 ++ app/actionmanager.h | 1 + app/graphicsview.cpp | 25 +++++++++++++++++++++++++ app/graphicsview.h | 1 + app/mainwindow.cpp | 6 ++++++ app/mainwindow.h | 1 + 6 files changed, 36 insertions(+) diff --git a/app/actionmanager.cpp b/app/actionmanager.cpp index f914f77..c876a61 100644 --- a/app/actionmanager.cpp +++ b/app/actionmanager.cpp @@ -24,6 +24,7 @@ void ActionManager::setupAction(MainWindow *mainWindow) CREATE_NEW_ACTION(mainWindow, actionZoomIn); CREATE_NEW_ACTION(mainWindow, actionZoomOut); CREATE_NEW_ACTION(mainWindow, actionHorizontalFlip); + CREATE_NEW_ACTION(mainWindow, actionFitByWidth); CREATE_NEW_ACTION(mainWindow, actionCopyPixmap); CREATE_NEW_ACTION(mainWindow, actionCopyFilePath); CREATE_NEW_ACTION(mainWindow, actionPaste); @@ -47,6 +48,7 @@ void ActionManager::retranslateUi(MainWindow *mainWindow) actionZoomIn->setText(QCoreApplication::translate("MainWindow", "Zoom in", nullptr)); actionZoomOut->setText(QCoreApplication::translate("MainWindow", "Zoom out", nullptr)); actionHorizontalFlip->setText(QCoreApplication::translate("MainWindow", "Flip &Horizontally", nullptr)); + actionFitByWidth->setText("Fit by width"); // TODO: what should it called? actionCopyPixmap->setText(QCoreApplication::translate("MainWindow", "Copy P&ixmap", nullptr)); actionCopyFilePath->setText(QCoreApplication::translate("MainWindow", "Copy &File Path", nullptr)); actionPaste->setText(QCoreApplication::translate("MainWindow", "&Paste", nullptr)); diff --git a/app/actionmanager.h b/app/actionmanager.h index cb31694..50e7978 100644 --- a/app/actionmanager.h +++ b/app/actionmanager.h @@ -19,6 +19,7 @@ public: QAction *actionZoomIn; QAction *actionZoomOut; QAction *actionHorizontalFlip; + QAction *actionFitByWidth; QAction *actionCopyPixmap; QAction *actionCopyFilePath; QAction *actionPaste; diff --git a/app/graphicsview.cpp b/app/graphicsview.cpp index b59a196..3bce171 100644 --- a/app/graphicsview.cpp +++ b/app/graphicsview.cpp @@ -171,6 +171,31 @@ void GraphicsView::fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadio applyTransformationModeByScaleFactor(); } +void GraphicsView::fitByOrientation(Qt::Orientation ori, bool scaleDownOnly) +{ + resetScale(); + + QRectF viewRect = this->viewport()->rect().adjusted(2, 2, -2, -2); + QRectF imageRect = transform().mapRect(sceneRect()); + + qreal ratio; + + if (ori == Qt::Horizontal) { + ratio = viewRect.width() / imageRect.width(); + } else { + ratio = viewRect.height() / imageRect.height(); + } + + if (scaleDownOnly && ratio > 1) ratio = 1; + + scale(ratio, ratio); + centerOn(imageRect.top(), 0); + m_enableFitInView = false; + + applyTransformationModeByScaleFactor(); + emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), transform()); +} + void GraphicsView::checkAndDoFitInView(bool markItOnAnyway) { if (!isThingSmallerThanWindowWith(transform())) { diff --git a/app/graphicsview.h b/app/graphicsview.h index ab324de..b741c4f 100644 --- a/app/graphicsview.h +++ b/app/graphicsview.h @@ -30,6 +30,7 @@ public: void flipView(bool horizontal = true); void resetScale(); void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio); + void fitByOrientation(Qt::Orientation ori = Qt::Horizontal, bool scaleDownOnly = false); void checkAndDoFitInView(bool markItOnAnyway = true); diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index bac7f8b..699f2c8 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -421,6 +421,7 @@ void MainWindow::contextMenuEvent(QContextMenuEvent *event) menu->addSeparator(); menu->addAction(m_am->actionHorizontalFlip); + menu->addAction(m_am->actionFitByWidth); menu->addSeparator(); menu->addAction(stayOnTopMode); @@ -561,6 +562,11 @@ void MainWindow::on_actionHorizontalFlip_triggered() m_graphicsView->flipView(); } +void MainWindow::on_actionFitByWidth_triggered() +{ + m_graphicsView->fitByOrientation(); +} + void MainWindow::on_actionCopyPixmap_triggered() { QClipboard *cb = QApplication::clipboard(); diff --git a/app/mainwindow.h b/app/mainwindow.h index 0eb2a8d..4ab6c65 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -66,6 +66,7 @@ private slots: void on_actionZoomIn_triggered(); void on_actionZoomOut_triggered(); void on_actionHorizontalFlip_triggered(); + void on_actionFitByWidth_triggered(); void on_actionCopyPixmap_triggered(); void on_actionCopyFilePath_triggered(); void on_actionPaste_triggered();