[packagechooser] Scale screenshot

- if the screenshot is too large, scale it down
 - (doesn't react to window resizes though)
This commit is contained in:
Adriaan de Groot 2019-08-26 21:40:16 +02:00
parent 6035a74a93
commit d8a587e16e

View File

@ -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() ) );
}
}
}