diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-02-08 12:06:57 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-02-08 10:29:36 -0800 |
commit | 1a85b0e941b57f4f3c3406a42be78d4d6642d17a (patch) | |
tree | 983d895fa634113ed4290f6c5d92306116a0aae7 /qt-models/divesummarymodel.h | |
parent | 48ccd114fcb5e1ca0a692c096143c2f24d8254d3 (diff) | |
download | subsurface-1a85b0e941b57f4f3c3406a42be78d4d6642d17a.tar.gz |
mobile/summary: create DiveSummaryModel
Instead of passing the dive summary via a completely unstructured
QStringList to QML, implement a dynamic model. For potential reuse
on desktop (though somewhat unlikely) the model has two interfaces,
one for QtWidgets and one for QML. The former is based on columns,
whereas the later is based on roles. The number of columns is
set dynamically. The roles currently support access to two columns.
If more columns should be accessed from QML, more roles have to
be added manually.
This commit only creates the model and hooks it into QMLs global
context, but does not yet change the QML page.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-models/divesummarymodel.h')
-rw-r--r-- | qt-models/divesummarymodel.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/qt-models/divesummarymodel.h b/qt-models/divesummarymodel.h new file mode 100644 index 000000000..13d37ba17 --- /dev/null +++ b/qt-models/divesummarymodel.h @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef DIVESUMMARYMODEL_H +#define DIVESUMMARYMODEL_H + +#include <QAbstractTableModel> +#include <vector> + +class DiveSummaryModel : public QAbstractTableModel { + Q_OBJECT +public: + enum Row { + DIVES, + DIVES_EAN, + DIVES_DEEP, + PLANS, + TIME, + TIME_MAX, + TIME_AVG, + DEPTH_MAX, + DEPTH_AVG, + SAC_MIN, + SAC_MAX, + SAC_AVG, + NUM_ROW + }; + + // Roles for QML. Amazingly it appears that QML before Qt 5.12 *cannot* + // display run-of-the-mill tabular data. + // Therefore we transform a fixed number of columns, including the header + // into roles that can be displayed by QML. The mind boggles. + enum QMLRoles { + HEADER_ROLE = Qt::UserRole + 1, + COLUMN0_ROLE, + COLUMN1_ROLE, + }; + + struct Result { + QString dives, divesEAN, divesDeep, plans; + QString time, time_max, time_avg; + QString depth_max, depth_avg; + QString sac_min, sac_max, sac_avg; + }; + + Q_INVOKABLE void setNumData(int num); + Q_INVOKABLE void calc(int column, int period); +private: + int rowCount(const QModelIndex &parent) const override; + int columnCount(const QModelIndex &parent) const override; + QVariant data(const QModelIndex &index, int role) const override; + QHash<int, QByteArray> roleNames() const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + + QVariant dataDisplay(int row, int col) const; + + std::vector<Result> results; +}; + +#endif |