diff options
Diffstat (limited to 'desktop-widgets')
-rw-r--r-- | desktop-widgets/command.cpp | 5 | ||||
-rw-r--r-- | desktop-widgets/command.h | 1 | ||||
-rw-r--r-- | desktop-widgets/command_divesite.cpp | 41 | ||||
-rw-r--r-- | desktop-widgets/command_divesite.h | 17 | ||||
-rw-r--r-- | desktop-widgets/locationinformation.cpp | 12 |
5 files changed, 66 insertions, 10 deletions
diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp index 785443390..adfd32960 100644 --- a/desktop-widgets/command.cpp +++ b/desktop-widgets/command.cpp @@ -118,4 +118,9 @@ void addDiveSite(const QString &name) execute(new AddDiveSite(name)); } +void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites) +{ + execute(new MergeDiveSites(ds, sites)); +} + } // namespace Command diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h index d1a8326ea..1bcc69089 100644 --- a/desktop-widgets/command.h +++ b/desktop-widgets/command.h @@ -48,6 +48,7 @@ void editDiveSiteCountry(dive_site *ds, const QString &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 addDiveSite(const QString &name); +void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites); } // namespace Command diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp index 04ee3b212..a5dce00d1 100644 --- a/desktop-widgets/command_divesite.cpp +++ b/desktop-widgets/command_divesite.cpp @@ -266,4 +266,45 @@ void EditDiveSiteTaxonomy::undo() redo(); } +MergeDiveSites::MergeDiveSites(dive_site *dsIn, const QVector<dive_site *> &sites) : ds(dsIn) +{ + setText(tr("merge dive sites")); + sitesToRemove.reserve(sites.size()); + for (dive_site *site: sites) { + if (site != ds) + sitesToRemove.push_back(site); + } +} + +bool MergeDiveSites::workToBeDone() +{ + return !sitesToRemove.empty(); +} + +void MergeDiveSites::redo() +{ + // First, remove all dive sites + sitesToAdd = std::move(removeDiveSites(sitesToRemove)); + + // The dives of the above dive sites were reset to no dive sites. + // Add them to the merged-into dive site. Thankfully, we remember + // the dives in the sitesToAdd vector. + for (const OwningDiveSitePtr &site: sitesToAdd) { + for (int i = 0; i < site->dives.nr; ++i) + add_dive_to_dive_site(site->dives.dives[i], ds); // TODO: send dive changed signal + } +} + +void MergeDiveSites::undo() +{ + // Before readding the dive sites, unregister the corresponding dives so that they can be + // readded to their old dive sites. + for (const OwningDiveSitePtr &site: sitesToAdd) { + for (int i = 0; i < site->dives.nr; ++i) + unregister_dive_from_dive_site(site->dives.dives[i]); + } + + sitesToRemove = std::move(addDiveSites(sitesToAdd)); +} + } // namespace Command diff --git a/desktop-widgets/command_divesite.h b/desktop-widgets/command_divesite.h index b73cc1b5a..58b3aabf5 100644 --- a/desktop-widgets/command_divesite.h +++ b/desktop-widgets/command_divesite.h @@ -117,6 +117,23 @@ private: taxonomy_data value; // Value to be set }; +class MergeDiveSites : public Base { +public: + MergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites); +private: + bool workToBeDone() override; + void undo() override; + void redo() override; + + dive_site *ds; + + // For redo + std::vector<dive_site *> sitesToRemove; + + // For undo + std::vector<OwningDiveSitePtr> sitesToAdd; +}; + } // namespace Command #endif // COMMAND_DIVESITE_H diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp index 244e85f8d..330ccd6f5 100644 --- a/desktop-widgets/locationinformation.cpp +++ b/desktop-widgets/locationinformation.cpp @@ -62,24 +62,16 @@ void LocationInformationWidget::mergeSelectedDiveSites() { if (!diveSite) return; - if (QMessageBox::warning(MainWindow::instance(), tr("Merging dive sites"), - tr("You are about to merge dive sites, you can't undo that action \n Are you sure you want to continue?"), - QMessageBox::Ok, QMessageBox::Cancel) != QMessageBox::Ok) - return; const QModelIndexList selection = ui.diveSiteListView->selectionModel()->selectedIndexes(); - // std::vector guarantees contiguous storage and can therefore be passed to C-code - std::vector<struct dive_site *> selected_dive_sites; + QVector<dive_site *> selected_dive_sites; selected_dive_sites.reserve(selection.count()); for (const QModelIndex &idx: selection) { dive_site *ds = idx.data(LocationInformationModel::DIVESITE_ROLE).value<dive_site *>(); if (ds) selected_dive_sites.push_back(ds); } - merge_dive_sites(diveSite, selected_dive_sites.data(), (int)selected_dive_sites.size()); - LocationInformationModel::instance()->update(); - QSortFilterProxyModel *m = (QSortFilterProxyModel *)ui.diveSiteListView->model(); - m->invalidate(); + Command::mergeDiveSites(diveSite, selected_dive_sites); } void LocationInformationWidget::updateLabels() |