Undo: isolate undo-commands

This refactors the undo-commands (which are now only "commands").

- Move everything in namespace Command. This allows shortening of
  names without polluting the global namespace. Moreover, the prefix
  Command:: will immediately signal that the undo-machinery is
  invoked. This is more terse than UndoCommands::instance()->...
- Remove the Undo in front of the class-names. Creating an "UndoX"
  object to do "X" is paradoxical.
- Create a base class for all commands that defines the Qt-translation
  functions. Thus all translations end up in the "Command" context.
- Add a workToBeDone() function, which signals whether this should be
  added to the UndoStack. Thus the caller doesn't have to check itself
  whether this any work will be done. Note: Qt5.9 introduces "setObsolete"
  which does the same.
- Split into public and internal header files. In the public header
  file only export the function calls, thus hiding all implementation
  details from the caller.
- Split in different translation units: One for the stubs, one for
  the base classes and one for groups of commands. Currently, there
  is only one class of commands: divelist-commands.
- Move the undoStack from the MainWindow class into commands_base.cpp.
  If we want to implement MDI, this can easily be moved into an
  appropriate Document class.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-07-23 23:41:23 +02:00 committed by Dirk Hohndel
parent f427226b3b
commit 43c3885249
16 changed files with 470 additions and 1175 deletions

View file

@ -20,7 +20,7 @@
#include <QMessageBox>
#include <QHeaderView>
#include "core/qthelper.h"
#include "desktop-widgets/undocommands.h"
#include "desktop-widgets/command.h"
#include "desktop-widgets/divelistview.h"
#include "qt-models/divepicturemodel.h"
#include "core/metrics.h"
@ -633,10 +633,8 @@ void DiveListView::mergeDives()
if (current_batch.count() > 1)
merge_batches.append(current_batch);
for (const QVector<dive *> &batch: merge_batches) {
UndoMergeDives *undoCommand = new UndoMergeDives(batch);
MainWindow::instance()->undoStack->push(undoCommand);
}
for (const QVector<dive *> &batch: merge_batches)
Command::mergeDives(batch);
}
void DiveListView::splitDives()
@ -650,10 +648,8 @@ void DiveListView::splitDives()
if (dive->selected)
dives.append(dive);
}
for (struct dive *d: dives) {
UndoSplitDives *undoCommand = new UndoSplitDives(d, duration_t{-1});
MainWindow::instance()->undoStack->push(undoCommand);
}
for (struct dive *d: dives)
Command::splitDives(d, duration_t{-1});
}
void DiveListView::renumberDives()
@ -671,8 +667,7 @@ void DiveListView::merge_trip(const QModelIndex &a, int offset)
dive_trip_t *trip_b = (dive_trip_t *)b.data(DiveTripModel::TRIP_ROLE).value<void *>();
if (trip_a == trip_b || !trip_a || !trip_b)
return;
UndoMergeTrips *undoCommand = new UndoMergeTrips(trip_a, trip_b);
MainWindow::instance()->undoStack->push(undoCommand);
Command::mergeTrips(trip_a, trip_b);
rememberSelection();
reload(currentLayout, false);
restoreSelection();
@ -700,11 +695,7 @@ void DiveListView::removeFromTrip()
if (d->selected && d->divetrip)
divesToRemove.append(d);
}
if (divesToRemove.isEmpty())
return;
UndoRemoveDivesFromTrip *undoCommand = new UndoRemoveDivesFromTrip(divesToRemove);
MainWindow::instance()->undoStack->push(undoCommand);
Command::removeDivesFromTrip(divesToRemove);
rememberSelection();
reload(currentLayout, false);
@ -725,8 +716,7 @@ void DiveListView::newTripAbove()
if (d->selected)
dives.append(d);
}
UndoCreateTrip *undoCommand = new UndoCreateTrip(dives);
MainWindow::instance()->undoStack->push(undoCommand);
Command::createTrip(dives);
reload(currentLayout, false);
mark_divelist_changed(true);
@ -774,8 +764,7 @@ void DiveListView::addToTrip(int delta)
dives.append(d);
}
}
UndoAddDivesToTrip *undoEntry = new UndoAddDivesToTrip(dives, trip);
MainWindow::instance()->undoStack->push(undoEntry);
Command::addDivesToTrip(dives, trip);
reload(currentLayout, false);
restoreSelection();
@ -814,7 +803,7 @@ void DiveListView::deleteDive()
int i;
int lastDiveNr = -1;
QVector<struct dive*> deletedDives; //a list of all deleted dives to be stored in the undo command
QVector<struct dive*> deletedDives;
for_each_dive (i, d) {
if (!d->selected)
continue;
@ -822,8 +811,7 @@ void DiveListView::deleteDive()
lastDiveNr = i;
}
// the actual dive deletion is happening in the redo command that is implicitly triggered
UndoDeleteDive *undoEntry = new UndoDeleteDive(deletedDives);
MainWindow::instance()->undoStack->push(undoEntry);
Command::deleteDive(deletedDives);
if (amount_selected == 0) {
MainWindow::instance()->cleanUpEmpty();
}