subsurface/qt-models/filtermodels.h
Berthold Stoeger 4da6a0a732 Replace bool * array by std::vector<char> in MultiFilterInterface
This replaces a dynamically allocated array of bool by std::vector<char>.
1) This makes the code shorter and less error prone, because memory
   management has not to be done by hand.
2) It fixes a bug in the old code:
   memset(checkState, false, list.count()) is wrong, because bool is
   not guaranteed to be the same size as char!

Two notes:
1) QMap<>, QVector<>, etc. are used numerous times in the code, so
   this doesn't introduce a new C++ concept. Here, the std:: version
   is used, because there is no need for reference counting, COW
   semantics, etc.
2) std::vector<char> is used instead of std::vector<bool>, because
   the latter does a pessimization where a bitfield is used!

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2017-11-25 08:24:36 -08:00

111 lines
3.4 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#ifndef FILTERMODELS_H
#define FILTERMODELS_H
#include <QStringListModel>
#include <QSortFilterProxyModel>
#include <stdint.h>
#include <vector>
class MultiFilterInterface {
public:
MultiFilterInterface() : anyChecked(false) {}
virtual bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const = 0;
virtual void clearFilter() = 0;
std::vector<char> checkState;
bool anyChecked;
};
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;
bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
void clearFilter();
public
slots:
void repopulate();
private:
explicit TagFilterModel(QObject *parent = 0);
};
class BuddyFilterModel : public QStringListModel, public MultiFilterInterface {
Q_OBJECT
public:
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;
void clearFilter();
public
slots:
void repopulate();
private:
explicit BuddyFilterModel(QObject *parent = 0);
};
class LocationFilterModel : public QStringListModel, public MultiFilterInterface {
Q_OBJECT
public:
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;
void clearFilter();
public
slots:
void repopulate();
private:
explicit LocationFilterModel(QObject *parent = 0);
};
class SuitsFilterModel : public QStringListModel, public MultiFilterInterface {
Q_OBJECT
public:
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;
void clearFilter();
public
slots:
void repopulate();
private:
explicit SuitsFilterModel(QObject *parent = 0);
};
class MultiFilterSortModel : public QSortFilterProxyModel {
Q_OBJECT
public:
static MultiFilterSortModel *instance();
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
void addFilterModel(MultiFilterInterface *model);
void removeFilterModel(MultiFilterInterface *model);
int divesDisplayed;
public
slots:
void myInvalidate();
void clearFilter();
void startFilterDiveSite(uint32_t uuid);
void stopFilterDiveSite();
signals:
void filterFinished();
private:
MultiFilterSortModel(QObject *parent = 0);
QList<MultiFilterInterface *> models;
bool justCleared;
struct dive_site *curr_dive_site;
};
#endif