diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-05-27 09:31:26 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-09-29 16:13:03 -0700 |
commit | 2f5223035a9d43b933b0baf64823dab84b5d8cea (patch) | |
tree | 91a75c82f307076079e247f68f19b04ea0ccc529 /commands | |
parent | 536fc05dd6acd67a459fe6decbd67071ad388df0 (diff) | |
download | subsurface-2f5223035a9d43b933b0baf64823dab84b5d8cea.tar.gz |
filter: add filter preset undo commands
Add undo commands to add / edit / delete filter presets.
These are styled after the other undo commands: On changes,
the UI is informed by DiveListNotifier signals. Editing is
a simple std::swap of values.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'commands')
-rw-r--r-- | commands/CMakeLists.txt | 2 | ||||
-rw-r--r-- | commands/command.cpp | 16 | ||||
-rw-r--r-- | commands/command.h | 7 | ||||
-rw-r--r-- | commands/command_filter.cpp | 91 | ||||
-rw-r--r-- | commands/command_filter.h | 61 |
5 files changed, 177 insertions, 0 deletions
diff --git a/commands/CMakeLists.txt b/commands/CMakeLists.txt index aa51dd066..bcd76bba9 100644 --- a/commands/CMakeLists.txt +++ b/commands/CMakeLists.txt @@ -16,6 +16,8 @@ set(SUBSURFACE_GENERIC_COMMANDS_SRCS command_edit_trip.h command_event.cpp command_event.h + command_filter.cpp + command_filter.h command_pictures.cpp command_pictures.h ) diff --git a/commands/command.cpp b/commands/command.cpp index 7c154ee24..f04d5cc7a 100644 --- a/commands/command.cpp +++ b/commands/command.cpp @@ -6,6 +6,7 @@ #include "command_edit.h" #include "command_edit_trip.h" #include "command_event.h" +#include "command_filter.h" #include "command_pictures.h" namespace Command { @@ -377,4 +378,19 @@ void addPictures(const std::vector<PictureListForAddition> &pictures) execute(new AddPictures(pictures)); } +void createFilterPreset(const QString &name, const FilterData &data) +{ + execute(new CreateFilterPreset(name, data)); +} + +void removeFilterPreset(int index) +{ + execute(new RemoveFilterPreset(index)); +} + +void editFilterPreset(int index, const FilterData &data) +{ + execute(new EditFilterPreset(index, data)); +} + } // namespace Command diff --git a/commands/command.h b/commands/command.h index 6072ca9c3..3cdec6d4e 100644 --- a/commands/command.h +++ b/commands/command.h @@ -9,6 +9,7 @@ #include <vector> struct DiveAndLocation; +struct FilterData; // We put everything in a namespace, so that we can shorten names without polluting the global namespace namespace Command { @@ -133,6 +134,12 @@ void setPictureOffset(dive *d, const QString &filename, offset_t offset); void removePictures(const std::vector<PictureListForDeletion> &pictures); void addPictures(const std::vector<PictureListForAddition> &pictures); +// 8) Filter commands + +void createFilterPreset(const QString &name, const FilterData &data); +void removeFilterPreset(int index); +void editFilterPreset(int index, const FilterData &data); + } // namespace Command #endif // COMMAND_H diff --git a/commands/command_filter.cpp b/commands/command_filter.cpp new file mode 100644 index 000000000..c79d4eff6 --- /dev/null +++ b/commands/command_filter.cpp @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "command_filter.h" +#include "core/filterpreset.h" +#include "core/subsurface-qt/divelistnotifier.h" + +namespace Command { + +static int createFilterPreset(const QString &name, const FilterData &data) +{ + int index = filter_preset_add(name, data); + emit diveListNotifier.filterPresetAdded(index); + return index; +} + +static std::pair<QString, FilterData> removeFilterPreset(int index) +{ + QString oldName = filter_preset_name_qstring(index); + FilterData oldData = filter_preset_get(index); + filter_preset_delete(index); + emit diveListNotifier.filterPresetRemoved(index); + return { oldName, oldData }; +} + +CreateFilterPreset::CreateFilterPreset(const QString &nameIn, const FilterData &dataIn) : + name(nameIn), data(dataIn), index(0) +{ + setText(Command::Base::tr("Create filter preset %1").arg(nameIn)); +} + +bool CreateFilterPreset::workToBeDone() +{ + return true; +} + +void CreateFilterPreset::redo() +{ + index = createFilterPreset(name, data); +} + +void CreateFilterPreset::undo() +{ + // with std::tie() we can conveniently assign tuples + std::tie(name, data) = removeFilterPreset(index); +} + +RemoveFilterPreset::RemoveFilterPreset(int indexIn) : index(indexIn) +{ + setText(Command::Base::tr("Delete filter preset %1").arg(filter_preset_name_qstring(index))); +} + +bool RemoveFilterPreset::workToBeDone() +{ + return true; +} + +void RemoveFilterPreset::redo() +{ + // with std::tie() we can conveniently assign tuples + std::tie(name, data) = removeFilterPreset(index); +} + +void RemoveFilterPreset::undo() +{ + index = createFilterPreset(name, data); +} + +EditFilterPreset::EditFilterPreset(int indexIn, const FilterData &dataIn) : + index(indexIn), data(dataIn) +{ + setText(Command::Base::tr("Edit filter preset %1").arg(filter_preset_name_qstring(index))); +} + +bool EditFilterPreset::workToBeDone() +{ + return true; +} + +void EditFilterPreset::redo() +{ + FilterData oldData = filter_preset_get(index); + filter_preset_set(index, data); + data = std::move(oldData); +} + +void EditFilterPreset::undo() +{ + redo(); // undo() and redo() do the same thing +} + +} // namespace Command diff --git a/commands/command_filter.h b/commands/command_filter.h new file mode 100644 index 000000000..e09753edf --- /dev/null +++ b/commands/command_filter.h @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +// Note: this header file is used by the undo-machinery and should not be included elsewhere. + +#ifndef COMMAND_FILTER_H +#define COMMAND_FILTER_H + +#include "command_base.h" +#include "core/divefilter.h" + +struct FilterData; + +// We put everything in a namespace, so that we can shorten names without polluting the global namespace +namespace Command { + +class CreateFilterPreset final : public Base { +public: + CreateFilterPreset(const QString &name, const FilterData &data); +private: + // for redo + QString name; + FilterData data; + + // for undo + int index; + + void undo() override; + void redo() override; + bool workToBeDone() override; +}; + +class RemoveFilterPreset final : public Base { +public: + RemoveFilterPreset(int index); +private: + // for undo + QString name; + FilterData data; + + // for redo + int index; + + void undo() override; + void redo() override; + bool workToBeDone() override; +}; + +class EditFilterPreset final : public Base { +public: + EditFilterPreset(int index, const FilterData &data); +private: + // for redo and undo + int index; + FilterData data; + + void undo() override; + void redo() override; + bool workToBeDone() override; +}; + +} // namespace Command +#endif |