1 /* 2 SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de> 3 4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 5 */ 6 7 #ifndef BASEMODEL_H 8 #define BASEMODEL_H 9 10 #include <QAbstractItemModel> 11 #include <QKeySequence> 12 #include <QSet> 13 #include <QVector> 14 15 class KConfigBase; 16 17 struct Action { 18 QString id; 19 QString displayName; 20 QSet<QKeySequence> activeShortcuts; 21 QSet<QKeySequence> defaultShortcuts; 22 QSet<QKeySequence> initialShortcuts; 23 }; 24 25 struct Component { 26 QString id; 27 QString displayName; 28 QString type; 29 QString icon; 30 QVector<Action> actions; 31 bool checked; 32 bool pendingDeletion; 33 }; 34 35 class BaseModel : public QAbstractItemModel 36 { 37 Q_OBJECT 38 39 public: 40 enum Roles { 41 SectionRole = Qt::UserRole, 42 ComponentRole, 43 ActionRole, 44 ActiveShortcutsRole, 45 DefaultShortcutsRole, 46 CustomShortcutsRole, 47 CheckedRole, 48 PendingDeletionRole, 49 IsDefaultRole, 50 SupportsMultipleKeysRole, 51 }; 52 Q_ENUM(Roles) 53 54 BaseModel(QObject *parent = nullptr); 55 56 Q_INVOKABLE void addShortcut(const QModelIndex &index, const QKeySequence &shortcut); 57 Q_INVOKABLE void disableShortcut(const QModelIndex &index, const QKeySequence &shortcut); 58 Q_INVOKABLE void changeShortcut(const QModelIndex &index, const QKeySequence &oldShortcut, const QKeySequence &newShortcut); 59 60 virtual void exportToConfig(const KConfigBase &config) = 0; 61 virtual void importConfig(const KConfigBase &config) = 0; 62 63 virtual void load() = 0; 64 virtual void save() = 0; 65 void defaults(); 66 bool needsSave() const; 67 bool isDefault() const; 68 69 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; 70 QModelIndex parent(const QModelIndex &child) const override; 71 int rowCount(const QModelIndex &parent = QModelIndex()) const override; 72 int columnCount(const QModelIndex &parent = QModelIndex()) const override; 73 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 74 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; 75 QHash<int, QByteArray> roleNames() const override; 76 77 protected: 78 QVector<Component> m_components; 79 }; 80 81 #endif 82