summaryrefslogtreecommitdiffstats
path: root/examples/widgets/tools/undoframework
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/tools/undoframework')
-rw-r--r--examples/widgets/tools/undoframework/commands.cpp168
-rw-r--r--examples/widgets/tools/undoframework/commands.h104
-rw-r--r--examples/widgets/tools/undoframework/diagramitem.cpp64
-rw-r--r--examples/widgets/tools/undoframework/diagramitem.h71
-rw-r--r--examples/widgets/tools/undoframework/diagramscene.cpp76
-rw-r--r--examples/widgets/tools/undoframework/diagramscene.h74
-rw-r--r--examples/widgets/tools/undoframework/images/cross.pngbin0 -> 114 bytes
-rw-r--r--examples/widgets/tools/undoframework/main.cpp57
-rw-r--r--examples/widgets/tools/undoframework/mainwindow.cpp206
-rw-r--r--examples/widgets/tools/undoframework/mainwindow.h99
-rw-r--r--examples/widgets/tools/undoframework/undoframework.desktop11
-rw-r--r--examples/widgets/tools/undoframework/undoframework.pro20
-rw-r--r--examples/widgets/tools/undoframework/undoframework.qrc6
13 files changed, 956 insertions, 0 deletions
diff --git a/examples/widgets/tools/undoframework/commands.cpp b/examples/widgets/tools/undoframework/commands.cpp
new file mode 100644
index 0000000000..2a1fad95ea
--- /dev/null
+++ b/examples/widgets/tools/undoframework/commands.cpp
@@ -0,0 +1,168 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia Plc 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 <QtWidgets>
+
+#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/widgets/tools/undoframework/commands.h b/examples/widgets/tools/undoframework/commands.h
new file mode 100644
index 0000000000..7318668285
--- /dev/null
+++ b/examples/widgets/tools/undoframework/commands.h
@@ -0,0 +1,104 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia Plc 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:
+ explicit 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/widgets/tools/undoframework/diagramitem.cpp b/examples/widgets/tools/undoframework/diagramitem.cpp
new file mode 100644
index 0000000000..611069cd88
--- /dev/null
+++ b/examples/widgets/tools/undoframework/diagramitem.cpp
@@ -0,0 +1,64 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia Plc 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 <QtWidgets>
+
+#include "diagramitem.h"
+
+DiagramItem::DiagramItem(DiagramType diagramType, QGraphicsItem *item)
+ : QGraphicsPolygonItem(item)
+{
+ 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/widgets/tools/undoframework/diagramitem.h b/examples/widgets/tools/undoframework/diagramitem.h
new file mode 100644
index 0000000000..7b7fefa7f7
--- /dev/null
+++ b/examples/widgets/tools/undoframework/diagramitem.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia Plc 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 };
+
+ explicit DiagramItem(DiagramType diagramType, QGraphicsItem *item = 0);
+
+ DiagramType diagramType() const {
+ return polygon() == boxPolygon ? Box : Triangle;
+ }
+ int type() const { return Type; }
+
+private:
+ QPolygonF boxPolygon;
+ QPolygonF trianglePolygon;
+};
+
+#endif
diff --git a/examples/widgets/tools/undoframework/diagramscene.cpp b/examples/widgets/tools/undoframework/diagramscene.cpp
new file mode 100644
index 0000000000..2c40b71219
--- /dev/null
+++ b/examples/widgets/tools/undoframework/diagramscene.cpp
@@ -0,0 +1,76 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia Plc 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 <QtWidgets>
+
+#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());
+ const QList<QGraphicsItem *> itemList = items(mousePos);
+ movingItem = itemList.isEmpty() ? 0 : itemList.first();
+
+ 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/widgets/tools/undoframework/diagramscene.h b/examples/widgets/tools/undoframework/diagramscene.h
new file mode 100644
index 0000000000..e6150a3da0
--- /dev/null
+++ b/examples/widgets/tools/undoframework/diagramscene.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia Plc 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/widgets/tools/undoframework/images/cross.png b/examples/widgets/tools/undoframework/images/cross.png
new file mode 100644
index 0000000000..09e5e8c2ad
--- /dev/null
+++ b/examples/widgets/tools/undoframework/images/cross.png
Binary files differ
diff --git a/examples/widgets/tools/undoframework/main.cpp b/examples/widgets/tools/undoframework/main.cpp
new file mode 100644
index 0000000000..19ba6ec095
--- /dev/null
+++ b/examples/widgets/tools/undoframework/main.cpp
@@ -0,0 +1,57 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia Plc 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 <QtWidgets>
+
+#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/widgets/tools/undoframework/mainwindow.cpp b/examples/widgets/tools/undoframework/mainwindow.cpp
new file mode 100644
index 0000000000..6ac3818113
--- /dev/null
+++ b/examples/widgets/tools/undoframework/mainwindow.cpp
@@ -0,0 +1,206 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia Plc 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 <QtWidgets>
+
+#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/widgets/tools/undoframework/mainwindow.h b/examples/widgets/tools/undoframework/mainwindow.h
new file mode 100644
index 0000000000..f61b0abb1b
--- /dev/null
+++ b/examples/widgets/tools/undoframework/mainwindow.h
@@ -0,0 +1,99 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia Plc 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/widgets/tools/undoframework/undoframework.desktop b/examples/widgets/tools/undoframework/undoframework.desktop
new file mode 100644
index 0000000000..24b7f320f4
--- /dev/null
+++ b/examples/widgets/tools/undoframework/undoframework.desktop
@@ -0,0 +1,11 @@
+[Desktop Entry]
+Encoding=UTF-8
+Version=1.0
+Type=Application
+Terminal=false
+Name=Undo Framework
+Exec=/opt/usr/bin/undoframework
+Icon=undoframework
+X-Window-Icon=
+X-HildonDesk-ShowInToolbar=true
+X-Osso-Type=application/x-executable
diff --git a/examples/widgets/tools/undoframework/undoframework.pro b/examples/widgets/tools/undoframework/undoframework.pro
new file mode 100644
index 0000000000..d50247442a
--- /dev/null
+++ b/examples/widgets/tools/undoframework/undoframework.pro
@@ -0,0 +1,20 @@
+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]/tools/undoframework
+sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS undoframework.pro README images
+sources.path = $$[QT_INSTALL_EXAMPLES]/tools/undoframework
+INSTALLS += target sources
+
+QT += widgets
+
+simulator: warning(This example might not fully work on Simulator platform)
diff --git a/examples/widgets/tools/undoframework/undoframework.qrc b/examples/widgets/tools/undoframework/undoframework.qrc
new file mode 100644
index 0000000000..6321d94d8d
--- /dev/null
+++ b/examples/widgets/tools/undoframework/undoframework.qrc
@@ -0,0 +1,6 @@
+<!DOCTYPE RCC><RCC version="1.0">
+ <qresource>
+ <file>images/cross.png</file>
+ </qresource>
+ </RCC>
+