Plasma-workspace

notificationsortproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2018-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 "notificationsortproxymodel_p.h"
8
9#include <QDateTime>
10
11#include "notifications.h"
12
13using namespace NotificationManager;
14
15NotificationSortProxyModel::NotificationSortProxyModel(QObject *parent)
16 : QSortFilterProxyModel(parent)
17{
18 setRecursiveFilteringEnabled(true);
19 sort(0);
20}
21
22NotificationSortProxyModel::~NotificationSortProxyModel() = default;
23
24Notifications::SortMode NotificationSortProxyModel::sortMode() const
25{
26 return m_sortMode;
27}
28
29void NotificationSortProxyModel::setSortMode(Notifications::SortMode sortMode)
30{
31 if (m_sortMode != sortMode) {
32 m_sortMode = sortMode;
33 invalidate();
34 Q_EMIT sortModeChanged();
35 }
36}
37
38Qt::SortOrder NotificationSortProxyModel::sortOrder() const
39{
40 return m_sortOrder;
41}
42
43void NotificationSortProxyModel::setSortOrder(Qt::SortOrder sortOrder)
44{
45 if (m_sortOrder != sortOrder) {
46 m_sortOrder = sortOrder;
47 invalidate();
48 Q_EMIT sortOrderChanged();
49 }
50}
51
52int sortScore(const QModelIndex &idx)
53{
54 const auto urgency = idx.data(Notifications::UrgencyRole).toInt();
55 if (urgency == Notifications::CriticalUrgency) {
56 return 3;
57 }
58
60 return 2;
61 }
62
63 if (urgency == Notifications::NormalUrgency) {
64 return 1;
65 }
66
67 if (urgency == Notifications::LowUrgency) {
68 return 0;
69 }
70
71 return -1;
72}
73
74bool NotificationSortProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
75{
76 // Sort order is (descending):
77 // - Critical notifications
78 // - Jobs
79 // - Normal notifications
80 // - Low urgency notifications
81 // Within each group it's descending by created or last modified
82
83 int scoreLeft = 0;
84 int scoreRight = 0;
85
86 if (m_sortMode == Notifications::SortByTypeAndUrgency) {
87 scoreLeft = sortScore(source_left);
88 Q_ASSERT(scoreLeft >= 0);
89 scoreRight = sortScore(source_right);
90 Q_ASSERT(scoreRight >= 0);
91 }
92
93 if (scoreLeft == scoreRight) {
94 const QDateTime timeLeft = source_left.data(Notifications::CreatedRole).toDateTime();
95 const QDateTime timeRight = source_right.data(Notifications::CreatedRole).toDateTime();
96
97 if (m_sortOrder == Qt::DescendingOrder) {
98 return timeLeft > timeRight;
99 } else {
100 return timeLeft < timeRight;
101 }
102 }
103
104 return scoreLeft > scoreRight;
105}
SortMode
The sort mode for the model.
@ SortByTypeAndUrgency
Sort notifications taking into account their type and urgency. The order is (descending): Critical,...
@ JobType
This item represents an application job.
@ LowUrgency
The notification has low urgency, it is not important and may not be shown or added to a history.
@ NormalUrgency
The notification has normal urgency. This is also the default if no urgecny is supplied.
@ CreatedRole
When the notification was first created.
@ UrgencyRole
The notification urgency, either LowUrgency, NormalUrgency, or CriticalUrgency. Jobs do not have an u...
@ TypeRole
The type of model entry, either NotificationType or JobType.
QVariant data(int role) const const
SortOrder
QDateTime toDateTime() const const
int toInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:42 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.