1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "mainwindow.h"
|
---|
44 | #include "diagramscene.h"
|
---|
45 | #include "diagramitem.h"
|
---|
46 | #include "commands.h"
|
---|
47 |
|
---|
48 | //! [0]
|
---|
49 | MainWindow::MainWindow()
|
---|
50 | {
|
---|
51 | undoStack = new QUndoStack(this);
|
---|
52 |
|
---|
53 | createActions();
|
---|
54 | createMenus();
|
---|
55 |
|
---|
56 | createUndoView();
|
---|
57 |
|
---|
58 | diagramScene = new DiagramScene();
|
---|
59 | QBrush pixmapBrush(QPixmap(":/images/cross.png").scaled(30, 30));
|
---|
60 | diagramScene->setBackgroundBrush(pixmapBrush);
|
---|
61 | diagramScene->setSceneRect(QRect(0, 0, 500, 500));
|
---|
62 |
|
---|
63 | connect(diagramScene, SIGNAL(itemMoved(DiagramItem*,QPointF)),
|
---|
64 | this, SLOT(itemMoved(DiagramItem*,QPointF)));
|
---|
65 |
|
---|
66 | setWindowTitle("Undo Framework");
|
---|
67 | QGraphicsView *view = new QGraphicsView(diagramScene);
|
---|
68 | setCentralWidget(view);
|
---|
69 | resize(700, 500);
|
---|
70 | }
|
---|
71 | //! [0]
|
---|
72 |
|
---|
73 | //! [1]
|
---|
74 | void MainWindow::createUndoView()
|
---|
75 | {
|
---|
76 | undoView = new QUndoView(undoStack);
|
---|
77 | undoView->setWindowTitle(tr("Command List"));
|
---|
78 | undoView->show();
|
---|
79 | undoView->setAttribute(Qt::WA_QuitOnClose, false);
|
---|
80 | }
|
---|
81 | //! [1]
|
---|
82 |
|
---|
83 | //! [2]
|
---|
84 | void MainWindow::createActions()
|
---|
85 | {
|
---|
86 | deleteAction = new QAction(tr("&Delete Item"), this);
|
---|
87 | deleteAction->setShortcut(tr("Del"));
|
---|
88 | connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
|
---|
89 | //! [2] //! [3]
|
---|
90 |
|
---|
91 | //! [3] //! [4]
|
---|
92 | addBoxAction = new QAction(tr("Add &Box"), this);
|
---|
93 | //! [4]
|
---|
94 | addBoxAction->setShortcut(tr("Ctrl+O"));
|
---|
95 | connect(addBoxAction, SIGNAL(triggered()), this, SLOT(addBox()));
|
---|
96 |
|
---|
97 | addTriangleAction = new QAction(tr("Add &Triangle"), this);
|
---|
98 | addTriangleAction->setShortcut(tr("Ctrl+T"));
|
---|
99 | connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle()));
|
---|
100 |
|
---|
101 | //! [5]
|
---|
102 | undoAction = undoStack->createUndoAction(this, tr("&Undo"));
|
---|
103 | undoAction->setShortcuts(QKeySequence::Undo);
|
---|
104 |
|
---|
105 | redoAction = undoStack->createRedoAction(this, tr("&Redo"));
|
---|
106 | redoAction->setShortcuts(QKeySequence::Redo);
|
---|
107 | //! [5]
|
---|
108 |
|
---|
109 | exitAction = new QAction(tr("E&xit"), this);
|
---|
110 | exitAction->setShortcuts(QKeySequence::Quit);
|
---|
111 | connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
|
---|
112 |
|
---|
113 | aboutAction = new QAction(tr("&About"), this);
|
---|
114 | QList<QKeySequence> aboutShortcuts;
|
---|
115 | aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B");
|
---|
116 | aboutAction->setShortcuts(aboutShortcuts);
|
---|
117 | connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
|
---|
118 | }
|
---|
119 |
|
---|
120 | //! [6]
|
---|
121 | void MainWindow::createMenus()
|
---|
122 | {
|
---|
123 | //! [6]
|
---|
124 | fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
125 | fileMenu->addAction(exitAction);
|
---|
126 |
|
---|
127 | //! [7]
|
---|
128 | editMenu = menuBar()->addMenu(tr("&Edit"));
|
---|
129 | editMenu->addAction(undoAction);
|
---|
130 | editMenu->addAction(redoAction);
|
---|
131 | editMenu->addSeparator();
|
---|
132 | editMenu->addAction(deleteAction);
|
---|
133 | connect(editMenu, SIGNAL(aboutToShow()),
|
---|
134 | this, SLOT(itemMenuAboutToShow()));
|
---|
135 | connect(editMenu, SIGNAL(aboutToHide()),
|
---|
136 | this, SLOT(itemMenuAboutToHide()));
|
---|
137 |
|
---|
138 | //! [7]
|
---|
139 | itemMenu = menuBar()->addMenu(tr("&Item"));
|
---|
140 | itemMenu->addAction(addBoxAction);
|
---|
141 | itemMenu->addAction(addTriangleAction);
|
---|
142 |
|
---|
143 | helpMenu = menuBar()->addMenu(tr("&About"));
|
---|
144 | helpMenu->addAction(aboutAction);
|
---|
145 | //! [8]
|
---|
146 | }
|
---|
147 | //! [8]
|
---|
148 |
|
---|
149 | //! [9]
|
---|
150 | void MainWindow::itemMoved(DiagramItem *movedItem,
|
---|
151 | const QPointF &oldPosition)
|
---|
152 | {
|
---|
153 | undoStack->push(new MoveCommand(movedItem, oldPosition));
|
---|
154 | }
|
---|
155 | //! [9]
|
---|
156 |
|
---|
157 | //! [10]
|
---|
158 | void MainWindow::deleteItem()
|
---|
159 | {
|
---|
160 | if (diagramScene->selectedItems().isEmpty())
|
---|
161 | return;
|
---|
162 |
|
---|
163 | QUndoCommand *deleteCommand = new DeleteCommand(diagramScene);
|
---|
164 | undoStack->push(deleteCommand);
|
---|
165 | }
|
---|
166 | //! [10]
|
---|
167 |
|
---|
168 | //! [11]
|
---|
169 | void MainWindow::itemMenuAboutToHide()
|
---|
170 | {
|
---|
171 | deleteAction->setEnabled(true);
|
---|
172 | }
|
---|
173 | //! [11]
|
---|
174 |
|
---|
175 | //! [12]
|
---|
176 | void MainWindow::itemMenuAboutToShow()
|
---|
177 | {
|
---|
178 | deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty());
|
---|
179 | }
|
---|
180 | //! [12]
|
---|
181 |
|
---|
182 | //! [13]
|
---|
183 | void MainWindow::addBox()
|
---|
184 | {
|
---|
185 | QUndoCommand *addCommand = new AddCommand(DiagramItem::Box, diagramScene);
|
---|
186 | undoStack->push(addCommand);
|
---|
187 | }
|
---|
188 | //! [13]
|
---|
189 |
|
---|
190 | //! [14]
|
---|
191 | void MainWindow::addTriangle()
|
---|
192 | {
|
---|
193 | QUndoCommand *addCommand = new AddCommand(DiagramItem::Triangle,
|
---|
194 | diagramScene);
|
---|
195 | undoStack->push(addCommand);
|
---|
196 | }
|
---|
197 | //! [14]
|
---|
198 |
|
---|
199 | //! [15]
|
---|
200 | void MainWindow::about()
|
---|
201 | {
|
---|
202 | QMessageBox::about(this, tr("About Undo"),
|
---|
203 | tr("The <b>Undo</b> example demonstrates how to "
|
---|
204 | "use Qt's undo framework."));
|
---|
205 | }
|
---|
206 | //! [15]
|
---|