diff options
author | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2020-09-10 17:20:21 +0300 |
---|---|---|
committer | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2020-09-16 09:45:13 +0300 |
commit | 913c7ea16f961a8f5688734cd12d00fbeaa5d48c (patch) | |
tree | 8cd9c58233ea51ffddbcfc2f879f78a6da17cb15 | |
parent | 244b3c4e92f0909085d5068d5a8c04137068c06b (diff) |
Fix image aspect ratio not preserved when resizing window
As this is not currently possible using only QLabel, subclass it
with a new AspectRatioLabel class to provide the functionality.
This should be usable also on other places where we need scaling
with aspect ratio maintaining.
Task-number: QTIFW-1944
Change-Id: I32147b2b487573bb054fcf66b09eb3f8a0350ccd
Reviewed-by: Katja Marttila <katja.marttila@qt.io>
-rw-r--r-- | src/libs/installer/aspectratiolabel.cpp | 98 | ||||
-rw-r--r-- | src/libs/installer/aspectratiolabel.h | 63 | ||||
-rw-r--r-- | src/libs/installer/installer.pro | 2 | ||||
-rw-r--r-- | src/libs/installer/performinstallationform.cpp | 5 | ||||
-rw-r--r-- | src/libs/installer/performinstallationform.h | 4 |
5 files changed, 168 insertions, 4 deletions
diff --git a/src/libs/installer/aspectratiolabel.cpp b/src/libs/installer/aspectratiolabel.cpp new file mode 100644 index 000000000..a9af93a55 --- /dev/null +++ b/src/libs/installer/aspectratiolabel.cpp @@ -0,0 +1,98 @@ +/************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +**************************************************************************/ + +#include "aspectratiolabel.h" + +using namespace QInstaller; + +/*! + \class QInstaller::AspectRatioLabel + \inmodule QtInstallerFramework + \brief The AspectRatioLabel class provides a label for displaying + a pixmap that maintains its original aspect ratio when resized. +*/ + +/*! + Constructs the label with \a parent as parent. +*/ +AspectRatioLabel::AspectRatioLabel(QWidget *parent) + : QLabel(parent) +{ + setMinimumSize(1, 1); + setScaledContents(false); +} + +/*! + Sets the \a pixmap shown on the label. Setting a new pixmap clears the previous content. +*/ +void AspectRatioLabel::setPixmap(const QPixmap &pixmap) +{ + m_pixmap = pixmap; + QLabel::setPixmap(scaledPixmap()); +} + +/*! + \reimp +*/ +int AspectRatioLabel::heightForWidth(int w) const +{ + return m_pixmap.isNull() + ? height() + : m_pixmap.height() * w / m_pixmap.width(); +} + +/*! + \reimp +*/ +QSize AspectRatioLabel::sizeHint() const +{ + const int w = width(); + return QSize(w, heightForWidth(w)); +} + +/*! + Returns the currently set pixmap scaled to the label size, + while preserving the original aspect ratio. +*/ +QPixmap AspectRatioLabel::scaledPixmap() const +{ + return m_pixmap.isNull() + ? QPixmap() + : m_pixmap.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); +} + +/*! + \reimp +*/ +void AspectRatioLabel::resizeEvent(QResizeEvent *event) +{ + Q_UNUSED(event); + + if (!m_pixmap.isNull()) + QLabel::setPixmap(scaledPixmap()); +} diff --git a/src/libs/installer/aspectratiolabel.h b/src/libs/installer/aspectratiolabel.h new file mode 100644 index 000000000..28fc5aea4 --- /dev/null +++ b/src/libs/installer/aspectratiolabel.h @@ -0,0 +1,63 @@ +/************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +**************************************************************************/ + +#ifndef ASPECTRATIOLABEL_H +#define ASPECTRATIOLABEL_H + +#include "installer_global.h" + +#include <QLabel> +#include <QPixmap> +#include <QResizeEvent> + +namespace QInstaller { + +class INSTALLER_EXPORT AspectRatioLabel : public QLabel +{ + Q_OBJECT + +public: + explicit AspectRatioLabel(QWidget *parent = nullptr); + + int heightForWidth(int w) const; + QSize sizeHint() const; + +public slots: + void setPixmap (const QPixmap &pixmap); + void resizeEvent(QResizeEvent *event); + +private: + QPixmap scaledPixmap() const; + +private: + QPixmap m_pixmap; +}; + +} // namespace QInstaller + +#endif // ASPECTRATIOLABEL_H diff --git a/src/libs/installer/installer.pro b/src/libs/installer/installer.pro index 2cfb079f5..225b1c8d1 100644 --- a/src/libs/installer/installer.pro +++ b/src/libs/installer/installer.pro @@ -42,6 +42,7 @@ QT += \ win32:QT += winextras HEADERS += packagemanagercore.h \ + aspectratiolabel.h \ packagemanagercore_p.h \ packagemanagergui.h \ binaryformat.h \ @@ -139,6 +140,7 @@ HEADERS += packagemanagercore.h \ commandlineparser_p.h SOURCES += packagemanagercore.cpp \ + aspectratiolabel.cpp \ packagemanagercore_p.cpp \ packagemanagergui.cpp \ binaryformat.cpp \ diff --git a/src/libs/installer/performinstallationform.cpp b/src/libs/installer/performinstallationform.cpp index 2c5edf70f..28e2e1f45 100644 --- a/src/libs/installer/performinstallationform.cpp +++ b/src/libs/installer/performinstallationform.cpp @@ -139,11 +139,10 @@ void PerformInstallationForm::setupUi(QWidget *widget) m_productImagesScrollArea->setObjectName(QLatin1String("ProductImagesScrollArea")); m_productImagesScrollArea->setWidgetResizable(true); m_productImagesScrollArea->setFrameShape(QFrame::NoFrame); + m_productImagesScrollArea->setStyleSheet(QLatin1String("background-color:transparent;")); - m_productImagesLabel = new QLabel(widget); + m_productImagesLabel = new AspectRatioLabel(widget); m_productImagesLabel->setObjectName(QLatin1String("ProductImagesLabel")); - m_productImagesLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); - m_productImagesLabel->setScaledContents(true); m_productImagesScrollArea->setWidget(m_productImagesLabel); bottomLayout->addWidget(m_productImagesScrollArea); diff --git a/src/libs/installer/performinstallationform.h b/src/libs/installer/performinstallationform.h index db0a35ee8..20b193857 100644 --- a/src/libs/installer/performinstallationform.h +++ b/src/libs/installer/performinstallationform.h @@ -29,6 +29,8 @@ #ifndef PERFORMINSTALLATIONFORM_H #define PERFORMINSTALLATIONFORM_H +#include "aspectratiolabel.h" + #include <QObject> QT_BEGIN_NAMESPACE @@ -77,7 +79,7 @@ private: QLabel *m_progressLabel; QLabel *m_downloadStatus; QScrollArea *m_productImagesScrollArea; - QLabel *m_productImagesLabel; + AspectRatioLabel *m_productImagesLabel; QPushButton *m_detailsButton; LazyPlainTextEdit *m_detailsBrowser; QTimer *m_updateTimer; |