From 138b8f188e09898bafed58cd05220c4406c72e92 Mon Sep 17 00:00:00 2001 From: "J.D. Purcell" Date: Sat, 7 Oct 2023 13:38:56 -0400 Subject: [PATCH] Testing --- src/mainwindow.cpp | 24 ++++++------ src/qvgraphicsview.cpp | 83 +++++++++++++++++++++--------------------- src/qvgraphicsview.h | 26 ++++++------- src/qvnamespace.h | 6 +++ 4 files changed, 73 insertions(+), 66 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 20bbdb12..15e4326e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -411,7 +411,7 @@ void MainWindow::zoomLevelChanged() void MainWindow::syncZoomToFitChecked() { const auto actions = qvApp->getActionManager().getAllClonesOfAction("zoomtofit", this); - const bool value = graphicsView->getZoomToFitEnabled(); + const bool value = graphicsView->getCalculatedZoomMode() == Qv::CalculatedZoomMode::ZoomToFit; for (const auto &action : actions) action->setChecked(value); } @@ -419,7 +419,7 @@ void MainWindow::syncZoomToFitChecked() void MainWindow::syncNavigationResetsZoomChecked() { const auto actions = qvApp->getActionManager().getAllClonesOfAction("navigationresetszoom", this); - const bool value = graphicsView->getNavigationResetsZoomEnabled(); + const bool value = graphicsView->getNavigationResetsZoom(); for (const auto &action : actions) action->setChecked(value); } @@ -1168,12 +1168,12 @@ void MainWindow::zoomOut() void MainWindow::setZoomToFitEnabled(bool value) { - graphicsView->setZoomToFitEnabled(value); + graphicsView->setCalculatedZoomMode(value ? Qv::CalculatedZoomMode::ZoomToFit : std::optional()); } void MainWindow::setNavigationResetsZoomEnabled(bool value) { - graphicsView->setNavigationResetsZoomEnabled(value); + graphicsView->setNavigationResetsZoom(value); } void MainWindow::originalSize() @@ -1184,25 +1184,25 @@ void MainWindow::originalSize() void MainWindow::rotateRight() { graphicsView->rotateImage(90); - graphicsView->zoomToFit(); + graphicsView->fitOrConstrainImage(); } void MainWindow::rotateLeft() { graphicsView->rotateImage(-90); - graphicsView->zoomToFit(); + graphicsView->fitOrConstrainImage(); } void MainWindow::mirror() { graphicsView->mirrorImage(); - graphicsView->zoomToFit(); + graphicsView->fitOrConstrainImage(); } void MainWindow::flip() { graphicsView->flipImage(); - graphicsView->zoomToFit(); + graphicsView->fitOrConstrainImage(); } void MainWindow::firstFile() @@ -1249,12 +1249,12 @@ void MainWindow::saveFrameAs() saveDialog->setAcceptMode(QFileDialog::AcceptSave); saveDialog->open(); connect(saveDialog, &QFileDialog::fileSelected, this, [=](const QString &fileName){ - graphicsView->originalSize(); - for(int i=0; i < graphicsView->getLoadedMovie().frameCount(); i++) - nextFrame(); + //graphicsView->originalSize(); + //for(int i=0; i < graphicsView->getLoadedMovie().frameCount(); i++) + // nextFrame(); graphicsView->getLoadedMovie().currentPixmap().save(fileName, nullptr, 100); - graphicsView->zoomToFit(); + //graphicsView->zoomToFit(); }); } diff --git a/src/qvgraphicsview.cpp b/src/qvgraphicsview.cpp index e4b450df..f679b32d 100644 --- a/src/qvgraphicsview.cpp +++ b/src/qvgraphicsview.cpp @@ -282,10 +282,7 @@ void QVGraphicsView::executeClickAction(const Qv::ViewportClickAction action) { if (action == Qv::ViewportClickAction::ZoomToFit) { - if (!getZoomToFitEnabled()) - setZoomToFitEnabled(true); - else - centerImage(); + setCalculatedZoomMode(Qv::CalculatedZoomMode::ZoomToFit); } else if (action == Qv::ViewportClickAction::OriginalSize) { @@ -430,9 +427,9 @@ void QVGraphicsView::postLoad() if (!loadIsFromSessionRestore) { - if (isNavigationResetsZoomEnabled && !isZoomToFitEnabled) - setZoomToFitEnabled(true); - else +// if (navigationResetsZoom && !isZoomToFitEnabled) // calculatedZoomMode != default? +// setZoomToFitEnabled(true); +// else fitOrConstrainImage(); } @@ -445,17 +442,17 @@ void QVGraphicsView::postLoad() loadIsFromSessionRestore = false; } -void QVGraphicsView::zoomIn(const QPoint &pos) +void QVGraphicsView::zoomIn() { - zoomRelative(zoomMultiplier, pos); + zoomRelative(zoomMultiplier); } -void QVGraphicsView::zoomOut(const QPoint &pos) +void QVGraphicsView::zoomOut() { - zoomRelative(qPow(zoomMultiplier, -1), pos); + zoomRelative(qPow(zoomMultiplier, -1)); } -void QVGraphicsView::zoomRelative(qreal relativeLevel, const QPoint &pos) +void QVGraphicsView::zoomRelative(qreal relativeLevel, const std::optional &pos) { const qreal absoluteLevel = zoomLevel * relativeLevel; @@ -465,17 +462,17 @@ void QVGraphicsView::zoomRelative(qreal relativeLevel, const QPoint &pos) zoomAbsolute(absoluteLevel, pos); } -void QVGraphicsView::zoomAbsolute(const qreal absoluteLevel, const QPoint &pos) +void QVGraphicsView::zoomAbsolute(const qreal absoluteLevel, const std::optional &pos, const bool isApplyingCalculation) { - if (!isApplyingZoomToFit) - setZoomToFitEnabled(false); + if (!isApplyingCalculation) + setCalculatedZoomMode({}); if (pos != lastZoomEventPos) { lastZoomEventPos = pos; lastZoomRoundingError = QPointF(); } - const QPointF scenePos = mapToScene(pos) - lastZoomRoundingError; + const QPointF scenePos = pos.has_value() ? mapToScene(pos.value()) - lastZoomRoundingError : QPointF(); if (appliedExpensiveScaleZoomLevel != 0.0) { @@ -492,13 +489,13 @@ void QVGraphicsView::zoomAbsolute(const qreal absoluteLevel, const QPoint &pos) scrollHelper->cancelAnimation(); // If we have a point to zoom towards and cursor zooming is enabled - if (pos != QPoint(-1, -1) && isCursorZoomEnabled) + if (pos.has_value() && isCursorZoomEnabled) { const QPointF p1mouse = mapFromScene(scenePos); - const QPointF move = p1mouse - pos; + const QPointF move = p1mouse - pos.value(); horizontalScrollBar()->setValue(horizontalScrollBar()->value() + (move.x() * (isRightToLeft() ? -1 : 1))); verticalScrollBar()->setValue(verticalScrollBar()->value() + move.y()); - lastZoomRoundingError = mapToScene(pos) - scenePos; + lastZoomRoundingError = mapToScene(pos.value()) - scenePos; constrainBoundsTimer->start(); } else if (!loadIsFromSessionRestore) @@ -510,34 +507,38 @@ void QVGraphicsView::zoomAbsolute(const qreal absoluteLevel, const QPoint &pos) emitZoomLevelChangedTimer->start(); } -bool QVGraphicsView::getZoomToFitEnabled() const +const std::optional &QVGraphicsView::getCalculatedZoomMode() const { - return isZoomToFitEnabled; + return calculatedZoomMode; } -void QVGraphicsView::setZoomToFitEnabled(bool value) +void QVGraphicsView::setCalculatedZoomMode(const std::optional &value) { - if (isZoomToFitEnabled == value) + if (calculatedZoomMode == value) + { + if (calculatedZoomMode.has_value()) + centerImage(); return; + } - isZoomToFitEnabled = value; - if (isZoomToFitEnabled) - zoomToFit(); + calculatedZoomMode = value; + if (calculatedZoomMode.has_value()) + recalculateZoom(); emit zoomToFitChanged(); } -bool QVGraphicsView::getNavigationResetsZoomEnabled() const +bool QVGraphicsView::getNavigationResetsZoom() const { - return isNavigationResetsZoomEnabled; + return navigationResetsZoom; } -void QVGraphicsView::setNavigationResetsZoomEnabled(bool value) +void QVGraphicsView::setNavigationResetsZoom(const bool value) { - if (isNavigationResetsZoomEnabled == value) + if (navigationResetsZoom == value) return; - isNavigationResetsZoomEnabled = value; + navigationResetsZoom = value; emit navigationResetsZoomChanged(); } @@ -601,7 +602,7 @@ void QVGraphicsView::animatedFrameChanged(QRect rect) } } -void QVGraphicsView::zoomToFit() +void QVGraphicsView::recalculateZoom() { if (!getCurrentFileDetails().isPixmapLoaded) return; @@ -634,6 +635,8 @@ void QVGraphicsView::zoomToFit() targetRatio = fitXRatio; break; default: + targetRatio = qMax(fitXRatio, fitYRatio); + break; if ((qRound(effectiveImageSize.height()) == viewSize.height() && qRound(effectiveImageSize.width()) <= viewSize.width()) || (qRound(effectiveImageSize.width()) == viewSize.width() && qRound(effectiveImageSize.height()) <= viewSize.height())) { @@ -658,9 +661,7 @@ void QVGraphicsView::zoomToFit() if (targetRatio > 1.0 && !isPastActualSizeEnabled) targetRatio = 1.0; - isApplyingZoomToFit = true; - zoomAbsolute(targetRatio); - isApplyingZoomToFit = false; + zoomAbsolute(targetRatio, {}, true); } void QVGraphicsView::originalSize() @@ -704,9 +705,9 @@ const QJsonObject QVGraphicsView::getSessionState() const state["vScroll"] = verticalScrollBar()->value(); - state["navResetsZoom"] = isNavigationResetsZoomEnabled; + state["navResetsZoom"] = navigationResetsZoom; - state["zoomToFit"] = isZoomToFitEnabled; + //state["zoomToFit"] = isZoomToFitEnabled; return state; } @@ -730,9 +731,9 @@ void QVGraphicsView::loadSessionState(const QJsonObject &state) verticalScrollBar()->setValue(state["vScroll"].toInt()); - isNavigationResetsZoomEnabled = state["navResetsZoom"].toBool(); + navigationResetsZoom = state["navResetsZoom"].toBool(); - isZoomToFitEnabled = state["zoomToFit"].toBool(); + //isZoomToFitEnabled = state["zoomToFit"].toBool(); emit navigationResetsZoomChanged(); emit zoomToFitChanged(); @@ -848,8 +849,8 @@ void QVGraphicsView::goToFile(const GoToFileMode &mode, int index) void QVGraphicsView::fitOrConstrainImage() { - if (isZoomToFitEnabled) - zoomToFit(); + if (calculatedZoomMode.has_value()) + recalculateZoom(); else scrollHelper->constrain(true); } diff --git a/src/qvgraphicsview.h b/src/qvgraphicsview.h index d35dce4e..11f53212 100644 --- a/src/qvgraphicsview.h +++ b/src/qvgraphicsview.h @@ -5,6 +5,7 @@ #include "qvimagecore.h" #include "axislocker.h" #include "scrollhelper.h" +#include #include #include #include @@ -44,24 +45,24 @@ class QVGraphicsView : public QGraphicsView void reloadFile(); - void zoomIn(const QPoint &pos = QPoint(-1, -1)); + void zoomIn(); - void zoomOut(const QPoint &pos = QPoint(-1, -1)); + void zoomOut(); - void zoomRelative(const qreal relativeLevel, const QPoint &pos = QPoint(-1, -1)); + void zoomRelative(const qreal relativeLevel, const std::optional &pos = {}); - void zoomAbsolute(const qreal absoluteLevel, const QPoint &pos = QPoint(-1, -1)); + void zoomAbsolute(const qreal absoluteLevel, const std::optional &pos = {}, const bool isApplyingCalculation = false); - bool getZoomToFitEnabled() const; - void setZoomToFitEnabled(bool value); + const std::optional &getCalculatedZoomMode() const; + void setCalculatedZoomMode(const std::optional &value); - bool getNavigationResetsZoomEnabled() const; - void setNavigationResetsZoomEnabled(bool value); + bool getNavigationResetsZoom() const; + void setNavigationResetsZoom(const bool value); void applyExpensiveScaling(); void removeExpensiveScaling(); - void zoomToFit(); + void recalculateZoom(); void originalSize(); void centerImage(); @@ -189,14 +190,13 @@ private slots: Qv::ViewportScrollAction altVerticalScrollAction {Qv::ViewportScrollAction::None}; Qv::ViewportScrollAction altHorizontalScrollAction {Qv::ViewportScrollAction::None}; - bool isZoomToFitEnabled {true}; - bool isApplyingZoomToFit {false}; - bool isNavigationResetsZoomEnabled {true}; + std::optional calculatedZoomMode {Qv::CalculatedZoomMode::ZoomToFit}; + bool navigationResetsZoom {true}; bool loadIsFromSessionRestore {false}; qreal zoomLevel {1.0}; qreal appliedDpiAdjustment {1.0}; qreal appliedExpensiveScaleZoomLevel {0.0}; - QPoint lastZoomEventPos {-1, -1}; + std::optional lastZoomEventPos; QPointF lastZoomRoundingError; QVImageCore imageCore {this}; diff --git a/src/qvnamespace.h b/src/qvnamespace.h index b0b06dd8..6d95f3e7 100644 --- a/src/qvnamespace.h +++ b/src/qvnamespace.h @@ -19,6 +19,12 @@ namespace Qv CenterOnScreen = 2 }; + enum class CalculatedZoomMode + { + ZoomToFit = 0, + FillWindow = 1 + }; + enum class ClickOrDrag { Click = 0,