From 56dcbd9588fa3b7d70a4b639b71cb18e1c462067 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 13 Mar 2019 20:58:25 +0100 Subject: Undo: implement undo of dive site addition Implement a dive site addition undo command and connect it to the add dive site button. The added dive site has a default name ("new dive site"). Signed-off-by: Berthold Stoeger --- desktop-widgets/command.cpp | 5 +++++ desktop-widgets/command.h | 1 + desktop-widgets/command_divesite.cpp | 22 ++++++++++++++++++++++ desktop-widgets/command_divesite.h | 17 +++++++++++++++++ desktop-widgets/tab-widgets/TabDiveSite.cpp | 8 ++++++++ desktop-widgets/tab-widgets/TabDiveSite.h | 2 ++ 6 files changed, 55 insertions(+) diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp index ee2016c6e..1d34d268b 100644 --- a/desktop-widgets/command.cpp +++ b/desktop-widgets/command.cpp @@ -93,4 +93,9 @@ void editDiveSiteDescription(dive_site *ds, const QString &value) execute(new EditDiveSiteDescription(ds, value)); } +void addDiveSite(const QString &name) +{ + execute(new AddDiveSite(name)); +} + } // namespace Command diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h index 785981831..4f89eb4c0 100644 --- a/desktop-widgets/command.h +++ b/desktop-widgets/command.h @@ -43,6 +43,7 @@ void mergeDives(const QVector &dives); void deleteDiveSites(const QVector &sites); void editDiveSiteName(dive_site *ds, const QString &value); void editDiveSiteDescription(dive_site *ds, const QString &value); +void addDiveSite(const QString &name); } // namespace Command diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp index 28d47d603..b3a4911e5 100644 --- a/desktop-widgets/command_divesite.cpp +++ b/desktop-widgets/command_divesite.cpp @@ -62,6 +62,28 @@ static std::vector removeDiveSites(std::vector & return res; } +AddDiveSite::AddDiveSite(const QString &name) +{ + setText(tr("add dive site")); + sitesToAdd.emplace_back(alloc_dive_site()); + sitesToAdd.back()->name = copy_qstring(name); +} + +bool AddDiveSite::workToBeDone() +{ + return true; +} + +void AddDiveSite::redo() +{ + sitesToRemove = std::move(addDiveSites(sitesToAdd)); +} + +void AddDiveSite::undo() +{ + sitesToAdd = std::move(removeDiveSites(sitesToRemove)); +} + DeleteDiveSites::DeleteDiveSites(const QVector &sites) : sitesToRemove(sites.toStdVector()) { setText(tr("delete %n dive site(s)", "", sites.size())); diff --git a/desktop-widgets/command_divesite.h b/desktop-widgets/command_divesite.h index 78622862e..f436a84b4 100644 --- a/desktop-widgets/command_divesite.h +++ b/desktop-widgets/command_divesite.h @@ -11,6 +11,23 @@ // We put everything in a namespace, so that we can shorten names without polluting the global namespace namespace Command { +class AddDiveSite : public Base { +public: + AddDiveSite(const QString &name); +private: + bool workToBeDone() override; + void undo() override; + void redo() override; + + // Note: we only add one dive site. Nevertheless, we use vectors so that we + // can reuse the dive site deletion code. + // For redo + std::vector sitesToRemove; + + // For undo + std::vector sitesToAdd; +}; + class DeleteDiveSites : public Base { public: DeleteDiveSites(const QVector &sites); diff --git a/desktop-widgets/tab-widgets/TabDiveSite.cpp b/desktop-widgets/tab-widgets/TabDiveSite.cpp index 5e2831c93..648df6395 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.cpp +++ b/desktop-widgets/tab-widgets/TabDiveSite.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include "TabDiveSite.h" #include "qt-models/divelocationmodel.h" +#include "desktop-widgets/command.h" #include @@ -16,6 +17,8 @@ TabDiveSite::TabDiveSite(QWidget *parent) : TabBase(parent) // Show only the first few columns for (int i = LocationInformationModel::COORDS; i < LocationInformationModel::COLUMNS; ++i) ui.diveSites->view()->setColumnHidden(i, true); + + connect(ui.diveSites, &TableView::addButtonClicked, this, &TabDiveSite::add); } void TabDiveSite::updateData() @@ -25,3 +28,8 @@ void TabDiveSite::updateData() void TabDiveSite::clear() { } + +void TabDiveSite::add() +{ + Command::addDiveSite(tr("New dive site")); +} diff --git a/desktop-widgets/tab-widgets/TabDiveSite.h b/desktop-widgets/tab-widgets/TabDiveSite.h index f0bf2d5d5..2538166b4 100644 --- a/desktop-widgets/tab-widgets/TabDiveSite.h +++ b/desktop-widgets/tab-widgets/TabDiveSite.h @@ -12,6 +12,8 @@ public: TabDiveSite(QWidget *parent = 0); void updateData() override; void clear() override; +private slots: + void add(); private: Ui::TabDiveSite ui; DiveSiteSortedModel model; -- cgit v1.2.3-70-g09d2