Changeset View
Changeset View
Standalone View
Standalone View
libnotificationmanager/notificationsortproxymodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018-2019 Kai Uwe Broulik <kde@privat.broulik.de> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library 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 GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | | ||||
22 | #include "notificationsortproxymodel_p.h" | ||||
23 | | ||||
24 | #include <QDateTime> | ||||
25 | | ||||
26 | #include "notifications.h" | ||||
27 | | ||||
28 | using namespace NotificationManager; | ||||
29 | | ||||
30 | NotificationSortProxyModel::NotificationSortProxyModel(QObject *parent) | ||||
31 | : QSortFilterProxyModel(parent) | ||||
32 | { | ||||
33 | setRecursiveFilteringEnabled(true); | ||||
34 | sort(0); | ||||
35 | } | ||||
36 | | ||||
37 | NotificationSortProxyModel::~NotificationSortProxyModel() = default; | ||||
38 | | ||||
39 | Notifications::SortMode NotificationSortProxyModel::sortMode() const | ||||
40 | { | ||||
41 | return m_sortMode; | ||||
42 | } | ||||
43 | | ||||
44 | void NotificationSortProxyModel::setSortMode(Notifications::SortMode sortMode) | ||||
45 | { | ||||
46 | if (m_sortMode != sortMode) { | ||||
47 | m_sortMode = sortMode; | ||||
48 | invalidate(); | ||||
49 | emit sortModeChanged(); | ||||
50 | } | ||||
51 | } | ||||
52 | | ||||
53 | int sortScore(const QModelIndex &idx) | ||||
54 | { | ||||
55 | const auto urgency = idx.data(Notifications::UrgencyRole).toInt(); | ||||
56 | if (urgency == Notifications::CriticalUrgency) { | ||||
57 | return 3; | ||||
58 | } | ||||
59 | | ||||
60 | const int type = idx.data(Notifications::TypeRole).toInt(); | ||||
61 | if (type == Notifications::JobType) { | ||||
62 | const int jobState = idx.data(Notifications::JobStateRole).toInt(); | ||||
63 | // Treat finished jobs as normal notifications but running jobs more important | ||||
64 | if (jobState == Notifications::JobStateStopped) { | ||||
65 | return 1; | ||||
66 | } else { | ||||
67 | return 2; | ||||
68 | } | ||||
69 | } | ||||
70 | | ||||
71 | if (urgency == Notifications::NormalUrgency) { | ||||
72 | return 1; | ||||
73 | } | ||||
74 | | ||||
75 | if (urgency == Notifications::LowUrgency) { | ||||
76 | return 0; | ||||
77 | } | ||||
78 | | ||||
79 | return -1; | ||||
80 | } | ||||
81 | | ||||
82 | bool NotificationSortProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const | ||||
83 | { | ||||
84 | // Sort order is (descending): | ||||
85 | // - Critical notifications | ||||
86 | // - Jobs | ||||
87 | // - Normal notifications | ||||
88 | // - Low urgency notifications | ||||
89 | // Within each group it's descending by created or last modified | ||||
90 | | ||||
91 | int scoreLeft = 0; | ||||
92 | int scoreRight = 0; | ||||
93 | | ||||
94 | if (m_sortMode == Notifications::SortByTypeAndUrgency) { | ||||
95 | scoreLeft = sortScore(source_left); | ||||
96 | Q_ASSERT(scoreLeft >= 0); | ||||
97 | scoreRight = sortScore(source_right); | ||||
98 | Q_ASSERT(scoreRight >= 0); | ||||
99 | } | ||||
100 | | ||||
101 | if (scoreLeft == scoreRight) { | ||||
102 | QDateTime timeLeft = source_left.data(Notifications::UpdatedRole).toDateTime(); | ||||
103 | if (!timeLeft.isValid()) { | ||||
104 | timeLeft = source_left.data(Notifications::CreatedRole).toDateTime(); | ||||
105 | } | ||||
106 | | ||||
107 | QDateTime timeRight = source_right.data(Notifications::UpdatedRole).toDateTime(); | ||||
108 | if (!timeRight.isValid()) { | ||||
109 | timeRight = source_right.data(Notifications::CreatedRole).toDateTime(); | ||||
110 | } | ||||
111 | | ||||
112 | // sorts descending by time (newest first) | ||||
113 | return timeLeft > timeRight; | ||||
114 | } | ||||
115 | | ||||
116 | return scoreLeft > scoreRight; | ||||
117 | } |