Skip to content

Commit

Permalink
Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpurcell committed Oct 7, 2023
1 parent 5f6dc8c commit 138b8f1
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 66 deletions.
24 changes: 12 additions & 12 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,15 @@ 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);
}

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);
}
Expand Down Expand Up @@ -1168,12 +1168,12 @@ void MainWindow::zoomOut()

void MainWindow::setZoomToFitEnabled(bool value)
{
graphicsView->setZoomToFitEnabled(value);
graphicsView->setCalculatedZoomMode(value ? Qv::CalculatedZoomMode::ZoomToFit : std::optional<Qv::CalculatedZoomMode>());
}

void MainWindow::setNavigationResetsZoomEnabled(bool value)
{
graphicsView->setNavigationResetsZoomEnabled(value);
graphicsView->setNavigationResetsZoom(value);
}

void MainWindow::originalSize()
Expand All @@ -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()
Expand Down Expand Up @@ -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();
});
}

Expand Down
83 changes: 42 additions & 41 deletions src/qvgraphicsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -430,9 +427,9 @@ void QVGraphicsView::postLoad()

if (!loadIsFromSessionRestore)
{
if (isNavigationResetsZoomEnabled && !isZoomToFitEnabled)
setZoomToFitEnabled(true);
else
// if (navigationResetsZoom && !isZoomToFitEnabled) // calculatedZoomMode != default?
// setZoomToFitEnabled(true);
// else
fitOrConstrainImage();
}

Expand All @@ -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<QPoint> &pos)
{
const qreal absoluteLevel = zoomLevel * relativeLevel;

Expand All @@ -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<QPoint> &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)
{
Expand All @@ -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)
Expand All @@ -510,34 +507,38 @@ void QVGraphicsView::zoomAbsolute(const qreal absoluteLevel, const QPoint &pos)
emitZoomLevelChangedTimer->start();
}

bool QVGraphicsView::getZoomToFitEnabled() const
const std::optional<Qv::CalculatedZoomMode> &QVGraphicsView::getCalculatedZoomMode() const
{
return isZoomToFitEnabled;
return calculatedZoomMode;
}

void QVGraphicsView::setZoomToFitEnabled(bool value)
void QVGraphicsView::setCalculatedZoomMode(const std::optional<Qv::CalculatedZoomMode> &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();
}
Expand Down Expand Up @@ -601,7 +602,7 @@ void QVGraphicsView::animatedFrameChanged(QRect rect)
}
}

void QVGraphicsView::zoomToFit()
void QVGraphicsView::recalculateZoom()
{
if (!getCurrentFileDetails().isPixmapLoaded)
return;
Expand Down Expand Up @@ -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()))
{
Expand All @@ -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()
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
26 changes: 13 additions & 13 deletions src/qvgraphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "qvimagecore.h"
#include "axislocker.h"
#include "scrollhelper.h"
#include <optional>
#include <QGraphicsView>
#include <QImageReader>
#include <QMimeData>
Expand Down Expand Up @@ -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<QPoint> &pos = {});

void zoomAbsolute(const qreal absoluteLevel, const QPoint &pos = QPoint(-1, -1));
void zoomAbsolute(const qreal absoluteLevel, const std::optional<QPoint> &pos = {}, const bool isApplyingCalculation = false);

bool getZoomToFitEnabled() const;
void setZoomToFitEnabled(bool value);
const std::optional<Qv::CalculatedZoomMode> &getCalculatedZoomMode() const;
void setCalculatedZoomMode(const std::optional<Qv::CalculatedZoomMode> &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();
Expand Down Expand Up @@ -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<Qv::CalculatedZoomMode> 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<QPoint> lastZoomEventPos;
QPointF lastZoomRoundingError;

QVImageCore imageCore {this};
Expand Down
6 changes: 6 additions & 0 deletions src/qvnamespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ namespace Qv
CenterOnScreen = 2
};

enum class CalculatedZoomMode
{
ZoomToFit = 0,
FillWindow = 1
};

enum class ClickOrDrag
{
Click = 0,
Expand Down

0 comments on commit 138b8f1

Please sign in to comment.