KItemViews

kcategorizedsortfilterproxymodel.cpp
1 /*
2  This file is part of the KDE project
3  SPDX-FileCopyrightText: 2007 Rafael Fernández López <[email protected]>
4  SPDX-FileCopyrightText: 2007 John Tapsell <[email protected]>
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 
24 {
25  d->sortColumn = column;
26  d->sortOrder = order;
27 
28  QSortFilterProxyModel::sort(column, order);
29 }
30 
32 {
33  return d->categorizedModel;
34 }
35 
37 {
38  if (categorizedModel == d->categorizedModel) {
39  return;
40  }
41 
42  d->categorizedModel = categorizedModel;
43 
44  invalidate();
45 }
46 
48 {
49  return d->sortColumn;
50 }
51 
53 {
54  return d->sortOrder;
55 }
56 
58 {
59  if (sortCategoriesByNaturalComparison == d->sortCategoriesByNaturalComparison) {
60  return;
61  }
62 
63  d->sortCategoriesByNaturalComparison = sortCategoriesByNaturalComparison;
64 
65  invalidate();
66 }
67 
69 {
70  return d->sortCategoriesByNaturalComparison;
71 }
72 
73 #if KITEMVIEWS_BUILD_DEPRECATED_SINCE(4, 4)
75 {
76  QCollator c;
77  c.setNumericMode(true);
79  return c.compare(a, b);
80 }
81 #endif
82 
84 {
85  if (d->categorizedModel) {
86  int compare = compareCategories(left, right);
87 
88  if (compare > 0) { // left is greater than right
89  return false;
90  } else if (compare < 0) { // left is less than right
91  return true;
92  }
93  }
94 
95  return subSortLessThan(left, right);
96 }
97 
99 {
100  return QSortFilterProxyModel::lessThan(left, right);
101 }
102 
104 {
105  QVariant l = (left.model() ? left.model()->data(left, CategorySortRole) : QVariant());
106  QVariant r = (right.model() ? right.model()->data(right, CategorySortRole) : QVariant());
107 
108  Q_ASSERT(l.isValid());
109  Q_ASSERT(r.isValid());
110  Q_ASSERT(l.type() == r.type());
111 
112  if (l.type() == QVariant::String) {
113  QString lstr = l.toString();
114  QString rstr = r.toString();
115 
116  if (d->sortCategoriesByNaturalComparison) {
117  return d->m_collator.compare(lstr, rstr);
118  } else {
119  if (lstr < rstr) {
120  return -1;
121  }
122 
123  if (lstr > rstr) {
124  return 1;
125  }
126 
127  return 0;
128  }
129  }
130 
131  qlonglong lint = l.toLongLong();
132  qlonglong rint = r.toLongLong();
133 
134  if (lint < rint) {
135  return -1;
136  }
137 
138  if (lint > rint) {
139  return 1;
140  }
141 
142  return 0;
143 }
int compare(const QString &s1, const QString &s2) const const
bool isValid() const const
CaseSensitive
SortOrder
virtual void sort(int column, Qt::SortOrder order) override
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
Overridden from QSortFilterProxyModel.
qlonglong toLongLong(bool *ok) const const
void setSortCategoriesByNaturalComparison(bool sortCategoriesByNaturalComparison)
Set if the sorting using CategorySortRole will use a natural comparison in the case that strings were...
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Overridden from QSortFilterProxyModel.
QVariant::Type type() const const
void setNumericMode(bool on)
@ CategorySortRole
This role is used for sorting categories.
virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const const
static int naturalCompare(const QString &a, const QString &b)
Does a natural comparing of the strings.
virtual bool subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
This method has a similar purpose as lessThan() has on QSortFilterProxyModel.
void setCategorizedModel(bool categorizedModel)
Enables or disables the categorization feature.
virtual int compareCategories(const QModelIndex &left, const QModelIndex &right) const
This method compares the category of the left index with the category of the right index.
void setCaseSensitivity(Qt::CaseSensitivity sensitivity)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:18:31 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.