From df634573bf0bad6834eb09c2693efbb9e7f76adc Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 8 Jun 2021 14:09:31 +0200 Subject: [PATCH] [partition] Resize combo box to show whole pop-up The (collapsed) combo box should be wide enough to show the entire pop-up (expanded) box data. FIXES #1700 --- src/modules/partition/gui/ChoicePage.cpp | 26 ++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/modules/partition/gui/ChoicePage.cpp b/src/modules/partition/gui/ChoicePage.cpp index 093f74b39..245ee0b92 100644 --- a/src/modules/partition/gui/ChoicePage.cpp +++ b/src/modules/partition/gui/ChoicePage.cpp @@ -134,6 +134,28 @@ ChoicePage::ChoicePage( Config* config, QWidget* parent ) ChoicePage::~ChoicePage() {} +/** @brief Sets the @p model for the given @p box and adjusts UI sizes to match. + * + * The model provides data for drawing the items in the model; the + * drawing itself is done by the delegate, which may end up drawing a + * different width in the popup than in the collapsed combo box. + * + * Make the box wide enough to accomodate the whole expanded delegate; + * this avoids cases where the popup would truncate data being drawn + * because the overall box is sized too narrow. + */ +void setModelToComboBox( QComboBox* box, QAbstractItemModel* model ) +{ + box->setModel( model ); + if ( model->rowCount() > 0 ) + { + QStyleOptionViewItem options; + options.initFrom( box ); + auto delegateSize = box->itemDelegate()->sizeHint(options, model->index(0, 0) ); + box->setMinimumWidth( delegateSize.width() ); + } +} + void ChoicePage::init( PartitionCoreModule* core ) { @@ -145,10 +167,10 @@ ChoicePage::init( PartitionCoreModule* core ) // We need to do this because a PCM revert invalidates the deviceModel. connect( core, &PartitionCoreModule::reverted, this, [=] { - m_drivesCombo->setModel( core->deviceModel() ); + setModelToComboBox( m_drivesCombo, core->deviceModel() ); m_drivesCombo->setCurrentIndex( m_lastSelectedDeviceIndex ); } ); - m_drivesCombo->setModel( core->deviceModel() ); + setModelToComboBox( m_drivesCombo, core->deviceModel() ); connect( m_drivesCombo, static_cast< void ( QComboBox::* )( int ) >( &QComboBox::currentIndexChanged ),