Debian Codegraph @

src/librssguard/services/abstract/accountcheckmodel.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "services/abstract/accountcheckmodel.h"
4
5 #include "3rd-party/boolinq/boolinq.h"
6 #include "definitions/definitions.h"
7 #include "services/abstract/feed.h"
8
9 AccountCheckModel::AccountCheckModel(QObject* parent)
10 : QAbstractItemModel(parent), m_rootItem(nullptr), m_recursiveChange(false) {}
11
12 RootItem* AccountCheckModel::itemForIndex(const QModelIndex& index) const {
13 if (index.isValid() && index.model() == this) {
14 return static_cast<RootItem*>(index.internalPointer());
15 }
16 else {
17 return m_rootItem;
18 }
19 }
20
21 RootItem* AccountCheckModel::rootItem() const {
22 return m_rootItem;
23 }
24
25 void AccountCheckModel::setRootItem(RootItem* root_item, bool delete_previous_root, bool with_layout_change) {
26 if (with_layout_change) {
27 emit layoutAboutToBeChanged();
28 }
29
30 beginResetModel();
31
32 if (delete_previous_root && m_rootItem != nullptr) {
33 m_rootItem->deleteLater();
34 }
35
36 m_checkStates.clear();
37 m_rootItem = root_item;
38
39 endResetModel();
40
41 if (with_layout_change) {
42 emit layoutChanged();
43 }
44 }
45
46 void AccountCheckModel::checkAllItems() {
47 if (m_rootItem != nullptr) {
48 auto chi = m_rootItem->childItems();
49
50 for (RootItem* root_child : std::as_const(chi)) {
51 if (root_child->kind() == RootItem::Kind::Feed || root_child->kind() == RootItem::Kind::Category) {
52 setItemChecked(root_child, Qt::CheckState::Checked);
53 }
54 }
55 }
56 }
57
58 void AccountCheckModel::uncheckAllItems() {
59 if (m_rootItem != nullptr) {
60 auto chi = m_rootItem->childItems();
61
62 for (RootItem* root_child : std::as_const(chi)) {
63 if (root_child->kind() == RootItem::Kind::Feed || root_child->kind() == RootItem::Kind::Category) {
64 setData(indexForItem(root_child), Qt::CheckState::Unchecked, Qt::ItemDataRole::CheckStateRole);
65 }
66 }
67 }
68 }
69
70 QModelIndex AccountCheckModel::index(int row, int column, const QModelIndex& parent) const {
71 if (!hasIndex(row, column, parent)) {
72 return {};
73 }
74
75 RootItem* parent_item = itemForIndex(parent);
76 RootItem* child_item = parent_item->child(row);
77
78 if (child_item != nullptr) {
79 return createIndex(row, column, child_item);
80 }
81 else {
82 return {};
83 }
84 }
85
86 QModelIndex AccountCheckModel::indexForItem(RootItem* item) const {
87 if (item == nullptr || item->kind() == RootItem::Kind::ServiceRoot || item->kind() == RootItem::Kind::Root) {
88 // Root item lies on invalid index.
89 return QModelIndex();
90 }
91
92 QList<QModelIndex> parents;
93
94 // Start with root item (which obviously has invalid index).
95 parents << indexForItem(m_rootItem);
96
97 while (!parents.isEmpty()) {
98 QModelIndex active_index = parents.takeFirst();
99 int row_count = rowCount(active_index);
100
101 if (row_count > 0) {
102 // This index has children.
103 // Lets take a look if our target item is among them.
104 RootItem* active_item = itemForIndex(active_index);
105 int candidate_index = active_item->childItems().indexOf(item);
106
107 if (candidate_index >= 0) {
108 // We found our item.
109 return index(candidate_index, 0, active_index);
110 }
111 else {
112 // Item is not found, add all "categories" from active_item.
113 for (int i = 0; i < row_count; i++) {
114 RootItem* possible_category = active_item->child(i);
115
116 if (possible_category->kind() == RootItem::Kind::Category) {
117 parents << index(i, 0, active_index);
118 }
119 }
120 }
121 }
122 }
123
124 return QModelIndex();
125 }
126
127 QModelIndex AccountCheckModel::parent(const QModelIndex& child) const {
128 if (!child.isValid()) {
129 return {};
130 }
131
132 RootItem* child_item = itemForIndex(child);
133 RootItem* parent_item = child_item->parent();
134
135 if (parent_item == m_rootItem || parent_item == nullptr) {
136 return {};
137 }
138 else {
139 return createIndex(parent_item->row(), 0, parent_item);
140 }
141 }
142
143 int AccountCheckModel::rowCount(const QModelIndex& parent) const {
144 if (parent.column() > 0) {
145 return 0;
146 }
147 else {
148 RootItem* item = itemForIndex(parent);
149
150 if (item != nullptr) {
151 return item->childCount();
152 }
153 else {
154 return 0;
155 }
156 }
157 }
158
159 int AccountCheckModel::columnCount(const QModelIndex& parent) const {
160 Q_UNUSED(parent)
161 return 1;
162 }
163
164 QVariant AccountCheckModel::data(const QModelIndex& index, int role) const {
165 if (index.column() != 0) {
166 return QVariant();
167 }
168
169 RootItem* item = itemForIndex(index);
170
171 if (role == Qt::ItemDataRole::CheckStateRole) {
172 if (m_checkStates.contains(item)) {
173 return m_checkStates.value(item);
174 }
175 else {
176 return static_cast<int>(Qt::CheckState::Unchecked);
177 }
178 }
179 else if (role == Qt::ItemDataRole::DecorationRole) {
180 return item->data(0, Qt::ItemDataRole::DecorationRole);
181 }
182 else if (role == Qt::ItemDataRole::EditRole) {
183 return QVariant::fromValue(item);
184 }
185 else if (role == Qt::ItemDataRole::DisplayRole) {
186 switch (item->kind()) {
187 case RootItem::Kind::Category:
188 return QVariant(item->data(index.column(), role).toString() + QSL(" ") + tr("(category)"));
189
190 case RootItem::Kind::Feed:
191 return QVariant(item->data(index.column(), role).toString() + QSL(" ") + tr("(feed)"));
192
193 default:
194 return item->title();
195 }
196 }
197 else if (role == Qt::ItemDataRole::ToolTipRole) {
198 if (item->kind() == RootItem::Kind::Feed) {
199 auto desc = item->description();
200 auto url = item->toFeed()->source();
201
202 QString txt = url;
203
204 if (!desc.isEmpty()) {
205 txt += QSL("\n\n%1").arg(desc);
206 }
207
208 return txt;
209 }
210 }
211
212 return QVariant();
213 }
214
215 bool AccountCheckModel::setData(const QModelIndex& index, const QVariant& value, int role) {
216 if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole) {
217 RootItem* item = itemForIndex(index);
218
219 if (item == m_rootItem) {
220 // Cannot set data on root item.
221 return false;
222 }
223
224 // Change data for the actual item.
225 m_checkStates[item] = static_cast<Qt::CheckState>(value.toInt());
226 emit dataChanged(index, index);
227 emit checkStateChanged(item, m_checkStates[item]);
228
229 if (m_recursiveChange) {
230 return true;
231 }
232
233 // Set new data for all descendants of this actual item.
234 auto chi = item->childItems();
235
236 for (RootItem* child : std::as_const(chi)) {
237 setData(indexForItem(child), value, Qt::CheckStateRole);
238 }
239
240 // Now we need to change new data to all parents.
241 QModelIndex parent_index = index;
242
243 m_recursiveChange = true;
244
245 // Iterate all valid parents.
246 while ((parent_index = parent_index.parent()).isValid()) {
247 // We now have parent index. Get parent item too.
248 item = item->parent();
249
250 // Check children of this new parent item.
251 bool all_checked = true;
252 bool all_unchecked = true;
253 auto childr = item->childItems();
254
255 for (RootItem* child_of_parent : std::as_const(childr)) {
256 if (m_checkStates.contains(child_of_parent)) {
257 all_checked &= m_checkStates[child_of_parent] == Qt::CheckState::Checked;
258 all_unchecked &= m_checkStates[child_of_parent] == Qt::CheckState::Unchecked;
259 }
260 else {
261 all_checked = false;
262 }
263 }
264
265 if (all_checked) {
266 setData(parent_index, Qt::CheckState::Checked, Qt::CheckStateRole);
267 }
268 else if (all_unchecked) {
269 setData(parent_index, Qt::CheckState::Unchecked, Qt::CheckStateRole);
270 }
271 else {
272 setData(parent_index, Qt::CheckState::PartiallyChecked, Qt::CheckStateRole);
273 }
274 }
275
276 m_recursiveChange = false;
277 return true;
278 }
279
280 return false;
281 }
282
283 Qt::ItemFlags AccountCheckModel::flags(const QModelIndex& index) const {
284 if (!index.isValid() || (itemForIndex(index)->kind() != RootItem::Kind::Category &&
285 itemForIndex(index)->kind() != RootItem::Kind::Feed)) {
286 return Qt::ItemFlag::NoItemFlags;
287 }
288
289 Qt::ItemFlags flags = Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsSelectable;
290
291 if (index.column() == 0) {
292 flags |= Qt::ItemFlag::ItemIsUserCheckable;
293 }
294
295 return flags;
296 }
297
298 QList<RootItem*> AccountCheckModel::checkedItems() const {
299 auto keys = m_checkStates.keys();
300 auto res = boolinq::from(keys)
301 .where([&](const auto& key) {
302 return m_checkStates.value(key) == Qt::CheckState::Checked;
303 })
304 .toStdList();
305
306 return FROM_STD_LIST(QList<RootItem*>, res);
307 }
308
309 bool AccountCheckModel::isItemChecked(RootItem* item) const {
310 return m_checkStates.value(item, Qt::CheckState::Unchecked) != Qt::CheckState::Unchecked;
311 }
312
313 bool AccountCheckModel::setItemChecked(RootItem* item, Qt::CheckState check) {
314 return setData(indexForItem(item), check, Qt::ItemDataRole::CheckStateRole);
315 }
316
317 AccountCheckSortedModel::AccountCheckSortedModel(QObject* parent)
318 : QSortFilterProxyModel(parent), m_sourceModel(new AccountCheckModel(parent)) {
319 setDynamicSortFilter(false);
320 setSourceModel(m_sourceModel);
321 sort(0, Qt::SortOrder::AscendingOrder);
322 }
323
324 bool AccountCheckSortedModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const {
325 auto* lhs = m_sourceModel->itemForIndex(source_left);
326 auto* rhs = m_sourceModel->itemForIndex(source_right);
327
328 if (lhs != nullptr && rhs != nullptr) {
329 QList<RootItem::Kind> priorities = {RootItem::Kind::Category,
330 RootItem::Kind::Feed,
331 RootItem::Kind::Labels,
332 RootItem::Kind::Important,
333 RootItem::Kind::Unread,
334 RootItem::Kind::Bin};
335
336 if (lhs->keepOnTop()) {
337 return sortOrder() == Qt::SortOrder::AscendingOrder;
338 }
339 else if (rhs->keepOnTop()) {
340 return sortOrder() == Qt::SortOrder::DescendingOrder;
341 }
342
343 auto left_priority = priorities.indexOf(lhs->kind());
344 auto right_priority = priorities.indexOf(rhs->kind());
345
346 if (left_priority == right_priority) {
347 return QString::localeAwareCompare(lhs->title().toLower(), rhs->title().toLower()) < 0;
348 }
349 else {
350 return sortOrder() == Qt::SortOrder::AscendingOrder ? left_priority < right_priority
351 : right_priority < left_priority;
352 }
353 }
354
355 return false;
356 }
357
358 AccountCheckModel* AccountCheckSortedModel::sourceModel() const {
359 return m_sourceModel;
360 }
361
362 void AccountCheckSortedModel::setRootItem(RootItem* root_item, bool delete_previous_root, bool with_layout_change) {
363 setSourceModel(nullptr);
364 m_sourceModel->setRootItem(root_item, delete_previous_root, with_layout_change);
365 setSourceModel(m_sourceModel);
366 }
367
368 void AccountCheckSortedModel::checkAllItems() {
369 m_sourceModel->checkAllItems();
370 }
371
372 void AccountCheckSortedModel::uncheckAllItems() {
373 m_sourceModel->uncheckAllItems();
374 }
375
376 bool AccountCheckSortedModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const {
377 auto kind = m_sourceModel->itemForIndex(m_sourceModel->index(source_row, 0, source_parent))->kind();
378
379 return kind == RootItem::Kind::Root || kind == RootItem::Kind::ServiceRoot || kind == RootItem::Kind::Category ||
380 kind == RootItem::Kind::Feed;
381 }