Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // multisorter.h
- #ifndef multisorter_h__
- #define multisorter_h__
- #include <QSortFilterProxyModel>
- #include <QPair>
- #include <QHash>
- #include <QDate>
- #include <QRegExp>
- class MultiSorter : public QSortFilterProxyModel
- {
- Q_OBJECT
- public:
- MultiSorter(QObject *parent);
- virtual void setDateRangeFilter(qint32 col, const QDate& minDate, const QDate& maxDate, qint32 role = Qt::DisplayRole);
- virtual void setRegExpFilter(qint32 col, const QRegExp& matcher, qint32 role=Qt::DisplayRole);
- virtual void clearFilters();
- virtual void removeFilterFromColumn(qint32 col, qint32 role);
- virtual void removeFilterFromColumn(qint32 col);
- protected:
- virtual bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override;
- private:
- QHash<qint64, QPair<QDate, QDate> > m_dateRangeFilter;
- QHash<qint64, QRegExp > m_regExpFilter;
- };
- #endif // multisorter_h__
- //////////////////////////////////////////////////////////////////////////
- #include "multisorter.h"
- MultiSorter::MultiSorter(QObject *parent)
- : QSortFilterProxyModel(parent)
- {}
- void MultiSorter::setDateRangeFilter(qint32 col, const QDate& minDate, const QDate& maxDate, qint32 role)
- {
- if (minDate.isNull() && maxDate.isNull())
- return removeFilterFromColumn(col,role);
- m_regExpFilter.remove((static_cast<qint64>(col) << 32) | static_cast<qint64>(role));
- QPair<QDate, QDate> result(minDate, maxDate);
- m_dateRangeFilter[(static_cast<qint64>(col) << 32) | static_cast<qint64>(role)] = result;
- invalidateFilter();
- }
- void MultiSorter::setRegExpFilter(qint32 col, const QRegExp& matcher, qint32 role)
- {
- if(matcher.isEmpty() || !matcher.isValid())
- return removeFilterFromColumn(col, role);
- m_dateRangeFilter.remove((static_cast<qint64>(col) << 32) | static_cast<qint64>(role));
- m_regExpFilter[(static_cast<qint64>(col) << 32) | static_cast<qint64>(role)] = matcher;
- invalidateFilter();
- }
- void MultiSorter::clearFilters()
- {
- m_dateRangeFilter.clear();
- m_regExpFilter.clear();
- invalidateFilter();
- }
- void MultiSorter::removeFilterFromColumn(qint32 col)
- {
- for (auto i = m_dateRangeFilter.begin(); i != m_dateRangeFilter.end();){
- if (static_cast<qint32>(i.key() >> 32) == col)
- i = m_dateRangeFilter.erase(i);
- else
- ++i;
- }
- for (auto i = m_regExpFilter.begin(); i != m_regExpFilter.end();) {
- if (static_cast<qint32>(i.key() >> 32) == col)
- i = m_regExpFilter.erase(i);
- else
- ++i;
- }
- invalidateFilter();
- }
- void MultiSorter::removeFilterFromColumn(qint32 col, qint32 role)
- {
- m_dateRangeFilter.remove((static_cast<qint64>(col) << 32) | static_cast<qint64>(role));
- m_regExpFilter.remove((static_cast<qint64>(col) << 32) | static_cast<qint64>(role));
- invalidateFilter();
- }
- bool MultiSorter::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
- {
- for (int i = 0; i < sourceModel()->columnCount(source_parent); ++i) {
- const QModelIndex currntIndex = sourceModel()->index(source_row, i, source_parent);
- for (auto dateRngIter = m_dateRangeFilter.constBegin(); dateRngIter != m_dateRangeFilter.constEnd(); ++dateRngIter) {
- if (static_cast<qint32>(dateRngIter.key() >> 32) == i) {
- const QDate testDate = currntIndex.data(static_cast<qint32>(dateRngIter.key() & ((1i64 << 32) - 1))).toDate();
- if (!((testDate >= dateRngIter.value().first || dateRngIter.value().first.isNull()) && (testDate <= dateRngIter.value().second || dateRngIter.value().second.isNull())))
- return false;
- }
- }
- for (auto regExpIter = m_regExpFilter.constBegin(); regExpIter != m_regExpFilter.constEnd(); ++regExpIter) {
- if (static_cast<qint32>(regExpIter.key() >> 32) == i) {
- if (regExpIter.value().indexIn(currntIndex.data(static_cast<qint32>(regExpIter.key() & ((1i64 << 32) - 1))).toString().trimmed()) < 0)
- return false;
- }
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement