File indexing completed on 2026-08-09 07:09:56

0001 /*
0002     SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
0003     SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KPLUGINMODEL_H
0009 #define KPLUGINMODEL_H
0010 
0011 #include "kcmutilscore_export.h"
0012 
0013 #include <QAbstractListModel>
0014 #include <QList>
0015 
0016 #include <KPluginMetaData>
0017 #include <memory>
0018 
0019 class KConfigGroup;
0020 class KPluginModelPrivate;
0021 
0022 class KCMUTILSCORE_EXPORT KPluginModel : public QAbstractListModel
0023 {
0024     Q_OBJECT
0025 
0026 public:
0027     enum Roles {
0028         NameRole = Qt::DisplayRole,
0029         IconRole = Qt::DecorationRole,
0030         EnabledRole = Qt::CheckStateRole,
0031         DescriptionRole = Qt::UserRole + 1,
0032         IsChangeableRole,
0033         MetaDataRole,
0034         ConfigRole,
0035         IdRole,
0036         EnabledByDefaultRole,
0037         SortableRole, /// @internal
0038     };
0039 
0040     explicit KPluginModel(QObject *parent = nullptr);
0041     ~KPluginModel() override;
0042 
0043     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0044     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0045     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0046     QHash<int, QByteArray> roleNames() const override;
0047 
0048     bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
0049 
0050     void addPlugins(const QList<KPluginMetaData> &plugins, const QString &categoryLabel);
0051 
0052     /**
0053      * Add plugins that should not be sorted automatically based on their name
0054      * This is useful in case your app has a custom sorting mechanism or implements reordering of plugins
0055      *
0056      * @since 6.0
0057      */
0058     void addUnsortablePlugins(const QList<KPluginMetaData> &plugins, const QString &categoryLabel);
0059     /// @since 6.0
0060     void removePlugin(const KPluginMetaData &data);
0061     void clear();
0062     void setConfig(const KConfigGroup &config);
0063     void save();
0064     void load();
0065     void defaults();
0066     bool isSaveNeeded();
0067 
0068     /**
0069      * Returns the KPluginMetaData object of the plugin's config module. If no plugin is found or the plugin does not have a config, the resulting
0070      * KPluginMetaData object will be invalid.
0071      * @since 5.94
0072      */
0073     KPluginMetaData findConfigForPluginId(const QString &pluginId) const;
0074 
0075     Q_SIGNAL void defaulted(bool isDefaulted);
0076     Q_SIGNAL void isSaveNeededChanged();
0077 
0078 private:
0079     const std::unique_ptr<KPluginModelPrivate> d;
0080     friend class KPluginProxyModel;
0081     QStringList getOrderedCategoryLabels();
0082 };
0083 #endif