diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-05-06 22:25:48 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-05-07 08:46:15 -0700 |
commit | 7a443423bc633d8ea085879f9ad675cb134cc93c (patch) | |
tree | 6c2d0025273594257530a63a54f3cf3c1990af22 /qt-models/divecomputerextradatamodel.cpp | |
parent | 2ff459c35691f1548cc8a587cd437f5215f8ccc1 (diff) | |
download | subsurface-7a443423bc633d8ea085879f9ad675cb134cc93c.tar.gz |
cleanup: generalize ExtraDataModel to display data of any dc
The goal here is to remove a dependency on displayed_dive.
While doing so, make the model more general and display any dc.
Pass in the dc of the current dive instead of displayed dive,
since all other tabs are already converted to show data of
the current dive. The QStrings are cached since we generate
them anyway, so we may just keep them. Thus, there is no
danger of the dc becoming invalid.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models/divecomputerextradatamodel.cpp')
-rw-r--r-- | qt-models/divecomputerextradatamodel.cpp | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/qt-models/divecomputerextradatamodel.cpp b/qt-models/divecomputerextradatamodel.cpp index e322a70a8..ef16a30f2 100644 --- a/qt-models/divecomputerextradatamodel.cpp +++ b/qt-models/divecomputerextradatamodel.cpp @@ -4,8 +4,7 @@ #include "core/metrics.h" -ExtraDataModel::ExtraDataModel(QObject *parent) : CleanerTableModel(parent), - rows(0) +ExtraDataModel::ExtraDataModel(QObject *parent) : CleanerTableModel(parent) { //enum Column {KEY, VALUE}; setHeaderDataStrings(QStringList() << tr("Key") << tr("Value")); @@ -14,18 +13,15 @@ ExtraDataModel::ExtraDataModel(QObject *parent) : CleanerTableModel(parent), void ExtraDataModel::clear() { beginResetModel(); - rows = 0; + items.clear(); endResetModel(); } QVariant ExtraDataModel::data(const QModelIndex &index, int role) const { - struct extra_data *ed = get_dive_dc(&displayed_dive, dc_number)->extra_data; - int i = -1; - while (ed && ++i < index.row()) - ed = ed->next; - if (!ed) + if (!index.isValid() || index.row() > (int)items.size()) return QVariant(); + const Item &item = items[index.row()]; switch (role) { case Qt::FontRole: @@ -35,9 +31,9 @@ QVariant ExtraDataModel::data(const QModelIndex &index, int role) const case Qt::DisplayRole: switch (index.column()) { case KEY: - return ed->key; + return item.key; case VALUE: - return ed->value; + return item.value; } return QVariant(); } @@ -46,16 +42,16 @@ QVariant ExtraDataModel::data(const QModelIndex &index, int role) const int ExtraDataModel::rowCount(const QModelIndex&) const { - return rows; + return (int)items.size(); } -void ExtraDataModel::updateDive() +void ExtraDataModel::updateDiveComputer(const struct divecomputer *dc) { beginResetModel(); - rows = 0; - struct extra_data *ed = get_dive_dc(&displayed_dive, dc_number)->extra_data; + struct extra_data *ed = dc ? dc->extra_data : nullptr; + items.clear(); while (ed) { - rows++; + items.push_back({ ed->key, ed->value }); ed = ed->next; } endResetModel(); |