KWin
Loading...
Searching...
No Matches
optionsmodel.cpp
Go to the documentation of this file.
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
11namespace KWin
12{
13
14QHash<int, QByteArray> OptionsModel::roleNames() const
15{
16 return {
17 {Qt::DisplayRole, QByteArrayLiteral("display")},
18 {Qt::DecorationRole, QByteArrayLiteral("decoration")},
19 {Qt::ToolTipRole, QByteArrayLiteral("tooltip")},
20 {ValueRole, QByteArrayLiteral("value")},
21 {IconNameRole, QByteArrayLiteral("iconName")},
22 {OptionTypeRole, QByteArrayLiteral("optionType")},
23 {BitMaskRole, QByteArrayLiteral("bitMask")},
24 };
25}
26
27int OptionsModel::rowCount(const QModelIndex &parent) const
28{
29 if (parent.isValid()) {
30 return 0;
31 }
32 return m_data.size();
33}
34
35QVariant OptionsModel::data(const QModelIndex &index, int role) const
36{
37 if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) {
38 return QVariant();
39 }
40
41 const Data item = m_data.at(index.row());
42
43 switch (role) {
44 case Qt::DisplayRole:
45 return item.text;
46 case Qt::UserRole:
47 return item.value;
48 case Qt::DecorationRole:
49 return item.icon;
50 case IconNameRole:
51 return item.icon.name();
52 case Qt::ToolTipRole:
53 return item.description;
54 case OptionTypeRole:
55 return item.optionType;
56 case BitMaskRole:
57 return bitMask(index.row());
58 }
59 return QVariant();
60}
61
63{
64 return m_index;
65}
66
67int OptionsModel::indexOf(const QVariant &value) const
68{
69 for (int index = 0; index < m_data.count(); index++) {
70 if (m_data.at(index).value == value) {
71 return index;
72 }
73 }
74 return -1;
75}
76
77QString OptionsModel::textOfValue(const QVariant &value) const
78{
79 int index = indexOf(value);
80 if (index < 0 || index >= m_data.count()) {
81 return QString();
82 }
83 return m_data.at(index).text;
84}
85
86QVariant OptionsModel::value() const
87{
88 if (m_data.isEmpty()) {
89 return QVariant();
90 }
91 if (m_data.at(m_index).optionType == SelectAllOption) {
92 return allValues();
93 }
94 return m_data.at(m_index).value;
95}
96
97void OptionsModel::setValue(QVariant value)
98{
99 if (this->value() == value) {
100 return;
101 }
102 int index = indexOf(value);
103 if (index >= 0 && index != m_index) {
104 m_index = index;
105 Q_EMIT selectedIndexChanged(index);
106 }
107}
108
110{
111 m_index = 0;
113}
114
116{
117 return m_useFlags;
118};
119
120uint OptionsModel::bitMask(int index) const
121{
122 const Data item = m_data.at(index);
123
124 if (item.optionType == SelectAllOption) {
125 return allOptionsMask();
126 }
127 if (m_useFlags) {
128 return item.value.toUInt();
129 }
130 return 1u << index;
131}
132
134{
135 if (m_useFlags) {
136 return allOptionsMask();
137 }
138
139 QVariantList list;
140 for (const Data &item : std::as_const(m_data)) {
141 if (item.optionType == NormalOption) {
142 list << item.value;
143 }
144 }
145 return list;
146}
147
149{
150 uint mask = 0;
151 for (int index = 0; index < m_data.count(); index++) {
152 if (m_data.at(index).optionType == NormalOption) {
153 mask += bitMask(index);
154 }
155 }
156 return mask;
157}
158
159void OptionsModel::updateModelData(const QList<Data> &data)
160{
161 beginResetModel();
162 m_data = data;
163 endResetModel();
164 Q_EMIT modelUpdated();
165}
166
168{
169 return m_type;
170}
171
173{
174 if (m_type == RulePolicy::NoPolicy) {
175 return Rules::Apply; // To simplify external checks when rule has no policy
176 }
177 return OptionsModel::value().toInt();
178}
179
180QString RulePolicy::policyKey(const QString &key) const
181{
182 switch (m_type) {
183 case NoPolicy:
184 return QString();
185 case StringMatch:
186 return QStringLiteral("%1match").arg(key);
187 case SetRule:
188 case ForceRule:
189 return QStringLiteral("%1rule").arg(key);
190 }
191
192 return QString();
193}
194
195QList<RulePolicy::Data> RulePolicy::policyOptions(RulePolicy::Type type)
196{
197 static const auto stringMatchOptions = QList<RulePolicy::Data>{
198 {Rules::UnimportantMatch, i18n("Unimportant")},
199 {Rules::ExactMatch, i18n("Exact Match")},
200 {Rules::SubstringMatch, i18n("Substring Match")},
201 {Rules::RegExpMatch, i18n("Regular Expression")}};
202
203 static const auto setRuleOptions = QList<RulePolicy::Data>{
205 i18n("Apply Initially"),
206 i18n("The window property will be only set to the given value after the window is created."
207 "\nNo further changes will be affected.")},
209 i18n("Apply Now"),
210 i18n("The window property will be set to the given value immediately and will not be affected later"
211 "\n(this action will be deleted afterwards).")},
213 i18n("Remember"),
214 i18n("The value of the window property will be remembered and, every time the window"
215 " is created, the last remembered value will be applied.")},
217 i18n("Do Not Affect"),
218 i18n("The window property will not be affected and therefore the default handling for it will be used."
219 "\nSpecifying this will block more generic window settings from taking effect.")},
221 i18n("Force"),
222 i18n("The window property will be always forced to the given value.")},
224 i18n("Force Temporarily"),
225 i18n("The window property will be forced to the given value until it is hidden"
226 "\n(this action will be deleted after the window is hidden).")}};
227
228 static auto forceRuleOptions = QList<RulePolicy::Data>{
229 setRuleOptions.at(4), // Rules::Force
230 setRuleOptions.at(5), // Rules::ForceTemporarily
231 setRuleOptions.at(3), // Rules::DontAffect
232 };
233
234 switch (type) {
235 case NoPolicy:
236 return {};
237 case StringMatch:
238 return stringMatchOptions;
239 case SetRule:
240 return setRuleOptions;
241 case ForceRule:
242 return forceRuleOptions;
243 }
244 return {};
245}
246
247} // namespace
248
249#include "moc_optionsmodel.cpp"
void selectedIndexChanged(int index)
QHash< int, QByteArray > roleNames() const override
uint bitMask(int index) const
void setValue(QVariant value)
Q_INVOKABLE int indexOf(const QVariant &value) const
QVariant allValues() const
Q_INVOKABLE QString textOfValue(const QVariant &value) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QList< Data > m_data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QVariant value() const
void updateModelData(const QList< Data > &data)
Type type() const
QString policyKey(const QString &key) const
@ ForceTemporarily
Definition rules.h:127
@ DontAffect
Definition rules.h:122
@ SubstringMatch
Definition rules.h:133
@ RegExpMatch
Definition rules.h:134
@ UnimportantMatch
Definition rules.h:131
@ ExactMatch
Definition rules.h:132
Session::Type type
Definition session.cpp:17