Undo: make profile-editing undoable

Recently, undo of dive-replanning was introduced. Therefore,
it appears logical to do the same thing for editing of the
profile of manually added dives.

For now, use the same undo-command, just change the displayed
text from "replan dive" to "edit profile". Move the fixup dive
call into the undo-command.

Eventually, every action on the profile should be made undoable.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-11-30 15:51:34 +01:00 committed by Dirk Hohndel
parent 7c024f12d2
commit a6fa6cdb41
5 changed files with 17 additions and 22 deletions

View file

@ -260,7 +260,12 @@ void pasteDives(const dive *d, dive_components what)
void replanDive(dive *d) void replanDive(dive *d)
{ {
execute(new ReplanDive(d)); execute(new ReplanDive(d, false));
}
void editProfile(dive *d)
{
execute(new ReplanDive(d, true));
} }
// Trip editing related commands // Trip editing related commands

View file

@ -80,6 +80,7 @@ int editBuddies(const QStringList &newList, bool currentDiveOnly);
int editDiveMaster(const QStringList &newList, bool currentDiveOnly); int editDiveMaster(const QStringList &newList, bool currentDiveOnly);
void pasteDives(const dive *d, dive_components what); void pasteDives(const dive *d, dive_components what);
void replanDive(dive *d); // dive computer(s) and cylinder(s) will be reset! void replanDive(dive *d); // dive computer(s) and cylinder(s) will be reset!
void editProfile(dive *d); // dive computer(s) and cylinder(s) will be reset!
// 5) Trip editing commands // 5) Trip editing commands

View file

@ -887,7 +887,7 @@ void PasteDives::redo()
} }
// ***** Paste ***** // ***** Paste *****
ReplanDive::ReplanDive(dive *source) : d(current_dive), ReplanDive::ReplanDive(dive *source, bool edit_profile) : d(current_dive),
dc({ 0 }), dc({ 0 }),
notes(nullptr) notes(nullptr)
{ {
@ -895,6 +895,10 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive),
if (!d) if (!d)
return; return;
// Fix source. Things might be inconsistent after modifying the profile.
source->maxdepth.mm = source->dc.maxdepth.mm = 0;
fixup_dive(source);
when = source->when; when = source->when;
maxdepth = source->maxdepth; maxdepth = source->maxdepth;
meandepth = source->meandepth; meandepth = source->meandepth;
@ -907,7 +911,7 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive),
std::swap(source->cylinders, cylinders); std::swap(source->cylinders, cylinders);
std::swap(source->dc, dc); std::swap(source->dc, dc);
setText(tr("Replan dive")); setText(edit_profile ? tr("Replan dive") : tr("Edit profile"));
} }
ReplanDive::~ReplanDive() ReplanDive::~ReplanDive()

View file

@ -320,7 +320,9 @@ class ReplanDive : public Base {
duration_t duration; duration_t duration;
int salinity; int salinity;
public: public:
ReplanDive(dive *source); // Dive computer(s) and cylinders(s) of the source dive will be reset! // Dive computer(s) and cylinders(s) of the source dive will be reset!
// If edit_profile is true, the text will be changed from "replan dive" to "edit profile".
ReplanDive(dive *source, bool edit_profile);
~ReplanDive(); ~ReplanDive();
private: private:
void undo() override; void undo() override;

View file

@ -529,8 +529,6 @@ void MainTab::refreshDisplayedDiveSite()
void MainTab::acceptChanges() void MainTab::acceptChanges()
{ {
int addedId = -1;
if (ui.location->hasFocus()) if (ui.location->hasFocus())
stealFocus(); stealFocus();
@ -541,33 +539,18 @@ void MainTab::acceptChanges()
ui.dateEdit->setEnabled(true); ui.dateEdit->setEnabled(true);
hideMessage(); hideMessage();
if (lastMode == MANUALLY_ADDED_DIVE) {
// preserve any changes to the profile
free(current_dive->dc.sample);
copy_samples(&displayed_dive.dc, &current_dive->dc);
addedId = displayed_dive.id;
}
// TODO: This is a temporary hack until the equipment tab is included in the undo system: // TODO: This is a temporary hack until the equipment tab is included in the undo system:
// The equipment tab is hardcoded at the first place of the "extra widgets". // The equipment tab is hardcoded at the first place of the "extra widgets".
((TabDiveEquipment *)extraWidgets[0])->acceptChanges(); ((TabDiveEquipment *)extraWidgets[0])->acceptChanges();
if (lastMode == MANUALLY_ADDED_DIVE) { if (lastMode == MANUALLY_ADDED_DIVE) {
// we just added or edited the dive, let fixup_dive() make
// sure we get the max. depth right
current_dive->maxdepth.mm = current_dc->maxdepth.mm = 0;
fixup_dive(current_dive);
set_dive_nr_for_current_dive();
MainWindow::instance()->showProfile(); MainWindow::instance()->showProfile();
mark_divelist_changed(true);
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::NOTHING); DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::NOTHING);
Command::editProfile(&displayed_dive);
} }
int scrolledBy = MainWindow::instance()->diveList->verticalScrollBar()->sliderPosition(); int scrolledBy = MainWindow::instance()->diveList->verticalScrollBar()->sliderPosition();
if (lastMode == MANUALLY_ADDED_DIVE) { if (lastMode == MANUALLY_ADDED_DIVE) {
MainWindow::instance()->diveList->reload(); MainWindow::instance()->diveList->reload();
int newDiveNr = get_divenr(get_dive_by_uniq_id(addedId));
MainWindow::instance()->diveList->unselectDives();
MainWindow::instance()->diveList->selectDive(newDiveNr, true);
MainWindow::instance()->refreshDisplay(); MainWindow::instance()->refreshDisplay();
MainWindow::instance()->graphics->replot(); MainWindow::instance()->graphics->replot();
} else { } else {