File indexing completed on 2024-05-12 17:08:50
0001 /* 0002 SPDX-FileCopyrightText: 2019 Konrad Materka <materka@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "sortedsystemtraymodel.h" 0008 #include "debug.h" 0009 #include "systemtraymodel.h" 0010 0011 #include <QList> 0012 0013 static const QList<QString> s_categoryOrder = { 0014 QStringLiteral("UnknownCategory"), 0015 QStringLiteral("ApplicationStatus"), 0016 QStringLiteral("Communications"), 0017 QStringLiteral("SystemServices"), 0018 QStringLiteral("Hardware"), 0019 }; 0020 0021 SortedSystemTrayModel::SortedSystemTrayModel(SortingType sorting, QObject *parent) 0022 : QSortFilterProxyModel(parent) 0023 , m_sorting(sorting) 0024 { 0025 setSortLocaleAware(true); 0026 sort(0); 0027 } 0028 0029 bool SortedSystemTrayModel::lessThan(const QModelIndex &left, const QModelIndex &right) const 0030 { 0031 switch (m_sorting) { 0032 case SortedSystemTrayModel::SortingType::ConfigurationPage: 0033 return lessThanConfigurationPage(left, right); 0034 case SortedSystemTrayModel::SortingType::SystemTray: 0035 return lessThanSystemTray(left, right); 0036 } 0037 0038 return QSortFilterProxyModel::lessThan(left, right); 0039 } 0040 0041 bool SortedSystemTrayModel::lessThanConfigurationPage(const QModelIndex &left, const QModelIndex &right) const 0042 { 0043 const int categoriesComparison = compareCategoriesAlphabetically(left, right); 0044 if (categoriesComparison == 0) { 0045 return QSortFilterProxyModel::lessThan(left, right); 0046 } else { 0047 return categoriesComparison < 0; 0048 } 0049 } 0050 0051 bool SortedSystemTrayModel::lessThanSystemTray(const QModelIndex &left, const QModelIndex &right) const 0052 { 0053 QVariant itemIdLeft = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::ItemId)); 0054 QVariant itemIdRight = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::ItemId)); 0055 if (itemIdRight.toString() == QLatin1String("org.kde.plasma.notifications")) { 0056 // return false when at least right is "org.kde.plasma.notifications" 0057 return false; 0058 } else if (itemIdLeft.toString() == QLatin1String("org.kde.plasma.notifications")) { 0059 // return true when only left is "org.kde.plasma.notifications" 0060 return true; 0061 } 0062 0063 const int categoriesComparison = compareCategoriesOrderly(left, right); 0064 if (categoriesComparison == 0) { 0065 return QSortFilterProxyModel::lessThan(left, right); 0066 } else { 0067 return categoriesComparison < 0; 0068 } 0069 } 0070 0071 int SortedSystemTrayModel::compareCategoriesAlphabetically(const QModelIndex &left, const QModelIndex &right) const 0072 { 0073 QVariant leftData = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::Category)); 0074 QString leftCategory = leftData.isNull() ? QStringLiteral("UnknownCategory") : leftData.toString(); 0075 0076 QVariant rightData = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::Category)); 0077 QString rightCategory = rightData.isNull() ? QStringLiteral("UnknownCategory") : rightData.toString(); 0078 0079 return QString::localeAwareCompare(leftCategory, rightCategory); 0080 } 0081 0082 int SortedSystemTrayModel::compareCategoriesOrderly(const QModelIndex &left, const QModelIndex &right) const 0083 { 0084 QVariant leftData = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::Category)); 0085 QString leftCategory = leftData.isNull() ? QStringLiteral("UnknownCategory") : leftData.toString(); 0086 0087 QVariant rightData = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::Category)); 0088 QString rightCategory = rightData.isNull() ? QStringLiteral("UnknownCategory") : rightData.toString(); 0089 0090 int leftIndex = s_categoryOrder.indexOf(leftCategory); 0091 if (leftIndex == -1) { 0092 leftIndex = s_categoryOrder.indexOf(QStringLiteral("UnknownCategory")); 0093 } 0094 0095 int rightIndex = s_categoryOrder.indexOf(rightCategory); 0096 if (rightIndex == -1) { 0097 rightIndex = s_categoryOrder.indexOf(QStringLiteral("UnknownCategory")); 0098 } 0099 0100 return leftIndex - rightIndex; 0101 }