Changeset View
Changeset View
Standalone View
Standalone View
kcmkwin/kwinrules/rulebookmodel.cpp
- 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 | #include "rulebookmodel.h" | ||||
22 | | ||||
23 | | ||||
24 | namespace KWin | ||||
25 | { | ||||
26 | | ||||
27 | RuleBookModel::RuleBookModel(QObject *parent) | ||||
28 | : QAbstractListModel(parent) | ||||
29 | , m_ruleBook(new RuleBookSettings(this)) | ||||
30 | { | ||||
31 | } | ||||
32 | | ||||
33 | RuleBookModel::~RuleBookModel() | ||||
34 | { | ||||
35 | qDeleteAll(m_rules); | ||||
36 | } | ||||
37 | | ||||
38 | int RuleBookModel::rowCount(const QModelIndex &parent) const | ||||
39 | { | ||||
40 | Q_UNUSED(parent) | ||||
41 | return m_rules.count(); | ||||
42 | } | ||||
43 | | ||||
44 | QVariant RuleBookModel::data(const QModelIndex &index, int role) const | ||||
45 | { | ||||
46 | if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) { | ||||
47 | return QVariant(); | ||||
48 | } | ||||
49 | | ||||
50 | switch (role) { | ||||
51 | case Qt::DisplayRole: | ||||
52 | return descriptionAt(index.row()); | ||||
53 | default: | ||||
54 | return QVariant(); | ||||
55 | } | ||||
56 | } | ||||
57 | | ||||
58 | bool RuleBookModel::setData(const QModelIndex &index, const QVariant &value, int role) | ||||
59 | { | ||||
60 | if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) { | ||||
61 | return false; | ||||
62 | } | ||||
63 | | ||||
64 | switch (role) { | ||||
65 | case Qt::DisplayRole: | ||||
66 | setDescriptionAt(index.row(), value.toString()); | ||||
67 | return true; | ||||
68 | default: | ||||
69 | return false; | ||||
70 | } | ||||
71 | } | ||||
72 | | ||||
73 | bool RuleBookModel::insertRows(int row, int count, const QModelIndex &parent) | ||||
74 | { | ||||
75 | if (row < 0 || row > rowCount() || parent.isValid()) { | ||||
76 | return false; | ||||
77 | } | ||||
78 | beginInsertRows(parent, row, row + count - 1); | ||||
79 | | ||||
80 | for (int i = 0; i < count; i++) { | ||||
81 | Rules *newRule = new Rules(); | ||||
82 | m_rules.insert(row + i, newRule); | ||||
83 | } | ||||
84 | | ||||
85 | m_ruleBook->setCount(m_rules.count()); | ||||
86 | | ||||
87 | endInsertRows(); | ||||
88 | return true; | ||||
89 | } | ||||
90 | | ||||
91 | bool RuleBookModel::removeRows(int row, int count, const QModelIndex &parent) | ||||
92 | { | ||||
93 | if (row < 0 || row > rowCount() || parent.isValid()) { | ||||
94 | return false; | ||||
95 | } | ||||
96 | beginRemoveRows(parent, row, row + count - 1); | ||||
97 | | ||||
98 | for (int i = 0; i < count; i++) { | ||||
99 | delete m_rules.at(row + i); | ||||
100 | } | ||||
101 | m_rules.remove(row, count); | ||||
102 | m_ruleBook->setCount(m_rules.count()); | ||||
103 | | ||||
104 | endRemoveRows(); | ||||
105 | return true; | ||||
106 | } | ||||
107 | | ||||
108 | bool RuleBookModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, | ||||
109 | const QModelIndex &destinationParent, int destinationChild) | ||||
110 | { | ||||
111 | if (sourceParent != destinationParent || sourceParent != QModelIndex()){ | ||||
112 | return false; | ||||
113 | } | ||||
114 | | ||||
115 | const bool isMoveDown = destinationChild > sourceRow; | ||||
116 | // QAbstractItemModel::beginMoveRows(): when moving rows down in the same parent, | ||||
117 | // the rows will be placed before the destinationChild index. | ||||
118 | if (!beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, | ||||
119 | destinationParent, isMoveDown ? destinationChild + 1 : destinationChild)) { | ||||
120 | return false; | ||||
121 | } | ||||
122 | | ||||
123 | for (int i = 0; i < count; i++) { | ||||
124 | m_rules.insert(destinationChild + i, | ||||
125 | m_rules.takeAt(isMoveDown ? sourceRow : sourceRow + i)); | ||||
126 | } | ||||
127 | | ||||
128 | endMoveRows(); | ||||
129 | return true; | ||||
130 | } | ||||
131 | | ||||
132 | | ||||
133 | QString RuleBookModel::descriptionAt(int row) const | ||||
134 | { | ||||
135 | Q_ASSERT (row >= 0 && row < m_rules.count()); | ||||
136 | return m_rules.at(row)->description; | ||||
137 | } | ||||
138 | | ||||
139 | Rules *RuleBookModel::ruleAt(int row) const | ||||
140 | { | ||||
141 | Q_ASSERT (row >= 0 && row < m_rules.count()); | ||||
142 | return m_rules.at(row); | ||||
143 | } | ||||
144 | | ||||
145 | void RuleBookModel::setDescriptionAt(int row, const QString &description) | ||||
146 | { | ||||
147 | Q_ASSERT (row >= 0 && row < m_rules.count()); | ||||
148 | if (description == m_rules.at(row)->description) { | ||||
149 | return; | ||||
150 | } | ||||
151 | | ||||
152 | m_rules.at(row)->description = description; | ||||
153 | | ||||
154 | emit dataChanged(index(row), index(row), QVector<int>{Qt::DisplayRole}); | ||||
155 | } | ||||
156 | | ||||
157 | void RuleBookModel::setRuleAt(int row, Rules *rule) | ||||
158 | { | ||||
159 | Q_ASSERT (row >= 0 && row < m_rules.count()); | ||||
160 | | ||||
161 | delete m_rules.at(row); | ||||
162 | m_rules[row] = rule; | ||||
163 | | ||||
164 | emit dataChanged(index(row), index(row), QVector<int>{Qt::DisplayRole}); | ||||
165 | } | ||||
166 | | ||||
167 | | ||||
168 | void RuleBookModel::load() | ||||
169 | { | ||||
170 | beginResetModel(); | ||||
171 | | ||||
172 | m_ruleBook->load(); | ||||
173 | qDeleteAll(m_rules); | ||||
174 | m_rules = m_ruleBook->rules(); | ||||
175 | | ||||
176 | endResetModel(); | ||||
177 | } | ||||
178 | | ||||
179 | void RuleBookModel::save() | ||||
180 | { | ||||
181 | m_ruleBook->setRules(m_rules); | ||||
182 | m_ruleBook->save(); | ||||
183 | } | ||||
184 | | ||||
185 | } // namespace |