File indexing completed on 2024-04-14 07:31:18

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2007 Rafael Fernández López <ereslibre@kde.org>
0004     SPDX-FileCopyrightText: 2007 John Tapsell <tapsell@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "kcategorizedsortfilterproxymodel.h"
0010 #include "kcategorizedsortfilterproxymodel_p.h"
0011 
0012 #include <QCollator>
0013 
0014 KCategorizedSortFilterProxyModel::KCategorizedSortFilterProxyModel(QObject *parent)
0015     : QSortFilterProxyModel(parent)
0016     , d(new KCategorizedSortFilterProxyModelPrivate())
0017 
0018 {
0019 }
0020 
0021 KCategorizedSortFilterProxyModel::~KCategorizedSortFilterProxyModel() = default;
0022 
0023 void KCategorizedSortFilterProxyModel::sort(int column, Qt::SortOrder order)
0024 {
0025     d->sortColumn = column;
0026     d->sortOrder = order;
0027 
0028     QSortFilterProxyModel::sort(column, order);
0029 }
0030 
0031 bool KCategorizedSortFilterProxyModel::isCategorizedModel() const
0032 {
0033     return d->categorizedModel;
0034 }
0035 
0036 void KCategorizedSortFilterProxyModel::setCategorizedModel(bool categorizedModel)
0037 {
0038     if (categorizedModel == d->categorizedModel) {
0039         return;
0040     }
0041 
0042     d->categorizedModel = categorizedModel;
0043 
0044     invalidate();
0045 }
0046 
0047 int KCategorizedSortFilterProxyModel::sortColumn() const
0048 {
0049     return d->sortColumn;
0050 }
0051 
0052 Qt::SortOrder KCategorizedSortFilterProxyModel::sortOrder() const
0053 {
0054     return d->sortOrder;
0055 }
0056 
0057 void KCategorizedSortFilterProxyModel::setSortCategoriesByNaturalComparison(bool sortCategoriesByNaturalComparison)
0058 {
0059     if (sortCategoriesByNaturalComparison == d->sortCategoriesByNaturalComparison) {
0060         return;
0061     }
0062 
0063     d->sortCategoriesByNaturalComparison = sortCategoriesByNaturalComparison;
0064 
0065     invalidate();
0066 }
0067 
0068 bool KCategorizedSortFilterProxyModel::sortCategoriesByNaturalComparison() const
0069 {
0070     return d->sortCategoriesByNaturalComparison;
0071 }
0072 
0073 bool KCategorizedSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0074 {
0075     if (d->categorizedModel) {
0076         int compare = compareCategories(left, right);
0077 
0078         if (compare > 0) { // left is greater than right
0079             return false;
0080         } else if (compare < 0) { // left is less than right
0081             return true;
0082         }
0083     }
0084 
0085     return subSortLessThan(left, right);
0086 }
0087 
0088 bool KCategorizedSortFilterProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
0089 {
0090     return QSortFilterProxyModel::lessThan(left, right);
0091 }
0092 
0093 int KCategorizedSortFilterProxyModel::compareCategories(const QModelIndex &left, const QModelIndex &right) const
0094 {
0095     QVariant l = (left.model() ? left.model()->data(left, CategorySortRole) : QVariant());
0096     QVariant r = (right.model() ? right.model()->data(right, CategorySortRole) : QVariant());
0097 
0098     Q_ASSERT(l.isValid());
0099     Q_ASSERT(r.isValid());
0100     Q_ASSERT(l.userType() == r.userType());
0101 
0102     if (l.userType() == QMetaType::QString) {
0103         QString lstr = l.toString();
0104         QString rstr = r.toString();
0105 
0106         if (d->sortCategoriesByNaturalComparison) {
0107             return d->m_collator.compare(lstr, rstr);
0108         } else {
0109             if (lstr < rstr) {
0110                 return -1;
0111             }
0112 
0113             if (lstr > rstr) {
0114                 return 1;
0115             }
0116 
0117             return 0;
0118         }
0119     }
0120 
0121     qlonglong lint = l.toLongLong();
0122     qlonglong rint = r.toLongLong();
0123 
0124     if (lint < rint) {
0125         return -1;
0126     }
0127 
0128     if (lint > rint) {
0129         return 1;
0130     }
0131 
0132     return 0;
0133 }