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>
This commit is contained in:
Berthold Stoeger 2017-12-23 15:25:54 +01:00 committed by Dirk Hohndel
parent 7eed752ba1
commit 668635e98e
2 changed files with 75 additions and 74 deletions

View file

@ -19,63 +19,10 @@ CLASS *CLASS::instance() \
return self; \ return self; \
} }
#define CREATE_MODEL_SET_DATA_METHOD( CLASS ) \ CREATE_INSTANCE_METHOD(TagFilterModel)
bool CLASS::setData(const QModelIndex &index, const QVariant &value, int role) \ CREATE_INSTANCE_METHOD(BuddyFilterModel)
{ \ CREATE_INSTANCE_METHOD(LocationFilterModel)
if (role == Qt::CheckStateRole) { \ CREATE_INSTANCE_METHOD(SuitsFilterModel)
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(MultiFilterSortModel) CREATE_INSTANCE_METHOD(MultiFilterSortModel)
FilterModelBase::FilterModelBase(QObject *parent) : QStringListModel(parent) FilterModelBase::FilterModelBase(QObject *parent) : QStringListModel(parent)
@ -112,10 +59,56 @@ void FilterModelBase::updateList(const QStringList &newList)
setStringList(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) 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 bool SuitsFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const
{ {
Q_UNUSED(index0); 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() void TagFilterModel::repopulate()
{ {
if (g_tag_list == NULL) 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 bool BuddyFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const
{ {
Q_UNUSED(index0); 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 bool LocationFilterModel::doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const
{ {
Q_UNUSED(index0); Q_UNUSED(index0);

View file

@ -10,57 +10,52 @@
class FilterModelBase : public QStringListModel { class FilterModelBase : public QStringListModel {
public: public:
virtual bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const = 0; virtual bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const = 0;
virtual void clearFilter() = 0; void clearFilter();
std::vector<char> checkState; std::vector<char> checkState;
bool anyChecked; bool anyChecked;
protected: protected:
explicit FilterModelBase(QObject *parent = 0); explicit FilterModelBase(QObject *parent = 0);
void updateList(const QStringList &new_list); void updateList(const QStringList &new_list);
virtual int countDives(const char *) const = 0;
private:
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
}; };
class TagFilterModel : public FilterModelBase { class TagFilterModel : public FilterModelBase {
Q_OBJECT Q_OBJECT
public: public:
static TagFilterModel *instance(); 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;
bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const; bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
void clearFilter();
public public
slots: slots:
void repopulate(); void repopulate();
private: private:
explicit TagFilterModel(QObject *parent = 0); explicit TagFilterModel(QObject *parent = 0);
int countDives(const char *) const;
}; };
class BuddyFilterModel : public FilterModelBase { class BuddyFilterModel : public FilterModelBase {
Q_OBJECT Q_OBJECT
public: public:
static BuddyFilterModel *instance(); static BuddyFilterModel *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;
bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const; bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
void clearFilter();
public public
slots: slots:
void repopulate(); void repopulate();
private: private:
explicit BuddyFilterModel(QObject *parent = 0); explicit BuddyFilterModel(QObject *parent = 0);
int countDives(const char *) const;
}; };
class LocationFilterModel : public FilterModelBase { class LocationFilterModel : public FilterModelBase {
Q_OBJECT Q_OBJECT
public: public:
static LocationFilterModel *instance(); static LocationFilterModel *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;
bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const; bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
void clearFilter();
public public
slots: slots:
void repopulate(); void repopulate();
@ -69,23 +64,21 @@ slots:
private: private:
explicit LocationFilterModel(QObject *parent = 0); explicit LocationFilterModel(QObject *parent = 0);
int countDives(const char *) const;
}; };
class SuitsFilterModel : public FilterModelBase { class SuitsFilterModel : public FilterModelBase {
Q_OBJECT Q_OBJECT
public: public:
static SuitsFilterModel *instance(); static SuitsFilterModel *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;
bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const; bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
void clearFilter();
public public
slots: slots:
void repopulate(); void repopulate();
private: private:
explicit SuitsFilterModel(QObject *parent = 0); explicit SuitsFilterModel(QObject *parent = 0);
int countDives(const char *) const;
}; };
class MultiFilterSortModel : public QSortFilterProxyModel { class MultiFilterSortModel : public QSortFilterProxyModel {