[packagechooser] Replace smartClip() with just-do-it

QLabel allows scaling of the Pixmap by itself, and we have a
FixedAspectRatioLabel that scales a pixmap nicely. Use that.
(The new label type needed to be introduced to designer)

The screenshot should expand more agressively, so that it
does not get margins -- that just leave space around the
name and description -- when the window expands. Adjust some
of the stretching and layout in the UI file.
This commit is contained in:
Adriaan de Groot 2021-10-29 12:07:18 +02:00
parent 65f5db29bb
commit 9e1b8622f2
2 changed files with 21 additions and 39 deletions

View File

@ -46,44 +46,13 @@ PackageChooserPage::PackageChooserPage( PackageChooserMode mode, QWidget* parent
ui->products->setMinimumWidth( 10 * CalamaresUtils::defaultFontHeight() );
}
/** @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, Qt::SmoothTransformation );
}
void
PackageChooserPage::currentChanged( const QModelIndex& index )
{
if ( !index.isValid() || !ui->products->selectionModel()->hasSelection() )
{
ui->productName->setText( m_introduction.name.get() );
ui->productScreenshot->setPixmap( smartClip( m_introduction.screenshot, ui->productScreenshot->size() ) );
ui->productScreenshot->setPixmap( m_introduction.screenshot );
ui->productDescription->setText( m_introduction.description.get() );
}
else
@ -96,11 +65,11 @@ PackageChooserPage::currentChanged( const QModelIndex& index )
QPixmap currentScreenshot = model->data( index, PackageListModel::ScreenshotRole ).value< QPixmap >();
if ( currentScreenshot.isNull() )
{
ui->productScreenshot->setPixmap( smartClip( m_introduction.screenshot, ui->productScreenshot->size() ) );
ui->productScreenshot->setPixmap( m_introduction.screenshot );
}
else
{
ui->productScreenshot->setPixmap( smartClip( currentScreenshot, ui->productScreenshot->size() ) );
ui->productScreenshot->setPixmap( currentScreenshot );
}
}
}

View File

@ -37,20 +37,26 @@ SPDX-License-Identifier: GPL-3.0-or-later
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,3,1">
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,30,1">
<item>
<widget class="QLabel" name="productName">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Product Name</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="productScreenshot">
<widget class="FixedAspectRatioLabel" name="productScreenshot">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="text">
@ -64,9 +70,9 @@ SPDX-License-Identifier: GPL-3.0-or-later
<item>
<widget class="QLabel" name="productDescription">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>2</verstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
@ -86,6 +92,13 @@ SPDX-License-Identifier: GPL-3.0-or-later
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>FixedAspectRatioLabel</class>
<extends>QLabel</extends>
<header>widgets/FixedAspectRatioLabel.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>