Add undo/redo commands for importing dive sites

ImportDiveSites adds the provided dive sites to the core dive site table
and stores the source data so it can be undone.

Signed-off-by: Doug Junkins <junkins@foghead.com>
This commit is contained in:
Doug Junkins 2019-05-04 20:40:27 -07:00 committed by bstoeger
parent c38a86bebf
commit 704ff9f82e
4 changed files with 59 additions and 1 deletions

View file

@ -120,6 +120,11 @@ void addDiveSite(const QString &name)
execute(new AddDiveSite(name)); execute(new AddDiveSite(name));
} }
void importDiveSites(struct dive_site_table *sites, const QString &source)
{
execute(new ImportDiveSites(sites, source));
}
void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites) void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites)
{ {
execute(new MergeDiveSites(ds, sites)); execute(new MergeDiveSites(ds, sites));

View file

@ -48,6 +48,7 @@ void editDiveSiteCountry(dive_site *ds, const QString &value);
void editDiveSiteLocation(dive_site *ds, location_t value); 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 editDiveSiteTaxonomy(dive_site *ds, taxonomy_data &value); // value is consumed (i.e. will be erased after call)!
void addDiveSite(const QString &name); void addDiveSite(const QString &name);
void importDiveSites(struct dive_site_table *sites, const QString &source);
void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites); void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites);
void purgeUnusedDiveSites(); void purgeUnusedDiveSites();

View file

@ -100,6 +100,42 @@ void AddDiveSite::undo()
sitesToAdd = std::move(removeDiveSites(sitesToRemove)); sitesToAdd = std::move(removeDiveSites(sitesToRemove));
} }
ImportDiveSites::ImportDiveSites(struct dive_site_table *sites, const QString &source)
{
setText(tr("import dive sites from %1").arg(source));
for (int i = 0; i < sites->nr; ++i) {
struct dive_site *new_ds = sites->dive_sites[i];
// Don't import dive sites that already exist. Currently we only check for
// the same name. We might want to be smarter here and merge dive site data, etc.
struct dive_site *old_ds = get_same_dive_site(new_ds);
if (old_ds) {
free_dive_site(new_ds);
continue;
}
sitesToAdd.emplace_back(new_ds);
}
// All site have been consumed
sites->nr = 0;
}
bool ImportDiveSites::workToBeDone()
{
return !sitesToAdd.empty();
}
void ImportDiveSites::redo()
{
sitesToRemove = std::move(addDiveSites(sitesToAdd));
}
void ImportDiveSites::undo()
{
sitesToAdd = std::move(removeDiveSites(sitesToRemove));
}
DeleteDiveSites::DeleteDiveSites(const QVector<dive_site *> &sites) : sitesToRemove(sites.toStdVector()) DeleteDiveSites::DeleteDiveSites(const QVector<dive_site *> &sites) : sitesToRemove(sites.toStdVector())
{ {
setText(tr("delete %n dive site(s)", "", sites.size())); setText(tr("delete %n dive site(s)", "", sites.size()));

View file

@ -21,10 +21,26 @@ private:
// Note: we only add one dive site. Nevertheless, we use vectors so that we // Note: we only add one dive site. Nevertheless, we use vectors so that we
// can reuse the dive site deletion code. // can reuse the dive site deletion code.
// For redo // For undo
std::vector<dive_site *> sitesToRemove; std::vector<dive_site *> sitesToRemove;
// For redo
std::vector<OwningDiveSitePtr> sitesToAdd;
};
class ImportDiveSites : public Base {
public:
// Note: the dive site table is consumed after the call it will be empty.
ImportDiveSites(struct dive_site_table *sites, const QString &source);
private:
bool workToBeDone() override;
void undo() override;
void redo() override;
// For undo // For undo
std::vector<dive_site *> sitesToRemove;
// For redo
std::vector<OwningDiveSitePtr> sitesToAdd; std::vector<OwningDiveSitePtr> sitesToAdd;
}; };