File indexing completed on 2024-05-12 05:35:45

0001 /*
0002     SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractItemModel>
0010 #include <QKeySequence>
0011 #include <QList>
0012 #include <QSet>
0013 
0014 class KConfigBase;
0015 
0016 // we need to do this to expose the enum to QML
0017 namespace ComponentNS
0018 {
0019 Q_NAMESPACE
0020 enum ComponentType {
0021     Application,
0022     Command,
0023     SystemService,
0024     CommonAction,
0025 };
0026 Q_ENUM_NS(ComponentType)
0027 };
0028 
0029 using namespace ComponentNS;
0030 
0031 struct Action {
0032     QString id;
0033     QString displayName;
0034     QSet<QKeySequence> activeShortcuts;
0035     QSet<QKeySequence> defaultShortcuts;
0036     QSet<QKeySequence> initialShortcuts;
0037 };
0038 
0039 struct Component {
0040     QString id;
0041     QString displayName;
0042     ComponentType type;
0043     QString icon;
0044     QList<Action> actions;
0045     bool checked;
0046     bool pendingDeletion;
0047 };
0048 
0049 class BaseModel : public QAbstractItemModel
0050 {
0051     Q_OBJECT
0052 
0053 public:
0054     enum Roles {
0055         SectionRole = Qt::UserRole,
0056         ComponentRole,
0057         ActionRole,
0058         ActiveShortcutsRole,
0059         DefaultShortcutsRole,
0060         CustomShortcutsRole,
0061         CheckedRole,
0062         PendingDeletionRole,
0063         IsDefaultRole,
0064         SupportsMultipleKeysRole,
0065     };
0066     Q_ENUM(Roles)
0067 
0068     BaseModel(QObject *parent = nullptr);
0069 
0070     Q_INVOKABLE void addShortcut(const QModelIndex &index, const QKeySequence &shortcut);
0071     Q_INVOKABLE void disableShortcut(const QModelIndex &index, const QKeySequence &shortcut);
0072     Q_INVOKABLE void changeShortcut(const QModelIndex &index, const QKeySequence &oldShortcut, const QKeySequence &newShortcut);
0073 
0074     virtual void exportToConfig(const KConfigBase &config) = 0;
0075     virtual void importConfig(const KConfigBase &config) = 0;
0076 
0077     virtual void load() = 0;
0078     virtual void save() = 0;
0079     void defaults();
0080     bool needsSave() const;
0081     bool isDefault() const;
0082 
0083     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0084     QModelIndex parent(const QModelIndex &child) const override;
0085     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0086     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0087     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0088     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0089     QHash<int, QByteArray> roleNames() const override;
0090 
0091 protected:
0092     QList<Component> m_components;
0093 };