diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-01-25 18:27:31 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-04-12 18:19:07 +0300 |
commit | 9e603cbe2bbcae1822859b284c39dd804bf321fb (patch) | |
tree | 9b4091c07aeea8b4abe65593ab07abc2c7326a52 /desktop-widgets/command_edit.h | |
parent | 8858bfa1f8cf30cc4eb070fb6e709f7051e61241 (diff) | |
download | subsurface-9e603cbe2bbcae1822859b284c39dd804bf321fb.tar.gz |
Undo: implement rudimentary undo of dive-notes editing
Implement a first rudimentary dive-editing command. The main code
resides in a base class Command::Edit, which calls virtual functions
to read / set the fields and extract the field name.
Implement an example: editing of dive notes.
This dose not yet update the UI on undo / redo.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/command_edit.h')
-rw-r--r-- | desktop-widgets/command_edit.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/desktop-widgets/command_edit.h b/desktop-widgets/command_edit.h new file mode 100644 index 000000000..0bcb8114c --- /dev/null +++ b/desktop-widgets/command_edit.h @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +// Note: this header file is used by the undo-machinery and should not be included elsewhere. + +#ifndef COMMAND_EDIT_H +#define COMMAND_EDIT_H + +#include "command_base.h" + +#include <QVector> + +// These are commands that edit individual fields of a set of dives. +// The implementation is very OO-style. Out-of-fashion and certainly +// not elegant, but in line with Qt's OO-based design. +// The actual code is in a common base class "Command::EditBase". To +// read and set the fields, the base class calls virtual functions of +// the derived classes. +// +// To deal with different data types, the base class is implemented +// as a template. The template parameter is the type to be read or +// set. Thus, switch-cascades and union trickery can be avoided. + +// We put everything in a namespace, so that we can shorten names without polluting the global namespace +namespace Command { + +template <typename T> +class EditBase : public Base { + T value; // Value to be set + T old; // Previous value + + void undo() override; + void redo() override; + bool workToBeDone() override; + + // Dives to be edited. For historical reasons, the *last* entry was + // the active dive when the user initialized the action. This dive + // will be made the current dive on redo / undo. + std::vector<dive *> dives; +public: + EditBase(const QVector<dive *> &dives, T newValue, T oldValue); + +protected: + // Get and set functions to be overriden by sub-classes. + virtual void set(struct dive *d, T) const = 0; + virtual T data(struct dive *d) const = 0; + virtual QString fieldName() const = 0; // Name of the field, used to create the undo menu-entry +}; + +class EditNotes : public EditBase<QString> { +public: + using EditBase<QString>::EditBase; // Use constructor of base class. + void set(struct dive *d, QString s) const override; + QString data(struct dive *d) const override; + QString fieldName() const override; +}; + +} // namespace Command + +#endif |