From e0b60167f2366092f2e38436d443f0e2bcfe417d Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Wed, 1 Oct 2014 16:23:02 -0300 Subject: [PATCH] Add possibility to filter by more than one criteria at a time This new version of the TagFilterSortModel actually accepts *any* new MultiFilterInterface. So, how to use it to create a new filter: Implement a class that inherits from MultiFilterInterface Implement the filterRow method TagFilterSortModel::instance->add( myClass ); and you are done. [Dirk Hohndel: removed some debug code and did whitespace cleanup] Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 2 +- qt-ui/models.cpp | 71 ++++++++++++++++++++++++++++++++--------- qt-ui/models.h | 17 ++++++++-- qt-ui/simplewidgets.cpp | 12 +++++++ qt-ui/simplewidgets.h | 3 +- 5 files changed, 86 insertions(+), 19 deletions(-) diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index a8f718c31..29a2301b9 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -34,7 +34,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec setItemDelegate(new DiveListDelegate(this)); setUniformRowHeights(true); setItemDelegateForColumn(DiveTripModel::RATING, new StarWidgetsDelegate(this)); - TagFilterSortModel *model = new TagFilterSortModel(this); + TagFilterSortModel *model = TagFilterSortModel::instance(); model->setSortRole(DiveTripModel::SORT_ROLE); model->setFilterKeyColumn(-1); // filter all columns model->setFilterCaseSensitivity(Qt::CaseInsensitive); diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 2cdea6851..30dc6f64d 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -2193,25 +2193,20 @@ bool TagFilterModel::setData(const QModelIndex &index, const QVariant &value, in return false; } -TagFilterSortModel::TagFilterSortModel(QObject *parent) : QSortFilterProxyModel(parent) -{ - connect(TagFilterModel::instance(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(invalidate())); -} - -bool TagFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const +bool TagFilterModel::filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const { // If there's nothing checked, this should show everythin. - if (!TagFilterModel::instance()->anyChecked) { + if (!anyChecked) { return true; } - QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent); - QVariant diveVariant = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE); + QModelIndex index0 = sourceModel->index(source_row, 0, source_parent); + QVariant diveVariant = sourceModel->data(index0, DiveTripModel::DIVE_ROLE); struct dive *d = (struct dive *)diveVariant.value(); if (!d) { // It's a trip, only show the ones that have dives to be shown. - for (int i = 0; i < sourceModel()->rowCount(index0); i++) { - if (filterAcceptsRow(i, index0)) + for (int i = 0; i < sourceModel->rowCount(index0); i++) { + if (filterRow(i, index0, sourceModel)) return true; } return false; @@ -2220,23 +2215,69 @@ bool TagFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &sou struct tag_entry *head = d->tag_list; if (!head) { // last tag means "Show empty tags"; - if (TagFilterModel::instance()->rowCount() > 0) - return TagFilterModel::instance()->checkState[TagFilterModel::instance()->rowCount() - 1]; + if (rowCount() > 0) + return checkState[rowCount() - 1]; else return true; } // have at least one tag. - QStringList tagList = TagFilterModel::instance()->stringList(); + QStringList tagList = stringList(); if (!tagList.isEmpty()) { tagList.removeLast(); // remove the "Show Empty Tags"; while (head) { QString tagName(head->tag->name); int index = tagList.indexOf(tagName); - if (TagFilterModel::instance()->checkState[index]) + if (checkState[index]) return true; head = head->next; } } return false; } + +TagFilterSortModel *TagFilterSortModel::instance() +{ + static TagFilterSortModel *self = new TagFilterSortModel(); + return self; +} + +TagFilterSortModel::TagFilterSortModel(QObject *parent) : QSortFilterProxyModel(parent) +{ +} + +bool TagFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const +{ + if (models.isEmpty()) { + return true; + } + + Q_FOREACH (MultiFilterInterface *model, models) { + if (model->filterRow(source_row, source_parent, sourceModel())) { + return true; + } + } + + return false; +} + +void TagFilterSortModel::myInvalidate() +{ + invalidate(); +} + +void TagFilterSortModel::addFilterModel(MultiFilterInterface *model) +{ + QAbstractItemModel *itemModel = dynamic_cast(model); + Q_ASSERT(itemModel); + models.append(model); + connect(itemModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(myInvalidate())); +} + +void TagFilterSortModel::removeFilterModel(MultiFilterInterface *model) +{ + QAbstractItemModel *itemModel = dynamic_cast(model); + Q_ASSERT(itemModel); + models.removeAll(model); + disconnect(itemModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(myInvalidate())); +} diff --git a/qt-ui/models.h b/qt-ui/models.h index 18c26df44..bf13063c8 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -417,13 +417,19 @@ private: QStringList languages; }; -class TagFilterModel : public QStringListModel { +class MultiFilterInterface { +public: + virtual bool filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const = 0; +}; + +class TagFilterModel : public QStringListModel, public MultiFilterInterface{ Q_OBJECT public: static TagFilterModel *instance(); virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); virtual Qt::ItemFlags flags(const QModelIndex &index) const; + virtual bool filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const; bool *checkState; bool anyChecked; public @@ -437,7 +443,14 @@ private: class TagFilterSortModel : public QSortFilterProxyModel { Q_OBJECT public: - TagFilterSortModel(QObject *parent = 0); + static TagFilterSortModel *instance(); virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const; + void addFilterModel(MultiFilterInterface *model); + void removeFilterModel(MultiFilterInterface *model); +public slots: + void myInvalidate(); +private: + TagFilterSortModel(QObject *parent = 0); + QList models; }; #endif // MODELS_H diff --git a/qt-ui/simplewidgets.cpp b/qt-ui/simplewidgets.cpp index 05532f805..b31261518 100644 --- a/qt-ui/simplewidgets.cpp +++ b/qt-ui/simplewidgets.cpp @@ -464,3 +464,15 @@ TagFilter::TagFilter(QWidget *parent) : QWidget(parent) connect(ui.filterTag, SIGNAL(textChanged(QString)), filter, SLOT(setFilterFixedString(QString))); ui.tagView->setModel(filter); } + +void TagFilter::showEvent(QShowEvent *event) +{ + TagFilterSortModel::instance()->addFilterModel(TagFilterModel::instance()); + QWidget::showEvent(event); +} + +void TagFilter::hideEvent(QHideEvent *event) +{ + TagFilterSortModel::instance()->removeFilterModel(TagFilterModel::instance()); + QWidget::hideEvent(event); +} diff --git a/qt-ui/simplewidgets.h b/qt-ui/simplewidgets.h index d665f133a..d3e27c06c 100644 --- a/qt-ui/simplewidgets.h +++ b/qt-ui/simplewidgets.h @@ -134,7 +134,8 @@ class TagFilter : public QWidget { Q_OBJECT public: TagFilter(QWidget *parent = 0); - + virtual void showEvent(QShowEvent *); + virtual void hideEvent(QHideEvent *); private: Ui::TagFilter ui; };