Brooklyn/plasma/workspace/applets/systemtray/sortedsystemtraymodel.cpp
2022-03-05 22:41:29 +05:00

102 lines
3.8 KiB
C++

/*
SPDX-FileCopyrightText: 2019 Konrad Materka <materka@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "sortedsystemtraymodel.h"
#include "debug.h"
#include "systemtraymodel.h"
#include <QList>
static const QList<QString> s_categoryOrder = {
QStringLiteral("UnknownCategory"),
QStringLiteral("ApplicationStatus"),
QStringLiteral("Communications"),
QStringLiteral("SystemServices"),
QStringLiteral("Hardware"),
};
SortedSystemTrayModel::SortedSystemTrayModel(SortingType sorting, QObject *parent)
: QSortFilterProxyModel(parent)
, m_sorting(sorting)
{
setSortLocaleAware(true);
sort(0);
}
bool SortedSystemTrayModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
switch (m_sorting) {
case SortedSystemTrayModel::SortingType::ConfigurationPage:
return lessThanConfigurationPage(left, right);
case SortedSystemTrayModel::SortingType::SystemTray:
return lessThanSystemTray(left, right);
}
return QSortFilterProxyModel::lessThan(left, right);
}
bool SortedSystemTrayModel::lessThanConfigurationPage(const QModelIndex &left, const QModelIndex &right) const
{
const int categoriesComparison = compareCategoriesAlphabetically(left, right);
if (categoriesComparison == 0) {
return QSortFilterProxyModel::lessThan(left, right);
} else {
return categoriesComparison < 0;
}
}
bool SortedSystemTrayModel::lessThanSystemTray(const QModelIndex &left, const QModelIndex &right) const
{
QVariant itemIdLeft = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::ItemId));
QVariant itemIdRight = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::ItemId));
if (itemIdRight.toString() == QLatin1String("org.kde.plasma.notifications")) {
// return false when at least right is "org.kde.plasma.notifications"
return false;
} else if (itemIdLeft.toString() == QLatin1String("org.kde.plasma.notifications")) {
// return true when only left is "org.kde.plasma.notifications"
return true;
}
const int categoriesComparison = compareCategoriesOrderly(left, right);
if (categoriesComparison == 0) {
return QSortFilterProxyModel::lessThan(left, right);
} else {
return categoriesComparison < 0;
}
}
int SortedSystemTrayModel::compareCategoriesAlphabetically(const QModelIndex &left, const QModelIndex &right) const
{
QVariant leftData = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::Category));
QString leftCategory = leftData.isNull() ? QStringLiteral("UnknownCategory") : leftData.toString();
QVariant rightData = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::Category));
QString rightCategory = rightData.isNull() ? QStringLiteral("UnknownCategory") : rightData.toString();
return QString::localeAwareCompare(leftCategory, rightCategory);
}
int SortedSystemTrayModel::compareCategoriesOrderly(const QModelIndex &left, const QModelIndex &right) const
{
QVariant leftData = sourceModel()->data(left, static_cast<int>(BaseModel::BaseRole::Category));
QString leftCategory = leftData.isNull() ? QStringLiteral("UnknownCategory") : leftData.toString();
QVariant rightData = sourceModel()->data(right, static_cast<int>(BaseModel::BaseRole::Category));
QString rightCategory = rightData.isNull() ? QStringLiteral("UnknownCategory") : rightData.toString();
int leftIndex = s_categoryOrder.indexOf(leftCategory);
if (leftIndex == -1) {
leftIndex = s_categoryOrder.indexOf(QStringLiteral("UnknownCategory"));
}
int rightIndex = s_categoryOrder.indexOf(rightCategory);
if (rightIndex == -1) {
rightIndex = s_categoryOrder.indexOf(QStringLiteral("UnknownCategory"));
}
return leftIndex - rightIndex;
}