From e61641c79cd57bfa55d2371615a7eef7c73b4eb7 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 14 Apr 2020 22:07:00 +0200 Subject: undo: implement undo of setting a picture time by drag&drop Even though the functionality is seemingly trivial, this is a bit invasive, as the code has to be split into two distinct parts: 1) Post undo command 2) React to changes to the divelist Don't compile that code on mobile. Signed-off-by: Berthold Stoeger --- commands/CMakeLists.txt | 2 ++ commands/command.cpp | 8 +++++++ commands/command.h | 4 ++++ commands/command_pictures.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++ commands/command_pictures.h | 26 ++++++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 commands/command_pictures.cpp create mode 100644 commands/command_pictures.h (limited to 'commands') diff --git a/commands/CMakeLists.txt b/commands/CMakeLists.txt index ceb2740f0..aa51dd066 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_pictures.cpp + command_pictures.h ) add_library(subsurface_commands STATIC ${SUBSURFACE_GENERIC_COMMANDS_SRCS}) target_link_libraries(subsurface_commands ${QT_LIBRARIES}) diff --git a/commands/command.cpp b/commands/command.cpp index 33c866e72..8195fbd22 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_pictures.h" namespace Command { @@ -359,4 +360,11 @@ void addGasSwitch(struct dive *d, int dcNr, int seconds, int tank) execute(new AddGasSwitch(d, dcNr, seconds, tank)); } +// Picture (media) commands + +void setPictureOffset(dive *d, const QString &filename, offset_t offset) +{ + execute(new SetPictureOffset(d, filename, offset)); +} + } // namespace Command diff --git a/commands/command.h b/commands/command.h index fc1ddf582..5d92eec03 100644 --- a/commands/command.h +++ b/commands/command.h @@ -118,6 +118,10 @@ void renameEvent(struct dive *d, int dcNr, struct event *ev, const char *name); void removeEvent(struct dive *d, int dcNr, struct event *ev); void addGasSwitch(struct dive *d, int dcNr, int seconds, int tank); +// 7) Picture (media) commands + +void setPictureOffset(dive *d, const QString &filename, offset_t offset); + } // namespace Command #endif // COMMAND_H diff --git a/commands/command_pictures.cpp b/commands/command_pictures.cpp new file mode 100644 index 000000000..b7f03b193 --- /dev/null +++ b/commands/command_pictures.cpp @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "command_pictures.h" +#include "core/subsurface-qt/divelistnotifier.h" + +namespace Command { + +static picture *dive_get_picture(const dive *d, const QString &fn) +{ + int idx = get_picture_idx(&d->pictures, qPrintable(fn)); + return idx < 0 ? nullptr : &d->pictures.pictures[idx]; +} + +SetPictureOffset::SetPictureOffset(dive *dIn, const QString &filenameIn, offset_t offsetIn) : + d(dIn), filename(filenameIn), offset(offsetIn) +{ + if (!dive_get_picture(d, filename)) + d = nullptr; + setText(Command::Base::tr("Change media time")); +} + +void SetPictureOffset::redo() +{ + picture *pic = dive_get_picture(d, filename); + if (!pic) { + fprintf(stderr, "SetPictureOffset::redo(): picture disappeared!"); + return; + } + std::swap(pic->offset, offset); + offset_t newOffset = pic->offset; + + // Instead of trying to be smart, let's simply resort the picture table. + // If someone complains about speed, do our usual "smart" thing. + sort_picture_table(&d->pictures); + emit diveListNotifier.pictureOffsetChanged(d, filename, newOffset); + invalidate_dive_cache(d); +} + +// Undo and redo do the same thing +void SetPictureOffset::undo() +{ + redo(); +} + +bool SetPictureOffset::workToBeDone() +{ + return !!d; +} + +} // namespace Command diff --git a/commands/command_pictures.h b/commands/command_pictures.h new file mode 100644 index 000000000..f2381d111 --- /dev/null +++ b/commands/command_pictures.h @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 +// Note: this header file is used by the undo-machinery and should not be included elsewhere. + +#ifndef COMMAND_PICTURES_H +#define COMMAND_PICTURES_H + +#include "command_base.h" + +// We put everything in a namespace, so that we can shorten names without polluting the global namespace +namespace Command { + +class SetPictureOffset final : public Base { +public: + SetPictureOffset(dive *d, const QString &filename, offset_t offset); // Pictures are identified by the unique (dive,filename) pair +private: + dive *d; // null means no work to be done + QString filename; + offset_t offset; + + void undo() override; + void redo() override; + bool workToBeDone() override; +}; + +} // namespace Command +#endif -- cgit v1.2.3-70-g09d2