Undo: implement undo of dive mode editing

Add a new UndoCommand for dive mode editing. This one is a bit
special, as the mode is associated with a dive computer (DC),
not a dive. Thus the edit command has an additional parameter,
viz. the index of the DC.

This does not fit properly to the EditBase class, as this class
isn't aware of additional parameters and therefore this parameter
is not sent via signals. At the moment this doesn't matter. In
any case, the semantics of editing are weird and therefore let's
do the simple thing (derive from EditBase) and let's see what
the future brings.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-01-28 18:35:27 +01:00 committed by Dirk Hohndel
parent 45ef879546
commit f11ac40593
6 changed files with 72 additions and 15 deletions

View file

@ -135,4 +135,9 @@ void editNotes(const QVector<dive *> dives, const QString &newValue, const QStri
execute(new EditNotes(dives, newValue, oldValue));
}
void editMode(const QVector<dive *> dives, int index, int newValue, int oldValue)
{
execute(new EditMode(dives, index, newValue, oldValue));
}
} // namespace Command

View file

@ -53,6 +53,7 @@ void purgeUnusedDiveSites();
// 4) Dive editing related commands
void editNotes(const QVector<dive *> dives, const QString &newValue, const QString &oldValue);
void editMode(const QVector<dive *> dives, int index, int newValue, int oldValue);
} // namespace Command

View file

@ -76,6 +76,9 @@ void EditBase<T>::redo()
undo();
}
// Implementation of virtual functions
// ***** Notes *****
void EditNotes::set(struct dive *d, QString s) const
{
free(d->notes);
@ -97,4 +100,43 @@ DiveField EditNotes::fieldId() const
return DiveField::NOTES;
}
// ***** Mode *****
// Editing the dive mode has very peculiar semantics for historic reasons:
// Since the dive-mode depends on the dive computer, the i-th dive computer
// of each dive will be edited. If the dive has less than i dive computers,
// the default dive computer will be edited.
// The index "i" will be stored as an additional payload with the command.
// Thus, we need an explicit constructor. Since the actual handling is done
// by the base class, which knows nothing about this index, it will not be
// sent via signals. Currently this is not needed. Should it turn out to
// become necessary, then we might either
// - Not derive EditMode from EditBase.
// - Change the semantics of the mode-editing.
// The future will tell.
EditMode::EditMode(const QVector<dive *> &dives, int indexIn, int newValue, int oldValue)
: EditBase(dives, newValue, oldValue), index(indexIn)
{
}
void EditMode::set(struct dive *d, int i) const
{
get_dive_dc(d, index)->divemode = (enum divemode_t)i;
update_setpoint_events(d, get_dive_dc(d, index));
}
int EditMode::data(struct dive *d) const
{
return get_dive_dc(d, index)->divemode;
}
QString EditMode::fieldName() const
{
return tr("dive mode");
}
DiveField EditMode::fieldId() const
{
return DiveField::MODE;
}
} // namespace Command

View file

@ -56,6 +56,16 @@ public:
DiveField fieldId() const override;
};
class EditMode : public EditBase<int> {
int index;
public:
EditMode(const QVector<dive *> &dives, int indexIn, int newValue, int oldValue);
void set(struct dive *d, int i) const override;
int data(struct dive *d) const override;
QString fieldName() const override;
DiveField fieldId() const override;
};
} // namespace Command
#endif

View file

@ -345,6 +345,9 @@ void MainTab::divesEdited(const QVector<dive *> &, DiveField field)
case DiveField::NOTES:
updateNotes(current_dive);
break;
case DiveField::MODE:
updateMode(current_dive);
break;
default:
break;
}
@ -399,6 +402,12 @@ void MainTab::updateNotes(const struct dive *d)
}
}
void MainTab::updateMode(struct dive *d)
{
ui.DiveType->setCurrentIndex(get_dive_dc(d, dc_number)->divemode);
MainWindow::instance()->graphics->recalcCeiling();
}
void MainTab::updateDiveInfo(bool clear)
{
ui.location->refreshDiveSiteCache();
@ -424,7 +433,7 @@ void MainTab::updateDiveInfo(bool clear)
UPDATE_TEXT(displayed_dive, buddy);
UPDATE_TEMP(displayed_dive, airtemp);
UPDATE_TEMP(displayed_dive, watertemp);
ui.DiveType->setCurrentIndex(get_dive_dc(&displayed_dive, dc_number)->divemode);
updateMode(&displayed_dive);
if (!clear) {
struct dive_site *ds = NULL;
@ -558,7 +567,6 @@ void MainTab::updateDiveInfo(bool clear)
}
ui.duration->setText(render_seconds_to_string(displayed_dive.duration.seconds));
ui.depth->setText(get_depth_string(displayed_dive.maxdepth, true));
ui.DiveType->setCurrentIndex(get_dive_dc(&displayed_dive, dc_number)->divemode);
volume_t gases[MAX_CYLINDERS] = {};
get_gas_used(&displayed_dive, gases);
@ -795,14 +803,6 @@ void MainTab::acceptChanges()
MODIFY_DIVES(selectedDives, EDIT_VALUE(visibility));
if (displayed_dive.airtemp.mkelvin != cd->airtemp.mkelvin)
MODIFY_DIVES(selectedDives, EDIT_VALUE(airtemp.mkelvin));
if (displayed_dc->divemode != current_dc->divemode) {
MODIFY_DIVES(selectedDives,
if (get_dive_dc(mydive, dc_number)->divemode == current_dc->divemode || copyPaste)
get_dive_dc(mydive, dc_number)->divemode = displayed_dc->divemode;
);
MODIFY_DIVES(selectedDives, update_setpoint_events(mydive, get_dive_dc(mydive, dc_number)));
do_replot = true;
}
if (displayed_dive.watertemp.mkelvin != cd->watertemp.mkelvin)
MODIFY_DIVES(selectedDives, EDIT_VALUE(watertemp.mkelvin));
@ -1099,12 +1099,10 @@ void MainTab::on_airtemp_textChanged(const QString &text)
void MainTab::divetype_Changed(int index)
{
if (editMode == IGNORE)
if (editMode == IGNORE || !current_dive)
return;
displayed_dc->divemode = (enum divemode_t) index;
update_setpoint_events(&displayed_dive, displayed_dc);
markChangedWidget(ui.DiveType);
MainWindow::instance()->graphics->recalcCeiling();
Command::editMode(getSelectedDivesCurrentLast(), dc_number, (enum divemode_t)index,
get_dive_dc(current_dive, dc_number)->divemode);
}
void MainTab::on_watertemp_textChanged(const QString &text)

View file

@ -68,6 +68,7 @@ slots:
void addWeight_clicked();
void updateDiveInfo(bool clear = false);
void updateNotes(const struct dive *d);
void updateMode(struct dive *d);
void updateDepthDuration();
void acceptChanges();
void rejectChanges();