diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2017-12-23 15:25:54 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2017-12-24 08:24:11 -0800 |
commit | 668635e98eb32e3c5ffd3cb3e65da51c71db2975 (patch) | |
tree | dc2356729fdac453a412c5c00c785c19c651ac23 /qt-models/filtermodels.cpp | |
parent | 7eed752ba15bcb152d448868d2d291abde8748aa (diff) | |
download | subsurface-668635e98eb32e3c5ffd3cb3e65da51c71db2975.tar.gz |
Move *FilterModel functions into base class
The *FilterModels had a number of of virtual functions, which only
accessed members of the base class. Moreover, these functions were
identical and generated with macros. Therefore, move these functions
to the base class.
The one excption is data(), which uses different count functions
(passed as a macro parameter). Thus, introduce a virtual countDives()
function and likewise move data() to the base class. A function pointer
might be even more clear, but since the rest of the code/Qt relies
heavily on runtime polymorphism, let's do the same here.
The only macros left are those creating the singleton accessors.
This could be more clearly realized by templates, but let's
likewise keep it the way is.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models/filtermodels.cpp')
-rw-r--r-- | qt-models/filtermodels.cpp | 122 |
1 files changed, 65 insertions, 57 deletions
diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp index abc8d3bac..26759571a 100644 --- a/qt-models/filtermodels.cpp +++ b/qt-models/filtermodels.cpp @@ -19,63 +19,10 @@ CLASS *CLASS::instance() \ return self; \ } -#define CREATE_MODEL_SET_DATA_METHOD( CLASS ) \ -bool CLASS::setData(const QModelIndex &index, const QVariant &value, int role) \ -{ \ - if (role == Qt::CheckStateRole) { \ - checkState[index.row()] = value.toBool(); \ - anyChecked = false; \ - for (int i = 0; i < rowCount(); i++) { \ - if (checkState[i] == true) { \ - anyChecked = true; \ - break; \ - } \ - } \ - dataChanged(index, index); \ - return true; \ - } \ - return false; \ -} - -#define CREATE_CLEAR_FILTER_METHOD( CLASS ) \ -void CLASS::clearFilter() \ -{ \ - std::fill(checkState.begin(), checkState.end(), false); \ - anyChecked = false; \ - emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0)); \ -} - -#define CREATE_FLAGS_METHOD( CLASS ) \ -Qt::ItemFlags CLASS::flags(const QModelIndex &index) const \ -{ \ - return QStringListModel::flags(index) | Qt::ItemIsUserCheckable; \ -} - -#define CREATE_DATA_METHOD( CLASS, COUNTER_FUNCTION ) \ -QVariant CLASS::data(const QModelIndex &index, int role) const \ -{ \ - if (role == Qt::CheckStateRole) { \ - return checkState[index.row()] ? Qt::Checked : Qt::Unchecked; \ - } else if (role == Qt::DisplayRole) { \ - QString value = stringList()[index.row()]; \ - int count = COUNTER_FUNCTION((index.row() == rowCount() - 1) ? "" : value.toUtf8().data()); \ - return value + QString(" (%1)").arg(count); \ - } \ - return QVariant(); \ -} - -#define CREATE_COMMON_METHODS_FOR_FILTER( CLASS, COUNTER_FUNCTION ) \ -CREATE_FLAGS_METHOD( CLASS ); \ -CREATE_CLEAR_FILTER_METHOD( CLASS ); \ -CREATE_MODEL_SET_DATA_METHOD( CLASS ); \ -CREATE_INSTANCE_METHOD( CLASS ); \ -CREATE_DATA_METHOD( CLASS, COUNTER_FUNCTION ) - -CREATE_COMMON_METHODS_FOR_FILTER(TagFilterModel, count_dives_with_tag) -CREATE_COMMON_METHODS_FOR_FILTER(BuddyFilterModel, count_dives_with_person) -CREATE_COMMON_METHODS_FOR_FILTER(LocationFilterModel, count_dives_with_location) -CREATE_COMMON_METHODS_FOR_FILTER(SuitsFilterModel, count_dives_with_suit) - +CREATE_INSTANCE_METHOD(TagFilterModel) +CREATE_INSTANCE_METHOD(BuddyFilterModel) +CREATE_INSTANCE_METHOD(LocationFilterModel) +CREATE_INSTANCE_METHOD(SuitsFilterModel) CREATE_INSTANCE_METHOD(MultiFilterSortModel) FilterModelBase::FilterModelBase(QObject *parent) : QStringListModel(parent) @@ -112,10 +59,56 @@ void FilterModelBase::updateList(const QStringList &newList) setStringList(newList); } +Qt::ItemFlags FilterModelBase::flags(const QModelIndex &index) const +{ + return QStringListModel::flags(index) | Qt::ItemIsUserCheckable; +} + +bool FilterModelBase::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (role == Qt::CheckStateRole) { + checkState[index.row()] = value.toBool(); + anyChecked = false; + for (int i = 0; i < rowCount(); i++) { + if (checkState[i] == true) { + anyChecked = true; + break; + } + } + dataChanged(index, index); + return true; + } + return false; +} + +QVariant FilterModelBase::data(const QModelIndex &index, int role) const +{ + if (role == Qt::CheckStateRole) { + return checkState[index.row()] ? Qt::Checked : Qt::Unchecked; + } else if (role == Qt::DisplayRole) { + QString value = stringList()[index.row()]; + int count = countDives((index.row() == rowCount() - 1) ? "" : value.toUtf8().data()); + return value + QString(" (%1)").arg(count); + } + return QVariant(); +} + +void FilterModelBase::clearFilter() +{ + std::fill(checkState.begin(), checkState.end(), false); + anyChecked = false; + emit dataChanged(createIndex(0,0), createIndex(rowCount()-1, 0)); +} + SuitsFilterModel::SuitsFilterModel(QObject *parent) : FilterModelBase(parent) { } +int SuitsFilterModel::countDives(const char *s) const +{ + return count_dives_with_suit(s); +} + bool SuitsFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const { Q_UNUSED(index0); @@ -165,6 +158,11 @@ TagFilterModel::TagFilterModel(QObject *parent) : FilterModelBase(parent) { } +int TagFilterModel::countDives(const char *s) const +{ + return count_dives_with_tag(s); +} + void TagFilterModel::repopulate() { if (g_tag_list == NULL) @@ -219,6 +217,11 @@ BuddyFilterModel::BuddyFilterModel(QObject *parent) : FilterModelBase(parent) { } +int BuddyFilterModel::countDives(const char *s) const +{ + return count_dives_with_person(s); +} + bool BuddyFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const { Q_UNUSED(index0); @@ -274,6 +277,11 @@ LocationFilterModel::LocationFilterModel(QObject *parent) : FilterModelBase(pare { } +int LocationFilterModel::countDives(const char *s) const +{ + return count_dives_with_location(s); +} + bool LocationFilterModel::doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const { Q_UNUSED(index0); |