summaryrefslogtreecommitdiffstats
path: root/qt-models/weightmodel.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-11-02 18:47:56 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-12-05 10:14:25 -0800
commit76a5a38f5e8aad339061f534df8130b426079a03 (patch)
treec86f07458d4af78431facdb45ccf6c0368e8baa9 /qt-models/weightmodel.cpp
parent936362c1020a87a4c34252913f2cd5d49716c432 (diff)
downloadsubsurface-76a5a38f5e8aad339061f534df8130b426079a03.tar.gz
Cleanup: remove displayed_dive from WeightModel
The WeightModel always acted on the displayed dive. To support undo of weightsystem changes, operate on an arbitrary dive. This is in line with other models, where the updateDive() function resets the model to represent a certain dive. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models/weightmodel.cpp')
-rw-r--r--qt-models/weightmodel.cpp40
1 files changed, 21 insertions, 19 deletions
diff --git a/qt-models/weightmodel.cpp b/qt-models/weightmodel.cpp
index 36971f12e..dafd9d277 100644
--- a/qt-models/weightmodel.cpp
+++ b/qt-models/weightmodel.cpp
@@ -9,6 +9,7 @@
WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
changed(false),
+ d(nullptr),
rows(0)
{
//enum Column {REMOVE, TYPE, WEIGHT};
@@ -18,33 +19,36 @@ WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index)
{
- return &displayed_dive.weightsystems.weightsystems[index.row()];
+ int row = index.row();
+ if (row < 0 || row >= d->weightsystems.nr) {
+ qWarning("WeightModel: Accessing invalid weightsystem %d (of %d)", row, d->weightsystems.nr);
+ return nullptr;
+ }
+ return &d->weightsystems.weightsystems[index.row()];
}
void WeightModel::remove(QModelIndex index)
{
- if (index.column() != REMOVE)
+ if (index.column() != REMOVE || !d)
return;
beginRemoveRows(QModelIndex(), index.row(), index.row());
rows--;
- remove_weightsystem(&displayed_dive, index.row());
+ remove_weightsystem(d, index.row());
changed = true;
endRemoveRows();
}
void WeightModel::clear()
{
- beginResetModel();
- rows = 0;
- endResetModel();
+ updateDive(nullptr);
}
QVariant WeightModel::data(const QModelIndex &index, int role) const
{
- if (!index.isValid() || index.row() >= displayed_dive.weightsystems.nr)
+ if (!index.isValid() || index.row() >= d->weightsystems.nr)
return QVariant();
- const weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
+ const weightsystem_t *ws = &d->weightsystems.weightsystems[index.row()];
switch (role) {
case Qt::FontRole:
@@ -81,7 +85,7 @@ QVariant WeightModel::data(const QModelIndex &index, int role) const
// so we only implement the two columns we care about
void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
{
- weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
+ weightsystem_t *ws = &d->weightsystems.weightsystems[index.row()];
if (index.column() == WEIGHT) {
if (ws->weight.grams != value.toInt()) {
ws->weight.grams = value.toInt();
@@ -94,7 +98,7 @@ void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
QString vString = value.toString();
- weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
+ weightsystem_t *ws = &d->weightsystems.weightsystems[index.row()];
switch (index.column()) {
case TYPE:
if (!value.isNull()) {
@@ -147,29 +151,27 @@ void WeightModel::add()
int row = rows;
weightsystem_t ws { {0}, "" };
beginInsertRows(QModelIndex(), row, row);
- add_cloned_weightsystem(&displayed_dive.weightsystems, ws);
+ add_cloned_weightsystem(&d->weightsystems, ws);
rows++;
changed = true;
endInsertRows();
}
-void WeightModel::updateDive()
+void WeightModel::updateDive(dive *dIn)
{
beginResetModel();
- rows = displayed_dive.weightsystems.nr;
+ d = dIn;
+ rows = d ? d->weightsystems.nr : 0;
endResetModel();
}
void WeightModel::weightsystemsReset(const QVector<dive *> &dives)
{
// This model only concerns the currently displayed dive. If this is not among the
- // dives that had their cylinders reset, exit.
- if (!current_dive || std::find(dives.begin(), dives.end(), current_dive) == dives.end())
+ // dives that had their weight reset, exit.
+ if (!d || std::find(dives.begin(), dives.end(), d) == dives.end())
return;
- // Copy the weights from the current dive to the displayed dive.
- copy_weights(&current_dive->weightsystems, &displayed_dive.weightsystems);
-
// And update the model..
- updateDive();
+ updateDive(d);
}