From 45f5a502c1344cba2a3b90954d55d4ed37b3a619 Mon Sep 17 00:00:00 2001 From: Arno Date: Sun, 24 Nov 2019 05:12:38 +0100 Subject: Fix scaling images in viewer Don't resize the image unconditionally. When doing that while keeping the aspect ratio, it may grow wider than the window/widget size. So check if it fits. If it doesn't scale down, otherwise leave it. --- viewer.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'viewer.cpp') diff --git a/viewer.cpp b/viewer.cpp index d0077f7..6a4f634 100644 --- a/viewer.cpp +++ b/viewer.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -11,14 +10,10 @@ #include "viewer.h" Viewer::Viewer(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f), mCurIndex(-1) { - QDesktopWidget *dw = qApp->desktop(); setAutoFillBackground(true); setBackgroundRole(QPalette::AlternateBase); mLabel = new QLabel; mLabel->setAlignment(Qt::AlignCenter); - mMaxSize = dw->size(); - //90 is an arbitrary value, but it works :) - mMaxSize.setHeight(mMaxSize.height() - 90); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(mLabel); setLayout(mainLayout); @@ -97,7 +92,9 @@ void Viewer::displayFile(int index){ setBackgroundRole(QPalette::AlternateBase); } QPixmap pm(mFiles.at(mCurIndex)); - pm = pm.scaled(mMaxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); + if((pm.height() > height()) || (pm.width() > width())){ + pm = pm.scaled(QSize(width() - 20, height() - 20), Qt::KeepAspectRatio, Qt::SmoothTransformation); + } mLabel->setPixmap(pm); QString winTitle = QString(tr("%1 Viewer [%2]")).arg(qApp->applicationName()).arg(mFiles.at(mCurIndex)); setWindowTitle(winTitle); -- cgit v1.2.3-70-g09d2