subsurface/qt-models/filtermodels.h
Berthold Stoeger 979f81f409 Filter: separate backend from frontend logic
The filter code was an unholy intermixture of backend and frontend
logic, which made it hard to access it from outside of the UI.
Notably, it expected that Qt would call filterAcceptsRow on all rows.
For trip-view, apparently the filter functions were called twice
(once for filtering the trip, then for filtering the individual dives).

Make the filtering explicit, by calling showDive() for all dives in
MultiFilterSortModel::myInvalidate(), setting the hidden_by_filter
flags accordingly and ultimately invalidating the filter.

The UI code only accesses the hidden_by_filter flag set previously.

The "justCleared" flag can then be removed, since accessing the filter
does not have side effects. Moreover, there is no noticeable performance
gain by returning out early.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-16 08:07:25 +02:00

117 lines
2.7 KiB
C++

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