diff --git a/desktop-widgets/tab-widgets/TabDiveExtraInfo.cpp b/desktop-widgets/tab-widgets/TabDiveExtraInfo.cpp index caf7c99bf..c078b7e40 100644 --- a/desktop-widgets/tab-widgets/TabDiveExtraInfo.cpp +++ b/desktop-widgets/tab-widgets/TabDiveExtraInfo.cpp @@ -1,12 +1,13 @@ // SPDX-License-Identifier: GPL-2.0 #include "TabDiveExtraInfo.h" #include "ui_TabDiveExtraInfo.h" +#include "core/dive.h" #include "qt-models/divecomputerextradatamodel.h" TabDiveExtraInfo::TabDiveExtraInfo(QWidget *parent) : TabBase(parent), ui(new Ui::TabDiveExtraInfo()), - extraDataModel(new ExtraDataModel()) + extraDataModel(new ExtraDataModel(this)) { ui->setupUi(this); ui->extraData->setModel(extraDataModel); @@ -19,11 +20,10 @@ TabDiveExtraInfo::~TabDiveExtraInfo() void TabDiveExtraInfo::updateData() { - extraDataModel->updateDive(); + extraDataModel->updateDiveComputer(current_dc); } void TabDiveExtraInfo::clear() { - extraDataModel->updateDive(); + extraDataModel->clear(); } - 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(); diff --git a/qt-models/divecomputerextradatamodel.h b/qt-models/divecomputerextradatamodel.h index ab78e4541..9af4028eb 100644 --- a/qt-models/divecomputerextradatamodel.h +++ b/qt-models/divecomputerextradatamodel.h @@ -4,6 +4,8 @@ #include "cleanertablemodel.h" +struct divecomputer; + /* extra data model for additional dive computer data */ class ExtraDataModel : public CleanerTableModel { Q_OBJECT @@ -17,10 +19,14 @@ public: int rowCount(const QModelIndex &parent = QModelIndex()) const override; void clear(); - void updateDive(); + void updateDiveComputer(const struct divecomputer *dc); private: - int rows; + struct Item { + QString key; + QString value; + }; + std::vector items; }; #endif