From 39a4090c0a6e1d8fe12516be06ee7b31dd4bc96d Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 17 Oct 2020 16:07:39 +0200 Subject: devices: add devices in Command::importTable() Add a device_table parameters to Command::importTable() and add_imported_dives(). The content of this table will be added to the global device list (respectively removed on undo). This is currently a no-op, as the parser doesn't yet fill out the device table, but adds devices directly to the global device table. Signed-off-by: Berthold Stoeger --- commands/command.cpp | 5 +++-- commands/command.h | 6 ++++-- commands/command_divelist.cpp | 20 ++++++++++++++++++-- commands/command_divelist.h | 5 ++++- 4 files changed, 29 insertions(+), 7 deletions(-) (limited to 'commands') diff --git a/commands/command.cpp b/commands/command.cpp index d70ca12fe..6ce7f18bd 100644 --- a/commands/command.cpp +++ b/commands/command.cpp @@ -18,9 +18,10 @@ void addDive(dive *d, bool autogroup, bool newNumber) } void importDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, - struct filter_preset_table *presets, int flags, const QString &source) + struct device_table *devices, struct filter_preset_table *presets, + int flags, const QString &source) { - execute(new ImportDives(dives, trips, sites, presets, flags, source)); + execute(new ImportDives(dives, trips, sites, devices, presets, flags, source)); } void deleteDive(const QVector &divesToDelete) diff --git a/commands/command.h b/commands/command.h index b2c6826a3..26340357e 100644 --- a/commands/command.h +++ b/commands/command.h @@ -3,7 +3,6 @@ #define COMMAND_H #include "core/dive.h" -#include "core/filterpreset.h" #include "core/pictureobj.h" #include #include @@ -11,6 +10,8 @@ struct DiveAndLocation; struct FilterData; +struct filter_preset_table; +struct device_table; // We put everything in a namespace, so that we can shorten names without polluting the global namespace namespace Command { @@ -33,7 +34,8 @@ QString changesMade(); // return a string with the texts from all commands on // insertion position. void addDive(dive *d, bool autogroup, bool newNumber); void importDives(struct dive_table *dives, struct trip_table *trips, - struct dive_site_table *sites, struct filter_preset_table *filter_presets, + struct dive_site_table *sites, struct device_table *devices, + struct filter_preset_table *filter_presets, int flags, const QString &source); // The tables are consumed! void deleteDive(const QVector &divesToDelete); void shiftTime(const std::vector &changedDives, int amount); diff --git a/commands/command_divelist.cpp b/commands/command_divelist.cpp index 54dc0983c..c5322f709 100644 --- a/commands/command_divelist.cpp +++ b/commands/command_divelist.cpp @@ -470,7 +470,8 @@ void AddDive::undoit() } ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, - struct filter_preset_table *filter_presets, int flags, const QString &source) + struct device_table *devices, struct filter_preset_table *filter_presets, int flags, + const QString &source) { setText(Command::Base::tr("import %n dive(s) from %1", "", dives->nr).arg(source)); @@ -481,7 +482,9 @@ ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, str struct dive_table dives_to_remove = empty_dive_table; struct trip_table trips_to_add = empty_trip_table; struct dive_site_table sites_to_add = empty_dive_site_table; - process_imported_dives(dives, trips, sites, flags, &dives_to_add, &dives_to_remove, &trips_to_add, &sites_to_add); + process_imported_dives(dives, trips, sites, devices, flags, + &dives_to_add, &dives_to_remove, &trips_to_add, + &sites_to_add, &devicesToAddAndRemove); // Add trips to the divesToAdd.trips structure divesToAdd.trips.reserve(trips_to_add.nr); @@ -550,6 +553,12 @@ void ImportDives::redoit() // Remember dives and sites to remove divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew); + // Add devices + for (const device &dev: devicesToAddAndRemove.devices) { + int idx = add_to_device_table(&device_table, &dev); + emit diveListNotifier.deviceAdded(idx); + } + // Add new filter presets for (auto &it: filterPresetsToAdd) { filterPresetsToRemove.push_back(filter_preset_add(it.first, it.second)); @@ -572,6 +581,13 @@ void ImportDives::undoit() // ...and restore the selection setSelection(selection, currentDive); + // Remove devices + for (const device &dev: devicesToAddAndRemove.devices) { + int idx = remove_device(&device_table, &dev); + if (idx >= 0) + emit diveListNotifier.deviceDeleted(idx); + } + // Remove filter presets. Do this in reverse order. for (auto it = filterPresetsToRemove.rbegin(); it != filterPresetsToRemove.rend(); ++it) { int index = *it; diff --git a/commands/command_divelist.h b/commands/command_divelist.h index cf204bedc..893badb39 100644 --- a/commands/command_divelist.h +++ b/commands/command_divelist.h @@ -6,6 +6,7 @@ #include "command_base.h" #include "core/filterpreset.h" +#include "core/device.h" #include @@ -99,7 +100,8 @@ class ImportDives : public DiveListBase { public: // Note: dives and trips are consumed - after the call they will be empty. ImportDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, - struct filter_preset_table *filter_presets, int flags, const QString &source); + struct device_table *devices, struct filter_preset_table *filter_presets, int flags, + const QString &source); private: void undoit() override; void redoit() override; @@ -108,6 +110,7 @@ private: // For redo and undo DivesAndTripsToAdd divesToAdd; DivesAndSitesToRemove divesAndSitesToRemove; + struct device_table devicesToAddAndRemove; // For redo std::vector sitesToAdd; -- cgit v1.2.3-70-g09d2