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