diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-10-25 07:53:40 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-10-25 13:59:04 -0700 |
commit | faebb539091ca5550bb6b60280d692c50d547bc0 (patch) | |
tree | 785284eedbc783bfd0f02318c05cabf48712e8ed /commands/command_device.cpp | |
parent | 572e2678a0c5e1a9c2a947e4a9a86f35cec125eb (diff) | |
download | subsurface-faebb539091ca5550bb6b60280d692c50d547bc0.tar.gz |
undo: add device related undo commands
Add commands for deleting devices and editing device nicknames
to include the device-handling in the undo system.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'commands/command_device.cpp')
-rw-r--r-- | commands/command_device.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/commands/command_device.cpp b/commands/command_device.cpp new file mode 100644 index 000000000..afd368b55 --- /dev/null +++ b/commands/command_device.cpp @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "command_device.h" +#include "core/subsurface-qt/divelistnotifier.h" + +namespace Command { + +RemoveDevice::RemoveDevice(int indexIn) : index(indexIn) +{ + const device *dev = get_device(&device_table, index); + if (!dev) + return; + + setText(Command::Base::tr("Delete device %1 (0x%2)").arg(QString::fromStdString(dev->model), + QString::number(dev->deviceId))); +} + +bool RemoveDevice::workToBeDone() +{ + return get_device(&device_table, index) != nullptr; +} + +void RemoveDevice::redo() +{ + dev = *get_device(&device_table, index); + remove_from_device_table(&device_table, index); + emit diveListNotifier.deviceDeleted(index); +} + +void RemoveDevice::undo() +{ + index = add_to_device_table(&device_table, &dev); + emit diveListNotifier.deviceAdded(index); +} + +EditDeviceNickname::EditDeviceNickname(int indexIn, const QString &nicknameIn) : + index(indexIn), nickname(nicknameIn.toStdString()) +{ + const device *dev = get_device(&device_table, index); + if (!dev) + return; + + setText(Command::Base::tr("Set nickname of device %1 (0x%2) to %3").arg(QString::fromStdString(dev->model), + QString::number(dev->deviceId,1 ,16), nicknameIn)); +} + +bool EditDeviceNickname::workToBeDone() +{ + return get_device(&device_table, index) != nullptr; +} + +void EditDeviceNickname::redo() +{ + device *dev = get_device_mutable(&device_table, index); + if (!dev) + return; + std::swap(dev->nickName, nickname); + emit diveListNotifier.deviceEdited(index); +} + +void EditDeviceNickname::undo() +{ + redo(); // undo() and redo() do the same thing +} + +} // namespace Command |