Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlstringsorter.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtQmlModels/private/qqmlstringsorter_p.h>
6#include <QtQmlModels/private/qqmlsortfilterproxymodel_p.h>
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \qmltype StringSorter
12 \inherits Sorter
13 \inqmlmodule QtQml.Models
14 \since 6.10
15 \preliminary
16 \brief Sort data in a \l SortFilterProxyModel based on ordering of the
17 locale.
18
19 StringSorter allows the user to sort the data according to the role name
20 as configured in the source model. StringSorter compares strings according
21 to a localized collation algorithm.
22
23 The StringSorter can be configured in the sort filter proxy model as below,
24
25 \qml
26 SortFilterProxyModel {
27 model: sourceModel
28 sorters: [
29 StringSorter { roleName: "name" }
30 ]
31 }
32 \endqml
33*/
34
35QQmlStringSorter::QQmlStringSorter(QObject *parent) :
36 QQmlRoleSorter (new QQmlStringSorterPrivate, parent)
37{
38}
39
40/*!
41 \qmlproperty Qt::CaseSensitivity StringSorter::caseSensitivity
42
43 This property holds the case sensitivity of the sorter.
44
45 The default value is Qt::CaseSensitive.
46*/
47Qt::CaseSensitivity QQmlStringSorter::caseSensitivity() const
48{
49 Q_D(const QQmlStringSorter);
50 return d->m_collator.caseSensitivity();
51}
52
53void QQmlStringSorter::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
54{
55 Q_D(QQmlStringSorter);
56 if (d->m_collator.caseSensitivity() == caseSensitivity)
57 return;
58 d->m_collator.setCaseSensitivity(caseSensitivity);
59 emit caseSensitivityChanged();
60 invalidate();
61}
62
63/*!
64 \qmlproperty bool StringSorter::ignorePunctuation
65
66 This property holds whether the sorter ignores punctation.
67 If \c ignorePunctuation is \c true, punctuation characters and symbols are
68 ignored when determining sort order.
69
70 The default value is \c false.
71*/
72bool QQmlStringSorter::ignorePunctuation() const
73{
74 Q_D(const QQmlStringSorter);
75 return d->m_collator.ignorePunctuation();
76}
77
78void QQmlStringSorter::setIgnorePunctuation(bool ignorePunctuation)
79{
80 Q_D(QQmlStringSorter);
81 if (d->m_collator.ignorePunctuation() == ignorePunctuation)
82 return;
83 d->m_collator.setIgnorePunctuation(ignorePunctuation);
84 emit ignorePunctuationChanged();
85 invalidate();
86}
87
88/*!
89 \qmlproperty Locale StringSorter::locale
90
91 This property holds the locale of the sorter.
92
93 The default value is \l QLocale::system()
94*/
95QLocale QQmlStringSorter::locale() const
96{
97 Q_D(const QQmlStringSorter);
98 return d->m_collator.locale();
99}
100
101void QQmlStringSorter::setLocale(const QLocale &locale)
102{
103 Q_D(QQmlStringSorter);
104 if (d->m_collator.locale() == locale)
105 return;
106 d->m_collator.setLocale(locale);
107 emit localeChanged();
108 invalidate();
109}
110
111/*!
112 \qmlproperty bool StringSorter::numericMode
113
114 This property holds whether the numeric mode of the sorter is enabled.
115
116 The default value is \c false.
117*/
118bool QQmlStringSorter::numericMode() const
119{
120 Q_D(const QQmlStringSorter);
121 return d->m_collator.numericMode();
122}
123
124void QQmlStringSorter::setNumericMode(bool numericMode)
125{
126 Q_D(QQmlStringSorter);
127 if (d->m_collator.numericMode() == numericMode)
128 return;
129
130 d->m_collator.setNumericMode(numericMode);
131 emit numericModeChanged();
132 invalidate();
133}
134/*!
135 \internal
136*/
137QPartialOrdering QQmlStringSorter::compare(const QModelIndex &sourceLeft, const QModelIndex &sourceRight, const QQmlSortFilterProxyModel* proxyModel) const
138{
139 Q_D(const QQmlStringSorter);
140 if (int role = proxyModel->itemRoleForName(d->m_roleName); role > -1) {
141 const QVariant first = proxyModel->sourceData(sourceLeft, role);
142 const QVariant second = proxyModel->sourceData(sourceRight, role);
143 const int result = d->m_collator.compare(first.toString(), second.toString());
144 return (result <= 0) ? ((result < 0) ? QPartialOrdering::Less : QPartialOrdering::Equivalent) : QPartialOrdering::Greater;
145 }
146 return QPartialOrdering::Unordered;
147}
148
149QT_END_NAMESPACE
150
151#include "moc_qqmlstringsorter_p.cpp"