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