diff options
author | Luis Gabriel Lima <luis.gabriel@openbossa.org> | 2012-03-28 10:34:20 -0300 |
---|---|---|
committer | Anselmo L. S. Melo <anselmo.melo@openbossa.org> | 2012-04-03 14:58:27 +0200 |
commit | 0469fb080dfcf609c463ee142a59c4b67a7d3e3d (patch) | |
tree | d8792171c47c0e8db90b2ee8fcc1e366acf6e031 | |
parent | 73cda7874a27bd72bf47159645bdf5190568fea2 (diff) |
Adding new features to UndoStack component
The private class impl. was moved to the .cpp file and the example
also was changed to use the new properties.
Exposed properties:
- undoLimit
- canUndo/canRedo (read-only)
- count (read-only)
Exposed functions:
- clear()
Change-Id: Ic647322afaa4926549e5826e7a593a583f7cc1c7
Reviewed-by: Daker Fernandes Pinheiro <daker.pinheiro@openbossa.org>
Reviewed-by: Anselmo L. S. Melo <anselmo.melo@openbossa.org>
-rw-r--r-- | examples/undo/quickundo/main.qml | 25 | ||||
-rw-r--r-- | src/imports/undo/uiquickundostack.cpp | 89 | ||||
-rw-r--r-- | src/imports/undo/uiquickundostack_p.h | 33 |
3 files changed, 126 insertions, 21 deletions
diff --git a/examples/undo/quickundo/main.qml b/examples/undo/quickundo/main.qml index 1b17c9a..4f6a6c3 100644 --- a/examples/undo/quickundo/main.qml +++ b/examples/undo/quickundo/main.qml @@ -9,6 +9,7 @@ Rectangle { UndoStack { id: stack + undoLimit: 5 } UndoPropertyCommand { @@ -18,8 +19,19 @@ Rectangle { UndoCommand { id: colorCommand - onUndo: target.color = Qt.rgba(0, 0, 0, 1); - onRedo: target.color = Qt.rgba(0.5, 0.2, 0.1, 1); + + property color black: Qt.rgba(0, 0, 0, 1) + property color lightRed: Qt.rgba(0.5, 0.2, 0.1, 1) + + function swapColors(target) { + if (target.color == lightRed) + target.color = black; + else + target.color = lightRed; + } + + onUndo: swapColors(target); + onRedo: swapColors(target); onCommandDestroyed: console.log("Command destroyed!"); } @@ -32,18 +44,23 @@ Rectangle { spacing: 20 Button { + color: stack.count ? "yellow" : "gray" + text: "Clear" + onClicked: stack.clear(); + } + Button { color: "red" width: 100 text: "Change color" onClicked: stack.push(colorCommand, rec); } Button { - color: "blue" + color: stack.canUndo ? "blue" : "gray" text: "Undo" onClicked: stack.undo(); } Button { - color: "green" + color: stack.canRedo ? "green" : "gray" text: "Redo" onClicked: stack.redo(); } diff --git a/src/imports/undo/uiquickundostack.cpp b/src/imports/undo/uiquickundostack.cpp index 95af5b8..92a849a 100644 --- a/src/imports/undo/uiquickundostack.cpp +++ b/src/imports/undo/uiquickundostack.cpp @@ -43,8 +43,26 @@ #include "uiquickundocommand_p.h" #include "uiquickundopropertycommand_p.h" -UiQuickUndoStackPrivate::UiQuickUndoStackPrivate(QObject *parent) - : UiUndoStack(parent) +#include <QtQml/QQmlInfo> + +class UiQuickUndoStackPrivate : public UiUndoStack +{ + Q_DECLARE_PUBLIC(UiQuickUndoStack) + +public: + UiQuickUndoStackPrivate(UiQuickUndoStack *q); + ~UiQuickUndoStackPrivate(); + + void setCurrentCommand(UiUndoCommand *cmd); + void commit(); + + UiQuickUndoStack *q_ptr; + UiUndoCommand *currentCommand; +}; + +UiQuickUndoStackPrivate::UiQuickUndoStackPrivate(UiQuickUndoStack *q) + : UiUndoStack(q) + , q_ptr(q) , currentCommand(0) { } @@ -53,13 +71,22 @@ UiQuickUndoStackPrivate::~UiQuickUndoStackPrivate() { } +void UiQuickUndoStackPrivate::setCurrentCommand(UiUndoCommand *cmd) +{ + Q_Q(UiQuickUndoStack); + currentCommand = cmd; + + if (!canUndo()) + emit q->canUndoChanged(); +} + void UiQuickUndoStackPrivate::commit() { if (!currentCommand) return; push(currentCommand); - currentCommand = 0; + setCurrentCommand(0); } @@ -67,12 +94,61 @@ UiQuickUndoStack::UiQuickUndoStack(QObject *parent) : QObject(parent) , d_ptr(new UiQuickUndoStackPrivate(this)) { + Q_D(const UiQuickUndoStack); + connect(d, SIGNAL(canUndoChanged(bool)), SIGNAL(canUndoChanged())); + connect(d, SIGNAL(canRedoChanged(bool)), SIGNAL(canRedoChanged())); } UiQuickUndoStack::~UiQuickUndoStack() { } +int UiQuickUndoStack::undoLimit() const +{ + Q_D(const UiQuickUndoStack); + return d->undoLimit(); +} + +void UiQuickUndoStack::setUndoLimit(int limit) +{ + Q_D(UiQuickUndoStack); + + int temp = d->undoLimit(); + d->setUndoLimit(limit); + + if (temp != limit) + emit undoLimitChanged(); +} + +bool UiQuickUndoStack::canUndo() const +{ + Q_D(const UiQuickUndoStack); + return d->canUndo() || (d->currentCommand != 0); +} + +bool UiQuickUndoStack::canRedo() const +{ + Q_D(const UiQuickUndoStack); + return d->canRedo(); +} + +int UiQuickUndoStack::count() const +{ + Q_D(const UiQuickUndoStack); + if ((d->undoLimit() && (d->count() == d->undoLimit())) || !d->currentCommand) + return d->count(); + else + return d->count() + 1; +} + +void UiQuickUndoStack::clear() +{ + Q_D(UiQuickUndoStack); + d->clear(); + d->setCurrentCommand(0); + emit countChanged(); +} + void UiQuickUndoStack::push(UiQuickBaseUndoCommand *quickCommand, QObject *target) { Q_D(UiQuickUndoStack); @@ -82,11 +158,16 @@ void UiQuickUndoStack::push(UiQuickBaseUndoCommand *quickCommand, QObject *targe d->commit(); + int countTemp = count(); + BaseUndoCommand *undoCommand = quickCommand->create(target); if (undoCommand->delayPush()) - d->currentCommand = undoCommand; + d->setCurrentCommand(undoCommand); else d->push(undoCommand); + + if (countTemp != count()) + emit countChanged(); } void UiQuickUndoStack::undo() diff --git a/src/imports/undo/uiquickundostack_p.h b/src/imports/undo/uiquickundostack_p.h index 0d43360..0ba4b36 100644 --- a/src/imports/undo/uiquickundostack_p.h +++ b/src/imports/undo/uiquickundostack_p.h @@ -54,15 +54,35 @@ class UiQuickUndoStack : public QObject { Q_OBJECT + Q_PROPERTY(int undoLimit READ undoLimit WRITE setUndoLimit NOTIFY undoLimitChanged) + Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged) + Q_PROPERTY(bool canRedo READ canRedo NOTIFY canRedoChanged) + Q_PROPERTY(int count READ count NOTIFY countChanged) + public: UiQuickUndoStack(QObject *parent = 0); ~UiQuickUndoStack(); + int undoLimit() const; + void setUndoLimit(int limit); + + bool canUndo() const; + bool canRedo() const; + int count() const; + + Q_INVOKABLE void clear(); + public slots: void push(UiQuickBaseUndoCommand *cmd, QObject *target); void undo(); void redo(); +Q_SIGNALS: + void undoLimitChanged(); + void canUndoChanged(); + void canRedoChanged(); + void countChanged(); + private: Q_DISABLE_COPY(UiQuickUndoStack) Q_DECLARE_PRIVATE(UiQuickUndoStack) @@ -70,17 +90,4 @@ private: QScopedPointer<UiQuickUndoStackPrivate> d_ptr; }; -class UiQuickUndoStackPrivate : public UiUndoStack -{ - Q_OBJECT - -public: - UiQuickUndoStackPrivate(QObject *parent = 0); - ~UiQuickUndoStackPrivate(); - - void commit(); - - UiUndoCommand *currentCommand; -}; - #endif |