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>
This commit is contained in:
Berthold Stoeger 2019-11-02 18:47:56 +01:00 committed by Dirk Hohndel
parent 936362c102
commit 76a5a38f5e
3 changed files with 25 additions and 22 deletions

View file

@ -129,7 +129,7 @@ void TabDiveEquipment::toggleTriggeredColumn()
void TabDiveEquipment::updateData()
{
cylindersModel->updateDive();
weightModel->updateDive();
weightModel->updateDive(current_dive);
suitModel.updateModel();
ui.cylinders->view()->hideColumn(CylindersModel::DEPTH);
@ -272,7 +272,7 @@ void TabDiveEquipment::rejectChanges()
cylindersModel->changed = false;
weightModel->changed = false;
cylindersModel->updateDive();
weightModel->updateDive();
weightModel->updateDive(current_dive);
}
void TabDiveEquipment::divesEdited(int i)

View file

@ -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);
}

View file

@ -25,7 +25,7 @@ public:
void passInData(const QModelIndex &index, const QVariant &value);
void add();
void clear();
void updateDive();
void updateDive(dive *d);
weightsystem_t *weightSystemAt(const QModelIndex &index);
bool changed;
@ -35,6 +35,7 @@ slots:
void weightsystemsReset(const QVector<dive *> &dives);
private:
dive *d;
int rows;
};