diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-06-28 15:24:19 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-09-29 16:13:03 -0700 |
commit | f9721fce4b12faee87398d23fd2f8b4d1b9a0309 (patch) | |
tree | 0ca37231d70028eb2fdaf615a812af8c9983b1cf /commands | |
parent | 631be569fe5b277c4c1729019305e06b02cec611 (diff) | |
download | subsurface-f9721fce4b12faee87398d23fd2f8b4d1b9a0309.tar.gz |
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 <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'commands')
-rw-r--r-- | commands/command.cpp | 5 | ||||
-rw-r--r-- | commands/command.h | 4 | ||||
-rw-r--r-- | commands/command_divelist.cpp | 38 | ||||
-rw-r--r-- | commands/command_divelist.h | 11 |
4 files changed, 51 insertions, 7 deletions
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<struct dive*> &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 <QVector> #include <QAction> @@ -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<struct dive*> &divesToDelete); void shiftTime(const std::vector<dive *> &changedDives, int amount); void renumberDives(const QVector<QPair<dive *, int>> &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<struct dive*> &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 <QVector> @@ -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<OwningDiveSitePtr> sitesToAdd; + std::vector<std::pair<QString,FilterData>> + filterPresetsToAdd; // For undo std::vector<dive_site *> sitesToRemove; - std::vector<dive *> selection; - dive * currentDive; + std::vector<dive *> selection; + dive *currentDive; + std::vector<int> filterPresetsToRemove; }; class DeleteDive : public DiveListBase { |