Add ability to undo deleted dives

Before the dive is deleted, a copy is made and passed to the
undo buffer. When edit->undo is clicked, this dive is restored
to the dive list.

Signed-off-by: Grace Karanja <gracie.karanja89@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Grace Karanja 2015-02-09 09:44:10 +01:00 committed by Dirk Hohndel
parent 8d1e4557a9
commit d60a620193
3 changed files with 35 additions and 10 deletions

View file

@ -11,6 +11,7 @@
#include <QSettings> #include <QSettings>
#include <QFileDialog> #include <QFileDialog>
#include "qthelper.h" #include "qthelper.h"
#include "undobuffer.h"
// # Date Rtg Dpth Dur Tmp Wght Suit Cyl Gas SAC OTU CNS Loc // # Date Rtg Dpth Dur Tmp Wght Suit Cyl Gas SAC OTU CNS Loc
static int defaultWidth[] = { 70, 140, 90, 50, 50, 50, 50, 70, 50, 50, 70, 50, 50, 500}; static int defaultWidth[] = { 70, 140, 90, 50, 50, 50, 50, 70, 50, 50, 70, 50, 50, 500};
@ -730,6 +731,9 @@ void DiveListView::deleteDive()
for_each_dive (i, d) { for_each_dive (i, d) {
if (!d->selected) if (!d->selected)
continue; continue;
struct dive* undo_entry = alloc_dive();
copy_dive(get_dive(i), undo_entry);
MainWindow::instance()->undoBuffer->recordbefore("Delete Dive", undo_entry);
delete_single_dive(i); delete_single_dive(i);
i--; // so the next dive isn't skipped... it's now #i i--; // so the next dive isn't skipped... it's now #i
lastDiveNr = i; lastDiveNr = i;

View file

@ -1,8 +1,9 @@
#include "undobuffer.h" #include "undobuffer.h"
#include "mainwindow.h"
UndoBuffer::UndoBuffer(QObject *parent) : QObject(parent) UndoBuffer::UndoBuffer(QObject *parent) : QObject(parent)
{ {
curIdx = 0;
} }
UndoBuffer::~UndoBuffer() UndoBuffer::~UndoBuffer()
@ -12,32 +13,46 @@ UndoBuffer::~UndoBuffer()
bool UndoBuffer::canUndo() bool UndoBuffer::canUndo()
{ {
return curIdx > 0;
} }
bool UndoBuffer::canRedo() bool UndoBuffer::canRedo()
{ {
return curIdx < list.count();
} }
void UndoBuffer::redo() void UndoBuffer::redo()
{ {
current()->redo();
curIdx++;
if (curIdx > list.count())
curIdx = list.count() - 1;
} }
void UndoBuffer::undo() void UndoBuffer::undo()
{ {
current()->undo();
curIdx = list.indexOf(current());
} }
void UndoBuffer::recordbefore(QString commandName, dive *affectedDive) void UndoBuffer::recordbefore(QString commandName, dive *affectedDive)
{ {
UndoCommand *cmd = new UndoCommand(commandName, affectedDive);
//If we are within the list, clear the extra UndoCommands.
if (list.count() > 0) {
if (curIdx + 1 < list.count()) {
for (int i = curIdx + 1; i < list.count(); i++) {
list.removeAt(i);
}
}
}
list.append(cmd);
curIdx = list.count();
} }
void UndoBuffer::recordAfter(dive *affectedDive) void UndoBuffer::recordAfter(dive *affectedDive)
{ {
list.at(curIdx - 1)->setStateAfter(affectedDive);
} }
@ -50,10 +65,14 @@ UndoCommand::UndoCommand(QString commandName, dive *affectedDive)
void UndoCommand::undo() void UndoCommand::undo()
{ {
if (name == "Delete Dive") {
record_dive(stateBefore);
MainWindow::instance()->recreateDiveList();
}
} }
void UndoCommand::redo() void UndoCommand::redo()
{ {
//To be implemented
} }

View file

@ -12,7 +12,7 @@ private:
public: public:
explicit UndoCommand(QString commandName, dive* affectedDive); explicit UndoCommand(QString commandName, dive* affectedDive);
void setStateAfter(dive* affectedDive); void setStateAfter(dive* affectedDive) { stateAfter = affectedDive; }
void undo(); void undo();
void redo(); void redo();
}; };
@ -25,7 +25,9 @@ public:
~UndoBuffer(); ~UndoBuffer();
bool canUndo(); bool canUndo();
bool canRedo(); bool canRedo();
UndoCommand *current() const { return list.at(curIdx - 1); }
private: private:
QList<UndoCommand*> list;
int curIdx; int curIdx;
public slots: public slots:
void redo(); void redo();