summaryrefslogtreecommitdiffstats
path: root/desktop-widgets
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-03-13 20:10:22 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-04-12 18:19:07 +0300
commit2dcc0a7d1ebf6435060168f343df2adb5f0abb3c (patch)
tree533995c902f5dc6a629ac606c08a8b3db834d525 /desktop-widgets
parent0fd85832b7f628d1bb6d308c11b5e53ea1e9f65b (diff)
downloadsubsurface-2dcc0a7d1ebf6435060168f343df2adb5f0abb3c.tar.gz
Undo: implement undo of dive site description editing
Simply duplicate the code of dive site name editing. Split out the common functionality that swaps a C and a Qt string. 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.cpp37
-rw-r--r--desktop-widgets/command_divesite.h12
-rw-r--r--desktop-widgets/locationinformation.cpp4
5 files changed, 55 insertions, 4 deletions
diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp
index 82e5ebe07..ee2016c6e 100644
--- a/desktop-widgets/command.cpp
+++ b/desktop-widgets/command.cpp
@@ -88,4 +88,9 @@ void editDiveSiteName(dive_site *ds, const QString &value)
execute(new EditDiveSiteName(ds, value));
}
+void editDiveSiteDescription(dive_site *ds, const QString &value)
+{
+ execute(new EditDiveSiteDescription(ds, value));
+}
+
} // namespace Command
diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h
index 09fb45526..785981831 100644
--- a/desktop-widgets/command.h
+++ b/desktop-widgets/command.h
@@ -42,6 +42,7 @@ void mergeDives(const QVector <dive *> &dives);
void deleteDiveSites(const QVector <dive_site *> &sites);
void editDiveSiteName(dive_site *ds, const QString &value);
+void editDiveSiteDescription(dive_site *ds, const QString &value);
} // namespace Command
diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp
index 96dcaa60c..28d47d603 100644
--- a/desktop-widgets/command_divesite.cpp
+++ b/desktop-widgets/command_divesite.cpp
@@ -82,6 +82,15 @@ void DeleteDiveSites::undo()
sitesToRemove = std::move(addDiveSites(sitesToAdd));
}
+// Helper function: swap C and Qt string
+static void swap(char *&c, QString &q)
+{
+ QString s = c;
+ free(c);
+ c = copy_qstring(q);
+ q = s;
+}
+
EditDiveSiteName::EditDiveSiteName(dive_site *dsIn, const QString &name) : ds(dsIn),
value(name)
{
@@ -95,10 +104,7 @@ bool EditDiveSiteName::workToBeDone()
void EditDiveSiteName::redo()
{
- QString s = ds->name;
- free(ds->name);
- ds->name = copy_qstring(value);
- value = s;
+ swap(ds->name, value);
emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::NAME); // Inform frontend of changed dive site.
}
@@ -108,4 +114,27 @@ void EditDiveSiteName::undo()
redo();
}
+EditDiveSiteDescription::EditDiveSiteDescription(dive_site *dsIn, const QString &description) : ds(dsIn),
+ value(description)
+{
+ setText(tr("Edit dive site description"));
+}
+
+bool EditDiveSiteDescription::workToBeDone()
+{
+ return value != QString(ds->description);
+}
+
+void EditDiveSiteDescription::redo()
+{
+ swap(ds->description, value);
+ emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::DESCRIPTION); // Inform frontend of changed dive site.
+}
+
+void EditDiveSiteDescription::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 692e5c109..78622862e 100644
--- a/desktop-widgets/command_divesite.h
+++ b/desktop-widgets/command_divesite.h
@@ -38,6 +38,18 @@ private:
QString value; // Value to be set
};
+class EditDiveSiteDescription : public Base {
+public:
+ EditDiveSiteDescription(dive_site *ds, const QString &description);
+private:
+ bool workToBeDone() override;
+ void undo() override;
+ void redo() override;
+
+ dive_site *ds;
+ QString value; // Value to be set
+};
+
} // namespace Command
#endif // COMMAND_DIVESITE_H
diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp
index 0da3e12ee..747a2d183 100644
--- a/desktop-widgets/locationinformation.cpp
+++ b/desktop-widgets/locationinformation.cpp
@@ -131,6 +131,10 @@ void LocationInformationWidget::diveSiteChanged(struct dive_site *ds, int field)
switch (field) {
case LocationInformationModel::NAME:
ui.diveSiteName->setText(diveSite->name);
+ return;
+ case LocationInformationModel::DESCRIPTION:
+ ui.diveSiteDescription->setText(diveSite->description);
+ return;
default:
return;
}