1 #include "stringsorter.h"
2 
3 namespace qqsfpm {
4 
5 /*!
6     \qmltype StringSorter
7     \inherits RoleSorter
8     \inqmlmodule SortFilterProxyModel
9     \brief Sorts rows based on a source model string role
10 
11     \l StringSorter is a specialized \l RoleSorter that sorts rows based on a source model string role.
12     \l StringSorter compares strings according to a localized collation algorithm.
13 
14     In the following example, rows with be sorted by their \c lastName role :
15     \code
16     SortFilterProxyModel {
17        sourceModel: contactModel
18        sorters: StringSorter { roleName: "lastName" }
19     }
20     \endcode
21 */
22 
23 /*!
24     \qmlproperty Qt.CaseSensitivity StringSorter::caseSensitivity
25 
26     This property holds the case sensitivity of the sorter.
27 */
caseSensitivity() const28 Qt::CaseSensitivity StringSorter::caseSensitivity() const
29 {
30     return m_collator.caseSensitivity();
31 }
32 
setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)33 void StringSorter::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
34 {
35     if (m_collator.caseSensitivity() == caseSensitivity)
36         return;
37 
38     m_collator.setCaseSensitivity(caseSensitivity);
39     Q_EMIT caseSensitivityChanged();
40     invalidate();
41 }
42 
43 /*!
44     \qmlproperty bool StringSorter::ignorePunctation
45 
46     This property holds whether the sorter ignores punctation.
47     if \c ignorePunctuation is \c true, punctuation characters and symbols are ignored when determining sort order.
48 
49     \note This property is not currently supported on Apple platforms or if Qt is configured to not use ICU on Linux.
50 */
ignorePunctation() const51 bool StringSorter::ignorePunctation() const
52 {
53     return m_collator.ignorePunctuation();
54 }
55 
setIgnorePunctation(bool ignorePunctation)56 void StringSorter::setIgnorePunctation(bool ignorePunctation)
57 {
58     if (m_collator.ignorePunctuation() == ignorePunctation)
59         return;
60 
61     m_collator.setIgnorePunctuation(ignorePunctation);
62     Q_EMIT ignorePunctationChanged();
63     invalidate();
64 }
65 
66 /*!
67     \qmlproperty Locale StringSorter::locale
68 
69     This property holds the locale of the sorter.
70 */
locale() const71 QLocale StringSorter::locale() const
72 {
73     return m_collator.locale();
74 }
75 
setLocale(const QLocale & locale)76 void StringSorter::setLocale(const QLocale &locale)
77 {
78     if (m_collator.locale() == locale)
79         return;
80 
81     m_collator.setLocale(locale);
82     Q_EMIT localeChanged();
83     invalidate();
84 }
85 
86 /*!
87     \qmlproperty bool StringSorter::numericMode
88 
89     This property holds whether the numeric mode of the sorter is enabled.
90     This will enable proper sorting of numeric digits, so that e.g. 100 sorts after 99.
91     By default this mode is off.
92 */
numericMode() const93 bool StringSorter::numericMode() const
94 {
95     return m_collator.numericMode();
96 }
97 
setNumericMode(bool numericMode)98 void StringSorter::setNumericMode(bool numericMode)
99 {
100     if (m_collator.numericMode() == numericMode)
101         return;
102 
103     m_collator.setNumericMode(numericMode);
104     Q_EMIT numericModeChanged();
105     invalidate();
106 }
107 
compare(const QModelIndex & sourceLeft,const QModelIndex & sourceRight,const QQmlSortFilterProxyModel & proxyModel) const108 int StringSorter::compare(const QModelIndex &sourceLeft, const QModelIndex &sourceRight, const QQmlSortFilterProxyModel& proxyModel) const
109 {
110     QPair<QVariant, QVariant> pair = sourceData(sourceLeft, sourceRight, proxyModel);
111     QString leftValue = pair.first.toString();
112     QString rightValue = pair.second.toString();
113     return m_collator.compare(leftValue, rightValue);
114 }
115 
116 }
117