OSDN Git Service

[plasmalnf] Move model to ThemeInfo files
authorAdriaan de Groot <groot@kde.org>
Mon, 16 Nov 2020 15:37:30 +0000 (16:37 +0100)
committerAdriaan de Groot <groot@kde.org>
Mon, 16 Nov 2020 17:14:23 +0000 (18:14 +0100)
src/modules/plasmalnf/CMakeLists.txt
src/modules/plasmalnf/Config.cpp
src/modules/plasmalnf/Config.h
src/modules/plasmalnf/PlasmaLnfPage.cpp
src/modules/plasmalnf/ThemeInfo.cpp [new file with mode: 0644]
src/modules/plasmalnf/ThemeInfo.h

index f13f4f9..027b3e1 100644 (file)
@@ -36,6 +36,7 @@ if ( KF5Plasma_FOUND AND KF5Package_FOUND )
             PlasmaLnfViewStep.cpp
             PlasmaLnfPage.cpp
             PlasmaLnfJob.cpp
+            ThemeInfo.cpp
             ThemeWidget.cpp
         RESOURCES
             page_plasmalnf.qrc
index 1fd6033..215aef1 100644 (file)
@@ -10,6 +10,7 @@
 #include "Config.h"
 
 #include "PlasmaLnfJob.h"
+#include "ThemeInfo.h"
 
 #include "utils/CalamaresUtilsSystem.h"
 #include "utils/Logger.h"
 #include <KSharedConfig>
 #endif
 
-#include <KPackage/Package>
-#include <KPackage/PackageLoader>
-
-#include <QAbstractListModel>
-#include <QList>
-
-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"
index cad7055..725563c 100644 (file)
@@ -12,9 +12,9 @@
 
 #include "Job.h"
 
-#include <QObject>
+#include "ThemeInfo.h"
 
-class QAbstractItemModel;
+#include <QObject>
 
 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
index d512b1d..22e2402 100644 (file)
 #include <KPackage/Package>
 #include <KPackage/PackageLoader>
 
-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 (file)
index 0000000..41bffd1
--- /dev/null
@@ -0,0 +1,69 @@
+/* === This file is part of Calamares - <https://calamares.io> ===
+ *
+ *   SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
+ *   SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ *   Calamares is Free Software: see the License-Identifier above.
+ *
+ */
+#include "ThemeInfo.h"
+
+#include <KPackage/Package>
+#include <KPackage/PackageLoader>
+
+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 )
+{
+}
index 6848fb8..a44c418 100644 (file)
@@ -10,6 +10,7 @@
 #ifndef PLASMALNF_THEMEINFO_H
 #define PLASMALNF_THEMEINFO_H
 
+#include <QAbstractListModel>
 #include <QList>
 #include <QString>
 
@@ -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