Skip to content

Commit

Permalink
qt: refactor styles combobox and print the default style
Browse files Browse the repository at this point in the history
- Currently style handling is handled in a really fragile
  manner. The order of keys provided by `QStyleFactory`
  is expected to directly match the combobox. At the
  same time, if the style is not overridden, then there
  should be no need to save this in the settings.
- Although the first entry is the default style, it does
  not provide what the style is. Now, the style itself is
  printed as well.
  • Loading branch information
fuzun authored and fkuehne committed Sep 6, 2024
1 parent 1b69314 commit ac0545c
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions modules/gui/qt/dialogs/preferences/simple_preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,7 @@ static int getDefaultAudioVolume(const char *aout)
return -1;
}

namespace
{
#if !defined( _WIN32)
void fillStylesCombo( QComboBox *stylesCombo, const QString &initialStyle)
{
stylesCombo->addItem( qtr("System's default") );
stylesCombo->addItems( QStyleFactory::keys() );
stylesCombo->setCurrentIndex( stylesCombo->findText( initialStyle ) );
stylesCombo->insertSeparator( 1 );
if ( stylesCombo->currentIndex() < 0 )
stylesCombo->setCurrentIndex( 0 ); /* default */
}
#endif

QString getQStyleKey(const QComboBox *stylesCombo, const QString &defaultStyleName)
{
vlc_assert( stylesCombo );
const int index = stylesCombo->currentIndex();
if (stylesCombo->currentIndex() == 0)
return defaultStyleName;
return QStyleFactory::keys().at( index - 2 );
}
}
static const QString styleSettingsKey = QStringLiteral("MainWindow/QtStyle");

class PropertyResetter
{
Expand Down Expand Up @@ -774,7 +752,25 @@ SPrefsPanel::SPrefsPanel( qt_intf_t *_p_intf, QWidget *_parent,
}

#if !defined( _WIN32)
fillStylesCombo( ui.stylesCombo, getSettings()->value( "MainWindow/QtStyle", "" ).toString() );
{
// Populate styles combobox:
assert(qApp->property("initialStyle").isValid());
const QString& initialStyle = qApp->property("initialStyle").toString();
ui.stylesCombo->addItem( qtr( "System's default (%1)" ).arg( initialStyle ), initialStyle );
const QStringList& styles = QStyleFactory::keys();
for ( const auto& i : styles )
{
ui.stylesCombo->addItem( i, i );
}
const auto style = getSettings()->value( styleSettingsKey );

if ( style.isValid() && style.canConvert<QString>() )
ui.stylesCombo->setCurrentText( style.toString() );
ui.stylesCombo->insertSeparator( 1 );
if ( ui.stylesCombo->currentIndex() < 0 )
ui.stylesCombo->setCurrentIndex( 0 ); /* default */
}

m_resetters.push_back( std::make_unique<PropertyResetter>( ui.stylesCombo, "currentIndex" ) );

connect( ui.stylesCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
Expand Down Expand Up @@ -1242,7 +1238,10 @@ void SPrefsPanel::apply()
//if( m_interfaceUI.qt->isChecked() )
config_PutPsz( "intf", "" );
#if !defined( _WIN32)
getSettings()->setValue( "MainWindow/QtStyle", getQStyleKey( m_interfaceUI.stylesCombo , "" ) );
if ( m_interfaceUI.stylesCombo->currentIndex() > 0 )
getSettings()->setValue( styleSettingsKey, m_interfaceUI.stylesCombo->currentData().toString() );
else
getSettings()->remove( styleSettingsKey );
#endif

#ifdef _WIN32
Expand Down Expand Up @@ -1350,14 +1349,13 @@ void SPrefsPanel::lastfm_Changed( int i_state )

void SPrefsPanel::changeStyle()
{
const QString key = getQStyleKey( m_interfaceUI.stylesCombo,
qApp->property("initialStyle").toString() );
const QString style = m_interfaceUI.stylesCombo->currentData().toString();

QMetaObject::invokeMethod( qApp, [key]() {
QMetaObject::invokeMethod( qApp, [style]() {
// Queue this call in order to prevent
// updating the preferences dialog when
// it is rejected:
QApplication::setStyle( key );
QApplication::setStyle( style );
}, Qt::QueuedConnection );
}

Expand Down

0 comments on commit ac0545c

Please sign in to comment.