[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[2] | 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[2] | 15 | **
|
---|
[846] | 16 | ** GNU Free Documentation License
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
| 18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
| 20 | ** file.
|
---|
[2] | 21 | **
|
---|
[561] | 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at [email protected].
|
---|
[2] | 24 | ** $QT_END_LICENSE$
|
---|
| 25 | **
|
---|
| 26 | ****************************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
| 29 | \example tools/undoframework
|
---|
| 30 | \title Undo Framework Example
|
---|
| 31 |
|
---|
| 32 | This example shows how to implement undo/redo functionality
|
---|
| 33 | with the Qt undo framework.
|
---|
| 34 |
|
---|
| 35 | \image undoframeworkexample.png The Undo Diagram Example
|
---|
| 36 |
|
---|
| 37 | In the Qt undo framework, all actions that the user performs are
|
---|
| 38 | implemented in classes that inherit QUndoCommand. An undo command
|
---|
| 39 | class knows how to both \l{QUndoCommand::}{redo()} - or just do
|
---|
| 40 | the first time - and \l{QUndoCommand::}{undo()} an action. For
|
---|
| 41 | each action the user performs, a command is placed on a
|
---|
| 42 | QUndoStack. Since the stack contains all commands executed
|
---|
| 43 | (stacked in chronological order) on the document, it can roll the
|
---|
| 44 | state of the document backwards and forwards by undoing and redoing
|
---|
| 45 | its commands. See the \l{Overview of Qt's Undo Framework}{overview
|
---|
| 46 | document} for a high-level introduction to the undo framework.
|
---|
| 47 |
|
---|
| 48 | The undo example implements a simple diagram application. It is
|
---|
| 49 | possible to add and delete items, which are either box or
|
---|
| 50 | rectangular shaped, and move the items by dragging them with the
|
---|
| 51 | mouse. The undo stack is shown in a QUndoView, which is a list in
|
---|
| 52 | which the commands are shown as list items. Undo and redo are
|
---|
| 53 | available through the edit menu. The user can also select a command
|
---|
| 54 | from the undo view.
|
---|
| 55 |
|
---|
[846] | 56 | We use the \l{Graphics View Framework}{graphics view
|
---|
[2] | 57 | framework} to implement the diagram. We only treat the related
|
---|
| 58 | code briefly as the framework has examples of its own (e.g., the
|
---|
| 59 | \l{Diagram Scene Example}).
|
---|
| 60 |
|
---|
| 61 | The example consists of the following classes:
|
---|
| 62 |
|
---|
| 63 | \list
|
---|
| 64 | \o \c MainWindow is the main window and arranges the
|
---|
| 65 | example's widgets. It creates the commands based
|
---|
| 66 | on user input and keeps them on the command stack.
|
---|
| 67 | \o \c AddCommand adds an item to the scene.
|
---|
| 68 | \o \c DeleteCommand deletes an item from the scene.
|
---|
| 69 | \o \c MoveCommand when an item is moved the MoveCommand keeps record
|
---|
| 70 | of the start and stop positions of the move, and it
|
---|
| 71 | moves the item according to these when \c redo() and \c undo()
|
---|
| 72 | is called.
|
---|
| 73 | \o \c DiagramScene inherits QGraphicsScene and
|
---|
| 74 | emits signals for the \c MoveComands when an item is moved.
|
---|
| 75 | \o \c DiagramItem inherits QGraphicsPolygonItem and represents
|
---|
| 76 | an item in the diagram.
|
---|
| 77 | \endlist
|
---|
| 78 |
|
---|
| 79 | \section1 MainWindow Class Definition
|
---|
| 80 |
|
---|
| 81 | \snippet examples/tools/undoframework/mainwindow.h 0
|
---|
| 82 |
|
---|
| 83 | The \c MainWindow class maintains the undo stack, i.e., it creates
|
---|
| 84 | \l{QUndoCommand}s and pushes and pops them from the stack when it
|
---|
| 85 | receives the \c triggered() signal from \c undoAction and \c
|
---|
| 86 | redoAction.
|
---|
| 87 |
|
---|
| 88 | \section1 MainWindow Class Implementation
|
---|
| 89 |
|
---|
| 90 | We will start with a look at the constructor:
|
---|
| 91 |
|
---|
| 92 | \snippet examples/tools/undoframework/mainwindow.cpp 0
|
---|
| 93 |
|
---|
| 94 | In the constructor, we set up the DiagramScene and QGraphicsView.
|
---|
| 95 |
|
---|
| 96 | Here is the \c createUndoView() function:
|
---|
| 97 |
|
---|
| 98 | \snippet examples/tools/undoframework/mainwindow.cpp 1
|
---|
| 99 |
|
---|
| 100 | The QUndoView is a widget that display the text, which is set with
|
---|
| 101 | the \l{QUndoCommand::}{setText()} function, for each QUndoCommand
|
---|
| 102 | in the undo stack in a list.
|
---|
| 103 |
|
---|
| 104 | Here is the \c createActions() function:
|
---|
| 105 |
|
---|
| 106 | \snippet examples/tools/undoframework/mainwindow.cpp 2
|
---|
| 107 | \codeline
|
---|
| 108 | \snippet examples/tools/undoframework/mainwindow.cpp 3
|
---|
| 109 | \dots
|
---|
| 110 | \snippet examples/tools/undoframework/mainwindow.cpp 5
|
---|
| 111 |
|
---|
| 112 | The \c createActions() function sets up all the examples actions
|
---|
| 113 | in the manner shown above. The
|
---|
| 114 | \l{QUndoStack::}{createUndoAction()} and
|
---|
| 115 | \l{QUndoStack::}{createRedoAction()} helps us crate actions that
|
---|
| 116 | are disabled and enabled based on the state of the stack. Also,
|
---|
| 117 | the text of the action will be updated automatically based on the
|
---|
| 118 | \l{QUndoCommand::}{text()} of the undo commands. For the other
|
---|
| 119 | actions we have implemented slots in the \c MainWindow class.
|
---|
| 120 |
|
---|
| 121 | Here is the \c createMenus() function:
|
---|
| 122 |
|
---|
| 123 | \snippet examples/tools/undoframework/mainwindow.cpp 6
|
---|
| 124 |
|
---|
| 125 | \dots
|
---|
| 126 | \snippet examples/tools/undoframework/mainwindow.cpp 7
|
---|
| 127 | \dots
|
---|
| 128 | \snippet examples/tools/undoframework/mainwindow.cpp 8
|
---|
| 129 |
|
---|
| 130 | We have to use the QMenu \c aboutToShow() and \c aboutToHide()
|
---|
| 131 | signals since we only want \c deleteAction to be enabled when we
|
---|
| 132 | have selected an item.
|
---|
| 133 |
|
---|
| 134 | Here is the \c itemMoved() slot:
|
---|
| 135 |
|
---|
| 136 | \snippet examples/tools/undoframework/mainwindow.cpp 9
|
---|
| 137 |
|
---|
| 138 | We simply push a MoveCommand on the stack, which calls \c redo()
|
---|
| 139 | on it.
|
---|
| 140 |
|
---|
| 141 | Here is the \c deleteItem() slot:
|
---|
| 142 |
|
---|
| 143 | \snippet examples/tools/undoframework/mainwindow.cpp 10
|
---|
| 144 |
|
---|
| 145 | An item must be selected to be deleted. We need to check if it is
|
---|
| 146 | selected as the \c deleteAction may be enabled even if an item is
|
---|
| 147 | not selected. This can happen as we do not catch a signal or event
|
---|
| 148 | when an item is selected.
|
---|
| 149 |
|
---|
| 150 | Here is the \c itemMenuAboutToShow() and itemMenuAboutToHide() slots:
|
---|
| 151 |
|
---|
| |
---|