diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-03-19 19:52:54 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-04-12 18:19:07 +0300 |
commit | 8858bfa1f8cf30cc4eb070fb6e709f7051e61241 (patch) | |
tree | 02c40484ff879ce3bba48085c6fab2809cef43ea | |
parent | fde80eeaa5f4ee4150f9292c5783eb790b0fcb96 (diff) | |
download | subsurface-8858bfa1f8cf30cc4eb070fb6e709f7051e61241.tar.gz |
Dive site: implement purge of unused dive sites
Add a "purge unused dive sites" button to the dive site list.
Connect it to a new PurgeUnusedDiveSites command. Implementation
was trivial: simply copy the DeleteDiveSites command.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | desktop-widgets/command.cpp | 5 | ||||
-rw-r--r-- | desktop-widgets/command.h | 1 | ||||
-rw-r--r-- | desktop-widgets/command_divesite.cpp | 25 | ||||
-rw-r--r-- | desktop-widgets/command_divesite.h | 15 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveSite.cpp | 5 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveSite.h | 1 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveSite.ui | 7 |
7 files changed, 59 insertions, 0 deletions
diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp index adfd32960..fc7478f94 100644 --- a/desktop-widgets/command.cpp +++ b/desktop-widgets/command.cpp @@ -123,4 +123,9 @@ void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites) execute(new MergeDiveSites(ds, sites)); } +void purgeUnusedDiveSites() +{ + execute(new PurgeUnusedDiveSites); +} + } // namespace Command diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h index 1bcc69089..134bb0147 100644 --- a/desktop-widgets/command.h +++ b/desktop-widgets/command.h @@ -49,6 +49,7 @@ void editDiveSiteLocation(dive_site *ds, location_t value); void editDiveSiteTaxonomy(dive_site *ds, taxonomy_data &value); // value is consumed (i.e. will be erased after call)! void addDiveSite(const QString &name); void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites); +void purgeUnusedDiveSites(); } // namespace Command diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp index a5dce00d1..3b35ddcc2 100644 --- a/desktop-widgets/command_divesite.cpp +++ b/desktop-widgets/command_divesite.cpp @@ -105,6 +105,31 @@ void DeleteDiveSites::undo() sitesToRemove = std::move(addDiveSites(sitesToAdd)); } +PurgeUnusedDiveSites::PurgeUnusedDiveSites() +{ + setText(tr("purge unused dive sites")); + for (int i = 0; i < dive_site_table.nr; ++i) { + dive_site *ds = dive_site_table.dive_sites[i]; + if (ds->dives.nr == 0) + sitesToRemove.push_back(ds); + } +} + +bool PurgeUnusedDiveSites::workToBeDone() +{ + return !sitesToRemove.empty(); +} + +void PurgeUnusedDiveSites::redo() +{ + sitesToAdd = std::move(removeDiveSites(sitesToRemove)); +} + +void PurgeUnusedDiveSites::undo() +{ + sitesToRemove = std::move(addDiveSites(sitesToAdd)); +} + // Helper function: swap C and Qt string static void swap(char *&c, QString &q) { diff --git a/desktop-widgets/command_divesite.h b/desktop-widgets/command_divesite.h index 58b3aabf5..9661bbffd 100644 --- a/desktop-widgets/command_divesite.h +++ b/desktop-widgets/command_divesite.h @@ -43,6 +43,21 @@ private: std::vector<OwningDiveSitePtr> sitesToAdd; }; +class PurgeUnusedDiveSites : public Base { +public: + PurgeUnusedDiveSites(); +private: + bool workToBeDone() override; + void undo() override; + void redo() override; + + // For redo + std::vector<dive_site *> sitesToRemove; + + // For undo + std::vector<OwningDiveSitePtr> sitesToAdd; +}; + class EditDiveSiteName : public Base { public: EditDiveSiteName(dive_site *ds, const QString &name); diff --git a/desktop-widgets/tab-widgets/TabDiveSite.cpp b/desktop-widgets/tab-widgets/TabDiveSite.cpp index f021f73dc..dc0b6be67 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.cpp +++ b/desktop-widgets/tab-widgets/TabDiveSite.cpp @@ -69,3 +69,8 @@ void TabDiveSite::diveSiteChanged(struct dive_site *ds, int field) QModelIndex localIdx = model.mapFromSource(globalIdx); ui.diveSites->view()->scrollTo(localIdx); } + +void TabDiveSite::on_purgeUnused_clicked() +{ + Command::purgeUnusedDiveSites(); +} diff --git a/desktop-widgets/tab-widgets/TabDiveSite.h b/desktop-widgets/tab-widgets/TabDiveSite.h index a31b1629d..b9c07ed69 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.h +++ b/desktop-widgets/tab-widgets/TabDiveSite.h @@ -16,6 +16,7 @@ private slots: void add(); void diveSiteAdded(struct dive_site *, int idx); void diveSiteChanged(struct dive_site *ds, int field); + void on_purgeUnused_clicked(); private: Ui::TabDiveSite ui; DiveSiteSortedModel model; diff --git a/desktop-widgets/tab-widgets/TabDiveSite.ui b/desktop-widgets/tab-widgets/TabDiveSite.ui index 1d52555e3..b536f101a 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.ui +++ b/desktop-widgets/tab-widgets/TabDiveSite.ui @@ -15,6 +15,13 @@ </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> + <widget class="QToolButton" name="purgeUnused"> + <property name="text"> + <string>Purge unused dive sites</string> + </property> + </widget> + </item> + <item> <widget class="TableView" name="diveSites" native="true"/> </item> </layout> |