|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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->setShortcuts(QKeySequence::Undo); |
|
105 |
|
106 redoAction = undoStack->createRedoAction(this, tr("&Redo")); |
|
107 redoAction->setShortcuts(QKeySequence::Redo); |
|
108 //! [5] |
|
109 |
|
110 exitAction = new QAction(tr("E&xit"), this); |
|
111 exitAction->setShortcuts(QKeySequence::Quit); |
|
112 connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); |
|
113 |
|
114 aboutAction = new QAction(tr("&About"), this); |
|
115 QList<QKeySequence> aboutShortcuts; |
|
116 aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B"); |
|
117 aboutAction->setShortcuts(aboutShortcuts); |
|
118 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); |
|
119 } |
|
120 |
|
121 //! [6] |
|
122 void MainWindow::createMenus() |
|
123 { |
|
124 //! [6] |
|
125 fileMenu = menuBar()->addMenu(tr("&File")); |
|
126 fileMenu->addAction(exitAction); |
|
127 |
|
128 //! [7] |
|
129 editMenu = menuBar()->addMenu(tr("&Edit")); |
|
130 editMenu->addAction(undoAction); |
|
131 editMenu->addAction(redoAction); |
|
132 editMenu->addSeparator(); |
|
133 editMenu->addAction(deleteAction); |
|
134 connect(editMenu, SIGNAL(aboutToShow()), |
|
135 this, SLOT(itemMenuAboutToShow())); |
|
136 connect(editMenu, SIGNAL(aboutToHide()), |
|
137 this, SLOT(itemMenuAboutToHide())); |
|
138 |
|
139 //! [7] |
|
140 itemMenu = menuBar()->addMenu(tr("&Item")); |
|
141 itemMenu->addAction(addBoxAction); |
|
142 itemMenu->addAction(addTriangleAction); |
|
143 |
|
144 helpMenu = menuBar()->addMenu(tr("&About")); |
|
145 helpMenu->addAction(aboutAction); |
|
146 //! [8] |
|
147 } |
|
148 //! [8] |
|
149 |
|
150 //! [9] |
|
151 void MainWindow::itemMoved(DiagramItem *movedItem, |
|
152 const QPointF &oldPosition) |
|
153 { |
|
154 undoStack->push(new MoveCommand(movedItem, oldPosition)); |
|
155 } |
|
156 //! [9] |
|
157 |
|
158 //! [10] |
|
159 void MainWindow::deleteItem() |
|
160 { |
|
161 if (diagramScene->selectedItems().isEmpty()) |
|
162 return; |
|
163 |
|
164 QUndoCommand *deleteCommand = new DeleteCommand(diagramScene); |
|
165 undoStack->push(deleteCommand); |
|
166 } |
|
167 //! [10] |
|
168 |
|
169 //! [11] |
|
170 void MainWindow::itemMenuAboutToHide() |
|
171 { |
|
172 deleteAction->setEnabled(true); |
|
173 } |
|
174 //! [11] |
|
175 |
|
176 //! [12] |
|
177 void MainWindow::itemMenuAboutToShow() |
|
178 { |
|
179 deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty()); |
|
180 } |
|
181 //! [12] |
|
182 |
|
183 //! [13] |
|
184 void MainWindow::addBox() |
|
185 { |
|
186 QUndoCommand *addCommand = new AddCommand(DiagramItem::Box, diagramScene); |
|
187 undoStack->push(addCommand); |
|
188 } |
|
189 //! [13] |
|
190 |
|
191 //! [14] |
|
192 void MainWindow::addTriangle() |
|
193 { |
|
194 QUndoCommand *addCommand = new AddCommand(DiagramItem::Triangle, |
|
195 diagramScene); |
|
196 undoStack->push(addCommand); |
|
197 } |
|
198 //! [14] |
|
199 |
|
200 //! [15] |
|
201 void MainWindow::about() |
|
202 { |
|
203 QMessageBox::about(this, tr("About Undo"), |
|
204 tr("The <b>Undo</b> example demonstrates how to " |
|
205 "use Qt's undo framework.")); |
|
206 } |
|
207 //! [15] |