From a19109ed0b800d2efd2eb02a2ed430fc09408224 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 12 Nov 2020 15:36:29 +0100 Subject: [PATCH] [plasmalnf] Prepare a new model - start a model for available themes, to replace the list held by the widget which just duplicates information. - move preselected theme to Config. --- src/modules/plasmalnf/Config.cpp | 102 ++++++++++++++++++++ src/modules/plasmalnf/Config.h | 20 +++- src/modules/plasmalnf/PlasmaLnfViewStep.cpp | 29 +----- 3 files changed, 122 insertions(+), 29 deletions(-) diff --git a/src/modules/plasmalnf/Config.cpp b/src/modules/plasmalnf/Config.cpp index c7aea240f..1fd6033df 100644 --- a/src/modules/plasmalnf/Config.cpp +++ b/src/modules/plasmalnf/Config.cpp @@ -15,8 +15,99 @@ #include "utils/Logger.h" #include "utils/Variant.h" +#ifdef WITH_KCONFIG +#include +#include +#endif + +#include +#include + +#include +#include + +class ThemesModel : public QAbstractListModel +{ + Q_OBJECT + +public: + enum + { + LabelRole = Qt::DisplayRole, + KeyRole = Qt::UserRole + }; + + explicit ThemesModel( QObject* parent ); + + int rowCount( const QModelIndex& = QModelIndex() ) const override; + QVariant data( const QModelIndex& index, int role ) const override; + + QHash< int, QByteArray > roleNames() const override; + +private: + QList< KPluginMetaData > m_themes; +}; + +ThemesModel::ThemesModel( QObject* parent ) + : QAbstractListModel( parent ) + , m_themes( KPackage::PackageLoader::self()->listPackages( "Plasma/LookAndFeel" ) ) +{ +} + +int +ThemesModel::rowCount( const QModelIndex& ) const +{ + return m_themes.count(); +} + +QVariant +ThemesModel::data( const QModelIndex& index, int role ) const +{ + if ( !index.isValid() ) + { + return QVariant(); + } + if ( index.row() < 0 || index.row() >= m_themes.count() ) + { + return QVariant(); + } + + const auto& item = m_themes.at( index.row() ); + switch ( role ) + { + case LabelRole: + return item.name(); + case KeyRole: + return item.pluginId(); + default: + return QVariant(); + } + __builtin_unreachable(); +} + +QHash< int, QByteArray > +ThemesModel::roleNames() const +{ + return { { LabelRole, "label" }, { KeyRole, "key" } }; +} + + +static QString +currentPlasmaTheme() +{ +#ifdef WITH_KCONFIG + KConfigGroup cg( KSharedConfig::openConfig( QStringLiteral( "kdeglobals" ) ), "KDE" ); + return cg.readEntry( "LookAndFeelPackage", QString() ); +#else + cWarning() << "No KConfig support, cannot determine Plasma theme."; + return QString(); +#endif +} + + Config::Config( QObject* parent ) : QObject( parent ) + , m_themeModel( new ThemesModel( this ) ) { } @@ -31,6 +122,13 @@ Config::setConfigurationMap( const QVariantMap& configurationMap ) } m_liveUser = CalamaresUtils::getString( configurationMap, "liveuser" ); + + QString preselect = CalamaresUtils::getString( configurationMap, "preselect" ); + if ( preselect == QStringLiteral( "*" ) ) + { + preselect = currentPlasmaTheme(); + } + m_preselectThemeId = preselect; } Calamares::JobList @@ -92,3 +190,7 @@ Config::setTheme( const QString& id ) } emit themeChanged( id ); } + +#include "utils/moc-warnings.h" + +#include "Config.moc" diff --git a/src/modules/plasmalnf/Config.h b/src/modules/plasmalnf/Config.h index ef32df2d9..cad705530 100644 --- a/src/modules/plasmalnf/Config.h +++ b/src/modules/plasmalnf/Config.h @@ -14,15 +14,19 @@ #include +class QAbstractItemModel; + class Config : public QObject { Q_OBJECT + Q_PROPERTY( QString preselectedTheme READ preselectedTheme CONSTANT ) Q_PROPERTY( QString theme READ theme WRITE setTheme NOTIFY themeChanged ) + Q_PROPERTY( QAbstractItemModel* themeModel READ themeModel CONSTANT ) public: Config( QObject* parent = nullptr ); - virtual ~Config() override = default; + virtual ~Config() override = default; // QObject cleans up the model pointer void setConfigurationMap( const QVariantMap& ); Calamares::JobList createJobs() const; @@ -42,6 +46,17 @@ public: */ QString theme() const { return m_themeId; } + /** @brief The theme we start with + * + * This can be configured, or is taken from the live environment + * if the environment is (also) KDE Plasma. + */ + QString preselectedTheme() const { return m_preselectThemeId; } + + /** @brief The (list) model of available themes. + */ + QAbstractItemModel* themeModel() const { return m_themeModel; } + public slots: void setTheme( const QString& id ); @@ -52,7 +67,10 @@ private: QString m_lnfPath; // Path to the lnf tool QString m_liveUser; // Name of the live user (for OEM mode) + QString m_preselectThemeId; QString m_themeId; // Id of selected theme + + QAbstractItemModel* m_themeModel = nullptr; }; #endif diff --git a/src/modules/plasmalnf/PlasmaLnfViewStep.cpp b/src/modules/plasmalnf/PlasmaLnfViewStep.cpp index 52435d5a6..d282cea09 100644 --- a/src/modules/plasmalnf/PlasmaLnfViewStep.cpp +++ b/src/modules/plasmalnf/PlasmaLnfViewStep.cpp @@ -15,28 +15,10 @@ #include "utils/Logger.h" #include "utils/Variant.h" -#include #include -#ifdef WITH_KCONFIG -#include -#include -#endif - CALAMARES_PLUGIN_FACTORY_DEFINITION( PlasmaLnfViewStepFactory, registerPlugin< PlasmaLnfViewStep >(); ) -static QString -currentPlasmaTheme() -{ -#ifdef WITH_KCONFIG - KConfigGroup cg( KSharedConfig::openConfig( QStringLiteral( "kdeglobals" ) ), "KDE" ); - return cg.readEntry( "LookAndFeelPackage", QString() ); -#else - cWarning() << "No KConfig support, cannot determine Plasma theme."; - return QString(); -#endif -} - PlasmaLnfViewStep::PlasmaLnfViewStep( QObject* parent ) : Calamares::ViewStep( parent ) , m_config( new Config( this ) ) @@ -117,16 +99,7 @@ PlasmaLnfViewStep::setConfigurationMap( const QVariantMap& configurationMap ) m_config->setConfigurationMap( configurationMap ); m_widget->setLnfPath( m_config->lnfToolPath() ); - - QString preselect = CalamaresUtils::getString( configurationMap, "preselect" ); - if ( preselect == QStringLiteral( "*" ) ) - { - preselect = currentPlasmaTheme(); - } - if ( !preselect.isEmpty() ) - { - m_widget->setPreselect( preselect ); - } + m_widget->setPreselect( m_config->preselectedTheme() ); bool showAll = CalamaresUtils::getBool( configurationMap, "showAll", false );