Changeset View
Changeset View
Standalone View
Standalone View
kcmkwin/kwinrules/optionsmodel.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2020 Ismael Asensio <isma.af@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #ifndef KWIN_OPTIONS_MODEL_H | ||||
22 | #define KWIN_OPTIONS_MODEL_H | ||||
23 | | ||||
24 | #include <rules.h> | ||||
25 | | ||||
26 | #include <QAbstractListModel> | ||||
27 | #include <QIcon> | ||||
28 | #include <QVariant> | ||||
29 | | ||||
30 | | ||||
31 | namespace KWin { | ||||
32 | | ||||
33 | class OptionsModel : public QAbstractListModel | ||||
34 | { | ||||
35 | Q_OBJECT | ||||
36 | //TODO: After Qt 5.14 the QML ComboBox will allow to use the valueRole directly | ||||
37 | // instead of changing the index. Remove this exposed property | ||||
38 | Q_PROPERTY(int selectedIndex READ selectedIndex NOTIFY selectedIndexChanged) | ||||
39 | | ||||
40 | public: | ||||
41 | struct Data { | ||||
42 | Data(const QVariant &value, const QString &text, const QIcon &icon = {}, const QString &description = {}) | ||||
43 | : value(value) | ||||
44 | , text(text) | ||||
45 | , icon(icon) | ||||
46 | , description(description) | ||||
47 | {} | ||||
48 | Data(const QVariant &value, const QString &text, const QString &description) | ||||
49 | : value(value) | ||||
50 | , text(text) | ||||
51 | , description(description) | ||||
52 | {} | ||||
53 | | ||||
54 | QVariant value; | ||||
55 | QString text; | ||||
56 | QIcon icon; | ||||
57 | QString description; | ||||
58 | }; | ||||
59 | | ||||
60 | public: | ||||
61 | OptionsModel() : QAbstractListModel(), m_data(), m_index(0) {}; | ||||
62 | OptionsModel(const QList<Data> &data) : QAbstractListModel(), m_data(data), m_index(0) {}; | ||||
63 | | ||||
64 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; | ||||
65 | QHash<int, QByteArray> roleNames() const override; | ||||
66 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; | ||||
67 | | ||||
68 | QVariant value() const; | ||||
69 | void setValue(QVariant value); | ||||
70 | void resetValue(); | ||||
71 | | ||||
72 | void updateModelData(const QList<Data> &data); | ||||
73 | | ||||
74 | //TODO: Remove after Qt 5.14 | ||||
75 | int selectedIndex() const; | ||||
76 | | ||||
77 | signals: | ||||
78 | void selectedIndexChanged(int index); | ||||
79 | | ||||
80 | public: | ||||
81 | QList<Data> m_data; | ||||
82 | | ||||
83 | protected: | ||||
84 | int m_index = 0; | ||||
85 | }; | ||||
86 | | ||||
87 | class RulePolicy : public OptionsModel | ||||
88 | { | ||||
89 | public: | ||||
90 | enum Type { | ||||
91 | NoPolicy, | ||||
92 | StringMatch, | ||||
93 | SetRule, | ||||
94 | ForceRule | ||||
95 | }; | ||||
96 | | ||||
97 | public: | ||||
98 | RulePolicy(Type type) | ||||
99 | : OptionsModel(policyOptions(type)) | ||||
100 | , m_type(type) | ||||
101 | {}; | ||||
102 | | ||||
103 | Type type() const; | ||||
104 | int value() const; | ||||
105 | QString policyKey(const QString &key) const; | ||||
106 | | ||||
107 | private: | ||||
108 | static QList<Data> policyOptions(RulePolicy::Type type); | ||||
109 | | ||||
110 | private: | ||||
111 | Type m_type; | ||||
112 | }; | ||||
113 | | ||||
114 | } //namespace | ||||
115 | | ||||
116 | #endif //KWIN_OPTIONS_MODEL_H |