From f9721fce4b12faee87398d23fd2f8b4d1b9a0309 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 28 Jun 2020 15:24:19 +0200 Subject: filter: implement importing of filter presets When importing a divelog, import filter presets. If there are equal names, import only if the presets differ. In that case, disambiguate the name. This made things a bit more complicated, as comparison of filter presets had to be implemented. Signed-off-by: Berthold Stoeger --- commands/command.cpp | 5 +++-- commands/command.h | 4 +++- commands/command_divelist.cpp | 38 +++++++++++++++++++++++++++++++++++++- commands/command_divelist.h | 11 ++++++++--- 4 files changed, 51 insertions(+), 7 deletions(-) (limited to 'commands') diff --git a/commands/command.cpp b/commands/command.cpp index f04d5cc7a..eaf84bab3 100644 --- a/commands/command.cpp +++ b/commands/command.cpp @@ -17,9 +17,10 @@ void addDive(dive *d, bool autogroup, bool newNumber) execute(new AddDive(d, autogroup, newNumber)); } -void importDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, int flags, const QString &source) +void importDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, + filter_preset_table_t *presets, int flags, const QString &source) { - execute(new ImportDives(dives, trips, sites, flags, source)); + execute(new ImportDives(dives, trips, sites, presets, flags, source)); } void deleteDive(const QVector &divesToDelete) diff --git a/commands/command.h b/commands/command.h index 3cdec6d4e..8936355e5 100644 --- a/commands/command.h +++ b/commands/command.h @@ -3,6 +3,7 @@ #define COMMAND_H #include "core/dive.h" +#include "core/filterpreset.h" #include "core/pictureobj.h" #include #include @@ -32,7 +33,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, int flags, const QString &source); // The tables are consumed! + struct dive_site_table *sites, filter_preset_table_t *filter_presets, + int flags, const QString &source); // The tables are consumed! void deleteDive(const QVector &divesToDelete); void shiftTime(const std::vector &changedDives, int amount); void renumberDives(const QVector> &divesToRenumber); diff --git a/commands/command_divelist.cpp b/commands/command_divelist.cpp index 6d81482e0..39232b731 100644 --- a/commands/command_divelist.cpp +++ b/commands/command_divelist.cpp @@ -469,7 +469,8 @@ void AddDive::undoit() setSelection(selection, currentDive); } -ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, int flags, const QString &source) +ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, + filter_preset_table_t *filter_presets, int flags, const QString &source) { setText(Command::Base::tr("import %n dive(s) from %1", "", dives->nr).arg(source)); @@ -509,6 +510,22 @@ ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, str divesAndSitesToRemove.dives.reserve(dives_to_remove.nr); for (int i = 0; i < dives_to_remove.nr; ++i) divesAndSitesToRemove.dives.push_back(dives_to_remove.dives[i]); + + // When encountering filter presets with equal names, check whether they are + // the same. If they are, ignore them. + if (filter_presets) { + for (const filter_preset &preset: *filter_presets) { + QString name = preset.name; + auto it = std::find_if(filter_preset_table.begin(), filter_preset_table.end(), + [&name](const filter_preset &preset) { return preset.name == name; }); + if (it != filter_preset_table.end() && it->data == preset.data) + continue; + filterPresetsToAdd.emplace_back(preset.name, preset.data); + } + + // Consume the table for analogy with the other tables. + filter_presets->clear(); + } } bool ImportDives::workToBeDone() @@ -532,6 +549,13 @@ void ImportDives::redoit() // Remember dives and sites to remove divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew); + + // Add new filter presets + for (auto &it: filterPresetsToAdd) { + filterPresetsToRemove.push_back(filter_preset_add(it.first, it.second)); + emit diveListNotifier.filterPresetAdded(filterPresetsToRemove.back()); + } + filterPresetsToAdd.clear(); } void ImportDives::undoit() @@ -547,6 +571,18 @@ void ImportDives::undoit() // ...and restore the selection setSelection(selection, currentDive); + + // Remove filter presets. Do this in reverse order. + for (auto it = filterPresetsToRemove.rbegin(); it != filterPresetsToRemove.rend(); ++it) { + int index = *it; + QString oldName = filter_preset_name_qstring(index); + FilterData oldData = filter_preset_get(index); + filter_preset_delete(index); + emit diveListNotifier.filterPresetRemoved(index); + filterPresetsToAdd.emplace_back(oldName, oldData); + } + filterPresetsToRemove.clear(); + std::reverse(filterPresetsToAdd.begin(), filterPresetsToAdd.end()); } DeleteDive::DeleteDive(const QVector &divesToDeleteIn) diff --git a/commands/command_divelist.h b/commands/command_divelist.h index f1bbc7a91..9569da031 100644 --- a/commands/command_divelist.h +++ b/commands/command_divelist.h @@ -5,6 +5,7 @@ #define COMMAND_DIVELIST_H #include "command_base.h" +#include "core/filterpreset.h" #include @@ -97,7 +98,8 @@ private: 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, int flags, const QString &source); + ImportDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, + filter_preset_table_t *filter_presets, int flags, const QString &source); private: void undoit() override; void redoit() override; @@ -109,11 +111,14 @@ private: // For redo std::vector sitesToAdd; + std::vector> + filterPresetsToAdd; // For undo std::vector sitesToRemove; - std::vector selection; - dive * currentDive; + std::vector selection; + dive *currentDive; + std::vector filterPresetsToRemove; }; class DeleteDive : public DiveListBase { -- cgit v1.2.3-70-g09d2