Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphicsView overhaul #699

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
QCoreApplication::setOrganizationName("qView");
QCoreApplication::setApplicationName("qView");
QCoreApplication::setApplicationVersion(QString::number(VERSION));
Expand Down
57 changes: 31 additions & 26 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ MainWindow::MainWindow(QWidget *parent) :

// Connect graphicsview signals
connect(graphicsView, &QVGraphicsView::fileChanged, this, &MainWindow::fileChanged);
connect(graphicsView, &QVGraphicsView::updatedLoadedPixmapItem, this, &MainWindow::setWindowSize);
connect(graphicsView, &QVGraphicsView::zoomLevelChanged, this, &MainWindow::zoomLevelChanged);
connect(graphicsView, &QVGraphicsView::cancelSlideshow, this, &MainWindow::cancelSlideshow);

// Initialize escape shortcut
Expand Down Expand Up @@ -381,11 +381,17 @@ void MainWindow::fileChanged()
if (info->isVisible())
refreshProperties();
buildWindowTitle();
setWindowSize();

// repaint to handle error message
update();
}

void MainWindow::zoomLevelChanged()
{
buildWindowTitle();
}

void MainWindow::disableActions()
{
const auto &actionLibrary = qvApp->getActionManager().getActionLibrary();
Expand Down Expand Up @@ -490,14 +496,16 @@ void MainWindow::buildWindowTitle()
}
case 2:
{
newString = QString::number(getCurrentFileDetails().loadedIndexInFolder+1);
newString = QString::number(graphicsView->getZoomLevel() * 100.0, 'f', 1) + "%";
newString += " - " + QString::number(getCurrentFileDetails().loadedIndexInFolder+1);
newString += "/" + QString::number(getCurrentFileDetails().folderFileInfoList.count());
newString += " - " + getCurrentFileDetails().fileInfo.fileName();
break;
}
case 3:
{
newString = QString::number(getCurrentFileDetails().loadedIndexInFolder+1);
newString = QString::number(graphicsView->getZoomLevel() * 100.0, 'f', 1) + "%";
newString += " - " + QString::number(getCurrentFileDetails().loadedIndexInFolder+1);
newString += "/" + QString::number(getCurrentFileDetails().folderFileInfoList.count());
newString += " - " + getCurrentFileDetails().fileInfo.fileName();
if (!getCurrentFileDetails().errorData.hasError)
Expand Down Expand Up @@ -546,11 +554,6 @@ void MainWindow::setWindowSize()
qreal minWindowResizedPercentage = qvApp->getSettingsManager().getInteger("minwindowresizedpercentage")/100.0;
qreal maxWindowResizedPercentage = qvApp->getSettingsManager().getInteger("maxwindowresizedpercentage")/100.0;


QSize imageSize = getCurrentFileDetails().loadedPixmapSize;
imageSize -= QSize(4, 4);


// Try to grab the current screen
QScreen *currentScreen = screenContaining(frameGeometry());

Expand All @@ -573,28 +576,30 @@ void MainWindow::setWindowSize()
const QSize hardLimitSize = currentScreen->availableSize() - windowFrameSize - extraWidgetsSize;
const QSize screenSize = currentScreen->size();
const QSize minWindowSize = (screenSize * minWindowResizedPercentage).boundedTo(hardLimitSize);
const QSize maxWindowSize = (screenSize * maxWindowResizedPercentage).boundedTo(hardLimitSize);
const QSize maxWindowSize = (screenSize * qMax(maxWindowResizedPercentage, minWindowResizedPercentage)).boundedTo(hardLimitSize);
const QSizeF imageSize = graphicsView->getEffectiveOriginalSize();
const int fitOverscan = graphicsView->getFitOverscan();
const QSize fitOverscanSize = QSize(fitOverscan * 2, fitOverscan * 2);
const bool enforceMinSizeBothDimensions = false;

if (imageSize.width() < minWindowSize.width() && imageSize.height() < minWindowSize.height())
{
imageSize.scale(minWindowSize, Qt::KeepAspectRatio);
}
else if (imageSize.width() > maxWindowSize.width() || imageSize.height() > maxWindowSize.height())
QSize targetSize = imageSize.toSize() - fitOverscanSize;

const bool limitToMin = targetSize.width() < minWindowSize.width() && targetSize.height() < minWindowSize.height();
const bool limitToMax = targetSize.width() > maxWindowSize.width() || targetSize.height() > maxWindowSize.height();
if (limitToMin || limitToMax)
{
imageSize.scale(maxWindowSize, Qt::KeepAspectRatio);
const QSizeF viewSize = (limitToMin ? minWindowSize : maxWindowSize) + fitOverscanSize;
const qreal fitRatio = qMin(viewSize.width() / imageSize.width(), viewSize.height() / imageSize.height());
targetSize = (imageSize * fitRatio).toSize() - fitOverscanSize;
}

// Windows reports the wrong minimum width, so we constrain the image size relative to the dpi to stop weirdness with tiny images
#ifdef Q_OS_WIN
auto minimumImageSize = QSize(qRound(logicalDpiX()*1.5), logicalDpiY()/2);
if (imageSize.boundedTo(minimumImageSize) == imageSize)
imageSize = minimumImageSize;
#endif
Comment on lines -593 to -598
Copy link
Contributor Author

@jdpurcell jdpurcell Dec 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was ancient code from before qView had configurable min/max window sizes; I think the minimum window size setting makes this obsolete in a lot of cases, plus Qt/Windows seems to enforce an absolute minimum anyway (I tested on the 5.15.2 build too) so this may have been working around a Qt bug that's since resolved.

if (enforceMinSizeBothDimensions)
targetSize = targetSize.expandedTo(minWindowSize);

// Match center after new geometry
// This is smoother than a single geometry set for some reason
QRect oldRect = geometry();
resize(imageSize + extraWidgetsSize);
resize(targetSize + extraWidgetsSize);
QRect newRect = geometry();
newRect.moveCenter(oldRect.center());

Expand Down Expand Up @@ -996,7 +1001,7 @@ void MainWindow::zoomOut()

void MainWindow::resetZoom()
{
graphicsView->resetScale();
graphicsView->zoomToFit();
}

void MainWindow::originalSize()
Expand All @@ -1018,13 +1023,13 @@ void MainWindow::rotateLeft()

void MainWindow::mirror()
{
graphicsView->scale(-1, 1);
graphicsView->mirrorImage();
resetZoom();
}

void MainWindow::flip()
{
graphicsView->scale(1, -1);
graphicsView->flipImage();
resetZoom();
}

Expand Down Expand Up @@ -1072,7 +1077,7 @@ void MainWindow::saveFrameAs()
nextFrame();

graphicsView->getLoadedMovie().currentPixmap().save(fileName, nullptr, 100);
graphicsView->resetScale();
graphicsView->zoomToFit();
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public slots:

void fileChanged();

void zoomLevelChanged();

void disableActions();

protected:
Expand Down
Loading