Changeset View
Changeset View
Standalone View
Standalone View
applets/systemtray/sortedsystemtraymodel.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Konrad Materka <materka@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program; if not, write to the * | ||||
16 | * Free Software Foundation, Inc., * | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | ||||
18 | ***************************************************************************/ | ||||
19 | | ||||
20 | #include "sortedsystemtraymodel.h" | ||||
21 | #include "systemtraymodel.h" | ||||
22 | #include "debug.h" | ||||
23 | | ||||
24 | #include <QList> | ||||
25 | | ||||
26 | static const QList<QString> s_categoryOrder = {QStringLiteral("UnknownCategory"), | ||||
27 | QStringLiteral("ApplicationStatus"), | ||||
28 | QStringLiteral("Communications"), | ||||
29 | QStringLiteral("SystemServices"), | ||||
30 | QStringLiteral("Hardware")}; | ||||
31 | | ||||
32 | SortedSystemTrayModel::SortedSystemTrayModel(SortingType sorting, QObject *parent) | ||||
33 | : QSortFilterProxyModel(parent), | ||||
34 | m_sorting(sorting) | ||||
35 | { | ||||
36 | sort(0); | ||||
37 | } | ||||
38 | | ||||
39 | bool SortedSystemTrayModel::lessThan(const QModelIndex &left, const QModelIndex &right) const | ||||
40 | { | ||||
41 | switch (m_sorting) { | ||||
42 | case SortedSystemTrayModel::SortingType::ConfigurationPage: | ||||
43 | return lessThanConfigurationPage(left, right); | ||||
44 | case SortedSystemTrayModel::SortingType::SystemTray: | ||||
45 | default: | ||||
46 | return lessThanByCategories(left, right); | ||||
47 | } | ||||
48 | } | ||||
49 | | ||||
50 | bool SortedSystemTrayModel::lessThanConfigurationPage(const QModelIndex &left, const QModelIndex &right) const | ||||
51 | { | ||||
52 | const int categoriesComparison = compareCategoriesAlphabetically(left, right); | ||||
53 | if (categoriesComparison == 0) { | ||||
54 | return compareDisplayAlphabetically(left, right); | ||||
55 | } else { | ||||
56 | return categoriesComparison < 0; | ||||
57 | } | ||||
58 | } | ||||
59 | | ||||
60 | bool SortedSystemTrayModel::lessThanByCategories(const QModelIndex &left, const QModelIndex &right) const | ||||
61 | { | ||||
62 | QVariant itemId = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::ItemId)); | ||||
63 | if (itemId.isValid() && itemId.toString() == QLatin1String("org.kde.plasma.notifications")) { | ||||
64 | return true; | ||||
65 | } | ||||
66 | | ||||
67 | const int categoriesComparison = compareCategoriesOrderly(left, right); | ||||
68 | if (categoriesComparison == 0) { | ||||
69 | return compareDisplayAlphabetically(left, right); | ||||
70 | } else { | ||||
71 | return categoriesComparison < 0; | ||||
72 | } | ||||
73 | } | ||||
74 | | ||||
75 | int SortedSystemTrayModel::compareDisplayAlphabetically(const QModelIndex &left, const QModelIndex &right) const | ||||
76 | { | ||||
77 | QString leftData = sourceModel()->data(left).toString(); | ||||
78 | QString rightData = sourceModel()->data(right).toString(); | ||||
79 | | ||||
80 | return QString::localeAwareCompare(leftData, rightData) < 0; | ||||
81 | } | ||||
82 | | ||||
83 | int SortedSystemTrayModel::compareCategoriesAlphabetically(const QModelIndex &left, const QModelIndex &right) const | ||||
84 | { | ||||
85 | QVariant leftData = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::Category)); | ||||
86 | QString leftCategory = leftData.isNull() ? QStringLiteral("UnknownCategory") : leftData.toString(); | ||||
87 | | ||||
88 | QVariant rightData = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::Category)); | ||||
89 | QString rightCategory = rightData.isNull() ? QStringLiteral("UnknownCategory") : rightData.toString(); | ||||
90 | | ||||
91 | return QString::localeAwareCompare(leftCategory, rightCategory); | ||||
92 | } | ||||
93 | | ||||
94 | int SortedSystemTrayModel::compareCategoriesOrderly(const QModelIndex &left, const QModelIndex &right) const | ||||
95 | { | ||||
96 | QVariant leftData = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::Category)); | ||||
97 | QString leftCategory = leftData.isNull() ? QStringLiteral("UnknownCategory") : leftData.toString(); | ||||
98 | | ||||
99 | QVariant rightData = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::Category)); | ||||
100 | QString rightCategory = rightData.isNull() ? QStringLiteral("UnknownCategory") : rightData.toString(); | ||||
101 | | ||||
102 | int leftIndex = s_categoryOrder.indexOf(leftCategory); | ||||
103 | if (leftIndex == -1) { | ||||
104 | leftIndex = s_categoryOrder.indexOf(QStringLiteral("UnknownCategory")); | ||||
105 | } | ||||
106 | | ||||
107 | int rightIndex = s_categoryOrder.indexOf(rightCategory); | ||||
108 | if (rightIndex == -1) { | ||||
109 | rightIndex = s_categoryOrder.indexOf(QStringLiteral("UnknownCategory")); | ||||
110 | } | ||||
111 | | ||||
112 | return leftIndex - rightIndex; | ||||
113 | } |