Plasma-workspace

notificationgroupcollapsingproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@privat.broulik.de>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "notificationgroupcollapsingproxymodel_p.h"
8
9#include "notifications.h"
10
11#include "debug.h"
12
13using namespace NotificationManager;
14
15NotificationGroupCollapsingProxyModel::NotificationGroupCollapsingProxyModel(QObject *parent)
16 : QSortFilterProxyModel(parent)
17{
18}
19
20NotificationGroupCollapsingProxyModel::~NotificationGroupCollapsingProxyModel() = default;
21
22void NotificationGroupCollapsingProxyModel::setSourceModel(QAbstractItemModel *source)
23{
24 if (source == QAbstractProxyModel::sourceModel()) {
25 return;
26 }
27
29 disconnect(QAbstractProxyModel::sourceModel(), nullptr, this, nullptr);
30 }
31
33
34 if (source) {
35 connect(source, &QAbstractItemModel::rowsInserted, this, &NotificationGroupCollapsingProxyModel::invalidateFilter);
36 connect(source, &QAbstractItemModel::rowsRemoved, this, &NotificationGroupCollapsingProxyModel::invalidateFilter);
37
38 // When a group is removed, there is no item that's being removed, instead the item morphs back into a single notification
39 connect(source,
41 this,
42 [this, source](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles) {
43 if (roles.isEmpty() || roles.contains(Notifications::IsGroupRole)) {
44 for (int i = topLeft.row(); i <= bottomRight.row(); ++i) {
45 const QModelIndex sourceIdx = source->index(i, 0);
46
47 if (!sourceIdx.data(Notifications::IsGroupRole).toBool()) {
48 if (m_expandedGroups.contains(sourceIdx)) {
49 setGroupExpanded(topLeft, false);
50 }
51 }
52 }
53 }
54 });
55 }
56}
57
58QVariant NotificationGroupCollapsingProxyModel::data(const QModelIndex &index, int role) const
59{
60 switch (role) {
62 if (m_limit > 0) {
63 // so each item in a group knows whether the group is expanded
64 const QModelIndex sourceIdx = mapToSource(index);
65 return m_expandedGroups.contains(sourceIdx.parent().isValid() ? sourceIdx.parent() : sourceIdx);
66 }
67 return true;
68 }
70 return rowCount(index.parent().isValid() ? index.parent() : index);
71 }
72
73 return QSortFilterProxyModel::data(index, role);
74}
75
76bool NotificationGroupCollapsingProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
77{
78 if (role == Notifications::IsGroupExpandedRole && m_limit > 0) {
79 QModelIndex groupIdx = index;
80 // so an item inside a group can expand/collapse the group
81 if (groupIdx.parent().isValid()) {
82 groupIdx = groupIdx.parent();
83 }
84
85 const bool expanded = value.toBool();
86 if (!groupIdx.data(Notifications::IsGroupRole).toBool()) {
87 qCWarning(NOTIFICATIONMANAGER) << "Cannot" << (expanded ? "expand" : "collapse") << "an item isn't a group or inside of one";
88 return false;
89 }
90
91 return setGroupExpanded(groupIdx, expanded);
92 }
93
94 return QSortFilterProxyModel::setData(index, value, role);
95}
96
97int NotificationGroupCollapsingProxyModel::limit() const
98{
99 return m_limit;
100}
101
102void NotificationGroupCollapsingProxyModel::setLimit(int limit)
103{
104 if (m_limit != limit) {
105 m_limit = limit;
106 invalidateFilter();
107 invalidateGroupRoles();
108 Q_EMIT limitChanged();
109 }
110}
111
112QDateTime NotificationGroupCollapsingProxyModel::lastRead() const
113{
114 return m_lastRead;
115}
116
117void NotificationGroupCollapsingProxyModel::setLastRead(const QDateTime &lastRead)
118{
119 if (m_lastRead != lastRead) {
120 m_lastRead = lastRead;
121 invalidateFilter();
122 invalidateGroupRoles();
123 Q_EMIT lastReadChanged();
124 }
125}
126
127bool NotificationGroupCollapsingProxyModel::expandUnread() const
128{
129 return m_expandUnread;
130}
131
132void NotificationGroupCollapsingProxyModel::setExpandUnread(bool expand)
133{
134 if (m_expandUnread != expand) {
135 m_expandUnread = expand;
136 invalidateFilter();
137 invalidateGroupRoles();
138 Q_EMIT expandUnreadChanged();
139 }
140}
141
142void NotificationGroupCollapsingProxyModel::collapseAll()
143{
144 m_expandedGroups.clear();
145
146 invalidateFilter();
147 invalidateGroupRoles();
148}
149
150bool NotificationGroupCollapsingProxyModel::setGroupExpanded(const QModelIndex &idx, bool expanded)
151{
152 if (idx.data(Notifications::IsGroupExpandedRole).toBool() == expanded) {
153 return false;
154 }
155
156 QPersistentModelIndex persistentIdx(mapToSource(idx));
157 if (expanded) {
158 m_expandedGroups.append(persistentIdx);
159 } else {
160 m_expandedGroups.removeOne(persistentIdx);
161 }
162
163 invalidateFilter();
164
166
167 Q_EMIT dataChanged(idx, idx, dirtyRoles);
168 Q_EMIT dataChanged(index(0, 0, idx), index(rowCount(idx) - 1, 0, idx), dirtyRoles);
169
170 return true;
171}
172
173void NotificationGroupCollapsingProxyModel::invalidateGroupRoles()
174{
176
177 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), dirtyRoles);
178
179 for (int row = 0; row < rowCount(); ++row) {
180 const QModelIndex groupIdx = index(row, 0);
181 Q_EMIT dataChanged(index(0, 0, groupIdx), index(rowCount(groupIdx) - 1, 0, groupIdx), dirtyRoles);
182 }
183}
184
185bool NotificationGroupCollapsingProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
186{
187 if (m_limit > 0 && source_parent.isValid()) {
188 if (!m_expandedGroups.isEmpty() && m_expandedGroups.contains(source_parent)) {
189 return true;
190 }
191
192 if (m_expandUnread && m_lastRead.isValid()) {
193 const QModelIndex sourceIdx = sourceModel()->index(source_row, 0, source_parent);
194
195 if (!sourceIdx.data(Notifications::ReadRole).toBool()) {
197 if (!time.isValid()) {
198 time = sourceIdx.data(Notifications::CreatedRole).toDateTime();
199 }
200
201 if (time.isValid() && m_lastRead < time) {
202 return true;
203 }
204 }
205 }
206
207 // should we raise the limit when there's just one group?
208
209 // FIXME why is this reversed?
210 // grouping proxy model seems to reverse the order?
211 return source_row >= sourceModel()->rowCount(source_parent) - m_limit;
212 }
213
214 return true;
215}
216
217#include "moc_notificationgroupcollapsingproxymodel_p.cpp"
@ UpdatedRole
When the notification was last updated, invalid when it hasn't been updated.
@ ReadRole
Whether the notification got read by the user.
@ IsGroupExpandedRole
Whether the group is expanded, this role is writable.
@ IsGroupRole
Whether the item is a group.
@ CreatedRole
When the notification was first created.
@ ExpandedGroupChildrenCountRole
The number of children in a group that are expanded.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
void rowsInserted(const QModelIndex &parent, int first, int last)
void rowsRemoved(const QModelIndex &parent, int first, int last)
bool isValid() const const
bool contains(const AT &value) const const
bool isEmpty() const const
QVariant data(int role) const const
bool isValid() const const
QModelIndex parent() const const
int row() const const
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool toBool() const const
QDateTime toDateTime() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 12:00:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.