diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:18:55 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:18:55 (GMT) |
commit | e5fcad302d86d316390c6b0f62759a067313e8a9 (patch) | |
tree | c2afbf6f1066b6ce261f14341cf6d310e5595bc1 /examples/tools/undoframework | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'examples/tools/undoframework')
-rw-r--r-- | examples/tools/undoframework/commands.cpp | 163 | ||||
-rw-r--r-- | examples/tools/undoframework/commands.h | 104 | ||||
-rw-r--r-- | examples/tools/undoframework/diagramitem.cpp | 66 | ||||
-rw-r--r-- | examples/tools/undoframework/diagramitem.h | 73 | ||||
-rw-r--r-- | examples/tools/undoframework/diagramscene.cpp | 76 | ||||
-rw-r--r-- | examples/tools/undoframework/diagramscene.h | 75 | ||||
-rw-r--r-- | examples/tools/undoframework/images/cross.png | bin | 0 -> 114 bytes | |||
-rw-r--r-- | examples/tools/undoframework/main.cpp | 58 | ||||
-rw-r--r-- | examples/tools/undoframework/mainwindow.cpp | 209 | ||||
-rw-r--r-- | examples/tools/undoframework/mainwindow.h | 100 | ||||
-rw-r--r-- | examples/tools/undoframework/undoframework.pro | 16 | ||||
-rw-r--r-- | examples/tools/undoframework/undoframework.qrc | 6 |
12 files changed, 946 insertions, 0 deletions
diff --git a/examples/tools/undoframework/commands.cpp b/examples/tools/undoframework/commands.cpp new file mode 100644 index 0000000..78f0c16 --- /dev/null +++ b/examples/tools/undoframework/commands.cpp @@ -0,0 +1,163 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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] + +//! [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 0000000..17e4f3c --- /dev/null +++ b/examples/tools/undoframework/commands.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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); + + 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 0000000..1b4301e --- /dev/null +++ b/examples/tools/undoframework/diagramitem.cpp @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..68da876 --- /dev/null +++ b/examples/tools/undoframework/diagramitem.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..fcc63c9 --- /dev/null +++ b/examples/tools/undoframework/diagramscene.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..e1537cc --- /dev/null +++ b/examples/tools/undoframework/diagramscene.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 Binary files differnew file mode 100644 index 0000000..09e5e8c --- /dev/null +++ b/examples/tools/undoframework/images/cross.png diff --git a/examples/tools/undoframework/main.cpp b/examples/tools/undoframework/main.cpp new file mode 100644 index 0000000..170d292 --- /dev/null +++ b/examples/tools/undoframework/main.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..f912ff2 --- /dev/null +++ b/examples/tools/undoframework/mainwindow.cpp @@ -0,0 +1,209 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "mainwindow.h" +#include "diagramscene.h" +#include "diagramitem.h" +#include "commands.h" + +//! [0] +MainWindow::MainWindow() +{ + undoStack = new QUndoStack(); + + 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 *, const QPointF &)), + this, SLOT(itemMoved(DiagramItem *, const 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->setShortcut(tr("Ctrl+Z")); + + redoAction = undoStack->createRedoAction(this, tr("&Redo")); + QList<QKeySequence> redoShortcuts; + redoShortcuts << tr("Ctrl+Y") << tr("Shift+Ctrl+Z"); + redoAction->setShortcuts(redoShortcuts); +//! [5] + + exitAction = new QAction(tr("E&xit"), this); + exitAction->setShortcut(tr("Ctrl+Q")); + 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 0000000..5f08ffc --- /dev/null +++ b/examples/tools/undoframework/mainwindow.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..42010f5 --- /dev/null +++ b/examples/tools/undoframework/undoframework.pro @@ -0,0 +1,16 @@ +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 diff --git a/examples/tools/undoframework/undoframework.qrc b/examples/tools/undoframework/undoframework.qrc new file mode 100644 index 0000000..6321d94 --- /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> + |