1 /*
2 SPDX-FileCopyrightText: 2020 Ismael Asensio <isma.af@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6
7 #include "optionsmodel.h"
8
9 #include <KLocalizedString>
10
11
12 namespace KWin
13 {
14
roleNames() const15 QHash<int, QByteArray> OptionsModel::roleNames() const
16 {
17 return {
18 {Qt::DisplayRole, QByteArrayLiteral("display")},
19 {Qt::DecorationRole, QByteArrayLiteral("decoration")},
20 {Qt::ToolTipRole, QByteArrayLiteral("tooltip")},
21 {Qt::UserRole, QByteArrayLiteral("value")},
22 {Qt::UserRole + 1, QByteArrayLiteral("iconName")},
23 };
24 }
25
rowCount(const QModelIndex & parent) const26 int OptionsModel::rowCount(const QModelIndex &parent) const
27 {
28 if (parent.isValid()) {
29 return 0;
30 }
31 return m_data.size();
32 }
33
data(const QModelIndex & index,int role) const34 QVariant OptionsModel::data(const QModelIndex &index, int role) const
35 {
36 if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) {
37 return QVariant();
38 }
39
40 const Data data = m_data.at(index.row());
41
42 switch (role) {
43 case Qt::DisplayRole:
44 return data.text;
45 case Qt::UserRole:
46 return data.value;
47 case Qt::DecorationRole:
48 return data.icon;
49 case Qt::UserRole + 1:
50 return data.icon.name();
51 case Qt::ToolTipRole:
52 return data.description;
53 }
54 return QVariant();
55 }
56
selectedIndex() const57 int OptionsModel::selectedIndex() const
58 {
59 return m_index;
60 }
61
indexOf(const QVariant & value) const62 int OptionsModel::indexOf(const QVariant &value) const
63 {
64 for (int index = 0; index < m_data.count(); index++) {
65 if (m_data.at(index).value == value) {
66 return index;
67 }
68 }
69 return -1;
70 }
71
textOfValue(const QVariant & value) const72 QString OptionsModel::textOfValue(const QVariant &value) const
73 {
74 int index = indexOf(value);
75 if (index < 0 || index >= m_data.count()) {
76 return QString();
77 }
78 return m_data.at(index).text;
79 }
80
value() const81 QVariant OptionsModel::value() const
82 {
83 if (m_data.isEmpty()) {
84 return QVariant();
85 }
86 return m_data.at(m_index).value;
87 }
88
setValue(QVariant value)89 void OptionsModel::setValue(QVariant value)
90 {
91 if (this->value() == value) {
92 return;
93 }
94 int index = indexOf(value);
95 if (index >= 0 && index != m_index) {
96 m_index = index;
97 Q_EMIT selectedIndexChanged(index);
98 }
99 }
100
resetValue()101 void OptionsModel::resetValue()
102 {
103 m_index = 0;
104 Q_EMIT selectedIndexChanged(m_index);
105 }
106
updateModelData(const QList<Data> & data)107 void OptionsModel::updateModelData(const QList<Data> &data) {
108 beginResetModel();
109 m_data = data;
110 endResetModel();
111 }
112
113
type() const114 RulePolicy::Type RulePolicy::type() const
115 {
116 return m_type;
117 }
118
value() const119 int RulePolicy::value() const
120 {
121 if (m_type == RulePolicy::NoPolicy) {
122 return Rules::Apply; // To simplify external checks when rule has no policy
123 }
124 return OptionsModel::value().toInt();
125 }
126
policyKey(const QString & key) const127 QString RulePolicy::policyKey(const QString &key) const
128 {
129 switch (m_type) {
130 case NoPolicy:
131 return QString();
132 case StringMatch:
133 return QStringLiteral("%1match").arg(key);
134 case SetRule:
135 case ForceRule:
136 return QStringLiteral("%1rule").arg(key);
137 }
138
139 return QString();
140 }
141
policyOptions(RulePolicy::Type type)142 QList<RulePolicy::Data> RulePolicy::policyOptions(RulePolicy::Type type)
143 {
144 static const auto stringMatchOptions = QList<RulePolicy::Data> {
145 {Rules::UnimportantMatch, i18n("Unimportant")},
146 {Rules::ExactMatch, i18n("Exact Match")},
147 {Rules::SubstringMatch, i18n("Substring Match")},
148 {Rules::RegExpMatch, i18n("Regular Expression")}
149 };
150
151 static const auto setRuleOptions = QList<RulePolicy::Data> {
152 {Rules::Apply,
153 i18n("Apply Initially"),
154 i18n("The window property will be only set to the given value after the window is created."
155 "\nNo further changes will be affected.")},
156 {Rules::ApplyNow,
157 i18n("Apply Now"),
158 i18n("The window property will be set to the given value immediately and will not be affected later"
159 "\n(this action will be deleted afterwards).")},
160 {Rules::Remember,
161 i18n("Remember"),
162 i18n("The value of the window property will be remembered and, every time the window"
163 " is created, the last remembered value will be applied.")},
164 {Rules::DontAffect,
165 i18n("Do Not Affect"),
166 i18n("The window property will not be affected and therefore the default handling for it will be used."
167 "\nSpecifying this will block more generic window settings from taking effect.")},
168 {Rules::Force,
169 i18n("Force"),
170 i18n("The window property will be always forced to the given value.")},
171 {Rules::ForceTemporarily,
172 i18n("Force Temporarily"),
173 i18n("The window property will be forced to the given value until it is hidden"
174 "\n(this action will be deleted after the window is hidden).")}
175 };
176
177 static auto forceRuleOptions = QList<RulePolicy::Data> {
178 setRuleOptions.at(4), // Rules::Force
179 setRuleOptions.at(5), // Rules::ForceTemporarily
180 setRuleOptions.at(3), // Rules::DontAffect
181 };
182
183 switch (type) {
184 case NoPolicy:
185 return {};
186 case StringMatch:
187 return stringMatchOptions;
188 case SetRule:
189 return setRuleOptions;
190 case ForceRule:
191 return forceRuleOptions;
192 }
193 return {};
194 }
195
196 } //namespace
197