subsurface/qt-models/divelistmodel.h
Berthold Stoeger 05200f9266 Cleanup: unify idiosyncratic singletons
The way we handle singletons in QML, QML insists on allocating the
objects. This leads to a very idiosyncratic way of handling
singletons: The global instance pointer is set in the constructor.

Unify all these by implementing a "SillySingleton" template. All
of the weird singleton-classes can derive from this template and
don't have to bother with reimplementing the instance() function
with all the safety-checks, etc.

This serves firstly as documentation but also improves debugging
as we will now see wanted and unwanted creation and destruction
of these weird singletons.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-09-25 13:35:30 -07:00

68 lines
1.7 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#ifndef DIVELISTMODEL_H
#define DIVELISTMODEL_H
#include <QAbstractListModel>
#include <QSortFilterProxyModel>
#include "core/subsurface-qt/DiveObjectHelper.h"
#include "core/singleton.h"
class DiveListSortModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
DiveListSortModel(QObject *parent = 0);
void setSourceModel(QAbstractItemModel *sourceModel);
Q_INVOKABLE void reload();
Q_INVOKABLE QString tripTitle(const QString &trip);
Q_INVOKABLE QString tripShortDate(const QString &trip);
public slots:
int getIdxForId(int id);
void setFilter(QString f);
void resetFilter();
int shown();
protected:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
private:
QString filterString;
void updateFilterState();
};
class DiveListModel : public QAbstractListModel, public SillySingleton<DiveListModel>
{
Q_OBJECT
public:
enum DiveListRoles {
DiveRole = Qt::UserRole + 1,
DiveDateRole,
TripIdRole,
TripNrDivesRole,
DateTimeRole,
IdRole,
NumberRole,
LocationRole,
DepthDurationRole,
};
DiveListModel(QObject *parent = 0);
void addDive(const QList<dive *> &listOfDives);
void addAllDives();
void insertDive(int i);
void removeDive(int i);
void removeDiveById(int id);
void updateDive(int i, dive *d);
void reload();
struct dive *getDive(int i);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int getDiveIdx(int id) const;
QModelIndex getDiveQIdx(int id);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;
QString startAddDive();
void resetInternalData();
Q_INVOKABLE DiveObjectHelper at(int i);
};
#endif // DIVELISTMODEL_H