aboutsummaryrefslogtreecommitdiffstats
path: root/desktop-widgets
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-03-14 22:07:48 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-04-12 18:19:07 +0300
commitfa4fedbb488eacff72d5d8691b4da8b34634a71e (patch)
tree8a751c99f2731832cdf71db420d55ed95f913bad /desktop-widgets
parent1deded787405f10af65c4dfe7c19b5e4a5ea3cdc (diff)
downloadsubsurface-fa4fedbb488eacff72d5d8691b4da8b34634a71e.tar.gz
Undo: implement undo of dive site location editing
Simply copy the code of note editing. It's a bit more complex, since we have to parse the Gps coordinates. For consitency, rename the COORD field to LOCATION (the field in the dive_site struct is called LOCATION). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets')
-rw-r--r--desktop-widgets/command.cpp5
-rw-r--r--desktop-widgets/command.h1
-rw-r--r--desktop-widgets/command_divesite.cpp36
-rw-r--r--desktop-widgets/command_divesite.h12
-rw-r--r--desktop-widgets/locationinformation.cpp32
-rw-r--r--desktop-widgets/locationinformation.h2
-rw-r--r--desktop-widgets/tab-widgets/TabDiveSite.cpp2
7 files changed, 67 insertions, 23 deletions
diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp
index fff60ddea..36d9faf5a 100644
--- a/desktop-widgets/command.cpp
+++ b/desktop-widgets/command.cpp
@@ -103,6 +103,11 @@ void editDiveSiteCountry(dive_site *ds, const QString &value)
execute(new EditDiveSiteCountry(ds, value));
}
+void editDiveSiteLocation(dive_site *ds, const QString &value)
+{
+ execute(new EditDiveSiteLocation(ds, value));
+}
+
void addDiveSite(const QString &name)
{
execute(new AddDiveSite(name));
diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h
index 77209a6bb..782a46ea5 100644
--- a/desktop-widgets/command.h
+++ b/desktop-widgets/command.h
@@ -45,6 +45,7 @@ void editDiveSiteName(dive_site *ds, const QString &value);
void editDiveSiteDescription(dive_site *ds, const QString &value);
void editDiveSiteNotes(dive_site *ds, const QString &value);
void editDiveSiteCountry(dive_site *ds, const QString &value);
+void editDiveSiteLocation(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 5534bc5bb..5e2a8b18a 100644
--- a/desktop-widgets/command_divesite.cpp
+++ b/desktop-widgets/command_divesite.cpp
@@ -208,4 +208,40 @@ void EditDiveSiteCountry::undo()
redo();
}
+// Parse GPS text into location_t
+static location_t parseGpsText(const QString &text)
+{
+ double lat, lon;
+ if (parseGpsText(text, &lat, &lon))
+ return create_location(lat, lon);
+ return { {0}, {0} };
+}
+
+EditDiveSiteLocation::EditDiveSiteLocation(dive_site *dsIn, const QString &location) : ds(dsIn),
+ value(parseGpsText(location))
+{
+ setText(tr("Edit dive site location"));
+}
+
+bool EditDiveSiteLocation::workToBeDone()
+{
+ bool ok = has_location(&value);
+ bool old_ok = has_location(&ds->location);
+ if (ok != old_ok)
+ return true;
+ return ok && !same_location(&value, &ds->location);
+}
+
+void EditDiveSiteLocation::redo()
+{
+ std::swap(value, ds->location);
+ emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::LOCATION); // Inform frontend of changed dive site.
+}
+
+void EditDiveSiteLocation::undo()
+{
+ // Undo and redo do the same
+ redo();
+}
+
} // namespace Command
diff --git a/desktop-widgets/command_divesite.h b/desktop-widgets/command_divesite.h
index f05bc765d..7ebc69ccb 100644
--- a/desktop-widgets/command_divesite.h
+++ b/desktop-widgets/command_divesite.h
@@ -92,6 +92,18 @@ private:
QString value; // Value to be set
};
+class EditDiveSiteLocation : public Base {
+public:
+ EditDiveSiteLocation(dive_site *ds, const QString &location);
+private:
+ bool workToBeDone() override;
+ void undo() override;
+ void redo() override;
+
+ dive_site *ds;
+ location_t value; // Value to be set
+};
+
} // namespace Command
#endif // COMMAND_DIVESITE_H
diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp
index 19d2befd2..17e9d488e 100644
--- a/desktop-widgets/locationinformation.cpp
+++ b/desktop-widgets/locationinformation.cpp
@@ -142,6 +142,15 @@ void LocationInformationWidget::diveSiteChanged(struct dive_site *ds, int field)
case LocationInformationModel::TAXONOMY:
ui.diveSiteCountry->setText(taxonomy_get_country(&diveSite->taxonomy));
return;
+ case LocationInformationModel::LOCATION:
+ filter_model.setCoordinates(diveSite->location);
+ if (has_location(&diveSite->location)) {
+ enableLocationButtons(true);
+ ui.diveSiteCoordinates->setText(printGPSCoords(&diveSite->location));
+ } else {
+ enableLocationButtons(false);
+ ui.diveSiteCoordinates->clear();
+ }
default:
return;
}
@@ -181,14 +190,6 @@ bool parseGpsText(const QString &text, location_t &location)
void LocationInformationWidget::acceptChanges()
{
- if (!diveSite) {
- qWarning() << "did not have valid dive site in LocationInformationWidget";
- return;
- }
-
- if (!ui.diveSiteCoordinates->text().isEmpty())
- parseGpsText(ui.diveSiteCoordinates->text(), diveSite->location);
- mark_divelist_changed(true);
resetState();
}
@@ -250,22 +251,11 @@ void LocationInformationWidget::enableEdition()
ui.diveSiteMessage->setText(tr("You are editing a dive site"));
}
-void LocationInformationWidget::on_diveSiteCoordinates_textChanged(const QString &text)
+void LocationInformationWidget::on_diveSiteCoordinates_editingFinished()
{
if (!diveSite)
return;
- location_t location;
- bool ok_old = has_location(&diveSite->location);
- bool ok = parseGpsText(text, location);
- if (ok != ok_old || !same_location(&location, &diveSite->location)) {
- if (ok) {
- markChangedWidget(ui.diveSiteCoordinates);
- enableLocationButtons(true);
- filter_model.setCoordinates(location);
- } else {
- enableLocationButtons(false);
- }
- }
+ Command::editDiveSiteLocation(diveSite, ui.diveSiteCoordinates->text());
}
void LocationInformationWidget::on_diveSiteCountry_editingFinished()
diff --git a/desktop-widgets/locationinformation.h b/desktop-widgets/locationinformation.h
index 04be64654..dc77da9e6 100644
--- a/desktop-widgets/locationinformation.h
+++ b/desktop-widgets/locationinformation.h
@@ -30,7 +30,7 @@ public slots:
void resetState();
void resetPallete();
void on_diveSiteCountry_editingFinished();
- void on_diveSiteCoordinates_textChanged(const QString& text);
+ void on_diveSiteCoordinates_editingFinished();
void on_diveSiteDescription_editingFinished();
void on_diveSiteName_editingFinished();
void on_diveSiteNotes_editingFinished();
diff --git a/desktop-widgets/tab-widgets/TabDiveSite.cpp b/desktop-widgets/tab-widgets/TabDiveSite.cpp
index 815627e2b..b8d7aa1c5 100644
--- a/desktop-widgets/tab-widgets/TabDiveSite.cpp
+++ b/desktop-widgets/tab-widgets/TabDiveSite.cpp
@@ -17,7 +17,7 @@ TabDiveSite::TabDiveSite(QWidget *parent) : TabBase(parent)
ui.diveSites->view()->setSortingEnabled(true);
// Show only the first few columns
- for (int i = LocationInformationModel::COORDS; i < LocationInformationModel::COLUMNS; ++i)
+ for (int i = LocationInformationModel::LOCATION; i < LocationInformationModel::COLUMNS; ++i)
ui.diveSites->view()->setColumnHidden(i, true);
connect(ui.diveSites, &TableView::addButtonClicked, this, &TabDiveSite::add);