Changeset View
Changeset View
Standalone View
Standalone View
applets/systemtray/sortedsystemtraymodel.cpp
Show All 17 Lines | |||||
18 | ***************************************************************************/ | 18 | ***************************************************************************/ | ||
19 | 19 | | |||
20 | #include "sortedsystemtraymodel.h" | 20 | #include "sortedsystemtraymodel.h" | ||
21 | #include "systemtraymodel.h" | 21 | #include "systemtraymodel.h" | ||
22 | #include "debug.h" | 22 | #include "debug.h" | ||
23 | 23 | | |||
24 | #include <QList> | 24 | #include <QList> | ||
25 | 25 | | |||
26 | static const QList<QString> s_categoryOrder = {QStringLiteral("UnknownCategory"), | ||||
27 | QStringLiteral("ApplicationStatus"), | ||||
28 | QStringLiteral("Communications"), | ||||
29 | QStringLiteral("SystemServices"), | ||||
30 | QStringLiteral("Hardware")}; | ||||
31 | | ||||
26 | SortedSystemTrayModel::SortedSystemTrayModel(SortingType sorting, QObject *parent) | 32 | SortedSystemTrayModel::SortedSystemTrayModel(SortingType sorting, QObject *parent) | ||
27 | : QSortFilterProxyModel(parent), | 33 | : QSortFilterProxyModel(parent), | ||
28 | m_sorting(sorting) | 34 | m_sorting(sorting) | ||
29 | { | 35 | { | ||
30 | setSortLocaleAware(true); | 36 | setSortLocaleAware(true); | ||
31 | sort(0); | 37 | sort(0); | ||
32 | } | 38 | } | ||
33 | 39 | | |||
34 | bool SortedSystemTrayModel::lessThan(const QModelIndex &left, const QModelIndex &right) const | 40 | bool SortedSystemTrayModel::lessThan(const QModelIndex &left, const QModelIndex &right) const | ||
35 | { | 41 | { | ||
36 | switch (m_sorting) { | 42 | switch (m_sorting) { | ||
37 | case SortedSystemTrayModel::SortingType::ConfigurationPage: | 43 | case SortedSystemTrayModel::SortingType::ConfigurationPage: | ||
38 | return lessThanConfigurationPage(left, right); | 44 | return lessThanConfigurationPage(left, right); | ||
45 | case SortedSystemTrayModel::SortingType::SystemTray: | ||||
46 | return lessThanSystemTray(left, right); | ||||
39 | } | 47 | } | ||
40 | 48 | | |||
41 | return QSortFilterProxyModel::lessThan(left, right); | 49 | return QSortFilterProxyModel::lessThan(left, right); | ||
42 | } | 50 | } | ||
43 | 51 | | |||
44 | bool SortedSystemTrayModel::lessThanConfigurationPage(const QModelIndex &left, const QModelIndex &right) const | 52 | bool SortedSystemTrayModel::lessThanConfigurationPage(const QModelIndex &left, const QModelIndex &right) const | ||
45 | { | 53 | { | ||
46 | const int categoriesComparison = compareCategoriesAlphabetically(left, right); | 54 | const int categoriesComparison = compareCategoriesAlphabetically(left, right); | ||
47 | if (categoriesComparison == 0) { | 55 | if (categoriesComparison == 0) { | ||
48 | return QSortFilterProxyModel::lessThan(left, right); | 56 | return QSortFilterProxyModel::lessThan(left, right); | ||
49 | } else { | 57 | } else { | ||
50 | return categoriesComparison < 0; | 58 | return categoriesComparison < 0; | ||
51 | } | 59 | } | ||
52 | } | 60 | } | ||
53 | 61 | | |||
62 | bool SortedSystemTrayModel::lessThanSystemTray(const QModelIndex &left, const QModelIndex &right) const | ||||
63 | { | ||||
64 | QVariant itemId = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::ItemId)); | ||||
65 | if (itemId.isValid() && itemId.toString() == QLatin1String("org.kde.plasma.notifications")) { | ||||
66 | return true; | ||||
67 | } | ||||
68 | | ||||
69 | const int categoriesComparison = compareCategoriesOrderly(left, right); | ||||
70 | if (categoriesComparison == 0) { | ||||
71 | return QSortFilterProxyModel::lessThan(left, right); | ||||
72 | } else { | ||||
73 | return categoriesComparison < 0; | ||||
74 | } | ||||
75 | } | ||||
76 | | ||||
54 | int SortedSystemTrayModel::compareCategoriesAlphabetically(const QModelIndex &left, const QModelIndex &right) const | 77 | int SortedSystemTrayModel::compareCategoriesAlphabetically(const QModelIndex &left, const QModelIndex &right) const | ||
55 | { | 78 | { | ||
56 | QVariant leftData = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::Category)); | 79 | QVariant leftData = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::Category)); | ||
57 | QString leftCategory = leftData.isNull() ? QStringLiteral("UnknownCategory") : leftData.toString(); | 80 | QString leftCategory = leftData.isNull() ? QStringLiteral("UnknownCategory") : leftData.toString(); | ||
58 | 81 | | |||
59 | QVariant rightData = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::Category)); | 82 | QVariant rightData = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::Category)); | ||
60 | QString rightCategory = rightData.isNull() ? QStringLiteral("UnknownCategory") : rightData.toString(); | 83 | QString rightCategory = rightData.isNull() ? QStringLiteral("UnknownCategory") : rightData.toString(); | ||
61 | 84 | | |||
62 | return QString::localeAwareCompare(leftCategory, rightCategory); | 85 | return QString::localeAwareCompare(leftCategory, rightCategory); | ||
63 | } | 86 | } | ||
87 | | ||||
88 | int SortedSystemTrayModel::compareCategoriesOrderly(const QModelIndex &left, const QModelIndex &right) const | ||||
89 | { | ||||
90 | QVariant leftData = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::Category)); | ||||
91 | QString leftCategory = leftData.isNull() ? QStringLiteral("UnknownCategory") : leftData.toString(); | ||||
92 | | ||||
93 | QVariant rightData = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::Category)); | ||||
94 | QString rightCategory = rightData.isNull() ? QStringLiteral("UnknownCategory") : rightData.toString(); | ||||
95 | | ||||
96 | int leftIndex = s_categoryOrder.indexOf(leftCategory); | ||||
97 | if (leftIndex == -1) { | ||||
98 | leftIndex = s_categoryOrder.indexOf(QStringLiteral("UnknownCategory")); | ||||
99 | } | ||||
100 | | ||||
101 | int rightIndex = s_categoryOrder.indexOf(rightCategory); | ||||
102 | if (rightIndex == -1) { | ||||
103 | rightIndex = s_categoryOrder.indexOf(QStringLiteral("UnknownCategory")); | ||||
104 | } | ||||
105 | | ||||
106 | return leftIndex - rightIndex; | ||||
107 | } |