From d8a587e16e3b45cb99abf289a6c7ec021c0b62c4 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 26 Aug 2019 21:40:16 +0200 Subject: [PATCH] [packagechooser] Scale screenshot - if the screenshot is too large, scale it down - (doesn't react to window resizes though) --- .../packagechooser/PackageChooserPage.cpp | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/modules/packagechooser/PackageChooserPage.cpp b/src/modules/packagechooser/PackageChooserPage.cpp index 6f565c914..f0a6579b4 100644 --- a/src/modules/packagechooser/PackageChooserPage.cpp +++ b/src/modules/packagechooser/PackageChooserPage.cpp @@ -52,6 +52,37 @@ PackageChooserPage::PackageChooserPage( PackageChooserMode mode, QWidget* parent } } +/** @brief size the given @p pixmap to @p size + * + * This is "smart" in the sense that it tries to keep the image un-scaled + * (if it's just a little too big) and otherwise scales as needed. + * + * Returns a copy if any modifications are done. + */ +static QPixmap +smartClip( const QPixmap& pixmap, QSize size ) +{ + auto pixSize = pixmap.size(); + if ( ( pixSize.width() <= size.width() ) && ( pixSize.height() <= size.height() ) ) + { + return pixmap; + } + + // only slightly bigger? Trim the edges + constexpr int margin = 16; + if ( ( pixSize.width() <= size.width() + margin ) && ( pixSize.height() <= size.height() + margin ) ) + { + int x = pixSize.width() <= size.width() ? 0 : ( pixSize.width() - size.width() / 2 ); + int new_width = pixSize.width() <= size.width() ? pixSize.width() : size.width(); + int y = pixSize.height() <= size.height() ? 0 : ( pixSize.height() - size.height() / 2 ); + int new_height = pixSize.height() <= size.height() ? pixSize.height() : size.height(); + + return pixmap.copy( x, y, new_width, new_height ); + } + + return pixmap.scaled( size, Qt::KeepAspectRatio ); +} + void PackageChooserPage::currentChanged( const QModelIndex& index ) { @@ -66,8 +97,17 @@ PackageChooserPage::currentChanged( const QModelIndex& index ) const auto* model = ui->products->model(); ui->productName->setText( model->data( index, PackageListModel::NameRole ).toString() ); - ui->productScreenshot->setPixmap( model->data( index, PackageListModel::ScreenshotRole ).value< QPixmap >() ); ui->productDescription->setText( model->data( index, PackageListModel::DescriptionRole ).toString() ); + + QPixmap currentScreenshot = model->data( index, PackageListModel::ScreenshotRole ).value< QPixmap >(); + if ( currentScreenshot.isNull() ) + { + ui->productScreenshot->setPixmap( m_introduction.screenshot ); + } + else + { + ui->productScreenshot->setPixmap( smartClip( currentScreenshot, ui->productScreenshot->size() ) ); + } } }