subsurface/qt-models/filtermodels.h
Berthold Stoeger f1fc89b978 Dive list: split DiveTripModel into distinct models (tree and list)
The DiveTripModel was used to represent both, trip and list views.
Thus many functions had conditionals checking for the current mode
and both modes had to be represented by the same data structure.

Instead, split the model in two and derive them from a base class,
which implements common functions and defines an interface.

The model can be switched by a call to resetModel(), which invalidates
any pointer obtained by instance(). This is quite surprising
behavior. To handle it, straighten out the control flow:

DiveListView --> MultiFilterSortModel --> DiveTripModelBase

Before, DiveListView accessed DiveTripModelBase directly.

A goal of this commit is to enable usage of the same model by mobile
and desktop.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-01-07 09:33:52 -08:00

63 lines
1.5 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#ifndef FILTERMODELS_H
#define FILTERMODELS_H
#include "divetripmodel.h"
#include <QStringListModel>
#include <QSortFilterProxyModel>
#include <QDateTime>
#include <stdint.h>
#include <vector>
struct dive;
struct dive_trip;
struct FilterData {
bool validFilter = false;
int minVisibility = 0;
int maxVisibility = 5;
int minRating = 0;
int maxRating = 5;
double minWaterTemp = 0;
double maxWaterTemp = 100;
double minAirTemp = 0;
double maxAirTemp = 100;
QDateTime from;
QDateTime to = QDateTime::currentDateTime();
QStringList tags;
QStringList people;
QStringList location;
QStringList equipment;
bool invertFilter;
};
class MultiFilterSortModel : public QSortFilterProxyModel {
Q_OBJECT
public:
static MultiFilterSortModel *instance();
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
bool showDive(const struct dive *d) const;
int divesDisplayed;
bool lessThan(const QModelIndex &, const QModelIndex &) const override;
public
slots:
void myInvalidate();
void clearFilter();
void startFilterDiveSite(struct dive_site *ds);
void stopFilterDiveSite();
void filterChanged(const QModelIndex &from, const QModelIndex &to, const QVector<int> &roles);
void resetModel(DiveTripModelBase::Layout layout);
void filterDataChanged(const FilterData& data);
signals:
void filterFinished();
private:
MultiFilterSortModel(QObject *parent = 0);
struct dive_site *curr_dive_site;
FilterData filterData;
};
#endif