KItemViews

kcategorizedsortfilterproxymodel.cpp
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 
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 
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 
89 {
90  return QSortFilterProxyModel::lessThan(left, right);
91 }
92 
94 {
95  QVariant l = (left.model() ? left.model()->data(left, CategorySortRole) : QVariant());
96  QVariant r = (right.model() ? right.model()->data(right, 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(lstr, 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 }
bool isValid() const const
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
int userType() 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.
@ CategorySortRole
This role is used for sorting categories.
virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const const
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.
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 04:01:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.