undo/device: adjust device management infrastructure

We no longer need the remove infrastructure, and the edit nickname function
becomes much more intuitive to use by passing in the dive computer for
which we want to create a nickname instead of the internal index into
the array of devices.

This also removes / simplifies the device list update signals in the
DiveListNotifier.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2021-08-17 11:14:42 -07:00
parent fbe17e620e
commit e7a5ec46f5
6 changed files with 15 additions and 72 deletions

View file

@ -381,14 +381,9 @@ void addPictures(const std::vector<PictureListForAddition> &pictures)
execute(new AddPictures(pictures)); execute(new AddPictures(pictures));
} }
void removeDevice(int idx) void editDeviceNickname(struct divecomputer *dc, const QString &nickname)
{ {
execute(new RemoveDevice(idx)); execute(new EditDeviceNickname(dc, nickname));
}
void editDeviceNickname(int idx, const QString &nickname)
{
execute(new EditDeviceNickname(idx, nickname));
} }
void createFilterPreset(const QString &name, const FilterData &data) void createFilterPreset(const QString &name, const FilterData &data)

View file

@ -141,8 +141,7 @@ void addPictures(const std::vector<PictureListForAddition> &pictures);
// 8) Device commands // 8) Device commands
void removeDevice(int idx); void editDeviceNickname(struct divecomputer *dc, const QString &nickname);
void editDeviceNickname(int idx, const QString &nickname);
// 9) Filter commands // 9) Filter commands

View file

@ -5,43 +5,14 @@
namespace Command { namespace Command {
RemoveDevice::RemoveDevice(int indexIn) : index(indexIn) EditDeviceNickname::EditDeviceNickname(const struct divecomputer *dc, const QString &nicknameIn) :
nickname(nicknameIn.toStdString())
{ {
const device *dev = get_device(&device_table, index); index = get_or_add_device_for_dc(&device_table, dc);
if (!dev) if (index == -1)
return; return;
setText(Command::Base::tr("Delete device %1 (0x%2)").arg(QString::fromStdString(dev->model), setText(Command::Base::tr("Set nickname of device %1 (serial %2) to %3").arg(dc->model, dc->serial, nicknameIn));
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() bool EditDeviceNickname::workToBeDone()
@ -55,7 +26,7 @@ void EditDeviceNickname::redo()
if (!dev) if (!dev)
return; return;
std::swap(dev->nickName, nickname); std::swap(dev->nickName, nickname);
emit diveListNotifier.deviceEdited(index); emit diveListNotifier.deviceEdited();
} }
void EditDeviceNickname::undo() void EditDeviceNickname::undo()

View file

@ -12,24 +12,9 @@ struct device;
// We put everything in a namespace, so that we can shorten names without polluting the global namespace // We put everything in a namespace, so that we can shorten names without polluting the global namespace
namespace Command { namespace Command {
class RemoveDevice final : public Base {
public:
RemoveDevice(int index);
private:
// for undo
device dev;
// for redo
int index;
void undo() override;
void redo() override;
bool workToBeDone() override;
};
class EditDeviceNickname final : public Base { class EditDeviceNickname final : public Base {
public: public:
EditDeviceNickname(int index, const QString &nickname); EditDeviceNickname(const divecomputer *dc, const QString &nickname);
private: private:
// for redo and undo // for redo and undo
int index; int index;

View file

@ -553,10 +553,8 @@ void ImportDives::redoit()
divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew); divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew);
// Add devices // Add devices
for (const device &dev: devicesToAddAndRemove.devices) { for (const device &dev: devicesToAddAndRemove.devices)
int idx = add_to_device_table(&device_table, &dev); add_to_device_table(&device_table, &dev);
emit diveListNotifier.deviceAdded(idx);
}
// Add new filter presets // Add new filter presets
for (auto &it: filterPresetsToAdd) { for (auto &it: filterPresetsToAdd) {
@ -583,11 +581,8 @@ void ImportDives::undoit()
setSelection(selection, currentDive); setSelection(selection, currentDive);
// Remove devices // Remove devices
for (const device &dev: devicesToAddAndRemove.devices) { for (const device &dev: devicesToAddAndRemove.devices)
int idx = remove_device(&device_table, &dev); remove_device(&device_table, &dev);
if (idx >= 0)
emit diveListNotifier.deviceDeleted(idx);
}
// Remove filter presets. Do this in reverse order. // Remove filter presets. Do this in reverse order.
for (auto it = filterPresetsToRemove.rbegin(); it != filterPresetsToRemove.rend(); ++it) { for (auto it = filterPresetsToRemove.rbegin(); it != filterPresetsToRemove.rend(); ++it) {

View file

@ -132,9 +132,7 @@ signals:
void picturesAdded(dive *d, QVector<PictureObj> pics); void picturesAdded(dive *d, QVector<PictureObj> pics);
// Devices related signals // Devices related signals
void deviceAdded(int index); void deviceEdited();
void deviceDeleted(int index);
void deviceEdited(int index);
// Filter related signals // Filter related signals
void filterPresetAdded(int index); void filterPresetAdded(int index);