KWin
Loading...
Searching...
No Matches
rulebookmodel.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 "rulebookmodel.h"
8
9namespace KWin
10{
11
13 : QAbstractListModel(parent)
14 , m_ruleBook(new RuleBookSettings(this))
15{
16}
17
21
22QHash<int, QByteArray> RuleBookModel::roleNames() const
23{
24 auto roles = QAbstractListModel::roleNames();
25 roles.insert(DescriptionRole, QByteArray("display"));
26 return roles;
27}
28
29int RuleBookModel::rowCount(const QModelIndex &parent) const
30{
31 return m_ruleBook->ruleCount();
32}
33
34QVariant RuleBookModel::data(const QModelIndex &index, int role) const
35{
36 if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) {
37 return QVariant();
38 }
39
40 if (index.row() < 0 || index.row() >= rowCount()) {
41 return QVariant();
42 }
43
44 const RuleSettings *settings = m_ruleBook->ruleSettingsAt(index.row());
45
46 switch (role) {
48 return settings->description();
49 }
50
51 return QVariant();
52}
53
54bool RuleBookModel::setData(const QModelIndex &index, const QVariant &value, int role)
55{
56 if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) {
57 return false;
58 }
59
60 RuleSettings *settings = m_ruleBook->ruleSettingsAt(index.row());
61
62 switch (role) {
64 if (settings->description() == value.toString()) {
65 return true;
66 }
67 settings->setDescription(value.toString());
68 break;
69 default:
70 return false;
71 }
72
73 Q_EMIT dataChanged(index, index, {role});
74
75 return true;
76}
77
78bool RuleBookModel::insertRows(int row, int count, const QModelIndex &parent)
79{
80 if (row < 0 || row > rowCount() || parent.isValid()) {
81 return false;
82 }
83
84 beginInsertRows(parent, row, row + count - 1);
85 for (int i = 0; i < count; i++) {
86 RuleSettings *settings = m_ruleBook->insertRuleSettingsAt(row + i);
87 settings->setWmclassmatch(Rules::ExactMatch); // We want ExactMatch as default for new rules in the UI
88 }
89 endInsertRows();
90
91 return true;
92}
93
94bool RuleBookModel::removeRows(int row, int count, const QModelIndex &parent)
95{
96 if (row < 0 || row > rowCount() || parent.isValid()) {
97 return false;
98 }
99
100 beginRemoveRows(parent, row, row + count - 1);
101 for (int i = 0; i < count; i++) {
102 m_ruleBook->removeRuleSettingsAt(row + i);
103 }
104 endRemoveRows();
105
106 return true;
107}
108
109bool RuleBookModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
110 const QModelIndex &destinationParent, int destinationChild)
111{
112 if (sourceParent != destinationParent || sourceParent != QModelIndex()) {
113 return false;
114 }
115
116 const bool isMoveDown = destinationChild > sourceRow;
117 // QAbstractItemModel::beginMoveRows(): when moving rows down in the same parent,
118 // the rows will be placed before the destinationChild index.
119 if (!beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1,
120 destinationParent, isMoveDown ? destinationChild + 1 : destinationChild)) {
121 return false;
122 }
123
124 for (int i = 0; i < count; i++) {
125 m_ruleBook->moveRuleSettings(isMoveDown ? sourceRow : sourceRow + i, destinationChild);
126 }
127
128 endMoveRows();
129 return true;
130}
131
132QString RuleBookModel::descriptionAt(int row) const
133{
134 Q_ASSERT(row >= 0 && row < rowCount());
135 return m_ruleBook->ruleSettingsAt(row)->description();
136}
137
138RuleSettings *RuleBookModel::ruleSettingsAt(int row) const
139{
140 Q_ASSERT(row >= 0 && row < rowCount());
141 return m_ruleBook->ruleSettingsAt(row);
142}
143
144void RuleBookModel::setDescriptionAt(int row, const QString &description)
145{
146 Q_ASSERT(row >= 0 && row < rowCount());
147 if (description == m_ruleBook->ruleSettingsAt(row)->description()) {
148 return;
149 }
150
151 m_ruleBook->ruleSettingsAt(row)->setDescription(description);
152
153 Q_EMIT dataChanged(index(row), index(row), {});
154}
155
156void RuleBookModel::setRuleSettingsAt(int row, const RuleSettings &settings)
157{
158 Q_ASSERT(row >= 0 && row < rowCount());
159
160 copySettingsTo(ruleSettingsAt(row), settings);
161
162 Q_EMIT dataChanged(index(row), index(row), {});
163}
164
166{
167 beginResetModel();
168
169 m_ruleBook->load();
170
171 endResetModel();
172}
173
175{
176 m_ruleBook->save();
177}
178
180{
181 return m_ruleBook->usrIsSaveNeeded();
182}
183
184void RuleBookModel::copySettingsTo(RuleSettings *dest, const RuleSettings &source)
185{
186 dest->setDefaults();
187 const KConfigSkeletonItem::List itemList = source.items();
188 for (const KConfigSkeletonItem *item : itemList) {
189 dest->findItem(item->name())->setProperty(item->property());
190 }
191}
192
193} // namespace
194
195#include "moc_rulebookmodel.cpp"
QHash< int, QByteArray > roleNames() const override
RuleSettings * ruleSettingsAt(int row) const
void setDescriptionAt(int row, const QString &description)
static void copySettingsTo(RuleSettings *dest, const RuleSettings &source)
QString descriptionAt(int row) const
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
RuleBookModel(QObject *parent=nullptr)
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
void setRuleSettingsAt(int row, const RuleSettings &settings)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
RuleSettings * ruleSettingsAt(int row) const
void moveRuleSettings(int srcRow, int destRow)
RuleSettings * insertRuleSettingsAt(int row)
@ ExactMatch
Definition rules.h:132