Undo: make dive site removal undoable

Create a new undo-command for deleting dive sites. If there are dives
associated with that site, the dives will be removed. The frontend
is not yet updated in such a case, as that infrastructure is in a
different PR.

Connect the trashcan icon of the dive site table to the undo command.
Currently, this code is in the dive site model, which makes little
sense, but is how the TableView class works. We might want to change
that when cylinder and weight editing are made undoable.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-03-12 22:35:43 +01:00 committed by Dirk Hohndel
parent e99c4c9059
commit 8e1f736d2b
7 changed files with 153 additions and 1 deletions

View file

@ -62,6 +62,8 @@ set(SUBSURFACE_INTERFACE
command_base.h
command_divelist.cpp
command_divelist.h
command_divesite.cpp
command_divesite.h
configuredivecomputerdialog.cpp
configuredivecomputerdialog.h
divecomputermanagementdialog.cpp

View file

@ -2,6 +2,7 @@
#include "command.h"
#include "command_divelist.h"
#include "command_divesite.h"
namespace Command {
@ -76,4 +77,10 @@ void mergeDives(const QVector <dive *> &dives)
execute(new MergeDives(dives));
}
// Dive site related commands
void deleteDiveSites(const QVector <dive_site *> &sites)
{
execute(new DeleteDiveSites(sites));
}
} // namespace Command

View file

@ -38,6 +38,10 @@ void splitDives(dive *d, duration_t time);
void splitDiveComputer(dive *d, int dc_num);
void mergeDives(const QVector <dive *> &dives);
// 3) Dive-site related commands
void deleteDiveSites(const QVector <dive_site *> &sites);
} // namespace Command
#endif // COMMAND_H

View file

@ -0,0 +1,83 @@
// SPDX-License-Identifier: GPL-2.0
#include "command_divesite.h"
#include "core/divesite.h"
#include "core/subsurface-qt/DiveListNotifier.h"
namespace Command {
// Helper functions to add / remove a set of dive sites
// Add a set of dive sites to the core. The dives that were associated with
// that dive site will be restored to that dive site.
static std::vector<dive_site *> addDiveSites(std::vector<OwningDiveSitePtr> &sites)
{
std::vector<dive_site *> res;
res.reserve(sites.size());
for (OwningDiveSitePtr &ds: sites) {
// Readd the dives that belonged to this site
for (int i = 0; i < ds->dives.nr; ++i) {
// TODO: send dive site changed signal
ds->dives.dives[i]->dive_site = ds.get();
}
// Add dive site to core, but remember a non-owning pointer first.
res.push_back(ds.get());
int idx = register_dive_site(ds.release()); // Return ownership to backend.
emit diveListNotifier.diveSiteAdded(res.back(), idx); // Inform frontend of new dive site.
}
// Clear vector of unused owning pointers
sites.clear();
return res;
}
// Remove a set of dive sites. Get owning pointers to them. The dives are set to
// being at no dive site, but the dive site will retain a list of dives, so
// that the dives can be readded to the site on undo.
static std::vector<OwningDiveSitePtr> removeDiveSites(std::vector<dive_site *> &sites)
{
std::vector<OwningDiveSitePtr> res;
res.reserve(sites.size());
for (dive_site *ds: sites) {
// Reset the dive_site field of the affected dives
for (int i = 0; i < ds->dives.nr; ++i) {
// TODO: send dive site changed signal
ds->dives.dives[i]->dive_site = nullptr;
}
// Remove dive site from core and take ownership.
int idx = unregister_dive_site(ds);
res.emplace_back(ds);
emit diveListNotifier.diveSiteDeleted(ds, idx); // Inform frontend of removed dive site.
}
sites.clear();
return res;
}
DeleteDiveSites::DeleteDiveSites(const QVector<dive_site *> &sites) : sitesToRemove(sites.toStdVector())
{
setText(tr("delete %n dive site(s)", "", sites.size()));
}
bool DeleteDiveSites::workToBeDone()
{
return !sitesToRemove.empty();
}
void DeleteDiveSites::redo()
{
sitesToAdd = std::move(removeDiveSites(sitesToRemove));
}
void DeleteDiveSites::undo()
{
sitesToRemove = std::move(addDiveSites(sitesToAdd));
}
} // namespace Command

View file

@ -0,0 +1,31 @@
// SPDX-License-Identifier: GPL-2.0
// Note: this header file is used by the undo-machinery and should not be included elsewhere.
#ifndef COMMAND_DIVESITE_H
#define COMMAND_DIVESITE_H
#include "command_base.h"
#include <QVector>
// We put everything in a namespace, so that we can shorten names without polluting the global namespace
namespace Command {
class DeleteDiveSites : public Base {
public:
DeleteDiveSites(const QVector<dive_site *> &sites);
private:
bool workToBeDone() override;
// For redo
std::vector<dive_site *> sitesToRemove;
// For undo
std::vector<OwningDiveSitePtr> sitesToAdd;
void undo() override;
void redo() override;
};
} // namespace Command
#endif // COMMAND_DIVESITE_H

View file

@ -5,8 +5,12 @@
#include "core/qthelper.h"
#include "core/divesite.h"
#include "core/metrics.h"
#ifndef SUBSURFACE_MOBILE
#include "cleanertablemodel.h" // for trashIcon();
#include <QDebug>
#include "desktop-widgets/mainwindow.h" // to place message box
#include "desktop-widgets/command.h"
#include <QMessageBox>
#endif
#include <QLineEdit>
#include <QIcon>
#include <core/gettextfromc.h>
@ -219,6 +223,25 @@ QStringList DiveSiteSortedModel::allSiteNames() const
return locationNames;
}
#ifndef SUBSURFACE_MOBILE
// TODO: Remove from model. It doesn't make sense to call the model here, which calls the undo command,
// which in turn calls the model.
void DiveSiteSortedModel::remove(const QModelIndex &index)
{
if (index.column() != LocationInformationModel::REMOVE)
return;
struct dive_site *ds = get_dive_site(mapToSource(index).row(), &dive_site_table);
if (!ds)
return;
if (ds->dives.nr > 0 &&
QMessageBox::warning(MainWindow::instance(), tr("Delete dive site?"),
tr("This dive site has %n dive(s). Do you really want to delete it?\n", "", ds->dives.nr),
QMessageBox::Yes|QMessageBox::No) == QMessageBox::No)
return;
Command::deleteDiveSites(QVector<dive_site *>{ds});
}
#endif
GeoReferencingOptionsModel *GeoReferencingOptionsModel::instance()
{
static GeoReferencingOptionsModel *self = new GeoReferencingOptionsModel();

View file

@ -42,6 +42,8 @@ class DiveSiteSortedModel : public QSortFilterProxyModel {
private:
bool filterAcceptsRow(int sourceRow, const QModelIndex &source_parent) const override;
bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override;
public slots:
void remove(const QModelIndex &index);
public:
DiveSiteSortedModel();
QStringList allSiteNames() const;