summaryrefslogtreecommitdiffstats
path: root/examples/tools/undoframework
diff options
context:
space:
mode:
authorQt by Nokia <qt-info@nokia.com>2011-04-27 12:05:43 +0200
committeraxis <qt-info@nokia.com>2011-04-27 12:05:43 +0200
commit38be0d13830efd2d98281c645c3a60afe05ffece (patch)
tree6ea73f3ec77f7d153333779883e8120f82820abe /examples/tools/undoframework
Initial import from the monolithic Qt.
This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the Qt Git wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://qt.gitorious.org/qt/pages/GitIntroductionWithQt If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, Qt master branch, at commit 896db169ea224deb96c59ce8af800d019de63f12
Diffstat (limited to 'examples/tools/undoframework')
-rw-r--r--examples/tools/undoframework/commands.cpp168
-rw-r--r--examples/tools/undoframework/commands.h104
-rw-r--r--examples/tools/undoframework/diagramitem.cpp65
-rw-r--r--examples/tools/undoframework/diagramitem.h72
-rw-r--r--examples/tools/undoframework/diagramscene.cpp75
-rw-r--r--examples/tools/undoframework/diagramscene.h74
-rw-r--r--examples/tools/undoframework/images/cross.pngbin0 -> 114 bytes
-rw-r--r--examples/tools/undoframework/main.cpp57
-rw-r--r--examples/tools/undoframework/mainwindow.cpp206
-rw-r--r--examples/tools/undoframework/mainwindow.h99
-rw-r--r--examples/tools/undoframework/undoframework.pro18
-rw-r--r--examples/tools/undoframework/undoframework.qrc6
12 files changed, 944 insertions, 0 deletions
diff --git a/examples/tools/undoframework/commands.cpp b/examples/tools/undoframework/commands.cpp
new file mode 100644
index 0000000000..ff7b0b7f1d
--- /dev/null
+++ b/examples/tools/undoframework/commands.cpp
@@ -0,0 +1,168 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtGui>
+
+#include "commands.h"
+#include "diagramitem.h"
+
+//! [0]
+MoveCommand::MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
+ QUndoCommand *parent)
+ : QUndoCommand(parent)
+{
+ myDiagramItem = diagramItem;
+ newPos = diagramItem->pos();
+ myOldPos = oldPos;
+}
+//! [0]
+
+//! [1]
+bool MoveCommand::mergeWith(const QUndoCommand *command)
+{
+ const MoveCommand *moveCommand = static_cast<const MoveCommand *>(command);
+ DiagramItem *item = moveCommand->myDiagramItem;
+
+ if (myDiagramItem != item)
+ return false;
+
+ newPos = item->pos();
+ setText(QObject::tr("Move %1")
+ .arg(createCommandString(myDiagramItem, newPos)));
+
+ return true;
+}
+//! [1]
+
+//! [2]
+void MoveCommand::undo()
+{
+ myDiagramItem->setPos(myOldPos);
+ myDiagramItem->scene()->update();
+ setText(QObject::tr("Move %1")
+ .arg(createCommandString(myDiagramItem, newPos)));
+}
+//! [2]
+
+//! [3]
+void MoveCommand::redo()
+{
+ myDiagramItem->setPos(newPos);
+ setText(QObject::tr("Move %1")
+ .arg(createCommandString(myDiagramItem, newPos)));
+}
+//! [3]
+
+//! [4]
+DeleteCommand::DeleteCommand(QGraphicsScene *scene, QUndoCommand *parent)
+ : QUndoCommand(parent)
+{
+ myGraphicsScene = scene;
+ QList<QGraphicsItem *> list = myGraphicsScene->selectedItems();
+ list.first()->setSelected(false);
+ myDiagramItem = static_cast<DiagramItem *>(list.first());
+ setText(QObject::tr("Delete %1")
+ .arg(createCommandString(myDiagramItem, myDiagramItem->pos())));
+}
+//! [4]
+
+//! [5]
+void DeleteCommand::undo()
+{
+ myGraphicsScene->addItem(myDiagramItem);
+ myGraphicsScene->update();
+}
+//! [5]
+
+//! [6]
+void DeleteCommand::redo()
+{
+ myGraphicsScene->removeItem(myDiagramItem);
+}
+//! [6]
+
+//! [7]
+AddCommand::AddCommand(DiagramItem::DiagramType addType,
+ QGraphicsScene *scene, QUndoCommand *parent)
+ : QUndoCommand(parent)
+{
+ static int itemCount = 0;
+
+ myGraphicsScene = scene;
+ myDiagramItem = new DiagramItem(addType);
+ initialPosition = QPointF((itemCount * 15) % int(scene->width()),
+ (itemCount * 15) % int(scene->height()));
+ scene->update();
+ ++itemCount;
+ setText(QObject::tr("Add %1")
+ .arg(createCommandString(myDiagramItem, initialPosition)));
+}
+//! [7]
+
+AddCommand::~AddCommand()
+{
+ if (!myDiagramItem->scene())
+ delete myDiagramItem;
+}
+
+//! [8]
+void AddCommand::undo()
+{
+ myGraphicsScene->removeItem(myDiagramItem);
+ myGraphicsScene->update();
+}
+//! [8]
+
+//! [9]
+void AddCommand::redo()
+{
+ myGraphicsScene->addItem(myDiagramItem);
+ myDiagramItem->setPos(initialPosition);
+ myGraphicsScene->clearSelection();
+ myGraphicsScene->update();
+}
+//! [9]
+
+QString createCommandString(DiagramItem *item, const QPointF &pos)
+{
+ return QObject::tr("%1 at (%2, %3)")
+ .arg(item->diagramType() == DiagramItem::Box ? "Box" : "Triangle")
+ .arg(pos.x()).arg(pos.y());
+}
diff --git a/examples/tools/undoframework/commands.h b/examples/tools/undoframework/commands.h
new file mode 100644
index 0000000000..a4e4ab9549
--- /dev/null
+++ b/examples/tools/undoframework/commands.h
@@ -0,0 +1,104 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef COMMANDS_H
+#define COMMANDS_H
+
+#include <QUndoCommand>
+
+#include "diagramitem.h"
+
+//! [0]
+class MoveCommand : public QUndoCommand
+{
+public:
+ enum { Id = 1234 };
+
+ MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
+ QUndoCommand *parent = 0);
+
+ void undo();
+ void redo();
+ bool mergeWith(const QUndoCommand *command);
+ int id() const { return Id; }
+
+private:
+ DiagramItem *myDiagramItem;
+ QPointF myOldPos;
+ QPointF newPos;
+};
+//! [0]
+
+//! [1]
+class DeleteCommand : public QUndoCommand
+{
+public:
+ DeleteCommand(QGraphicsScene *graphicsScene, QUndoCommand *parent = 0);
+
+ void undo();
+ void redo();
+
+private:
+ DiagramItem *myDiagramItem;
+ QGraphicsScene *myGraphicsScene;
+};
+//! [1]
+
+//! [2]
+class AddCommand : public QUndoCommand
+{
+public:
+ AddCommand(DiagramItem::DiagramType addType, QGraphicsScene *graphicsScene,
+ QUndoCommand *parent = 0);
+ ~AddCommand();
+
+ void undo();
+ void redo();
+
+private:
+ DiagramItem *myDiagramItem;
+ QGraphicsScene *myGraphicsScene;
+ QPointF initialPosition;
+};
+//! [2]
+
+QString createCommandString(DiagramItem *item, const QPointF &point);
+
+#endif
diff --git a/examples/tools/undoframework/diagramitem.cpp b/examples/tools/undoframework/diagramitem.cpp
new file mode 100644
index 0000000000..3e896f3926
--- /dev/null
+++ b/examples/tools/undoframework/diagramitem.cpp
@@ -0,0 +1,65 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtGui>
+
+#include "diagramitem.h"
+
+DiagramItem::DiagramItem(DiagramType diagramType, QGraphicsItem *item,
+ QGraphicsScene *scene)
+ : QGraphicsPolygonItem(item, scene)
+{
+ if (diagramType == Box) {
+ boxPolygon << QPointF(0, 0) << QPointF(0, 30) << QPointF(30, 30)
+ << QPointF(30, 0) << QPointF(0, 0);
+ setPolygon(boxPolygon);
+ } else {
+ trianglePolygon << QPointF(15, 0) << QPointF(30, 30) << QPointF(0, 30)
+ << QPointF(15, 0);
+ setPolygon(trianglePolygon);
+ }
+
+ QColor color(static_cast<int>(qrand()) % 256,
+ static_cast<int>(qrand()) % 256, static_cast<int>(qrand()) % 256);
+ QBrush brush(color);
+ setBrush(brush);
+ setFlag(QGraphicsItem::ItemIsSelectable);
+ setFlag(QGraphicsItem::ItemIsMovable);
+}
diff --git a/examples/tools/undoframework/diagramitem.h b/examples/tools/undoframework/diagramitem.h
new file mode 100644
index 0000000000..9ca3c0a399
--- /dev/null
+++ b/examples/tools/undoframework/diagramitem.h
@@ -0,0 +1,72 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef DIAGRAMITEM_H
+#define DIAGRAMITEM_H
+
+#include <QGraphicsPolygonItem>
+
+QT_BEGIN_NAMESPACE
+class QGraphicsItem;
+class QGraphicsScene;
+class QGraphicsSceneMouseEvent;
+class QPointF;
+QT_END_NAMESPACE
+
+class DiagramItem : public QGraphicsPolygonItem
+{
+public:
+ enum { Type = UserType + 1 };
+ enum DiagramType { Box, Triangle };
+
+ DiagramItem(DiagramType diagramType, QGraphicsItem *item = 0,
+ QGraphicsScene *scene = 0);
+
+ DiagramType diagramType() const {
+ return polygon() == boxPolygon ? Box : Triangle;
+ }
+ int type() const { return Type; }
+
+private:
+ QPolygonF boxPolygon;
+ QPolygonF trianglePolygon;
+};
+
+#endif
diff --git a/examples/tools/undoframework/diagramscene.cpp b/examples/tools/undoframework/diagramscene.cpp
new file mode 100644
index 0000000000..7ffd2db3ac
--- /dev/null
+++ b/examples/tools/undoframework/diagramscene.cpp
@@ -0,0 +1,75 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtGui>
+
+#include "diagramscene.h"
+#include "diagramitem.h"
+
+DiagramScene::DiagramScene(QObject *parent)
+ : QGraphicsScene(parent)
+{
+ movingItem = 0;
+}
+
+void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
+{
+ QPointF mousePos(event->buttonDownScenePos(Qt::LeftButton).x(),
+ event->buttonDownScenePos(Qt::LeftButton).y());
+ movingItem = itemAt(mousePos.x(), mousePos.y());
+
+ if (movingItem != 0 && event->button() == Qt::LeftButton) {
+ oldPos = movingItem->pos();
+ }
+
+ clearSelection();
+ QGraphicsScene::mousePressEvent(event);
+}
+
+void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
+{
+ if (movingItem != 0 && event->button() == Qt::LeftButton) {
+ if (oldPos != movingItem->pos())
+ emit itemMoved(qgraphicsitem_cast<DiagramItem *>(movingItem),
+ oldPos);
+ movingItem = 0;
+ }
+ QGraphicsScene::mouseReleaseEvent(event);
+}
diff --git a/examples/tools/undoframework/diagramscene.h b/examples/tools/undoframework/diagramscene.h
new file mode 100644
index 0000000000..6ee50fb337
--- /dev/null
+++ b/examples/tools/undoframework/diagramscene.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef DIAGRAMSCENE_H
+#define DIAGRAMSCENE_H
+
+#include <QObject>
+#include <QGraphicsScene>
+
+class DiagramItem;
+QT_BEGIN_NAMESPACE
+class QGraphicsSceneDragDropEvent;
+class QGraphicsViewItem;
+QT_END_NAMESPACE
+
+//! [0]
+class DiagramScene : public QGraphicsScene
+{
+ Q_OBJECT
+
+public:
+ DiagramScene(QObject *parent = 0);
+
+signals:
+ void itemMoved(DiagramItem *movedItem, const QPointF &movedFromPosition);
+
+protected:
+ void mousePressEvent(QGraphicsSceneMouseEvent *event);
+ void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
+
+private:
+ QGraphicsItem *movingItem;
+ QPointF oldPos;
+};
+//! [0]
+
+#endif
diff --git a/examples/tools/undoframework/images/cross.png b/examples/tools/undoframework/images/cross.png
new file mode 100644
index 0000000000..09e5e8c2ad
--- /dev/null
+++ b/examples/tools/undoframework/images/cross.png
Binary files differ
diff --git a/examples/tools/undoframework/main.cpp b/examples/tools/undoframework/main.cpp
new file mode 100644
index 0000000000..abfa0aa462
--- /dev/null
+++ b/examples/tools/undoframework/main.cpp
@@ -0,0 +1,57 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtGui>
+
+#include "mainwindow.h"
+
+//! [0]
+int main(int argv, char *args[])
+{
+ Q_INIT_RESOURCE(undoframework);
+
+ QApplication app(argv, args);
+
+ MainWindow mainWindow;
+ mainWindow.show();
+
+ return app.exec();
+}
+//! [0]
diff --git a/examples/tools/undoframework/mainwindow.cpp b/examples/tools/undoframework/mainwindow.cpp
new file mode 100644
index 0000000000..72c9cc14f4
--- /dev/null
+++ b/examples/tools/undoframework/mainwindow.cpp
@@ -0,0 +1,206 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtGui>
+
+#include "mainwindow.h"
+#include "diagramscene.h"
+#include "diagramitem.h"
+#include "commands.h"
+
+//! [0]
+MainWindow::MainWindow()
+{
+ undoStack = new QUndoStack(this);
+
+ createActions();
+ createMenus();
+
+ createUndoView();
+
+ diagramScene = new DiagramScene();
+ QBrush pixmapBrush(QPixmap(":/images/cross.png").scaled(30, 30));
+ diagramScene->setBackgroundBrush(pixmapBrush);
+ diagramScene->setSceneRect(QRect(0, 0, 500, 500));
+
+ connect(diagramScene, SIGNAL(itemMoved(DiagramItem*,QPointF)),
+ this, SLOT(itemMoved(DiagramItem*,QPointF)));
+
+ setWindowTitle("Undo Framework");
+ QGraphicsView *view = new QGraphicsView(diagramScene);
+ setCentralWidget(view);
+ resize(700, 500);
+}
+//! [0]
+
+//! [1]
+void MainWindow::createUndoView()
+{
+ undoView = new QUndoView(undoStack);
+ undoView->setWindowTitle(tr("Command List"));
+ undoView->show();
+ undoView->setAttribute(Qt::WA_QuitOnClose, false);
+}
+//! [1]
+
+//! [2]
+void MainWindow::createActions()
+{
+ deleteAction = new QAction(tr("&Delete Item"), this);
+ deleteAction->setShortcut(tr("Del"));
+ connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
+//! [2] //! [3]
+
+//! [3] //! [4]
+ addBoxAction = new QAction(tr("Add &Box"), this);
+//! [4]
+ addBoxAction->setShortcut(tr("Ctrl+O"));
+ connect(addBoxAction, SIGNAL(triggered()), this, SLOT(addBox()));
+
+ addTriangleAction = new QAction(tr("Add &Triangle"), this);
+ addTriangleAction->setShortcut(tr("Ctrl+T"));
+ connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle()));
+
+//! [5]
+ undoAction = undoStack->createUndoAction(this, tr("&Undo"));
+ undoAction->setShortcuts(QKeySequence::Undo);
+
+ redoAction = undoStack->createRedoAction(this, tr("&Redo"));
+ redoAction->setShortcuts(QKeySequence::Redo);
+//! [5]
+
+ exitAction = new QAction(tr("E&xit"), this);
+ exitAction->setShortcuts(QKeySequence::Quit);
+ connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
+
+ aboutAction = new QAction(tr("&About"), this);
+ QList<QKeySequence> aboutShortcuts;
+ aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B");
+ aboutAction->setShortcuts(aboutShortcuts);
+ connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
+}
+
+//! [6]
+void MainWindow::createMenus()
+{
+//! [6]
+ fileMenu = menuBar()->addMenu(tr("&File"));
+ fileMenu->addAction(exitAction);
+
+//! [7]
+ editMenu = menuBar()->addMenu(tr("&Edit"));
+ editMenu->addAction(undoAction);
+ editMenu->addAction(redoAction);
+ editMenu->addSeparator();
+ editMenu->addAction(deleteAction);
+ connect(editMenu, SIGNAL(aboutToShow()),
+ this, SLOT(itemMenuAboutToShow()));
+ connect(editMenu, SIGNAL(aboutToHide()),
+ this, SLOT(itemMenuAboutToHide()));
+
+//! [7]
+ itemMenu = menuBar()->addMenu(tr("&Item"));
+ itemMenu->addAction(addBoxAction);
+ itemMenu->addAction(addTriangleAction);
+
+ helpMenu = menuBar()->addMenu(tr("&About"));
+ helpMenu->addAction(aboutAction);
+//! [8]
+}
+//! [8]
+
+//! [9]
+void MainWindow::itemMoved(DiagramItem *movedItem,
+ const QPointF &oldPosition)
+{
+ undoStack->push(new MoveCommand(movedItem, oldPosition));
+}
+//! [9]
+
+//! [10]
+void MainWindow::deleteItem()
+{
+ if (diagramScene->selectedItems().isEmpty())
+ return;
+
+ QUndoCommand *deleteCommand = new DeleteCommand(diagramScene);
+ undoStack->push(deleteCommand);
+}
+//! [10]
+
+//! [11]
+void MainWindow::itemMenuAboutToHide()
+{
+ deleteAction->setEnabled(true);
+}
+//! [11]
+
+//! [12]
+void MainWindow::itemMenuAboutToShow()
+{
+ deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty());
+}
+//! [12]
+
+//! [13]
+void MainWindow::addBox()
+{
+ QUndoCommand *addCommand = new AddCommand(DiagramItem::Box, diagramScene);
+ undoStack->push(addCommand);
+}
+//! [13]
+
+//! [14]
+void MainWindow::addTriangle()
+{
+ QUndoCommand *addCommand = new AddCommand(DiagramItem::Triangle,
+ diagramScene);
+ undoStack->push(addCommand);
+}
+//! [14]
+
+//! [15]
+void MainWindow::about()
+{
+ QMessageBox::about(this, tr("About Undo"),
+ tr("The <b>Undo</b> example demonstrates how to "
+ "use Qt's undo framework."));
+}
+//! [15]
diff --git a/examples/tools/undoframework/mainwindow.h b/examples/tools/undoframework/mainwindow.h
new file mode 100644
index 0000000000..c21ffa0a5e
--- /dev/null
+++ b/examples/tools/undoframework/mainwindow.h
@@ -0,0 +1,99 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+
+QT_BEGIN_NAMESPACE
+class QAction;
+class QToolBar;
+class QMenu;
+class QUndoStack;
+class QUndoView;
+QT_END_NAMESPACE
+class DiagramScene;
+class DiagramItem;
+
+//! [0]
+class MainWindow : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ MainWindow();
+
+public slots:
+ void itemMoved(DiagramItem *movedDiagram, const QPointF &moveStartPosition);
+
+private slots:
+ void deleteItem();
+ void addBox();
+ void addTriangle();
+ void about();
+ void itemMenuAboutToShow();
+ void itemMenuAboutToHide();
+
+private:
+ void createActions();
+ void createMenus();
+ void createUndoView();
+
+ QAction *deleteAction;
+ QAction *addBoxAction;
+ QAction *addTriangleAction;
+ QAction *undoAction;
+ QAction *redoAction;
+ QAction *exitAction;
+ QAction *aboutAction;
+
+ QMenu *fileMenu;
+ QMenu *editMenu;
+ QMenu *itemMenu;
+ QMenu *helpMenu;
+
+ DiagramScene *diagramScene;
+ QUndoStack *undoStack;
+ QUndoView *undoView;
+};
+//! [0]
+
+#endif
diff --git a/examples/tools/undoframework/undoframework.pro b/examples/tools/undoframework/undoframework.pro
new file mode 100644
index 0000000000..0f03091ce0
--- /dev/null
+++ b/examples/tools/undoframework/undoframework.pro
@@ -0,0 +1,18 @@
+HEADERS = commands.h \
+ diagramitem.h \
+ diagramscene.h \
+ mainwindow.h
+SOURCES = commands.cpp \
+ diagramitem.cpp \
+ diagramscene.cpp \
+ main.cpp \
+ mainwindow.cpp
+RESOURCES = undoframework.qrc
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tools/undoframework
+sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS undoframework.pro README images
+sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/tools/undoframework
+INSTALLS += target sources
+
+symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)
diff --git a/examples/tools/undoframework/undoframework.qrc b/examples/tools/undoframework/undoframework.qrc
new file mode 100644
index 0000000000..6321d94d8d
--- /dev/null
+++ b/examples/tools/undoframework/undoframework.qrc
@@ -0,0 +1,6 @@
+<!DOCTYPE RCC><RCC version="1.0">
+ <qresource>
+ <file>images/cross.png</file>
+ </qresource>
+ </RCC>
+