File indexing completed on 2024-10-13 12:16:00
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 #if KITEMVIEWS_BUILD_DEPRECATED_SINCE(4, 4) 0074 int KCategorizedSortFilterProxyModel::naturalCompare(const QString &a, const QString &b) 0075 { 0076 QCollator c; 0077 c.setNumericMode(true); 0078 c.setCaseSensitivity(Qt::CaseSensitive); 0079 return c.compare(a, b); 0080 } 0081 #endif 0082 0083 bool KCategorizedSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const 0084 { 0085 if (d->categorizedModel) { 0086 int compare = compareCategories(left, right); 0087 0088 if (compare > 0) { // left is greater than right 0089 return false; 0090 } else if (compare < 0) { // left is less than right 0091 return true; 0092 } 0093 } 0094 0095 return subSortLessThan(left, right); 0096 } 0097 0098 bool KCategorizedSortFilterProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const 0099 { 0100 return QSortFilterProxyModel::lessThan(left, right); 0101 } 0102 0103 int KCategorizedSortFilterProxyModel::compareCategories(const QModelIndex &left, const QModelIndex &right) const 0104 { 0105 QVariant l = (left.model() ? left.model()->data(left, CategorySortRole) : QVariant()); 0106 QVariant r = (right.model() ? right.model()->data(right, CategorySortRole) : QVariant()); 0107 0108 Q_ASSERT(l.isValid()); 0109 Q_ASSERT(r.isValid()); 0110 Q_ASSERT(l.type() == r.type()); 0111 0112 if (l.type() == QVariant::String) { 0113 QString lstr = l.toString(); 0114 QString rstr = r.toString(); 0115 0116 if (d->sortCategoriesByNaturalComparison) { 0117 return d->m_collator.compare(lstr, rstr); 0118 } else { 0119 if (lstr < rstr) { 0120 return -1; 0121 } 0122 0123 if (lstr > rstr) { 0124 return 1; 0125 } 0126 0127 return 0; 0128 } 0129 } 0130 0131 qlonglong lint = l.toLongLong(); 0132 qlonglong rint = r.toLongLong(); 0133 0134 if (lint < rint) { 0135 return -1; 0136 } 0137 0138 if (lint > rint) { 0139 return 1; 0140 } 0141 0142 return 0; 0143 }