/ sorters / sorter.cpp
sorter.cpp
  1  #include "sorter.h"
  2  #include "qqmlsortfilterproxymodel.h"
  3  
  4  namespace qqsfpm {
  5  
  6  /*!
  7      \qmltype Sorter
  8      \qmlabstract
  9      \inqmlmodule SortFilterProxyModel
 10      \ingroup Sorters
 11      \brief Base type for the \l SortFilterProxyModel sorters.
 12  
 13      The Sorter type cannot be used directly in a QML file.
 14      It exists to provide a set of common properties and methods,
 15      available across all the other sorters types that inherit from it.
 16      Attempting to use the Sorter type directly will result in an error.
 17  */
 18  
 19  Sorter::Sorter(QObject *parent) : QObject(parent)
 20  {
 21  }
 22  
 23  Sorter::~Sorter() = default;
 24  
 25  /*!
 26      \qmlproperty bool Sorter::enabled
 27  
 28      This property holds whether the sorter is enabled.
 29      A disabled sorter will not change the order of the rows.
 30  
 31      By default, sorters are enabled.
 32  */
 33  bool Sorter::enabled() const
 34  {
 35      return m_enabled;
 36  }
 37  
 38  void Sorter::setEnabled(bool enabled)
 39  {
 40      if (m_enabled == enabled)
 41          return;
 42  
 43      m_enabled = enabled;
 44      Q_EMIT enabledChanged();
 45      Q_EMIT invalidated();
 46  }
 47  
 48  bool Sorter::ascendingOrder() const
 49  {
 50      return sortOrder() == Qt::AscendingOrder;
 51  }
 52  
 53  void Sorter::setAscendingOrder(bool ascendingOrder)
 54  {
 55      setSortOrder(ascendingOrder ? Qt::AscendingOrder : Qt::DescendingOrder);
 56  }
 57  
 58  
 59  /*!
 60      \qmlproperty Qt::SortOrder Sorter::sortOrder
 61  
 62      This property holds the sort order of this sorter.
 63  
 64      \value Qt.AscendingOrder The items are sorted ascending e.g. starts with 'AAA' ends with 'ZZZ' in Latin-1 locales
 65      \value Qt.DescendingOrder The items are sorted descending e.g. starts with 'ZZZ' ends with 'AAA' in Latin-1 locales
 66  
 67      By default, sorting is in ascending order.
 68  */
 69  Qt::SortOrder Sorter::sortOrder() const
 70  {
 71      return m_sortOrder;
 72  }
 73  
 74  void Sorter::setSortOrder(Qt::SortOrder sortOrder)
 75  {
 76      if (m_sortOrder == sortOrder)
 77          return;
 78  
 79      m_sortOrder = sortOrder;
 80      Q_EMIT sortOrderChanged();
 81      invalidate();
 82  }
 83  
 84  /*!
 85      \qmlproperty int Sorter::priority
 86  
 87      This property holds the sort priority of this sorter.
 88      Sorters with a higher priority are applied first.
 89      In case of equal priority, Sorters are ordered by their insertion order.
 90  
 91      By default, the priority is 0.
 92  */
 93  int Sorter::priority() const
 94  {
 95      return m_priority;
 96  }
 97  
 98  void Sorter::setPriority(int priority)
 99  {
100      if (m_priority == priority)
101          return;
102  
103      m_priority = priority;
104      Q_EMIT priorityChanged();
105      invalidate();
106  }
107  
108  int Sorter::compareRows(const QModelIndex &source_left, const QModelIndex &source_right, const QQmlSortFilterProxyModel& proxyModel) const
109  {
110      int comparison = compare(source_left, source_right, proxyModel);
111      return (m_sortOrder == Qt::AscendingOrder) ? comparison : -comparison;
112  }
113  
114  int Sorter::compare(const QModelIndex &sourceLeft, const QModelIndex &sourceRight, const QQmlSortFilterProxyModel& proxyModel) const
115  {
116      if (lessThan(sourceLeft, sourceRight, proxyModel))
117          return -1;
118      if (lessThan(sourceRight, sourceLeft, proxyModel))
119          return 1;
120      return 0;
121  }
122  
123  void Sorter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
124  {
125      Q_UNUSED(proxyModel)
126  }
127  
128  bool Sorter::lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight, const QQmlSortFilterProxyModel& proxyModel) const
129  {
130      Q_UNUSED(sourceLeft)
131      Q_UNUSED(sourceRight)
132      Q_UNUSED(proxyModel)
133      return false;
134  }
135  
136  void Sorter::invalidate()
137  {
138      if (m_enabled)
139          Q_EMIT invalidated();
140  }
141  
142  }