From bcff0454a30afe52d4e71e63f4f12f84adf22770 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 30 Nov 2020 12:11:11 +0100 Subject: [PATCH] [plasmalnf] Give the themes a selected-state - This kind of runs around the selection model on the view, but we're drawing radio buttons ourselves **anyway** and the list of themes knows which is selected / current independent of the view. --- src/modules/plasmalnf/Config.cpp | 3 +++ src/modules/plasmalnf/PlasmaLnfPage.cpp | 3 ++- src/modules/plasmalnf/ThemeInfo.cpp | 29 ++++++++++++++++++++++++- src/modules/plasmalnf/ThemeInfo.h | 12 +++++++++- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/modules/plasmalnf/Config.cpp b/src/modules/plasmalnf/Config.cpp index e418e885b..e022109b4 100644 --- a/src/modules/plasmalnf/Config.cpp +++ b/src/modules/plasmalnf/Config.cpp @@ -98,6 +98,8 @@ Config::setConfigurationMap( const QVariantMap& configurationMap ) m_themeModel->showOnlyThemes( listedThemes ); } } + + m_themeModel->select( m_preselectThemeId ); } Calamares::JobList @@ -157,5 +159,6 @@ Config::setTheme( const QString& id ) cDebug() << "Plasma look-and-feel applied" << id; } } + m_themeModel->select( id ); emit themeChanged( id ); } diff --git a/src/modules/plasmalnf/PlasmaLnfPage.cpp b/src/modules/plasmalnf/PlasmaLnfPage.cpp index fc3be4471..0e7f05e43 100644 --- a/src/modules/plasmalnf/PlasmaLnfPage.cpp +++ b/src/modules/plasmalnf/PlasmaLnfPage.cpp @@ -43,6 +43,7 @@ ThemeDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, con { auto label = index.data( ThemesModel::LabelRole ).toString(); auto description = index.data( ThemesModel::DescriptionRole ).toString(); + auto selected = index.data( ThemesModel::SelectedRole ).toBool() ? QStyle::State_On : QStyle::State_Off; auto image_v = index.data( ThemesModel::ImageRole ); QPixmap image = image_v.canConvert< QPixmap >() ? qvariant_cast< QPixmap >( image_v ) : QPixmap(); @@ -52,7 +53,7 @@ ThemeDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, con labelRect.setWidth( labelRect.width() / 3 ); QStyleOptionButton rbOption; - rbOption.state |= QStyle::State_Enabled | QStyle::State_On; + rbOption.state |= QStyle::State_Enabled | selected; rbOption.rect = labelRect; rbOption.text = label; option.widget->style()->drawControl( QStyle::CE_RadioButton, &rbOption, painter, option.widget ); diff --git a/src/modules/plasmalnf/ThemeInfo.cpp b/src/modules/plasmalnf/ThemeInfo.cpp index 8c922ceef..77f0a30ab 100644 --- a/src/modules/plasmalnf/ThemeInfo.cpp +++ b/src/modules/plasmalnf/ThemeInfo.cpp @@ -34,6 +34,7 @@ struct ThemeInfo QString imagePath; mutable QPixmap pixmap; bool show = true; + bool selected = false; ThemeInfo() {} @@ -138,6 +139,8 @@ ThemesModel::data( const QModelIndex& index, int role ) const return item.id; case ShownRole: return item.show; + case SelectedRole: + return item.selected; case DescriptionRole: return item.description; case ImageRole: @@ -151,7 +154,11 @@ ThemesModel::data( const QModelIndex& index, int role ) const QHash< int, QByteArray > ThemesModel::roleNames() const { - return { { LabelRole, "label" }, { KeyRole, "key" }, { ShownRole, "show" }, { ImageRole, "image" } }; + return { { LabelRole, "label" }, + { KeyRole, "key" }, + { SelectedRole, "selected" }, + { ShownRole, "show" }, + { ImageRole, "image" } }; } void @@ -219,6 +226,26 @@ ThemesModel::imageSize() qMax( 8 * CalamaresUtils::defaultFontHeight(), 80 ) }; } +void +ThemesModel::select( const QString& themeId ) +{ + int i = 0; + for ( auto& t : *m_themes ) + { + if ( t.selected && t.id != themeId ) + { + t.selected = false; + emit dataChanged( index( i, 0 ), index( i, 0 ), { SelectedRole } ); + } + if ( !t.selected && t.id == themeId ) + { + t.selected = true; + emit dataChanged( index( i, 0 ), index( i, 0 ), { SelectedRole } ); + } + ++i; + } +} + /** * Massage the given @p path to the most-likely diff --git a/src/modules/plasmalnf/ThemeInfo.h b/src/modules/plasmalnf/ThemeInfo.h index eab2bba6c..c859bcd2d 100644 --- a/src/modules/plasmalnf/ThemeInfo.h +++ b/src/modules/plasmalnf/ThemeInfo.h @@ -25,7 +25,8 @@ public: { LabelRole = Qt::DisplayRole, KeyRole = Qt::UserRole, - ShownRole, + ShownRole, // Should theme be displayed + SelectedRole, // Is theme selected DescriptionRole, ImageRole }; @@ -49,6 +50,15 @@ public: /// @brief Shows the keys in the @p onlyThese map, and hides the rest void showOnlyThemes( const QMap< QString, QString >& onlyThese ); + /** @brief Mark the @p themeId as current / selected + * + * One theme can be selected at a time; this will emit data + * changed signals for any (one) theme already selected, and + * the newly-selected theme. If @p themeId does not name any + * theme, none are selected. + */ + void select( const QString& themeId ); + /** @brief The size of theme Images * * The size is dependent on the font size used by Calamares,