2017-04-27 20:26:05 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-04 22:02:03 -07:00
|
|
|
#include "desktop-widgets/undocommands.h"
|
|
|
|
#include "desktop-widgets/mainwindow.h"
|
2018-07-19 22:35:25 +02:00
|
|
|
#include "desktop-widgets/divelistview.h"
|
2016-04-04 22:02:03 -07:00
|
|
|
#include "core/divelist.h"
|
2018-05-11 08:25:41 -07:00
|
|
|
#include "core/subsurface-string.h"
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
#include "core/gettextfromc.h"
|
2015-02-11 09:28:43 +03:00
|
|
|
|
2018-07-19 22:35:25 +02:00
|
|
|
// This helper function removes a dive, takes ownership of the dive and adds it to a DiveToAdd structure.
|
|
|
|
// It is crucial that dives are added in reverse order of deletion, so the the indices are correctly
|
|
|
|
// set and that the trips are added before they are used!
|
|
|
|
static DiveToAdd removeDive(struct dive *d)
|
|
|
|
{
|
|
|
|
DiveToAdd res;
|
|
|
|
res.idx = get_divenr(d);
|
|
|
|
if (res.idx < 0)
|
|
|
|
qWarning() << "Deletion of unknown dive!";
|
|
|
|
|
|
|
|
// remove dive from trip - if this is the last dive in the trip
|
|
|
|
// remove the whole trip.
|
|
|
|
res.trip = unregister_dive_from_trip(d, false);
|
|
|
|
if (res.trip && res.trip->nrdives == 0) {
|
|
|
|
unregister_trip(res.trip); // Remove trip from backend
|
|
|
|
res.tripToAdd.reset(res.trip); // Take ownership of trip
|
|
|
|
}
|
|
|
|
|
|
|
|
res.dive.reset(unregister_dive(res.idx)); // Remove dive from backend
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This helper function adds a dive and returns ownership to the backend. It may also add a dive trip.
|
|
|
|
// It is crucial that dives are added in reverse order of deletion (see comment above)!
|
|
|
|
// Returns pointer to added dive (which is owned by the backend!)
|
|
|
|
static dive *addDive(DiveToAdd &d)
|
|
|
|
{
|
|
|
|
if (d.tripToAdd) {
|
|
|
|
dive_trip *t = d.tripToAdd.release(); // Give up ownership of trip
|
|
|
|
insert_trip(&t); // Return ownership to backend
|
|
|
|
}
|
|
|
|
if (d.trip)
|
|
|
|
add_dive_to_trip(d.dive.get(), d.trip);
|
|
|
|
dive *res = d.dive.release(); // Give up ownership of dive
|
|
|
|
add_single_dive(d.idx, res); // Return ownership to backend
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
UndoAddDive::UndoAddDive(dive *d)
|
|
|
|
{
|
|
|
|
setText(gettextFromC::tr("add dive"));
|
|
|
|
// TODO: handle tags
|
|
|
|
//saveTags();
|
|
|
|
d->maxdepth.mm = 0;
|
|
|
|
fixup_dive(d);
|
|
|
|
diveToAdd.trip = d->divetrip;
|
|
|
|
d->divetrip = nullptr;
|
|
|
|
diveToAdd.idx = dive_get_insertion_index(d);
|
|
|
|
d->number = get_dive_nr_at_idx(diveToAdd.idx);
|
|
|
|
diveToAdd.dive.reset(clone_dive(d));
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoAddDive::redo()
|
|
|
|
{
|
|
|
|
diveToRemove = addDive(diveToAdd);
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
|
|
|
MainWindow::instance()->dive_list()->unselectDives();
|
|
|
|
MainWindow::instance()->dive_list()->selectDive(diveToAdd.idx, true);
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoAddDive::undo()
|
|
|
|
{
|
|
|
|
// Simply remove the dive that was previously added
|
|
|
|
diveToAdd = removeDive(diveToRemove);
|
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
|
|
|
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
UndoDeleteDive::UndoDeleteDive(const QVector<struct dive*> &divesToDeleteIn) : divesToDelete(divesToDeleteIn)
|
2015-02-11 09:28:43 +03:00
|
|
|
{
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
setText(tr("delete %n dive(s)", "", divesToDelete.size()));
|
2015-02-11 09:28:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoDeleteDive::undo()
|
|
|
|
{
|
2018-07-19 22:35:25 +02:00
|
|
|
for (auto it = divesToAdd.rbegin(); it != divesToAdd.rend(); ++it)
|
|
|
|
divesToDelete.append(addDive(*it));
|
2015-10-02 14:50:12 -04:00
|
|
|
|
2015-02-11 10:57:56 +03:00
|
|
|
mark_divelist_changed(true);
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
divesToAdd.clear();
|
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
2015-02-11 10:57:56 +03:00
|
|
|
MainWindow::instance()->refreshDisplay();
|
2015-02-11 09:28:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoDeleteDive::redo()
|
|
|
|
{
|
2018-07-19 22:35:25 +02:00
|
|
|
for (dive *d: divesToDelete)
|
|
|
|
divesToAdd.push_back(removeDive(d));
|
|
|
|
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
divesToDelete.clear();
|
2015-02-11 10:57:56 +03:00
|
|
|
mark_divelist_changed(true);
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
2015-02-11 10:57:56 +03:00
|
|
|
MainWindow::instance()->refreshDisplay();
|
2015-02-11 09:28:43 +03:00
|
|
|
}
|
2015-02-14 20:12:05 +03:00
|
|
|
|
|
|
|
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
UndoShiftTime::UndoShiftTime(QVector<int> changedDives, int amount)
|
2015-03-17 07:48:45 +03:00
|
|
|
: diveList(changedDives), timeChanged(amount)
|
2015-02-14 20:12:05 +03:00
|
|
|
{
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
setText(tr("delete %n dive(s)", "", changedDives.size()));
|
2015-02-14 20:12:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoShiftTime::undo()
|
|
|
|
{
|
2015-03-17 07:48:45 +03:00
|
|
|
for (int i = 0; i < diveList.count(); i++) {
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
dive *d = get_dive_by_uniq_id(diveList.at(i));
|
2015-02-14 20:12:05 +03:00
|
|
|
d->when -= timeChanged;
|
|
|
|
}
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
// Changing times may have unsorted the dive table
|
|
|
|
sort_table(&dive_table);
|
2015-02-14 20:12:05 +03:00
|
|
|
mark_divelist_changed(true);
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
|
|
|
|
// Negate the time-shift so that the next call does the reverse
|
|
|
|
timeChanged = -timeChanged;
|
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
2015-02-14 20:12:05 +03:00
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoShiftTime::redo()
|
|
|
|
{
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
// Same as undo(), since after undo() we reversed the timeOffset
|
|
|
|
undo();
|
2015-02-14 20:12:05 +03:00
|
|
|
}
|
2015-02-28 07:42:37 +03:00
|
|
|
|
|
|
|
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
UndoRenumberDives::UndoRenumberDives(const QVector<QPair<int, int>> &divesToRenumberIn) : divesToRenumber(divesToRenumberIn)
|
2015-02-28 07:42:37 +03:00
|
|
|
{
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
setText(tr("renumber %n dive(s)", "", divesToRenumber.count()));
|
2015-02-28 07:42:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRenumberDives::undo()
|
|
|
|
{
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
for (auto &pair: divesToRenumber) {
|
|
|
|
dive *d = get_dive_by_uniq_id(pair.first);
|
|
|
|
if (!d)
|
|
|
|
continue;
|
|
|
|
std::swap(d->number, pair.second);
|
2015-02-28 07:42:37 +03:00
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
2015-02-28 07:42:37 +03:00
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRenumberDives::redo()
|
|
|
|
{
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
// Redo and undo do the same thing!
|
|
|
|
undo();
|
2015-02-28 07:42:37 +03:00
|
|
|
}
|
2015-04-10 09:39:51 +03:00
|
|
|
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
UndoRemoveDivesFromTrip::UndoRemoveDivesFromTrip(const QVector<dive *> &divesToRemoveIn) : divesToRemove(divesToRemoveIn)
|
2015-04-10 09:39:51 +03:00
|
|
|
{
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
setText(tr("remove %n dive(s) from trip", "", divesToRemove.size()));
|
2015-04-10 09:39:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRemoveDivesFromTrip::undo()
|
|
|
|
{
|
2018-07-18 19:31:59 +02:00
|
|
|
// first bring back the trip(s)
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
for (auto &trip: tripsToAdd) {
|
|
|
|
dive_trip *t = trip.release(); // Give up ownership
|
|
|
|
insert_trip(&t); // Return ownership to backend
|
2015-04-10 09:39:51 +03:00
|
|
|
}
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
tripsToAdd.clear();
|
|
|
|
|
|
|
|
for (auto &pair: divesToAdd)
|
|
|
|
add_dive_to_trip(pair.first, pair.second);
|
|
|
|
divesToAdd.clear();
|
2018-07-20 20:26:06 +02:00
|
|
|
mark_divelist_changed(true);
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
2015-04-10 09:39:51 +03:00
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRemoveDivesFromTrip::redo()
|
|
|
|
{
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
for (dive *d: divesToRemove) {
|
|
|
|
// remove dive from trip - if this is the last dive in the trip
|
|
|
|
// remove the whole trip.
|
|
|
|
dive_trip *trip = unregister_dive_from_trip(d, false);
|
|
|
|
if (!trip)
|
|
|
|
continue; // This was not part of a trip
|
|
|
|
if (trip->nrdives == 0) {
|
|
|
|
unregister_trip(trip); // Remove trip from backend
|
|
|
|
tripsToAdd.emplace_back(trip); // Take ownership of trip
|
2018-07-18 19:31:59 +02:00
|
|
|
}
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
divesToAdd.emplace_back(d, trip);
|
2015-04-10 09:39:51 +03:00
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 14:44:27 +02:00
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
2015-04-10 09:39:51 +03:00
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
2018-07-20 20:26:06 +02:00
|
|
|
|
|
|
|
UndoSplitDives::UndoSplitDives(dive *d, duration_t time)
|
|
|
|
{
|
|
|
|
setText(gettextFromC::tr("split dive"));
|
|
|
|
|
|
|
|
// Split the dive
|
|
|
|
dive *new1, *new2;
|
|
|
|
int idx = time.seconds < 0 ?
|
|
|
|
split_dive_dont_insert(d, &new1, &new2) :
|
|
|
|
split_dive_at_time_dont_insert(d, time, &new1, &new2);
|
|
|
|
|
|
|
|
// If this didn't work, reset pointers so that redo() and undo() do nothing
|
|
|
|
if (idx < 0) {
|
|
|
|
diveToSplit = nullptr;
|
|
|
|
divesToUnsplit[0] = divesToUnsplit[1];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
diveToSplit = d;
|
|
|
|
splitDives[0].dive.reset(new1);
|
|
|
|
splitDives[0].trip = d->divetrip;
|
|
|
|
splitDives[0].idx = idx;
|
|
|
|
splitDives[1].dive.reset(new2);
|
|
|
|
splitDives[1].trip = d->divetrip;
|
|
|
|
splitDives[1].idx = idx + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoSplitDives::redo()
|
|
|
|
{
|
|
|
|
if (!diveToSplit)
|
|
|
|
return;
|
|
|
|
divesToUnsplit[0] = addDive(splitDives[0]);
|
|
|
|
divesToUnsplit[1] = addDive(splitDives[1]);
|
|
|
|
unsplitDive = removeDive(diveToSplit);
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
MainWindow::instance()->refreshProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoSplitDives::undo()
|
|
|
|
{
|
|
|
|
if (!unsplitDive.dive)
|
|
|
|
return;
|
|
|
|
// Note: reverse order with respect to redo()
|
|
|
|
diveToSplit = addDive(unsplitDive);
|
|
|
|
splitDives[1] = removeDive(divesToUnsplit[1]);
|
|
|
|
splitDives[0] = removeDive(divesToUnsplit[0]);
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
|
|
|
|
// Finally, do the UI stuff:
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
MainWindow::instance()->refreshProfile();
|
|
|
|
}
|