subsurface/qt-models/divecomputerextradatamodel.cpp
Berthold Stoeger 7a443423bc 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>
2020-05-07 08:46:15 -07:00

58 lines
1.2 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#include "qt-models/divecomputerextradatamodel.h"
#include "core/dive.h"
#include "core/metrics.h"
ExtraDataModel::ExtraDataModel(QObject *parent) : CleanerTableModel(parent)
{
//enum Column {KEY, VALUE};
setHeaderDataStrings(QStringList() << tr("Key") << tr("Value"));
}
void ExtraDataModel::clear()
{
beginResetModel();
items.clear();
endResetModel();
}
QVariant ExtraDataModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() > (int)items.size())
return QVariant();
const Item &item = items[index.row()];
switch (role) {
case Qt::FontRole:
return defaultModelFont();
case Qt::TextAlignmentRole:
return static_cast<int>(Qt::AlignLeft | Qt::AlignVCenter);
case Qt::DisplayRole:
switch (index.column()) {
case KEY:
return item.key;
case VALUE:
return item.value;
}
return QVariant();
}
return QVariant();
}
int ExtraDataModel::rowCount(const QModelIndex&) const
{
return (int)items.size();
}
void ExtraDataModel::updateDiveComputer(const struct divecomputer *dc)
{
beginResetModel();
struct extra_data *ed = dc ? dc->extra_data : nullptr;
items.clear();
while (ed) {
items.push_back({ ed->key, ed->value });
ed = ed->next;
}
endResetModel();
}