From 57907ca992be505f70933e1fb3ad71c94d2be648 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 16 Nov 2020 16:37:30 +0100 Subject: [PATCH] [plasmalnf] Move model to ThemeInfo files --- src/modules/plasmalnf/CMakeLists.txt | 1 + src/modules/plasmalnf/Config.cpp | 77 +------------------------ src/modules/plasmalnf/Config.h | 6 +- src/modules/plasmalnf/PlasmaLnfPage.cpp | 8 --- src/modules/plasmalnf/ThemeInfo.cpp | 69 ++++++++++++++++++++++ src/modules/plasmalnf/ThemeInfo.h | 27 ++++++++- 6 files changed, 100 insertions(+), 88 deletions(-) create mode 100644 src/modules/plasmalnf/ThemeInfo.cpp diff --git a/src/modules/plasmalnf/CMakeLists.txt b/src/modules/plasmalnf/CMakeLists.txt index f13f4f9c7..027b3e12a 100644 --- a/src/modules/plasmalnf/CMakeLists.txt +++ b/src/modules/plasmalnf/CMakeLists.txt @@ -36,6 +36,7 @@ if ( KF5Plasma_FOUND AND KF5Package_FOUND ) PlasmaLnfViewStep.cpp PlasmaLnfPage.cpp PlasmaLnfJob.cpp + ThemeInfo.cpp ThemeWidget.cpp RESOURCES page_plasmalnf.qrc diff --git a/src/modules/plasmalnf/Config.cpp b/src/modules/plasmalnf/Config.cpp index 1fd6033df..215aef1d6 100644 --- a/src/modules/plasmalnf/Config.cpp +++ b/src/modules/plasmalnf/Config.cpp @@ -10,6 +10,7 @@ #include "Config.h" #include "PlasmaLnfJob.h" +#include "ThemeInfo.h" #include "utils/CalamaresUtilsSystem.h" #include "utils/Logger.h" @@ -20,78 +21,6 @@ #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() { @@ -190,7 +119,3 @@ 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 cad705530..725563c34 100644 --- a/src/modules/plasmalnf/Config.h +++ b/src/modules/plasmalnf/Config.h @@ -12,9 +12,9 @@ #include "Job.h" -#include +#include "ThemeInfo.h" -class QAbstractItemModel; +#include class Config : public QObject { @@ -70,7 +70,7 @@ private: QString m_preselectThemeId; QString m_themeId; // Id of selected theme - QAbstractItemModel* m_themeModel = nullptr; + ThemesModel* m_themeModel = nullptr; }; #endif diff --git a/src/modules/plasmalnf/PlasmaLnfPage.cpp b/src/modules/plasmalnf/PlasmaLnfPage.cpp index d512b1dc0..22e240266 100644 --- a/src/modules/plasmalnf/PlasmaLnfPage.cpp +++ b/src/modules/plasmalnf/PlasmaLnfPage.cpp @@ -21,14 +21,6 @@ #include #include -ThemeInfo::ThemeInfo( const KPluginMetaData& data ) - : id( data.pluginId() ) - , name( data.name() ) - , description( data.description() ) - , widget( nullptr ) -{ -} - static ThemeInfoList plasma_themes() { diff --git a/src/modules/plasmalnf/ThemeInfo.cpp b/src/modules/plasmalnf/ThemeInfo.cpp new file mode 100644 index 000000000..41bffd1f0 --- /dev/null +++ b/src/modules/plasmalnf/ThemeInfo.cpp @@ -0,0 +1,69 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2020 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ +#include "ThemeInfo.h" + +#include +#include + +ThemesModel::ThemesModel( QObject* parent ) + : QAbstractListModel( parent ) +{ + auto packages = KPackage::PackageLoader::self()->listPackages( "Plasma/LookAndFeel" ); + m_themes.reserve( packages.length() ); + + for ( const auto& p : packages ) + { + m_themes.append( ThemeInfo { p } ); + } +} + +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.id; + default: + return QVariant(); + } + __builtin_unreachable(); +} + +QHash< int, QByteArray > +ThemesModel::roleNames() const +{ + return { { LabelRole, "label" }, { KeyRole, "key" } }; +} + +ThemeInfo::ThemeInfo( const KPluginMetaData& data ) + : id( data.pluginId() ) + , name( data.name() ) + , description( data.description() ) + , widget( nullptr ) +{ +} diff --git a/src/modules/plasmalnf/ThemeInfo.h b/src/modules/plasmalnf/ThemeInfo.h index 6848fb8c3..a44c41858 100644 --- a/src/modules/plasmalnf/ThemeInfo.h +++ b/src/modules/plasmalnf/ThemeInfo.h @@ -10,6 +10,7 @@ #ifndef PLASMALNF_THEMEINFO_H #define PLASMALNF_THEMEINFO_H +#include #include #include @@ -49,7 +50,6 @@ struct ThemeInfo { } - // Defined in PlasmaLnfPage.cpp explicit ThemeInfo( const KPluginMetaData& ); bool isValid() const { return !id.isEmpty(); } @@ -88,4 +88,29 @@ public: bool contains( const QString& id ) const { return findById( id ) != nullptr; } }; +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; + + const ThemeInfo* findById( const QString& id ) const { return m_themes.findById( id ); } + +private: + ThemeInfoList m_themes; +}; + + #endif