Reverse undo buffer

Reverse all the code using the UndoBuffer class so that we can
use the QUndoStack and QUndoCommand classes. These are Qt's own
inbuild undo framework classes, offering a better undo/redo
process.

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-11 09:10:34 +03:00 committed by Dirk Hohndel
parent 6374d9cc03
commit 013da6b0af
7 changed files with 3 additions and 152 deletions

View file

@ -14,7 +14,6 @@
#include <QKeyEvent>
#include <QFileDialog>
#include "qthelper.h"
#include "undobuffer.h"
// # 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};
@ -735,9 +734,6 @@ void DiveListView::deleteDive()
for_each_dive (i, d) {
if (!d->selected)
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);
i--; // so the next dive isn't skipped... it's now #i
lastDiveNr = i;

View file

@ -36,7 +36,6 @@
#include "usermanual.h"
#endif
#include <QNetworkProxy>
#include "undobuffer.h"
MainWindow *MainWindow::m_Instance = NULL;
@ -108,7 +107,6 @@ MainWindow::MainWindow() : QMainWindow(),
connect(DivePlannerPointsModel::instance(), SIGNAL(planCreated()), this, SLOT(planCreated()));
connect(DivePlannerPointsModel::instance(), SIGNAL(planCanceled()), this, SLOT(planCanceled()));
connect(plannerDetails->printPlan(), SIGNAL(pressed()), divePlannerWidget(), SLOT(printDecoPlan()));
connect(ui.menu_Edit, SIGNAL(aboutToShow()), this, SLOT(checkForUndoAndRedo()));
#ifdef NO_PRINTING
ui.printPlan->hide();
ui.menuFile->removeAction(ui.actionPrint);
@ -182,7 +180,6 @@ MainWindow::MainWindow() : QMainWindow(),
toolBar->setContentsMargins(zeroMargins);
updateManager = new UpdateManager(this);
undoBuffer = new UndoBuffer(this);
}
MainWindow::~MainWindow()
@ -1502,22 +1499,6 @@ void MainWindow::on_actionFilterTags_triggered()
ui.multiFilter->setVisible(true);
}
void MainWindow::on_action_Undo_triggered()
{
undoBuffer->undo();
}
void MainWindow::on_action_Redo_triggered()
{
undoBuffer->redo();
}
void MainWindow::checkForUndoAndRedo()
{
ui.action_Undo->setEnabled(undoBuffer->canUndo());
ui.action_Redo->setEnabled(undoBuffer->canRedo());
}
void MainWindow::registerApplicationState(const QByteArray& state, QWidget *topLeft, QWidget *topRight, QWidget *bottomLeft, QWidget *bottomRight)
{
applicationState[state] = WidgetForQuadrant(topLeft, topRight, bottomLeft, bottomRight);

View file

@ -34,7 +34,6 @@ class DivePlannerWidget;
class ProfileWidget2;
class PlannerDetails;
class PlannerSettingsWidget;
class UndoBuffer;
enum MainWindowTitleFormat {
MWTF_DEFAULT,
@ -89,7 +88,6 @@ public:
void printPlan();
void checkSurvey(QSettings *s);
void setApplicationState(const QByteArray& state);
UndoBuffer *undoBuffer;
private
slots:
/* file menu action */
@ -159,9 +157,6 @@ slots:
void on_paste_triggered();
void on_actionFilterTags_triggered();
void on_actionConfigure_Dive_Computer_triggered();
void on_action_Undo_triggered();
void on_action_Redo_triggered();
void checkForUndoAndRedo();
protected:
void closeEvent(QCloseEvent *);

View file

@ -50,7 +50,7 @@
<x>0</x>
<y>0</y>
<width>861</width>
<height>32</height>
<height>25</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -130,8 +130,6 @@
<property name="title">
<string>&amp;Edit</string>
</property>
<addaction name="action_Undo"/>
<addaction name="action_Redo"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menu_Edit"/>

View file

@ -1,78 +0,0 @@
#include "undobuffer.h"
#include "mainwindow.h"
UndoBuffer::UndoBuffer(QObject *parent) : QObject(parent)
{
curIdx = 0;
}
UndoBuffer::~UndoBuffer()
{
}
bool UndoBuffer::canUndo()
{
return curIdx > 0;
}
bool UndoBuffer::canRedo()
{
return curIdx < list.count();
}
void UndoBuffer::redo()
{
current()->redo();
curIdx++;
if (curIdx > list.count())
curIdx = list.count() - 1;
}
void UndoBuffer::undo()
{
current()->undo();
curIdx = list.indexOf(current());
}
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)
{
list.at(curIdx - 1)->setStateAfter(affectedDive);
}
UndoCommand::UndoCommand(QString commandName, dive *affectedDive)
{
name = commandName;
stateBefore = affectedDive;
}
void UndoCommand::undo()
{
if (name == "Delete Dive") {
record_dive(stateBefore);
MainWindow::instance()->recreateDiveList();
}
}
void UndoCommand::redo()
{
//To be implemented
}

View file

@ -1,39 +0,0 @@
#ifndef UNDOBUFFER_H
#define UNDOBUFFER_H
#include <QObject>
#include "dive.h"
class UndoCommand {
private:
dive* stateBefore;
dive* stateAfter;
QString name;
public:
explicit UndoCommand(QString commandName, dive* affectedDive);
void setStateAfter(dive* affectedDive) { stateAfter = affectedDive; }
void undo();
void redo();
};
class UndoBuffer : public QObject
{
Q_OBJECT
public:
explicit UndoBuffer(QObject *parent = 0);
~UndoBuffer();
bool canUndo();
bool canRedo();
UndoCommand *current() const { return list.at(curIdx - 1); }
private:
QList<UndoCommand*> list;
int curIdx;
public slots:
void redo();
void undo();
void recordbefore(QString commandName, dive *affectedDive);
void recordAfter(dive *affectedDive);
};
#endif // UNDOBUFFER_H

View file

@ -104,8 +104,7 @@ HEADERS = \
qt-ui/statistics/statisticsbar.h \
qt-ui/statistics/yearstatistics.h \
qt-ui/diveshareexportdialog.h \
qt-ui/filtermodels.h \
qt-ui/undobuffer.h
qt-ui/filtermodels.h
android: HEADERS -= \
qt-ui/usermanual.h \
@ -199,8 +198,7 @@ SOURCES = \
qt-ui/statistics/statisticsbar.cpp \
qt-ui/statistics/monthstatistics.cpp \
qt-ui/diveshareexportdialog.cpp \
qt-ui/filtermodels.cpp \
qt-ui/undobuffer.cpp
qt-ui/filtermodels.cpp
android: SOURCES += android.cpp
else: win32: SOURCES += windows.c