diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-04-14 22:07:00 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-05-06 13:58:09 -0700 |
commit | e61641c79cd57bfa55d2371615a7eef7c73b4eb7 (patch) | |
tree | 85333dfed1630b1d968498495d1d1c151570e015 /commands/command_pictures.cpp | |
parent | ebdb3e3c3029d3762207e8dcadfa3a61bf0a9293 (diff) | |
download | subsurface-e61641c79cd57bfa55d2371615a7eef7c73b4eb7.tar.gz |
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 <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'commands/command_pictures.cpp')
-rw-r--r-- | commands/command_pictures.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
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 |