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>
This commit is contained in:
Berthold Stoeger 2020-05-06 22:25:48 +02:00 committed by Dirk Hohndel
parent 2ff459c356
commit 7a443423bc
3 changed files with 23 additions and 21 deletions

View file

@ -1,12 +1,13 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "TabDiveExtraInfo.h" #include "TabDiveExtraInfo.h"
#include "ui_TabDiveExtraInfo.h" #include "ui_TabDiveExtraInfo.h"
#include "core/dive.h"
#include "qt-models/divecomputerextradatamodel.h" #include "qt-models/divecomputerextradatamodel.h"
TabDiveExtraInfo::TabDiveExtraInfo(QWidget *parent) : TabDiveExtraInfo::TabDiveExtraInfo(QWidget *parent) :
TabBase(parent), TabBase(parent),
ui(new Ui::TabDiveExtraInfo()), ui(new Ui::TabDiveExtraInfo()),
extraDataModel(new ExtraDataModel()) extraDataModel(new ExtraDataModel(this))
{ {
ui->setupUi(this); ui->setupUi(this);
ui->extraData->setModel(extraDataModel); ui->extraData->setModel(extraDataModel);
@ -19,11 +20,10 @@ TabDiveExtraInfo::~TabDiveExtraInfo()
void TabDiveExtraInfo::updateData() void TabDiveExtraInfo::updateData()
{ {
extraDataModel->updateDive(); extraDataModel->updateDiveComputer(current_dc);
} }
void TabDiveExtraInfo::clear() void TabDiveExtraInfo::clear()
{ {
extraDataModel->updateDive(); extraDataModel->clear();
} }

View file

@ -4,8 +4,7 @@
#include "core/metrics.h" #include "core/metrics.h"
ExtraDataModel::ExtraDataModel(QObject *parent) : CleanerTableModel(parent), ExtraDataModel::ExtraDataModel(QObject *parent) : CleanerTableModel(parent)
rows(0)
{ {
//enum Column {KEY, VALUE}; //enum Column {KEY, VALUE};
setHeaderDataStrings(QStringList() << tr("Key") << tr("Value")); setHeaderDataStrings(QStringList() << tr("Key") << tr("Value"));
@ -14,18 +13,15 @@ ExtraDataModel::ExtraDataModel(QObject *parent) : CleanerTableModel(parent),
void ExtraDataModel::clear() void ExtraDataModel::clear()
{ {
beginResetModel(); beginResetModel();
rows = 0; items.clear();
endResetModel(); endResetModel();
} }
QVariant ExtraDataModel::data(const QModelIndex &index, int role) const QVariant ExtraDataModel::data(const QModelIndex &index, int role) const
{ {
struct extra_data *ed = get_dive_dc(&displayed_dive, dc_number)->extra_data; if (!index.isValid() || index.row() > (int)items.size())
int i = -1;
while (ed && ++i < index.row())
ed = ed->next;
if (!ed)
return QVariant(); return QVariant();
const Item &item = items[index.row()];
switch (role) { switch (role) {
case Qt::FontRole: case Qt::FontRole:
@ -35,9 +31,9 @@ QVariant ExtraDataModel::data(const QModelIndex &index, int role) const
case Qt::DisplayRole: case Qt::DisplayRole:
switch (index.column()) { switch (index.column()) {
case KEY: case KEY:
return ed->key; return item.key;
case VALUE: case VALUE:
return ed->value; return item.value;
} }
return QVariant(); return QVariant();
} }
@ -46,16 +42,16 @@ QVariant ExtraDataModel::data(const QModelIndex &index, int role) const
int ExtraDataModel::rowCount(const QModelIndex&) const int ExtraDataModel::rowCount(const QModelIndex&) const
{ {
return rows; return (int)items.size();
} }
void ExtraDataModel::updateDive() void ExtraDataModel::updateDiveComputer(const struct divecomputer *dc)
{ {
beginResetModel(); beginResetModel();
rows = 0; struct extra_data *ed = dc ? dc->extra_data : nullptr;
struct extra_data *ed = get_dive_dc(&displayed_dive, dc_number)->extra_data; items.clear();
while (ed) { while (ed) {
rows++; items.push_back({ ed->key, ed->value });
ed = ed->next; ed = ed->next;
} }
endResetModel(); endResetModel();

View file

@ -4,6 +4,8 @@
#include "cleanertablemodel.h" #include "cleanertablemodel.h"
struct divecomputer;
/* extra data model for additional dive computer data */ /* extra data model for additional dive computer data */
class ExtraDataModel : public CleanerTableModel { class ExtraDataModel : public CleanerTableModel {
Q_OBJECT Q_OBJECT
@ -17,10 +19,14 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
void clear(); void clear();
void updateDive(); void updateDiveComputer(const struct divecomputer *dc);
private: private:
int rows; struct Item {
QString key;
QString value;
};
std::vector<Item> items;
}; };
#endif #endif