1 | /* |
2 | This file is part of the KDE project |
3 | SPDX-FileCopyrightText: 2007 Rafael Fernández López <ereslibre@kde.org> |
4 | SPDX-FileCopyrightText: 2007 John Tapsell <tapsell@kde.org> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | */ |
8 | |
9 | #include "kcategorizedsortfilterproxymodel.h" |
10 | #include "kcategorizedsortfilterproxymodel_p.h" |
11 | |
12 | #include <QCollator> |
13 | |
14 | KCategorizedSortFilterProxyModel::KCategorizedSortFilterProxyModel(QObject *parent) |
15 | : QSortFilterProxyModel(parent) |
16 | , d(new KCategorizedSortFilterProxyModelPrivate()) |
17 | |
18 | { |
19 | } |
20 | |
21 | KCategorizedSortFilterProxyModel::~KCategorizedSortFilterProxyModel() = default; |
22 | |
23 | void KCategorizedSortFilterProxyModel::sort(int column, Qt::SortOrder order) |
24 | { |
25 | d->sortColumn = column; |
26 | d->sortOrder = order; |
27 | |
28 | QSortFilterProxyModel::sort(column, order); |
29 | } |
30 | |
31 | bool KCategorizedSortFilterProxyModel::isCategorizedModel() const |
32 | { |
33 | return d->categorizedModel; |
34 | } |
35 | |
36 | void KCategorizedSortFilterProxyModel::setCategorizedModel(bool categorizedModel) |
37 | { |
38 | if (categorizedModel == d->categorizedModel) { |
39 | return; |
40 | } |
41 | |
42 | d->categorizedModel = categorizedModel; |
43 | |
44 | invalidate(); |
45 | } |
46 | |
47 | int KCategorizedSortFilterProxyModel::sortColumn() const |
48 | { |
49 | return d->sortColumn; |
50 | } |
51 | |
52 | Qt::SortOrder KCategorizedSortFilterProxyModel::sortOrder() const |
53 | { |
54 | return d->sortOrder; |
55 | } |
56 | |
57 | void KCategorizedSortFilterProxyModel::setSortCategoriesByNaturalComparison(bool sortCategoriesByNaturalComparison) |
58 | { |
59 | if (sortCategoriesByNaturalComparison == d->sortCategoriesByNaturalComparison) { |
60 | return; |
61 | } |
62 | |
63 | d->sortCategoriesByNaturalComparison = sortCategoriesByNaturalComparison; |
64 | |
65 | invalidate(); |
66 | } |
67 | |
68 | bool KCategorizedSortFilterProxyModel::sortCategoriesByNaturalComparison() const |
69 | { |
70 | return d->sortCategoriesByNaturalComparison; |
71 | } |
72 | |
73 | bool KCategorizedSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const |
74 | { |
75 | if (d->categorizedModel) { |
76 | int compare = compareCategories(left, right); |
77 | |
78 | if (compare > 0) { // left is greater than right |
79 | return false; |
80 | } else if (compare < 0) { // left is less than right |
81 | return true; |
82 | } |
83 | } |
84 | |
85 | return subSortLessThan(left, right); |
86 | } |
87 | |
88 | bool KCategorizedSortFilterProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const |
89 | { |
90 | return QSortFilterProxyModel::lessThan(source_left: left, source_right: right); |
91 | } |
92 | |
93 | int KCategorizedSortFilterProxyModel::compareCategories(const QModelIndex &left, const QModelIndex &right) const |
94 | { |
95 | QVariant l = (left.model() ? left.model()->data(index: left, role: CategorySortRole) : QVariant()); |
96 | QVariant r = (right.model() ? right.model()->data(index: right, role: CategorySortRole) : QVariant()); |
97 | |
98 | Q_ASSERT(l.isValid()); |
99 | Q_ASSERT(r.isValid()); |
100 | Q_ASSERT(l.userType() == r.userType()); |
101 | |
102 | if (l.userType() == QMetaType::QString) { |
103 | QString lstr = l.toString(); |
104 | QString rstr = r.toString(); |
105 | |
106 | if (d->sortCategoriesByNaturalComparison) { |
107 | return d->m_collator.compare(s1: lstr, s2: rstr); |
108 | } else { |
109 | if (lstr < rstr) { |
110 | return -1; |
111 | } |
112 | |
113 | if (lstr > rstr) { |
114 | return 1; |
115 | } |
116 | |
117 | return 0; |
118 | } |
119 | } |
120 | |
121 | qlonglong lint = l.toLongLong(); |
122 | qlonglong rint = r.toLongLong(); |
123 | |
124 | if (lint < rint) { |
125 | return -1; |
126 | } |
127 | |
128 | if (lint > rint) { |
129 | return 1; |
130 | } |
131 | |
132 | return 0; |
133 | } |
134 | |
135 | #include "moc_kcategorizedsortfilterproxymodel.cpp" |
136 | |