From 2f5223035a9d43b933b0baf64823dab84b5d8cea Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 27 May 2020 09:31:26 +0200 Subject: [PATCH] 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 --- commands/CMakeLists.txt | 2 + commands/command.cpp | 16 +++++ commands/command.h | 7 +++ commands/command_filter.cpp | 91 +++++++++++++++++++++++++++ commands/command_filter.h | 61 ++++++++++++++++++ core/subsurface-qt/divelistnotifier.h | 5 ++ packaging/ios/Subsurface-mobile.pro | 2 + 7 files changed, 184 insertions(+) create mode 100644 commands/command_filter.cpp create mode 100644 commands/command_filter.h 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 &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 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 &pictures); void addPictures(const std::vector &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 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 diff --git a/core/subsurface-qt/divelistnotifier.h b/core/subsurface-qt/divelistnotifier.h index aa82a4bef..eb44d6cde 100644 --- a/core/subsurface-qt/divelistnotifier.h +++ b/core/subsurface-qt/divelistnotifier.h @@ -125,6 +125,11 @@ signals: void picturesRemoved(dive *d, QVector filenames); void picturesAdded(dive *d, QVector pics); + // Filter related signals + void filterPresetAdded(int index); + void filterPresetRemoved(int index); + void filterPresetChanged(int index); + // This signal is emited every time a command is executed. // This is used to hide an old multi-dives-edited warning message. // This is necessary, so that the user can't click on the "undo" button and undo diff --git a/packaging/ios/Subsurface-mobile.pro b/packaging/ios/Subsurface-mobile.pro index d924f5c23..c83ebb57e 100644 --- a/packaging/ios/Subsurface-mobile.pro +++ b/packaging/ios/Subsurface-mobile.pro @@ -21,6 +21,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \ ../../commands/command_divesite.cpp \ ../../commands/command_edit.cpp \ ../../commands/command_edit_trip.cpp \ + ../../commands/command_filter.cpp \ ../../commands/command_pictures.cpp \ ../../core/cloudstorage.cpp \ ../../core/configuredivecomputerthreads.cpp \ @@ -192,6 +193,7 @@ HEADERS += \ ../../commands/command_divesite.h \ ../../commands/command_edit.h \ ../../commands/command_edit_trip.h \ + ../../commands/command_filter.h \ ../../commands/command_pictures.h \ ../../core/libdivecomputer.h \ ../../core/cloudstorage.h \