SHARE
TWEET

Qt Multi Sort Proxy

VRonin Sep 14th, 2016 182 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // multisorter.h
  2.  
  3. #ifndef multisorter_h__
  4. #define multisorter_h__
  5.  
  6. #include <QSortFilterProxyModel>
  7. #include <QPair>
  8. #include <QHash>
  9. #include <QDate>
  10. #include <QRegExp>
  11. class MultiSorter : public QSortFilterProxyModel
  12. {
  13.     Q_OBJECT
  14.  
  15. public:
  16.     MultiSorter(QObject *parent);
  17.     virtual void setDateRangeFilter(qint32 col, const QDate& minDate, const QDate& maxDate, qint32 role = Qt::DisplayRole);
  18.     virtual void setRegExpFilter(qint32 col, const QRegExp& matcher, qint32 role=Qt::DisplayRole);
  19.     virtual void clearFilters();
  20.     virtual void removeFilterFromColumn(qint32 col, qint32 role);
  21.     virtual void removeFilterFromColumn(qint32 col);
  22. protected:
  23.     virtual bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override;
  24. private:
  25.     QHash<qint64, QPair<QDate, QDate> >  m_dateRangeFilter;
  26.     QHash<qint64, QRegExp > m_regExpFilter;
  27. };
  28.  
  29. #endif // multisorter_h__
  30.  
  31. //////////////////////////////////////////////////////////////////////////
  32.  
  33. #include "multisorter.h"
  34.  
  35. MultiSorter::MultiSorter(QObject *parent)
  36.     : QSortFilterProxyModel(parent)
  37. {}
  38.  
  39. void MultiSorter::setDateRangeFilter(qint32 col, const QDate& minDate, const QDate& maxDate, qint32 role)
  40. {
  41.     if (minDate.isNull() && maxDate.isNull())
  42.         return removeFilterFromColumn(col,role);
  43.     m_regExpFilter.remove((static_cast<qint64>(col) << 32) | static_cast<qint64>(role));
  44.     QPair<QDate, QDate> result(minDate, maxDate);
  45.     m_dateRangeFilter[(static_cast<qint64>(col) << 32) | static_cast<qint64>(role)] = result;
  46.     invalidateFilter();
  47. }
  48.  
  49. void MultiSorter::setRegExpFilter(qint32 col, const QRegExp& matcher, qint32 role)
  50. {
  51.     if(matcher.isEmpty() || !matcher.isValid())
  52.         return removeFilterFromColumn(col, role);
  53.     m_dateRangeFilter.remove((static_cast<qint64>(col) << 32) | static_cast<qint64>(role));
  54.     m_regExpFilter[(static_cast<qint64>(col) << 32) | static_cast<qint64>(role)] = matcher;
  55.     invalidateFilter();
  56. }
  57.  
  58. void MultiSorter::clearFilters()
  59. {
  60.     m_dateRangeFilter.clear();
  61.     m_regExpFilter.clear();
  62.     invalidateFilter();
  63. }
  64.  
  65. void MultiSorter::removeFilterFromColumn(qint32 col)
  66. {
  67.     for (auto i = m_dateRangeFilter.begin(); i != m_dateRangeFilter.end();){
  68.         if (static_cast<qint32>(i.key() >> 32) == col)
  69.             i = m_dateRangeFilter.erase(i);
  70.         else
  71.             ++i;
  72.     }
  73.     for (auto i = m_regExpFilter.begin(); i != m_regExpFilter.end();) {
  74.         if (static_cast<qint32>(i.key() >> 32) == col)
  75.             i = m_regExpFilter.erase(i);
  76.         else
  77.             ++i;
  78.     }
  79.     invalidateFilter();
  80. }
  81. void MultiSorter::removeFilterFromColumn(qint32 col, qint32 role)
  82. {
  83.     m_dateRangeFilter.remove((static_cast<qint64>(col) << 32) | static_cast<qint64>(role));
  84.     m_regExpFilter.remove((static_cast<qint64>(col) << 32) | static_cast<qint64>(role));
  85.     invalidateFilter();
  86. }
  87.  
  88. bool MultiSorter::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
  89. {
  90.  
  91.     for (int i = 0; i < sourceModel()->columnCount(source_parent); ++i) {
  92.         const QModelIndex currntIndex = sourceModel()->index(source_row, i, source_parent);
  93.         for (auto dateRngIter = m_dateRangeFilter.constBegin(); dateRngIter != m_dateRangeFilter.constEnd(); ++dateRngIter) {
  94.             if (static_cast<qint32>(dateRngIter.key() >> 32) == i) {
  95.                 const QDate testDate = currntIndex.data(static_cast<qint32>(dateRngIter.key() & ((1i64 << 32) - 1))).toDate();
  96.                 if (!((testDate >= dateRngIter.value().first || dateRngIter.value().first.isNull()) && (testDate <= dateRngIter.value().second || dateRngIter.value().second.isNull())))
  97.                     return false;
  98.             }
  99.         }
  100.         for (auto regExpIter = m_regExpFilter.constBegin(); regExpIter != m_regExpFilter.constEnd(); ++regExpIter) {
  101.             if (static_cast<qint32>(regExpIter.key() >> 32) == i) {
  102.                 if (regExpIter.value().indexIn(currntIndex.data(static_cast<qint32>(regExpIter.key() & ((1i64 << 32) - 1))).toString().trimmed()) < 0)
  103.                     return false;
  104.             }
  105.         }
  106.     }
  107.     return true;
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top