summaryrefslogtreecommitdiffstats
path: root/desktop-widgets
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-05-19 14:27:10 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-05-20 21:23:16 -0700
commit0bc96905bf92de5e7bac88efde181d93d95998f1 (patch)
treedbfda7a6c615d0b8452bb6ae87e3d98c618df40f /desktop-widgets
parenteba6e76b963115a77b5f8607bc6c3ea1040a466f (diff)
downloadsubsurface-0bc96905bf92de5e7bac88efde181d93d95998f1.tar.gz
Undo: make "delete dive computer" undoable
Simply reuse the code for "move dive computer" by creating a DiveComputerBase base class. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets')
-rw-r--r--desktop-widgets/command.cpp5
-rw-r--r--desktop-widgets/command.h1
-rw-r--r--desktop-widgets/command_divelist.cpp29
-rw-r--r--desktop-widgets/command_divelist.h23
4 files changed, 43 insertions, 15 deletions
diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp
index b6283ec8d..df1ae299d 100644
--- a/desktop-widgets/command.cpp
+++ b/desktop-widgets/command.cpp
@@ -79,6 +79,11 @@ void moveDiveComputerToFront(dive *d, int dc_num)
execute(new MoveDiveComputerToFront(d, dc_num));
}
+void deleteDiveComputer(dive *d, int dc_num)
+{
+ execute(new DeleteDiveComputer(d, dc_num));
+}
+
void mergeDives(const QVector <dive *> &dives)
{
execute(new MergeDives(dives));
diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h
index 543e3bcae..497c59a9a 100644
--- a/desktop-widgets/command.h
+++ b/desktop-widgets/command.h
@@ -37,6 +37,7 @@ void mergeTrips(dive_trip *trip1, dive_trip *trip2);
void splitDives(dive *d, duration_t time);
void splitDiveComputer(dive *d, int dc_num);
void moveDiveComputerToFront(dive *d, int dc_num);
+void deleteDiveComputer(dive *d, int dc_num);
void mergeDives(const QVector <dive *> &dives);
// 3) Dive-site related commands
diff --git a/desktop-widgets/command_divelist.cpp b/desktop-widgets/command_divelist.cpp
index 4c4a7e7d3..eb3de65cd 100644
--- a/desktop-widgets/command_divelist.cpp
+++ b/desktop-widgets/command_divelist.cpp
@@ -803,15 +803,12 @@ SplitDiveComputer::SplitDiveComputer(dive *d, int dc_num) : SplitDivesBase(d, sp
setText(tr("split dive computer"));
}
-MoveDiveComputerToFront::MoveDiveComputerToFront(dive *d, int dc_num)
+DiveComputerBase::DiveComputerBase(dive *old_dive, dive *new_dive)
{
- setText(tr("move dive computer to front"));
-
- dive *new_dive = make_first_dc(d, dc_num);
if (!new_dive)
return;
- diveToRemove.dives.push_back(d);
+ diveToRemove.dives.push_back(old_dive);
// Currently, the core code selects the dive -> this is not what we want, as
// we manually manage the selection post-command.
@@ -825,16 +822,16 @@ MoveDiveComputerToFront::MoveDiveComputerToFront(dive *d, int dc_num)
diveToAdd.dives.resize(1);
diveToAdd.dives[0].dive.reset(new_dive);
- diveToAdd.dives[0].trip = d->divetrip;
- diveToAdd.dives[0].site = d->dive_site;
+ diveToAdd.dives[0].trip = old_dive->divetrip;
+ diveToAdd.dives[0].site = old_dive->dive_site;
}
-bool MoveDiveComputerToFront::workToBeDone()
+bool DiveComputerBase::workToBeDone()
{
return !diveToRemove.dives.empty() || !diveToAdd.dives.empty();
}
-void MoveDiveComputerToFront::redoit()
+void DiveComputerBase::redoit()
{
DivesAndSitesToRemove addedDive = addDives(diveToAdd);
diveToAdd = removeDives(diveToRemove);
@@ -848,12 +845,24 @@ void MoveDiveComputerToFront::redoit()
MainWindow::instance()->graphics->replot(current_dive);
}
-void MoveDiveComputerToFront::undoit()
+void DiveComputerBase::undoit()
{
// Undo and redo do the same
redoit();
}
+MoveDiveComputerToFront::MoveDiveComputerToFront(dive *d, int dc_num)
+ : DiveComputerBase(d, make_first_dc(d, dc_num))
+{
+ setText(tr("move dive computer to front"));
+}
+
+DeleteDiveComputer::DeleteDiveComputer(dive *d, int dc_num)
+ : DiveComputerBase(d, clone_delete_divecomputer(d, dc_num))
+{
+ setText(tr("delete dive computer"));
+}
+
MergeDives::MergeDives(const QVector <dive *> &dives)
{
setText(tr("merge dive"));
diff --git a/desktop-widgets/command_divelist.h b/desktop-widgets/command_divelist.h
index d46697454..7de168e9a 100644
--- a/desktop-widgets/command_divelist.h
+++ b/desktop-widgets/command_divelist.h
@@ -236,23 +236,36 @@ public:
SplitDiveComputer(dive *d, int dc_num);
};
-// When moving the dive computer to the front, we go the ineffective,
-// but easy way: We keep two full copies of the dive (before and after).
+// When manipulating dive computers (moving, deleting) we go the ineffective,
+// but simple and robust way: We keep two full copies of the dive (before and after).
// Removing and readding assures that the dive stays at the correct
// position in the list (the dive computer list is used for sorting dives).
-class MoveDiveComputerToFront : public DiveListBase {
-public:
- MoveDiveComputerToFront(dive *d, int dc_num);
+class DiveComputerBase : public DiveListBase {
+protected:
+ // old_dive must be a dive known to the core.
+ // new_dive must be new dive whose ownership is taken.
+ DiveComputerBase(dive *old_dive, dive *new_dive);
private:
void undoit() override;
void redoit() override;
bool workToBeDone() override;
+protected:
// For redo and undo
DivesAndTripsToAdd diveToAdd;
DivesAndSitesToRemove diveToRemove;
};
+class MoveDiveComputerToFront : public DiveComputerBase {
+public:
+ MoveDiveComputerToFront(dive *d, int dc_num);
+};
+
+class DeleteDiveComputer : public DiveComputerBase {
+public:
+ DeleteDiveComputer(dive *d, int dc_num);
+};
+
class MergeDives : public DiveListBase {
public:
MergeDives(const QVector<dive *> &dives);