From 2dcc0a7d1ebf6435060168f343df2adb5f0abb3c Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 13 Mar 2019 20:10:22 +0100 Subject: 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 --- desktop-widgets/command_divesite.cpp | 37 ++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'desktop-widgets/command_divesite.cpp') 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 -- cgit v1.2.3-70-g09d2