summaryrefslogtreecommitdiffstats
path: root/qt-models/filtermodels.h
blob: f579df1a909e9cf8a8fa92a6848e6294f17f1107 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// SPDX-License-Identifier: GPL-2.0
#ifndef FILTERMODELS_H
#define FILTERMODELS_H

#include "divetripmodel.h" // For DiveTripModel::Layout. TODO: remove in due course

#include <QStringListModel>
#include <QSortFilterProxyModel>
#include <stdint.h>
#include <vector>

struct dive;
struct dive_trip;

class FilterModelBase : public QAbstractListModel {
	Q_OBJECT
private:
	int findInsertionIndex(const QString &name);
protected:
	struct Item {
		QString name;
		bool checked;
		int count;
	};
	std::vector<Item> items;
	int indexOf(const QString &name) const;
	void addItem(const QString &name, bool checked, int count);
	int rowCount(const QModelIndex &parent = QModelIndex()) const override;
	void decreaseCount(const QString &d);
	void increaseCount(const QString &d);
public:
	virtual bool doFilter(const dive *d) const = 0;
	virtual void diveAdded(const dive *d) = 0;
	virtual void diveDeleted(const dive *d) = 0;
	void clearFilter();
	void selectAll();
	void invertSelection();
	bool anyChecked;
	bool negate;
public
slots:
	void setNegate(bool negate);
	void changeName(const QString &oldName, const QString &newName);
protected:
	explicit FilterModelBase(QObject *parent = 0);
	void updateList(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;
	void diveAdded(const dive *d);
	void diveDeleted(const dive *d);
};

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;
	void diveAdded(const dive *d);
	void diveDeleted(const dive *d);
};

class LocationFilterModel : public FilterModelBase {
	Q_OBJECT
public:
	static LocationFilterModel *instance();
	bool doFilter(const dive *d) const;
public
slots:
	void repopulate();
	void addName(const QString &newName);

private:
	explicit LocationFilterModel(QObject *parent = 0);
	int countDives(const char *) const;
	void diveAdded(const dive *d);
	void diveDeleted(const dive *d);
};

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;
	void diveAdded(const dive *d);
	void diveDeleted(const dive *d);
};

class MultiFilterSortModel : public QSortFilterProxyModel {
	Q_OBJECT
public:
	static MultiFilterSortModel *instance();
	bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
	void addFilterModel(FilterModelBase *model);
	void removeFilterModel(FilterModelBase *model);
	void divesAdded(const QVector<dive *> &dives);
	void divesDeleted(const QVector<dive *> &dives);
	bool showDive(const struct dive *d) const;
	int divesDisplayed;
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 setLayout(DiveTripModel::Layout layout);

signals:
	void filterFinished();
private:
	MultiFilterSortModel(QObject *parent = 0);
	QList<FilterModelBase *> models;
	struct dive_site *curr_dive_site;
};

#endif