2016-04-05 05:02:03 +00:00
|
|
|
#include "desktop-widgets/undocommands.h"
|
|
|
|
#include "desktop-widgets/mainwindow.h"
|
|
|
|
#include "core/divelist.h"
|
2015-02-11 06:28:43 +00:00
|
|
|
|
2015-10-02 18:50:12 +00:00
|
|
|
UndoDeleteDive::UndoDeleteDive(QList<dive *> deletedDives) : diveList(deletedDives)
|
2015-02-11 06:28:43 +00:00
|
|
|
{
|
2015-02-11 07:57:56 +00:00
|
|
|
setText("delete dive");
|
2015-03-17 04:48:45 +00:00
|
|
|
if (diveList.count() > 1)
|
|
|
|
setText(QString("delete %1 dives").arg(QString::number(diveList.count())));
|
2015-02-11 06:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoDeleteDive::undo()
|
|
|
|
{
|
2015-10-02 18:50:12 +00:00
|
|
|
// first bring back the trip(s)
|
|
|
|
Q_FOREACH(struct dive_trip *trip, tripList)
|
|
|
|
insert_trip(&trip);
|
|
|
|
|
|
|
|
// now walk the list of deleted dives
|
|
|
|
for (int i = 0; i < diveList.count(); i++) {
|
|
|
|
struct dive *d = diveList.at(i);
|
|
|
|
// we adjusted the divetrip to point to the "new" divetrip
|
|
|
|
if (d->divetrip) {
|
|
|
|
struct dive_trip *trip = d->divetrip;
|
|
|
|
tripflag_t tripflag = d->tripflag; // this gets overwritten in add_dive_to_trip()
|
|
|
|
d->divetrip = NULL;
|
|
|
|
d->next = NULL;
|
|
|
|
d->pprev = NULL;
|
|
|
|
add_dive_to_trip(d, trip);
|
|
|
|
d->tripflag = tripflag;
|
|
|
|
}
|
2015-03-17 04:48:45 +00:00
|
|
|
record_dive(diveList.at(i));
|
2015-10-02 18:50:12 +00:00
|
|
|
}
|
2015-02-11 07:57:56 +00:00
|
|
|
mark_divelist_changed(true);
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
2015-02-11 06:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoDeleteDive::redo()
|
|
|
|
{
|
2015-02-11 07:57:56 +00:00
|
|
|
QList<struct dive*> newList;
|
2015-03-17 04:48:45 +00:00
|
|
|
for (int i = 0; i < diveList.count(); i++) {
|
2015-10-02 18:50:12 +00:00
|
|
|
// make a copy of the dive before deleting it
|
2015-02-11 07:57:56 +00:00
|
|
|
struct dive* d = alloc_dive();
|
2015-03-17 04:48:45 +00:00
|
|
|
copy_dive(diveList.at(i), d);
|
2015-02-11 07:57:56 +00:00
|
|
|
newList.append(d);
|
2015-10-02 18:50:12 +00:00
|
|
|
// check for trip - if this is the last dive in the trip
|
|
|
|
// the trip will get deleted, so we need to remember it as well
|
|
|
|
if (d->divetrip && d->divetrip->nrdives == 1) {
|
|
|
|
struct dive_trip *undo_trip = (struct dive_trip *)calloc(1, sizeof(struct dive_trip));
|
|
|
|
*undo_trip = *d->divetrip;
|
2015-10-03 11:19:55 +00:00
|
|
|
undo_trip->location = copy_string(d->divetrip->location);
|
|
|
|
undo_trip->notes = copy_string(d->divetrip->notes);
|
2015-10-02 18:50:12 +00:00
|
|
|
undo_trip->nrdives = 0;
|
|
|
|
undo_trip->next = NULL;
|
|
|
|
undo_trip->dives = NULL;
|
|
|
|
// update all the dives who were in this trip to point to the copy of the
|
|
|
|
// trip that we are about to delete implicitly when deleting its last dive below
|
|
|
|
Q_FOREACH(struct dive *inner_dive, newList)
|
|
|
|
if (inner_dive->divetrip == d->divetrip)
|
|
|
|
inner_dive->divetrip = undo_trip;
|
|
|
|
d->divetrip = undo_trip;
|
|
|
|
tripList.append(undo_trip);
|
|
|
|
}
|
2015-02-11 07:57:56 +00:00
|
|
|
//delete the dive
|
2015-03-17 04:48:45 +00:00
|
|
|
delete_single_dive(get_divenr(diveList.at(i)));
|
2015-02-11 07:57:56 +00:00
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
2015-03-17 04:48:45 +00:00
|
|
|
diveList.clear();
|
|
|
|
diveList = newList;
|
2015-02-11 06:28:43 +00:00
|
|
|
}
|
2015-02-14 17:12:05 +00:00
|
|
|
|
|
|
|
|
2015-03-17 04:48:45 +00:00
|
|
|
UndoShiftTime::UndoShiftTime(QList<int> changedDives, int amount)
|
|
|
|
: diveList(changedDives), timeChanged(amount)
|
2015-02-14 17:12:05 +00:00
|
|
|
{
|
|
|
|
setText("shift time");
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoShiftTime::undo()
|
|
|
|
{
|
2015-03-17 04:48:45 +00:00
|
|
|
for (int i = 0; i < diveList.count(); i++) {
|
|
|
|
struct dive* d = get_dive_by_uniq_id(diveList.at(i));
|
2015-02-14 17:12:05 +00:00
|
|
|
d->when -= timeChanged;
|
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoShiftTime::redo()
|
|
|
|
{
|
2015-03-17 04:48:45 +00:00
|
|
|
for (int i = 0; i < diveList.count(); i++) {
|
|
|
|
struct dive* d = get_dive_by_uniq_id(diveList.at(i));
|
2015-02-14 17:12:05 +00:00
|
|
|
d->when += timeChanged;
|
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
2015-02-28 04:42:37 +00:00
|
|
|
|
|
|
|
|
2015-06-15 04:49:34 +00:00
|
|
|
UndoRenumberDives::UndoRenumberDives(QMap<int, QPair<int, int> > originalNumbers)
|
2015-02-28 04:42:37 +00:00
|
|
|
{
|
|
|
|
oldNumbers = originalNumbers;
|
|
|
|
if (oldNumbers.count() > 1)
|
|
|
|
setText(QString("renumber %1 dives").arg(QString::number(oldNumbers.count())));
|
2015-06-15 04:49:34 +00:00
|
|
|
else
|
|
|
|
setText("renumber dive");
|
2015-02-28 04:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRenumberDives::undo()
|
|
|
|
{
|
|
|
|
foreach (int key, oldNumbers.keys()) {
|
|
|
|
struct dive* d = get_dive_by_uniq_id(key);
|
2015-06-15 04:49:34 +00:00
|
|
|
d->number = oldNumbers.value(key).first;
|
2015-02-28 04:42:37 +00:00
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRenumberDives::redo()
|
|
|
|
{
|
|
|
|
foreach (int key, oldNumbers.keys()) {
|
|
|
|
struct dive* d = get_dive_by_uniq_id(key);
|
2015-06-15 04:49:34 +00:00
|
|
|
d->number = oldNumbers.value(key).second;
|
2015-02-28 04:42:37 +00:00
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
2015-04-10 06:39:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
UndoRemoveDivesFromTrip::UndoRemoveDivesFromTrip(QMap<dive *, dive_trip *> removedDives)
|
|
|
|
{
|
|
|
|
divesToUndo = removedDives;
|
|
|
|
setText("remove dive(s) from trip");
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRemoveDivesFromTrip::undo()
|
|
|
|
{
|
|
|
|
QMapIterator<dive*, dive_trip*> i(divesToUndo);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
add_dive_to_trip(i.key (), i.value());
|
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndoRemoveDivesFromTrip::redo()
|
|
|
|
{
|
|
|
|
QMapIterator<dive*, dive_trip*> i(divesToUndo);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
remove_dive_from_trip(i.key(), false);
|
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
MainWindow::instance()->refreshDisplay();
|
|
|
|
}
|