2017-04-27 20:25:32 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-11-13 16:31:03 -02:00
|
|
|
#ifndef FILTERMODELS_H
|
|
|
|
#define FILTERMODELS_H
|
|
|
|
|
|
|
|
#include <QStringListModel>
|
|
|
|
#include <QSortFilterProxyModel>
|
2015-05-26 17:42:45 -03:00
|
|
|
#include <stdint.h>
|
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 15:04:38 +01:00
|
|
|
#include <vector>
|
2014-11-13 16:31:03 -02:00
|
|
|
|
2018-08-14 14:09:30 -04:00
|
|
|
struct dive;
|
2018-09-06 09:52:02 +02:00
|
|
|
struct dive_trip;
|
2018-08-14 14:09:30 -04:00
|
|
|
|
2018-08-28 20:44:11 +02:00
|
|
|
class FilterModelBase : public QAbstractListModel {
|
2017-12-24 14:35:59 +01:00
|
|
|
Q_OBJECT
|
2018-08-28 20:44:11 +02:00
|
|
|
private:
|
|
|
|
int findInsertionIndex(const QString &name);
|
2018-08-27 11:49:16 +02:00
|
|
|
protected:
|
|
|
|
struct Item {
|
2018-08-28 20:44:11 +02:00
|
|
|
QString name;
|
2018-08-27 11:49:16 +02:00
|
|
|
bool checked;
|
2018-08-27 12:53:46 +02:00
|
|
|
int count;
|
2018-08-27 11:49:16 +02:00
|
|
|
};
|
|
|
|
std::vector<Item> items;
|
2018-08-28 20:44:11 +02:00
|
|
|
int indexOf(const QString &name) const;
|
|
|
|
void addItem(const QString &name, bool checked, int count);
|
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
2018-09-06 09:52:02 +02:00
|
|
|
void decreaseCount(const QString &d);
|
|
|
|
void increaseCount(const QString &d);
|
2014-11-13 16:31:03 -02:00
|
|
|
public:
|
2018-08-14 14:09:30 -04:00
|
|
|
virtual bool doFilter(const dive *d) const = 0;
|
2018-09-06 09:52:02 +02:00
|
|
|
virtual void diveAdded(const dive *d) = 0;
|
|
|
|
virtual void diveDeleted(const dive *d) = 0;
|
2017-12-23 15:25:54 +01:00
|
|
|
void clearFilter();
|
2017-12-23 15:49:21 +01:00
|
|
|
void selectAll();
|
|
|
|
void invertSelection();
|
2014-11-13 16:31:03 -02:00
|
|
|
bool anyChecked;
|
2017-12-24 14:35:59 +01:00
|
|
|
bool negate;
|
|
|
|
public
|
|
|
|
slots:
|
|
|
|
void setNegate(bool negate);
|
2018-08-28 20:44:11 +02:00
|
|
|
void changeName(const QString &oldName, const QString &newName);
|
2017-11-25 16:15:57 +01:00
|
|
|
protected:
|
|
|
|
explicit FilterModelBase(QObject *parent = 0);
|
2018-08-28 20:54:53 +02:00
|
|
|
void updateList(QStringList &new_list);
|
2017-12-23 15:25:54 +01:00
|
|
|
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);
|
2017-11-25 16:15:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class TagFilterModel : public FilterModelBase {
|
2014-11-13 16:31:03 -02:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
static TagFilterModel *instance();
|
2018-08-14 14:09:30 -04:00
|
|
|
bool doFilter(const dive *d) const;
|
2014-11-13 16:31:03 -02:00
|
|
|
public
|
|
|
|
slots:
|
|
|
|
void repopulate();
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit TagFilterModel(QObject *parent = 0);
|
2017-12-23 15:25:54 +01:00
|
|
|
int countDives(const char *) const;
|
2018-09-06 09:52:02 +02:00
|
|
|
void diveAdded(const dive *d);
|
|
|
|
void diveDeleted(const dive *d);
|
2014-11-13 16:31:03 -02:00
|
|
|
};
|
|
|
|
|
2017-11-25 16:15:57 +01:00
|
|
|
class BuddyFilterModel : public FilterModelBase {
|
2014-11-13 16:31:03 -02:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
static BuddyFilterModel *instance();
|
2018-08-14 14:09:30 -04:00
|
|
|
bool doFilter(const dive *d) const;
|
2014-11-13 16:31:03 -02:00
|
|
|
public
|
|
|
|
slots:
|
|
|
|
void repopulate();
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit BuddyFilterModel(QObject *parent = 0);
|
2017-12-23 15:25:54 +01:00
|
|
|
int countDives(const char *) const;
|
2018-09-06 09:52:02 +02:00
|
|
|
void diveAdded(const dive *d);
|
|
|
|
void diveDeleted(const dive *d);
|
2014-11-13 16:31:03 -02:00
|
|
|
};
|
|
|
|
|
2017-11-25 16:15:57 +01:00
|
|
|
class LocationFilterModel : public FilterModelBase {
|
2014-11-13 16:31:03 -02:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
static LocationFilterModel *instance();
|
2018-08-14 14:09:30 -04:00
|
|
|
bool doFilter(const dive *d) const;
|
2014-11-13 16:31:03 -02:00
|
|
|
public
|
|
|
|
slots:
|
|
|
|
void repopulate();
|
2017-11-26 22:21:58 +01:00
|
|
|
void addName(const QString &newName);
|
2014-11-13 16:31:03 -02:00
|
|
|
|
|
|
|
private:
|
|
|
|
explicit LocationFilterModel(QObject *parent = 0);
|
2017-12-23 15:25:54 +01:00
|
|
|
int countDives(const char *) const;
|
2018-09-06 09:52:02 +02:00
|
|
|
void diveAdded(const dive *d);
|
|
|
|
void diveDeleted(const dive *d);
|
2014-11-13 16:31:03 -02:00
|
|
|
};
|
|
|
|
|
2017-11-25 16:15:57 +01:00
|
|
|
class SuitsFilterModel : public FilterModelBase {
|
2014-11-13 17:07:05 -02:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
static SuitsFilterModel *instance();
|
2018-08-14 14:09:30 -04:00
|
|
|
bool doFilter(const dive *d) const;
|
2014-11-13 17:07:05 -02:00
|
|
|
public
|
|
|
|
slots:
|
|
|
|
void repopulate();
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit SuitsFilterModel(QObject *parent = 0);
|
2017-12-23 15:25:54 +01:00
|
|
|
int countDives(const char *) const;
|
2018-09-06 09:52:02 +02:00
|
|
|
void diveAdded(const dive *d);
|
|
|
|
void diveDeleted(const dive *d);
|
2014-11-13 17:07:05 -02:00
|
|
|
};
|
|
|
|
|
2014-11-13 16:31:03 -02:00
|
|
|
class MultiFilterSortModel : public QSortFilterProxyModel {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
static MultiFilterSortModel *instance();
|
2018-09-29 22:13:44 +02:00
|
|
|
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
|
2017-12-23 14:55:59 +01:00
|
|
|
void addFilterModel(FilterModelBase *model);
|
|
|
|
void removeFilterModel(FilterModelBase *model);
|
2018-09-06 09:52:02 +02:00
|
|
|
void divesAdded(const QVector<dive *> &dives);
|
|
|
|
void divesDeleted(const QVector<dive *> &dives);
|
2018-08-14 14:16:25 -04:00
|
|
|
bool showDive(const struct dive *d) const;
|
2014-11-16 14:04:06 -02:00
|
|
|
int divesDisplayed;
|
2014-11-13 12:51:23 -08:00
|
|
|
public
|
|
|
|
slots:
|
2014-11-13 16:31:03 -02:00
|
|
|
void myInvalidate();
|
|
|
|
void clearFilter();
|
2015-05-26 17:42:45 -03:00
|
|
|
void startFilterDiveSite(uint32_t uuid);
|
2015-05-26 17:36:06 -03:00
|
|
|
void stopFilterDiveSite();
|
2018-09-06 22:03:03 +02:00
|
|
|
void filterChanged(const QModelIndex &from, const QModelIndex &to, const QVector<int> &roles);
|
2015-05-26 17:36:06 -03:00
|
|
|
|
2014-11-16 14:04:06 -02:00
|
|
|
signals:
|
|
|
|
void filterFinished();
|
2014-11-13 16:31:03 -02:00
|
|
|
private:
|
|
|
|
MultiFilterSortModel(QObject *parent = 0);
|
2017-12-23 14:55:59 +01:00
|
|
|
QList<FilterModelBase *> models;
|
2015-05-26 17:36:06 -03:00
|
|
|
struct dive_site *curr_dive_site;
|
2014-11-13 16:31:03 -02:00
|
|
|
};
|
|
|
|
|
2014-11-13 13:45:32 -08:00
|
|
|
#endif
|