1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 |
|
---|
44 | #include "mainwindow.h"
|
---|
45 | #include "diagramscene.h"
|
---|
46 | #include "diagramitem.h"
|
---|
47 | #include "commands.h"
|
---|
48 |
|
---|
49 | //! [0]
|
---|
50 | MainWindow::MainWindow()
|
---|
51 | {
|
---|
52 | undoStack = new QUndoStack();
|
---|
53 |
|
---|
54 | createActions();
|
---|
55 | createMenus();
|
---|
56 |
|
---|
57 | createUndoView();
|
---|
58 |
|
---|
59 | diagramScene = new DiagramScene();
|
---|
60 | QBrush pixmapBrush(QPixmap(":/images/cross.png").scaled(30, 30));
|
---|
61 | diagramScene->setBackgroundBrush(pixmapBrush);
|
---|
62 | diagramScene->setSceneRect(QRect(0, 0, 500, 500));
|
---|
63 |
|
---|
64 | connect(diagramScene, SIGNAL(itemMoved(DiagramItem *, const QPointF &)),
|
---|
65 | this, SLOT(itemMoved(DiagramItem *, const QPointF &)));
|
---|
66 |
|
---|
67 | setWindowTitle("Undo Framework");
|
---|
68 | QGraphicsView *view = new QGraphicsView(diagramScene);
|
---|
69 | setCentralWidget(view);
|
---|
70 | resize(700, 500);
|
---|
71 | }
|
---|
72 | //! [0]
|
---|
73 |
|
---|
74 | //! [1]
|
---|
75 | void MainWindow::createUndoView()
|
---|
76 | {
|
---|
77 | undoView = new QUndoView(undoStack);
|
---|
78 | undoView->setWindowTitle(tr("Command List"));
|
---|
79 | undoView->show();
|
---|
80 | undoView->setAttribute(Qt::WA_QuitOnClose, false);
|
---|
81 | }
|
---|
82 | //! [1]
|
---|
83 |
|
---|
84 | //! [2]
|
---|
85 | void MainWindow::createActions()
|
---|
86 | {
|
---|
87 | deleteAction = new QAction(tr("&Delete Item"), this);
|
---|
88 | deleteAction->setShortcut(tr("Del"));
|
---|
89 | connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
|
---|
90 | //! [2] //! [3]
|
---|
91 |
|
---|
92 | //! [3] //! [4]
|
---|
93 | addBoxAction = new QAction(tr("Add &Box"), this);
|
---|
94 | //! [4]
|
---|
95 | addBoxAction->setShortcut(tr("Ctrl+O"));
|
---|
96 | connect(addBoxAction, SIGNAL(triggered()), this, SLOT(addBox()));
|
---|
97 |
|
---|
98 | addTriangleAction = new QAction(tr("Add &Triangle"), this);
|
---|
99 | addTriangleAction->setShortcut(tr("Ctrl+T"));
|
---|
100 | connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle()));
|
---|
101 |
|
---|
102 | //! [5]
|
---|
103 | undoAction = undoStack->createUndoAction(this, tr("&Undo"));
|
---|
104 | undoAction->setShortcut(tr("Ctrl+Z"));
|
---|
105 |
|
---|
106 | redoAction = undoStack->createRedoAction(this, tr("&Redo"));
|
---|
107 | QList<QKeySequence> redoShortcuts;
|
---|
108 | redoShortcuts << tr("Ctrl+Y") << tr("Shift+Ctrl+Z");
|
---|
109 | redoAction->setShortcuts(redoShortcuts);
|
---|
110 | //! [5]
|
---|
111 |
|
---|
112 | exitAction = new QAction(tr("E&xit"), this);
|
---|
113 | exitAction->setShortcut(tr("Ctrl+Q"));
|
---|
114 | connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
|
---|
115 |
|
---|
116 | aboutAction = new QAction(tr("&About"), this);
|
---|
117 | QList<QKeySequence> aboutShortcuts;
|
---|
118 | aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B");
|
---|
119 | aboutAction->setShortcuts(aboutShortcuts);
|
---|
120 | connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
|
---|
121 | }
|
---|
122 |
|
---|
123 | //! [6]
|
---|
124 | void MainWindow::createMenus()
|
---|
125 | {
|
---|
126 | //! [6]
|
---|
127 | fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
128 | fileMenu->addAction(exitAction);
|
---|
129 |
|
---|
130 | //! [7]
|
---|
131 | editMenu = menuBar()->addMenu(tr("&Edit"));
|
---|
132 | editMenu->addAction(undoAction);
|
---|
133 | editMenu->addAction(redoAction);
|
---|
134 | editMenu->addSeparator();
|
---|
135 | editMenu->addAction(deleteAction);
|
---|
136 | connect(editMenu, SIGNAL(aboutToShow()),
|
---|
137 | this, SLOT(itemMenuAboutToShow()));
|
---|
138 | connect(editMenu, SIGNAL(aboutToHide()),
|
---|
139 | this, SLOT(itemMenuAboutToHide()));
|
---|
140 |
|
---|
141 | //! [7]
|
---|
142 | itemMenu = menuBar()->addMenu(tr("&Item"));
|
---|
143 | itemMenu->addAction(addBoxAction);
|
---|
144 | itemMenu->addAction(addTriangleAction);
|
---|
145 |
|
---|
146 | helpMenu = menuBar()->addMenu(tr("&About"));
|
---|
147 | helpMenu->addAction(aboutAction);
|
---|
148 | //! [8]
|
---|
149 | }
|
---|
150 | //! [8]
|
---|
151 |
|
---|
152 | //! [9]
|
---|
153 | void MainWindow::itemMoved(DiagramItem *movedItem,
|
---|
154 | const QPointF &oldPosition)
|
---|
155 | {
|
---|
156 | undoStack->push(new MoveCommand(movedItem, oldPosition));
|
---|
157 | }
|
---|
158 | //! [9]
|
---|
159 |
|
---|
160 | //! [10]
|
---|
161 | void MainWindow::deleteItem()
|
---|
162 | {
|
---|
163 | if (diagramScene->selectedItems().isEmpty())
|
---|
164 | return;
|
---|
165 |
|
---|
166 | QUndoCommand *deleteCommand = new DeleteCommand(diagramScene);
|
---|
167 | undoStack->push(deleteCommand);
|
---|
168 | }
|
---|
169 | //! [10]
|
---|
170 |
|
---|
171 | //! [11]
|
---|
172 | void MainWindow::itemMenuAboutToHide()
|
---|
173 | {
|
---|
174 | deleteAction->setEnabled(true);
|
---|
175 | }
|
---|
176 | //! [11]
|
---|
177 |
|
---|
178 | //! [12]
|
---|
179 | void MainWindow::itemMenuAboutToShow()
|
---|
180 | {
|
---|
181 | deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty());
|
---|
182 | }
|
---|
183 | //! [12]
|
---|
184 |
|
---|
185 | //! [13]
|
---|
186 | void MainWindow::addBox()
|
---|
187 | {
|
---|
188 | QUndoCommand *addCommand = new AddCommand(DiagramItem::Box, diagramScene);
|
---|
189 | undoStack->push(addCommand);
|
---|
190 | }
|
---|
191 | //! [13]
|
---|
192 |
|
---|
193 | //! [14]
|
---|
194 | void MainWindow::addTriangle()
|
---|
195 | {
|
---|
196 | QUndoCommand *addCommand = new AddCommand(DiagramItem::Triangle,
|
---|
197 | diagramScene);
|
---|
198 | undoStack->push(addCommand);
|
---|
199 | }
|
---|
200 | //! [14]
|
---|
201 |
|
---|
202 | //! [15]
|
---|
203 | void MainWindow::about()
|
---|
204 | {
|
---|
205 | QMessageBox::about(this, tr("About Undo"),
|
---|
206 | tr("The <b>Undo</b> example demonstrates how to "
|
---|
207 | "use Qt's undo framework."));
|
---|
208 | }
|
---|
209 | //! [15]
|
---|