• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • itemviews
kcategorizedsortfilterproxymodel.cpp
Go to the documentation of this file.
1 
22 #include "kcategorizedsortfilterproxymodel.h"
23 #include "kcategorizedsortfilterproxymodel_p.h"
24 
25 #include <limits.h>
26 
27 #include <QItemSelection>
28 #include <QStringList>
29 #include <QSize>
30 
31 #include <kstringhandler.h>
32 
33 KCategorizedSortFilterProxyModel::KCategorizedSortFilterProxyModel(QObject *parent)
34  : QSortFilterProxyModel(parent)
35  , d(new Private())
36 
37 {
38 }
39 
40 KCategorizedSortFilterProxyModel::~KCategorizedSortFilterProxyModel()
41 {
42  delete d;
43 }
44 
45 void KCategorizedSortFilterProxyModel::sort(int column, Qt::SortOrder order)
46 {
47  d->sortColumn = column;
48  d->sortOrder = order;
49 
50  QSortFilterProxyModel::sort(column, order);
51 }
52 
53 bool KCategorizedSortFilterProxyModel::isCategorizedModel() const
54 {
55  return d->categorizedModel;
56 }
57 
58 void KCategorizedSortFilterProxyModel::setCategorizedModel(bool categorizedModel)
59 {
60  if (categorizedModel == d->categorizedModel)
61  {
62  return;
63  }
64 
65  d->categorizedModel = categorizedModel;
66 
67  invalidate();
68 }
69 
70 int KCategorizedSortFilterProxyModel::sortColumn() const
71 {
72  return d->sortColumn;
73 }
74 
75 Qt::SortOrder KCategorizedSortFilterProxyModel::sortOrder() const
76 {
77  return d->sortOrder;
78 }
79 
80 void KCategorizedSortFilterProxyModel::setSortCategoriesByNaturalComparison(bool sortCategoriesByNaturalComparison)
81 {
82  if (sortCategoriesByNaturalComparison == d->sortCategoriesByNaturalComparison)
83  {
84  return;
85  }
86 
87  d->sortCategoriesByNaturalComparison = sortCategoriesByNaturalComparison;
88 
89  invalidate();
90 }
91 
92 bool KCategorizedSortFilterProxyModel::sortCategoriesByNaturalComparison() const
93 {
94  return d->sortCategoriesByNaturalComparison;
95 }
96 
97 #ifndef KDE_NO_DEPRECATED
98 int KCategorizedSortFilterProxyModel::naturalCompare(const QString &a,
99  const QString &b)
100 {
101  return KStringHandler::naturalCompare(a, b);
102 }
103 #endif
104 
105 bool KCategorizedSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
106 {
107  if (d->categorizedModel)
108  {
109  int compare = compareCategories(left, right);
110 
111  if (compare > 0) // left is greater than right
112  {
113  return false;
114  }
115  else if (compare < 0) // left is less than right
116  {
117  return true;
118  }
119  }
120 
121  return subSortLessThan(left, right);
122 }
123 
124 bool KCategorizedSortFilterProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
125 {
126  return QSortFilterProxyModel::lessThan(left, right);
127 }
128 
129 int KCategorizedSortFilterProxyModel::compareCategories(const QModelIndex &left, const QModelIndex &right) const
130 {
131  QVariant l = (left.model() ? left.model()->data(left, CategorySortRole) : QVariant());
132  QVariant r = (right.model() ? right.model()->data(right, CategorySortRole) : QVariant());
133 
134  Q_ASSERT(l.isValid());
135  Q_ASSERT(r.isValid());
136  Q_ASSERT(l.type() == r.type());
137 
138  if (l.type() == QVariant::String)
139  {
140  QString lstr = l.toString();
141  QString rstr = r.toString();
142 
143  if (d->sortCategoriesByNaturalComparison)
144  {
145  return KStringHandler::naturalCompare(lstr, rstr);
146  }
147  else
148  {
149  if (lstr < rstr)
150  {
151  return -1;
152  }
153 
154  if (lstr > rstr)
155  {
156  return 1;
157  }
158 
159  return 0;
160  }
161  }
162 
163  qlonglong lint = l.toLongLong();
164  qlonglong rint = r.toLongLong();
165 
166  if (lint < rint)
167  {
168  return -1;
169  }
170 
171  if (lint > rint)
172  {
173  return 1;
174  }
175 
176  return 0;
177 }
KCategorizedSortFilterProxyModel::subSortLessThan
virtual bool subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
This method has a similar purpose as lessThan() has on QSortFilterProxyModel.
Definition: kcategorizedsortfilterproxymodel.cpp:124
kcategorizedsortfilterproxymodel.h
KCategorizedSortFilterProxyModel::KCategorizedSortFilterProxyModel
KCategorizedSortFilterProxyModel(QObject *parent=0)
This file is part of the KDE project Copyright (C) 2007 Rafael Fernández López ereslibre@kde.org Copyright (C) 2007 John Tapsell tapsell@kde.org
Definition: kcategorizedsortfilterproxymodel.cpp:33
KCategorizedSortFilterProxyModel::setSortCategoriesByNaturalComparison
void setSortCategoriesByNaturalComparison(bool sortCategoriesByNaturalComparison)
Set if the sorting using CategorySortRole will use a natural comparison in the case that strings were...
Definition: kcategorizedsortfilterproxymodel.cpp:80
KStringHandler::naturalCompare
int naturalCompare(const QString &a, const QString &b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
KCategorizedSortFilterProxyModel::sortCategoriesByNaturalComparison
bool sortCategoriesByNaturalComparison() const
Definition: kcategorizedsortfilterproxymodel.cpp:92
KCategorizedSortFilterProxyModel::compareCategories
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...
Definition: kcategorizedsortfilterproxymodel.cpp:129
QObject
KCategorizedSortFilterProxyModel::naturalCompare
static int naturalCompare(const QString &a, const QString &b)
Does a natural comparing of the strings.
Definition: kcategorizedsortfilterproxymodel.cpp:98
KCategorizedSortFilterProxyModel::setCategorizedModel
void setCategorizedModel(bool categorizedModel)
Enables or disables the categorization feature.
Definition: kcategorizedsortfilterproxymodel.cpp:58
KCategorizedSortFilterProxyModel::isCategorizedModel
bool isCategorizedModel() const
Definition: kcategorizedsortfilterproxymodel.cpp:53
KCategorizedSortFilterProxyModel::sortColumn
int sortColumn() const
Definition: kcategorizedsortfilterproxymodel.cpp:70
KCategorizedSortFilterProxyModel::CategorySortRole
This role is used for sorting categories.
Definition: kcategorizedsortfilterproxymodel.h:54
lessThan
bool lessThan(const QString &left, const QString &right)
Definition: kcompletion.cpp:812
QSortFilterProxyModel
kstringhandler.h
KCategorizedSortFilterProxyModel::lessThan
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const
Overridden from QSortFilterProxyModel.
Definition: kcategorizedsortfilterproxymodel.cpp:105
KCategorizedSortFilterProxyModel::sortOrder
Qt::SortOrder sortOrder() const
Definition: kcategorizedsortfilterproxymodel.cpp:75
KCategorizedSortFilterProxyModel::sort
virtual void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Overridden from QSortFilterProxyModel.
Definition: kcategorizedsortfilterproxymodel.cpp:45
KCategorizedSortFilterProxyModel::~KCategorizedSortFilterProxyModel
virtual ~KCategorizedSortFilterProxyModel()
Definition: kcategorizedsortfilterproxymodel.cpp:40
This file is part of the KDE documentation.
Documentation copyright © 1996-2018 The KDE developers.
Generated on Tue Aug 21 2018 18:46:23 by doxygen 1.8.14 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal