diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-08-03 11:35:43 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-10-11 16:22:27 -0700 |
commit | 7067e335964de97d51381ca9e9b09f7236ffc619 (patch) | |
tree | 88587c3ed4664b81c605866aa126bd41af3073db /desktop-widgets/command_divelist.h | |
parent | 23db0ba68df5f785a8a2db5ff5b76bbbee31d69f (diff) | |
download | subsurface-7067e335964de97d51381ca9e9b09f7236ffc619.tar.gz |
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/command_divelist.h')
-rw-r--r-- | desktop-widgets/command_divelist.h | 86 |
1 files changed, 65 insertions, 21 deletions
diff --git a/desktop-widgets/command_divelist.h b/desktop-widgets/command_divelist.h index e679369af..01dd4db77 100644 --- a/desktop-widgets/command_divelist.h +++ b/desktop-widgets/command_divelist.h @@ -38,12 +38,54 @@ struct DivesToTrip std::vector<OwningTripPtr> tripsToAdd; }; -class AddDive : public Base { -public: - AddDive(dive *dive, bool autogroup); +// All divelist commands derive from a common base class, which has a flag +// for when then selection changed. In such a case, in the redo() and undo() +// methods a signal will be sent. The base-class implements redo() and undo(), +// which resets the flag and sends a signal. Derived classes implement the +// virtual methods redoit() and undoit() [Yes, the names could be more expressive]. +// Moreover, the class implements helper methods, which set the selectionChanged +// flag accordingly. +class DiveListBase : public Base { +protected: + DiveListBase(); + + // These are helper functions to add / remove dive from the C-core structures, + // which set the selectionChanged flag if the added / removed dive was selected. + DiveToAdd removeDive(struct dive *d); + dive *addDive(DiveToAdd &d); + std::vector<DiveToAdd> removeDives(std::vector<dive *> &divesToDelete); + std::vector<dive *> addDives(std::vector<DiveToAdd> &divesToAdd); + + // Set the selection to a given state. Set the selectionChanged flag if anything changed. + void restoreSelection(const std::vector<dive *> &selection, dive *currentDive); + + // On first execution, the selections before and after execution will + // be remembered. On all further executions, the selection will be reset to + // the remembered values. + // Note: Therefore, commands should manipulate the selection and send the + // corresponding signals only on first execution! + bool firstExecution; + + // Commands set this flag if the selection changed on first execution. + // Only then, a new the divelist will be scanned again after the command. + // If this flag is set on first execution, a selectionChanged signal will + // be sent. + bool selectionChanged; private: + void initWork(); // reset selectionChanged flag + void finishWork(); // emit signal if selection changed void undo() override; void redo() override; + virtual void redoit() = 0; + virtual void undoit() = 0; +}; + +class AddDive : public DiveListBase { +public: + AddDive(dive *dive, bool autogroup); +private: + void undoit() override; + void redoit() override; bool workToBeDone() override; // For redo @@ -53,14 +95,16 @@ private: // For undo std::vector<dive *> divesToRemove; + std::vector<dive *> selection; + dive * currentDive; }; -class DeleteDive : public Base { +class DeleteDive : public DiveListBase { public: DeleteDive(const QVector<dive *> &divesToDelete); private: - void undo() override; - void redo() override; + void undoit() override; + void redoit() override; bool workToBeDone() override; // For redo @@ -70,12 +114,12 @@ private: std::vector<DiveToAdd> divesToAdd; }; -class ShiftTime : public Base { +class ShiftTime : public DiveListBase { public: ShiftTime(const QVector<dive *> &changedDives, int amount); private: - void undo() override; - void redo() override; + void undoit() override; + void redoit() override; bool workToBeDone() override; // For redo and undo @@ -83,12 +127,12 @@ private: int timeChanged; }; -class RenumberDives : public Base { +class RenumberDives : public DiveListBase { public: RenumberDives(const QVector<QPair<dive *, int>> &divesToRenumber); private: - void undo() override; - void redo() override; + void undoit() override; + void redoit() override; bool workToBeDone() override; // For redo and undo: pairs of dive-id / new number @@ -99,10 +143,10 @@ private: // and MergeTrips all do the same thing, just the intialization differs. // Therefore, define a base class with the proper data-structures, redo() // and undo() functions and derive to specialize the initialization. -class TripBase : public Base { +class TripBase : public DiveListBase { protected: - void undo() override; - void redo() override; + void undoit() override; + void redoit() override; bool workToBeDone() override; // For redo and undo @@ -127,13 +171,13 @@ struct MergeTrips : public TripBase { MergeTrips(dive_trip *trip1, dive_trip *trip2); }; -class SplitDives : public Base { +class SplitDives : public DiveListBase { public: // If time is < 0, split at first surface interval SplitDives(dive *d, duration_t time); private: - void undo() override; - void redo() override; + void undoit() override; + void redoit() override; bool workToBeDone() override; // For redo @@ -147,12 +191,12 @@ private: dive *divesToUnsplit[2]; }; -class MergeDives : public Base { +class MergeDives : public DiveListBase { public: MergeDives(const QVector<dive *> &dives); private: - void undo() override; - void redo() override; + void undoit() override; + void redoit() override; bool workToBeDone() override; // For redo |