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>
This commit is contained in:
Berthold Stoeger 2019-03-13 20:10:22 +01:00 committed by Dirk Hohndel
parent 0fd85832b7
commit 2dcc0a7d1e
6 changed files with 58 additions and 4 deletions

View file

@ -88,4 +88,9 @@ void editDiveSiteName(dive_site *ds, const QString &value)
execute(new EditDiveSiteName(ds, value)); execute(new EditDiveSiteName(ds, value));
} }
void editDiveSiteDescription(dive_site *ds, const QString &value)
{
execute(new EditDiveSiteDescription(ds, value));
}
} // namespace Command } // namespace Command

View file

@ -42,6 +42,7 @@ void mergeDives(const QVector <dive *> &dives);
void deleteDiveSites(const QVector <dive_site *> &sites); void deleteDiveSites(const QVector <dive_site *> &sites);
void editDiveSiteName(dive_site *ds, const QString &value); void editDiveSiteName(dive_site *ds, const QString &value);
void editDiveSiteDescription(dive_site *ds, const QString &value);
} // namespace Command } // namespace Command

View file

@ -82,6 +82,15 @@ void DeleteDiveSites::undo()
sitesToRemove = std::move(addDiveSites(sitesToAdd)); 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), EditDiveSiteName::EditDiveSiteName(dive_site *dsIn, const QString &name) : ds(dsIn),
value(name) value(name)
{ {
@ -95,10 +104,7 @@ bool EditDiveSiteName::workToBeDone()
void EditDiveSiteName::redo() void EditDiveSiteName::redo()
{ {
QString s = ds->name; swap(ds->name, value);
free(ds->name);
ds->name = copy_qstring(value);
value = s;
emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::NAME); // Inform frontend of changed dive site. emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::NAME); // Inform frontend of changed dive site.
} }
@ -108,4 +114,27 @@ void EditDiveSiteName::undo()
redo(); 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 } // namespace Command

View file

@ -38,6 +38,18 @@ private:
QString value; // Value to be set 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 } // namespace Command
#endif // COMMAND_DIVESITE_H #endif // COMMAND_DIVESITE_H

View file

@ -131,6 +131,10 @@ void LocationInformationWidget::diveSiteChanged(struct dive_site *ds, int field)
switch (field) { switch (field) {
case LocationInformationModel::NAME: case LocationInformationModel::NAME:
ui.diveSiteName->setText(diveSite->name); ui.diveSiteName->setText(diveSite->name);
return;
case LocationInformationModel::DESCRIPTION:
ui.diveSiteDescription->setText(diveSite->description);
return;
default: default:
return; return;
} }

View file

@ -234,6 +234,9 @@ bool DiveSiteSortedModel::setData(const QModelIndex &index, const QVariant &valu
case LocationInformationModel::NAME: case LocationInformationModel::NAME:
Command::editDiveSiteName(ds, value.toString()); Command::editDiveSiteName(ds, value.toString());
return true; return true;
case LocationInformationModel::DESCRIPTION:
Command::editDiveSiteDescription(ds, value.toString());
return true;
default: default:
return false; return false;
} }