author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
#include <QLabel> |
|
43 |
||
44 |
#include "mainwindow.h" |
|
45 |
#include "diagramitem.h" |
|
46 |
#include "diagramscene.h" |
|
47 |
#include "diagramtextitem.h" |
|
48 |
||
49 |
const int InsertTextButton = 10; |
|
50 |
||
51 |
//! [0] |
|
52 |
MainWindow::MainWindow() |
|
53 |
{ |
|
54 |
createActions(); |
|
55 |
createToolBox(); |
|
56 |
createMenus(); |
|
57 |
||
58 |
scene = new DiagramScene(itemMenu); |
|
59 |
scene->setSceneRect(QRectF(0, 0, 5000, 5000)); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
60 |
connect(scene, SIGNAL(itemInserted(DiagramItem*)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
61 |
this, SLOT(itemInserted(DiagramItem*))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
62 |
connect(scene, SIGNAL(textInserted(QGraphicsTextItem*)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
63 |
this, SLOT(textInserted(QGraphicsTextItem*))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
64 |
connect(scene, SIGNAL(itemSelected(QGraphicsItem*)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
this, SLOT(itemSelected(QGraphicsItem*))); |
0 | 66 |
createToolbars(); |
67 |
||
68 |
QHBoxLayout *layout = new QHBoxLayout; |
|
69 |
layout->addWidget(toolBox); |
|
70 |
view = new QGraphicsView(scene); |
|
71 |
layout->addWidget(view); |
|
72 |
||
73 |
QWidget *widget = new QWidget; |
|
74 |
widget->setLayout(layout); |
|
75 |
||
76 |
setCentralWidget(widget); |
|
77 |
setWindowTitle(tr("Diagramscene")); |
|
78 |
setUnifiedTitleAndToolBarOnMac(true); |
|
79 |
} |
|
80 |
//! [0] |
|
81 |
||
82 |
//! [1] |
|
83 |
void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button) |
|
84 |
{ |
|
85 |
QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons(); |
|
86 |
foreach (QAbstractButton *myButton, buttons) { |
|
87 |
if (myButton != button) |
|
88 |
button->setChecked(false); |
|
89 |
} |
|
90 |
QString text = button->text(); |
|
91 |
if (text == tr("Blue Grid")) |
|
92 |
scene->setBackgroundBrush(QPixmap(":/images/background1.png")); |
|
93 |
else if (text == tr("White Grid")) |
|
94 |
scene->setBackgroundBrush(QPixmap(":/images/background2.png")); |
|
95 |
else if (text == tr("Gray Grid")) |
|
96 |
scene->setBackgroundBrush(QPixmap(":/images/background3.png")); |
|
97 |
else |
|
98 |
scene->setBackgroundBrush(QPixmap(":/images/background4.png")); |
|
99 |
||
100 |
scene->update(); |
|
101 |
view->update(); |
|
102 |
} |
|
103 |
//! [1] |
|
104 |
||
105 |
//! [2] |
|
106 |
void MainWindow::buttonGroupClicked(int id) |
|
107 |
{ |
|
108 |
QList<QAbstractButton *> buttons = buttonGroup->buttons(); |
|
109 |
foreach (QAbstractButton *button, buttons) { |
|
110 |
if (buttonGroup->button(id) != button) |
|
111 |
button->setChecked(false); |
|
112 |
} |
|
113 |
if (id == InsertTextButton) { |
|
114 |
scene->setMode(DiagramScene::InsertText); |
|
115 |
} else { |
|
116 |
scene->setItemType(DiagramItem::DiagramType(id)); |
|
117 |
scene->setMode(DiagramScene::InsertItem); |
|
118 |
} |
|
119 |
} |
|
120 |
//! [2] |
|
121 |
||
122 |
//! [3] |
|
123 |
void MainWindow::deleteItem() |
|
124 |
{ |
|
125 |
foreach (QGraphicsItem *item, scene->selectedItems()) { |
|
126 |
if (item->type() == DiagramItem::Type) { |
|
127 |
qgraphicsitem_cast<DiagramItem *>(item)->removeArrows(); |
|
128 |
} |
|
129 |
scene->removeItem(item); |
|
130 |
} |
|
131 |
} |
|
132 |
//! [3] |
|
133 |
||
134 |
//! [4] |
|
135 |
void MainWindow::pointerGroupClicked(int) |
|
136 |
{ |
|
137 |
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); |
|
138 |
} |
|
139 |
//! [4] |
|
140 |
||
141 |
//! [5] |
|
142 |
void MainWindow::bringToFront() |
|
143 |
{ |
|
144 |
if (scene->selectedItems().isEmpty()) |
|
145 |
return; |
|
146 |
||
147 |
QGraphicsItem *selectedItem = scene->selectedItems().first(); |
|
148 |
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); |
|
149 |
||
150 |
qreal zValue = 0; |
|
151 |
foreach (QGraphicsItem *item, overlapItems) { |
|
152 |
if (item->zValue() >= zValue && |
|
153 |
item->type() == DiagramItem::Type) |
|
154 |
zValue = item->zValue() + 0.1; |
|
155 |
} |
|
156 |
selectedItem->setZValue(zValue); |
|
157 |
} |
|
158 |
//! [5] |
|
159 |
||
160 |
//! [6] |
|
161 |
void MainWindow::sendToBack() |
|
162 |
{ |
|
163 |
if (scene->selectedItems().isEmpty()) |
|
164 |
return; |
|
165 |
||
166 |
QGraphicsItem *selectedItem = scene->selectedItems().first(); |
|
167 |
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); |
|
168 |
||
169 |
qreal zValue = 0; |
|
170 |
foreach (QGraphicsItem *item, overlapItems) { |
|
171 |
if (item->zValue() <= zValue && |
|
172 |
item->type() == DiagramItem::Type) |
|
173 |
zValue = item->zValue() - 0.1; |
|
174 |
} |
|
175 |
selectedItem->setZValue(zValue); |
|
176 |
} |
|
177 |
//! [6] |
|
178 |
||
179 |
//! [7] |
|
180 |
void MainWindow::itemInserted(DiagramItem *item) |
|
181 |
{ |
|
182 |
pointerTypeGroup->button(int(DiagramScene::MoveItem))->setChecked(true); |
|
183 |
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); |
|
184 |
buttonGroup->button(int(item->diagramType()))->setChecked(false); |
|
185 |
} |
|
186 |
//! [7] |
|
187 |
||
188 |
//! [8] |
|
189 |
void MainWindow::textInserted(QGraphicsTextItem *) |
|
190 |
{ |
|
191 |
buttonGroup->button(InsertTextButton)->setChecked(false); |
|
192 |
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); |
|
193 |
} |
|
194 |
//! [8] |
|
195 |
||
196 |
//! [9] |
|
197 |
void MainWindow::currentFontChanged(const QFont &) |
|
198 |
{ |
|
199 |
handleFontChange(); |
|
200 |
} |
|
201 |
//! [9] |
|
202 |
||
203 |
//! [10] |
|
204 |
void MainWindow::fontSizeChanged(const QString &) |
|
205 |
{ |
|
206 |
handleFontChange(); |
|
207 |
} |
|
208 |
//! [10] |
|
209 |
||
210 |
//! [11] |
|
211 |
void MainWindow::sceneScaleChanged(const QString &scale) |
|
212 |
{ |
|
213 |
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0; |
|
214 |
QMatrix oldMatrix = view->matrix(); |
|
215 |
view->resetMatrix(); |
|
216 |
view->translate(oldMatrix.dx(), oldMatrix.dy()); |
|
217 |
view->scale(newScale, newScale); |
|
218 |
} |
|
219 |
//! [11] |
|
220 |
||
221 |
//! [12] |
|
222 |
void MainWindow::textColorChanged() |
|
223 |
{ |
|
224 |
textAction = qobject_cast<QAction *>(sender()); |
|
225 |
fontColorToolButton->setIcon(createColorToolButtonIcon( |
|
226 |
":/images/textpointer.png", |
|
227 |
qVariantValue<QColor>(textAction->data()))); |
|
228 |
textButtonTriggered(); |
|
229 |
} |
|
230 |
//! [12] |
|
231 |
||
232 |
//! [13] |
|
233 |
void MainWindow::itemColorChanged() |
|
234 |
{ |
|
235 |
fillAction = qobject_cast<QAction *>(sender()); |
|
236 |
fillColorToolButton->setIcon(createColorToolButtonIcon( |
|
237 |
":/images/floodfill.png", |
|
238 |
qVariantValue<QColor>(fillAction->data()))); |
|
239 |
fillButtonTriggered(); |
|
240 |
} |
|
241 |
//! [13] |
|
242 |
||
243 |
//! [14] |
|
244 |
void MainWindow::lineColorChanged() |
|
245 |
{ |
|
246 |
lineAction = qobject_cast<QAction *>(sender()); |
|
247 |
lineColorToolButton->setIcon(createColorToolButtonIcon( |
|
248 |
":/images/linecolor.png", |
|
249 |
qVariantValue<QColor>(lineAction->data()))); |
|
250 |
lineButtonTriggered(); |
|
251 |
} |
|
252 |
//! [14] |
|
253 |
||
254 |
//! [15] |
|
255 |
void MainWindow::textButtonTriggered() |
|
256 |
{ |
|
257 |
scene->setTextColor(qVariantValue<QColor>(textAction->data())); |
|
258 |
} |
|
259 |
//! [15] |
|
260 |
||
261 |
//! [16] |
|
262 |
void MainWindow::fillButtonTriggered() |
|
263 |
{ |
|
264 |
scene->setItemColor(qVariantValue<QColor>(fillAction->data())); |
|
265 |
} |
|
266 |
//! [16] |
|
267 |
||
268 |
//! [17] |
|
269 |
void MainWindow::lineButtonTriggered() |
|
270 |
{ |
|
271 |
scene->setLineColor(qVariantValue<QColor>(lineAction->data())); |
|
272 |
} |
|
273 |
//! [17] |
|
274 |
||
275 |
//! [18] |
|
276 |
void MainWindow::handleFontChange() |
|
277 |
{ |
|
278 |
QFont font = fontCombo->currentFont(); |
|
279 |
font.setPointSize(fontSizeCombo->currentText().toInt()); |
|
280 |
font.setWeight(boldAction->isChecked() ? QFont::Bold : QFont::Normal); |
|
281 |
font.setItalic(italicAction->isChecked()); |
|
282 |
font.setUnderline(underlineAction->isChecked()); |
|
283 |
||
284 |
scene->setFont(font); |
|
285 |
} |
|
286 |
//! [18] |
|
287 |
||
288 |
//! [19] |
|
289 |
void MainWindow::itemSelected(QGraphicsItem *item) |
|
290 |
{ |
|
291 |
DiagramTextItem *textItem = |
|
292 |
qgraphicsitem_cast<DiagramTextItem *>(item); |
|
293 |
||
294 |
QFont font = textItem->font(); |
|
295 |
QColor color = textItem->defaultTextColor(); |
|
296 |
fontCombo->setCurrentFont(font); |
|
297 |
fontSizeCombo->setEditText(QString().setNum(font.pointSize())); |
|
298 |
boldAction->setChecked(font.weight() == QFont::Bold); |
|
299 |
italicAction->setChecked(font.italic()); |
|
300 |
underlineAction->setChecked(font.underline()); |
|
301 |
} |
|
302 |
//! [19] |
|
303 |
||
304 |
//! [20] |
|
305 |
void MainWindow::about() |
|
306 |
{ |
|
307 |
QMessageBox::about(this, tr("About Diagram Scene"), |
|
308 |
tr("The <b>Diagram Scene</b> example shows " |
|
309 |
"use of the graphics framework.")); |
|
310 |
} |
|
311 |
//! [20] |
|
312 |
||
313 |
//! [21] |
|
314 |
void MainWindow::createToolBox() |
|
315 |
{ |
|
316 |
buttonGroup = new QButtonGroup; |
|
317 |
buttonGroup->setExclusive(false); |
|
318 |
connect(buttonGroup, SIGNAL(buttonClicked(int)), |
|
319 |
this, SLOT(buttonGroupClicked(int))); |
|
320 |
QGridLayout *layout = new QGridLayout; |
|
321 |
layout->addWidget(createCellWidget(tr("Conditional"), |
|
322 |
DiagramItem::Conditional), 0, 0); |
|
323 |
layout->addWidget(createCellWidget(tr("Process"), |
|
324 |
DiagramItem::Step),0, 1); |
|
325 |
layout->addWidget(createCellWidget(tr("Input/Output"), |
|
326 |
DiagramItem::Io), 1, 0); |
|
327 |
//! [21] |
|
328 |
||
329 |
QToolButton *textButton = new QToolButton; |
|
330 |
textButton->setCheckable(true); |
|
331 |
buttonGroup->addButton(textButton, InsertTextButton); |
|
332 |
textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png") |
|
333 |
.scaled(30, 30))); |
|
334 |
textButton->setIconSize(QSize(50, 50)); |
|
335 |
QGridLayout *textLayout = new QGridLayout; |
|
336 |
textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter); |
|
337 |
textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter); |
|
338 |
QWidget *textWidget = new QWidget; |
|
339 |
textWidget->setLayout(textLayout); |
|
340 |
layout->addWidget(textWidget, 1, 1); |
|
341 |
||
342 |
layout->setRowStretch(3, 10); |
|
343 |
layout->setColumnStretch(2, 10); |
|
344 |
||
345 |
QWidget *itemWidget = new QWidget; |
|
346 |
itemWidget->setLayout(layout); |
|
347 |
||
348 |
backgroundButtonGroup = new QButtonGroup; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
349 |
connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton*)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
350 |
this, SLOT(backgroundButtonGroupClicked(QAbstractButton*))); |
0 | 351 |
|
352 |
QGridLayout *backgroundLayout = new QGridLayout; |
|
353 |
backgroundLayout->addWidget(createBackgroundCellWidget(tr("Blue Grid"), |
|
354 |
":/images/background1.png"), 0, 0); |
|
355 |
backgroundLayout->addWidget(createBackgroundCellWidget(tr("White Grid"), |
|
356 |
":/images/background2.png"), 0, 1); |
|
357 |
backgroundLayout->addWidget(createBackgroundCellWidget(tr("Gray Grid"), |
|
358 |
":/images/background3.png"), 1, 0); |
|
359 |
backgroundLayout->addWidget(createBackgroundCellWidget(tr("No Grid"), |
|
360 |
":/images/background4.png"), 1, 1); |
|
361 |
||
362 |
backgroundLayout->setRowStretch(2, 10); |
|
363 |
backgroundLayout->setColumnStretch(2, 10); |
|
364 |
||
365 |
QWidget *backgroundWidget = new QWidget; |
|
366 |
backgroundWidget->setLayout(backgroundLayout); |
|
367 |
||
368 |
||
369 |
//! [22] |
|
370 |
toolBox = new QToolBox; |
|
371 |
toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored)); |
|
372 |
toolBox->setMinimumWidth(itemWidget->sizeHint().width()); |
|
373 |
toolBox->addItem(itemWidget, tr("Basic Flowchart Shapes")); |
|
374 |
toolBox->addItem(backgroundWidget, tr("Backgrounds")); |
|
375 |
} |
|
376 |
//! [22] |
|
377 |
||
378 |
//! [23] |
|
379 |
void MainWindow::createActions() |
|
380 |
{ |
|
381 |
toFrontAction = new QAction(QIcon(":/images/bringtofront.png"), |
|
382 |
tr("Bring to &Front"), this); |
|
383 |
toFrontAction->setShortcut(tr("Ctrl+F")); |
|
384 |
toFrontAction->setStatusTip(tr("Bring item to front")); |
|
385 |
connect(toFrontAction, SIGNAL(triggered()), |
|
386 |
this, SLOT(bringToFront())); |
|
387 |
//! [23] |
|
388 |
||
389 |
sendBackAction = new QAction(QIcon(":/images/sendtoback.png"), |
|
390 |
tr("Send to &Back"), this); |
|
391 |
sendBackAction->setShortcut(tr("Ctrl+B")); |
|
392 |
sendBackAction->setStatusTip(tr("Send item to back")); |
|
393 |
connect(sendBackAction, SIGNAL(triggered()), |
|
394 |
this, SLOT(sendToBack())); |
|
395 |
||
396 |
deleteAction = new QAction(QIcon(":/images/delete.png"), |
|
397 |
tr("&Delete"), this); |
|
398 |
deleteAction->setShortcut(tr("Delete")); |
|
399 |
deleteAction->setStatusTip(tr("Delete item from diagram")); |
|
400 |
connect(deleteAction, SIGNAL(triggered()), |
|
401 |
this, SLOT(deleteItem())); |
|
402 |
||
403 |
exitAction = new QAction(tr("E&xit"), this); |
|
404 |
exitAction->setShortcuts(QKeySequence::Quit); |
|
405 |
exitAction->setStatusTip(tr("Quit Scenediagram example")); |
|
406 |
connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); |
|
407 |
||
408 |
boldAction = new QAction(tr("Bold"), this); |
|
409 |
boldAction->setCheckable(true); |
|
410 |
QPixmap pixmap(":/images/bold.png"); |
|
411 |
boldAction->setIcon(QIcon(pixmap)); |
|
412 |
boldAction->setShortcut(tr("Ctrl+B")); |
|
413 |
connect(boldAction, SIGNAL(triggered()), |
|
414 |
this, SLOT(handleFontChange())); |
|
415 |
||
416 |
italicAction = new QAction(QIcon(":/images/italic.png"), |
|
417 |
tr("Italic"), this); |
|
418 |
italicAction->setCheckable(true); |
|
419 |
italicAction->setShortcut(tr("Ctrl+I")); |
|
420 |
connect(italicAction, SIGNAL(triggered()), |
|
421 |
this, SLOT(handleFontChange())); |
|
422 |
||
423 |
underlineAction = new QAction(QIcon(":/images/underline.png"), |
|
424 |
tr("Underline"), this); |
|
425 |
underlineAction->setCheckable(true); |
|
426 |
underlineAction->setShortcut(tr("Ctrl+U")); |
|
427 |
connect(underlineAction, SIGNAL(triggered()), |
|
428 |
this, SLOT(handleFontChange())); |
|
429 |
||
430 |
aboutAction = new QAction(tr("A&bout"), this); |
|
431 |
aboutAction->setShortcut(tr("Ctrl+B")); |
|
432 |
connect(aboutAction, SIGNAL(triggered()), |
|
433 |
this, SLOT(about())); |
|
434 |
} |
|
435 |
||
436 |
//! [24] |
|
437 |
void MainWindow::createMenus() |
|
438 |
{ |
|
439 |
fileMenu = menuBar()->addMenu(tr("&File")); |
|
440 |
fileMenu->addAction(exitAction); |
|
441 |
||
442 |
itemMenu = menuBar()->addMenu(tr("&Item")); |
|
443 |
itemMenu->addAction(deleteAction); |
|
444 |
itemMenu->addSeparator(); |
|
445 |
itemMenu->addAction(toFrontAction); |
|
446 |
itemMenu->addAction(sendBackAction); |
|
447 |
||
448 |
aboutMenu = menuBar()->addMenu(tr("&Help")); |
|
449 |
aboutMenu->addAction(aboutAction); |
|
450 |
} |
|
451 |
//! [24] |
|
452 |
||
453 |
//! [25] |
|
454 |
void MainWindow::createToolbars() |
|
455 |
{ |
|
456 |
//! [25] |
|
457 |
editToolBar = addToolBar(tr("Edit")); |
|
458 |
editToolBar->addAction(deleteAction); |
|
459 |
editToolBar->addAction(toFrontAction); |
|
460 |
editToolBar->addAction(sendBackAction); |
|
461 |
||
462 |
fontCombo = new QFontComboBox(); |
|
463 |
fontSizeCombo = new QComboBox(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
464 |
connect(fontCombo, SIGNAL(currentFontChanged(QFont)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
465 |
this, SLOT(currentFontChanged(QFont))); |
0 | 466 |
|
467 |
fontSizeCombo = new QComboBox; |
|
468 |
fontSizeCombo->setEditable(true); |
|
469 |
for (int i = 8; i < 30; i = i + 2) |
|
470 |
fontSizeCombo->addItem(QString().setNum(i)); |
|
471 |
QIntValidator *validator = new QIntValidator(2, 64, this); |
|
472 |
fontSizeCombo->setValidator(validator); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
473 |
connect(fontSizeCombo, SIGNAL(currentIndexChanged(QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
474 |
this, SLOT(fontSizeChanged(QString))); |
0 | 475 |
|
476 |
fontColorToolButton = new QToolButton; |
|
477 |
fontColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); |
|
478 |
fontColorToolButton->setMenu(createColorMenu(SLOT(textColorChanged()), |
|
479 |
Qt::black)); |
|
480 |
textAction = fontColorToolButton->menu()->defaultAction(); |
|
481 |
fontColorToolButton->setIcon(createColorToolButtonIcon( |
|
482 |
":/images/textpointer.png", Qt::black)); |
|
483 |
fontColorToolButton->setAutoFillBackground(true); |
|
484 |
connect(fontColorToolButton, SIGNAL(clicked()), |
|
485 |
this, SLOT(textButtonTriggered())); |
|
486 |
||
487 |
//! [26] |
|
488 |
fillColorToolButton = new QToolButton; |
|
489 |
fillColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); |
|
490 |
fillColorToolButton->setMenu(createColorMenu(SLOT(itemColorChanged()), |
|
491 |
Qt::white)); |
|
492 |
fillAction = fillColorToolButton->menu()->defaultAction(); |
|
493 |
fillColorToolButton->setIcon(createColorToolButtonIcon( |
|
494 |
":/images/floodfill.png", Qt::white)); |
|
495 |
connect(fillColorToolButton, SIGNAL(clicked()), |
|
496 |
this, SLOT(fillButtonTriggered())); |
|
497 |
//! [26] |
|
498 |
||
499 |
lineColorToolButton = new QToolButton; |
|
500 |
lineColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); |
|
501 |
lineColorToolButton->setMenu(createColorMenu(SLOT(lineColorChanged()), |
|
502 |
Qt::black)); |
|
503 |
lineAction = lineColorToolButton->menu()->defaultAction(); |
|
504 |
lineColorToolButton->setIcon(createColorToolButtonIcon( |
|
505 |
":/images/linecolor.png", Qt::black)); |
|
506 |
connect(lineColorToolButton, SIGNAL(clicked()), |
|
507 |
this, SLOT(lineButtonTriggered())); |
|
508 |
||
509 |
textToolBar = addToolBar(tr("Font")); |
|
510 |
textToolBar->addWidget(fontCombo); |
|
511 |
textToolBar->addWidget(fontSizeCombo); |
|
512 |
textToolBar->addAction(boldAction); |
|
513 |
textToolBar->addAction(italicAction); |
|
514 |
textToolBar->addAction(underlineAction); |
|
515 |
||
516 |
colorToolBar = addToolBar(tr("Color")); |
|
517 |
colorToolBar->addWidget(fontColorToolButton); |
|
518 |
colorToolBar->addWidget(fillColorToolButton); |
|
519 |
colorToolBar->addWidget(lineColorToolButton); |
|
520 |
||
521 |
QToolButton *pointerButton = new QToolButton; |
|
522 |
pointerButton->setCheckable(true); |
|
523 |
pointerButton->setChecked(true); |
|
524 |
pointerButton->setIcon(QIcon(":/images/pointer.png")); |
|
525 |
QToolButton *linePointerButton = new QToolButton; |
|
526 |
linePointerButton->setCheckable(true); |
|
527 |
linePointerButton->setIcon(QIcon(":/images/linepointer.png")); |
|
528 |
||
529 |
pointerTypeGroup = new QButtonGroup; |
|
530 |
pointerTypeGroup->addButton(pointerButton, int(DiagramScene::MoveItem)); |
|
531 |
pointerTypeGroup->addButton(linePointerButton, |
|
532 |
int(DiagramScene::InsertLine)); |
|
533 |
connect(pointerTypeGroup, SIGNAL(buttonClicked(int)), |
|
534 |
this, SLOT(pointerGroupClicked(int))); |
|
535 |
||
536 |
sceneScaleCombo = new QComboBox; |
|
537 |
QStringList scales; |
|
538 |
scales << tr("50%") << tr("75%") << tr("100%") << tr("125%") << tr("150%"); |
|
539 |
sceneScaleCombo->addItems(scales); |
|
540 |
sceneScaleCombo->setCurrentIndex(2); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
541 |
connect(sceneScaleCombo, SIGNAL(currentIndexChanged(QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
542 |
this, SLOT(sceneScaleChanged(QString))); |
0 | 543 |
|
544 |
pointerToolbar = addToolBar(tr("Pointer type")); |
|
545 |
pointerToolbar->addWidget(pointerButton); |
|
546 |
pointerToolbar->addWidget(linePointerButton); |
|
547 |
pointerToolbar->addWidget(sceneScaleCombo); |
|
548 |
//! [27] |
|
549 |
} |
|
550 |
//! [27] |
|
551 |
||
552 |
//! [28] |
|
553 |
QWidget *MainWindow::createBackgroundCellWidget(const QString &text, |
|
554 |
const QString &image) |
|
555 |
{ |
|
556 |
QToolButton *button = new QToolButton; |
|
557 |
button->setText(text); |
|
558 |
button->setIcon(QIcon(image)); |
|
559 |
button->setIconSize(QSize(50, 50)); |
|
560 |
button->setCheckable(true); |
|
561 |
backgroundButtonGroup->addButton(button); |
|
562 |
||
563 |
QGridLayout *layout = new QGridLayout; |
|
564 |
layout->addWidget(button, 0, 0, Qt::AlignHCenter); |
|
565 |
layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); |
|
566 |
||
567 |
QWidget *widget = new QWidget; |
|
568 |
widget->setLayout(layout); |
|
569 |
||
570 |
return widget; |
|
571 |
} |
|
572 |
//! [28] |
|
573 |
||
574 |
//! [29] |
|
575 |
QWidget *MainWindow::createCellWidget(const QString &text, |
|
576 |
DiagramItem::DiagramType type) |
|
577 |
{ |
|
578 |
||
579 |
DiagramItem item(type, itemMenu); |
|
580 |
QIcon icon(item.image()); |
|
581 |
||
582 |
QToolButton *button = new QToolButton; |
|
583 |
button->setIcon(icon); |
|
584 |
button->setIconSize(QSize(50, 50)); |
|
585 |
button->setCheckable(true); |
|
586 |
buttonGroup->addButton(button, int(type)); |
|
587 |
||
588 |
QGridLayout *layout = new QGridLayout; |
|
589 |
layout->addWidget(button, 0, 0, Qt::AlignHCenter); |
|
590 |
layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); |
|
591 |
||
592 |
QWidget *widget = new QWidget; |
|
593 |
widget->setLayout(layout); |
|
594 |
||
595 |
return widget; |
|
596 |
} |
|
597 |
//! [29] |
|
598 |
||
599 |
//! [30] |
|
600 |
QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor) |
|
601 |
{ |
|
602 |
QList<QColor> colors; |
|
603 |
colors << Qt::black << Qt::white << Qt::red << Qt::blue << Qt::yellow; |
|
604 |
QStringList names; |
|
605 |
names << tr("black") << tr("white") << tr("red") << tr("blue") |
|
606 |
<< tr("yellow"); |
|
607 |
||
608 |
QMenu *colorMenu = new QMenu; |
|
609 |
for (int i = 0; i < colors.count(); ++i) { |
|
610 |
QAction *action = new QAction(names.at(i), this); |
|
611 |
action->setData(colors.at(i)); |
|
612 |
action->setIcon(createColorIcon(colors.at(i))); |
|
613 |
connect(action, SIGNAL(triggered()), |
|
614 |
this, slot); |
|
615 |
colorMenu->addAction(action); |
|
616 |
if (colors.at(i) == defaultColor) { |
|
617 |
colorMenu->setDefaultAction(action); |
|
618 |
} |
|
619 |
} |
|
620 |
return colorMenu; |
|
621 |
} |
|
622 |
//! [30] |
|
623 |
||
624 |
//! [31] |
|
625 |
QIcon MainWindow::createColorToolButtonIcon(const QString &imageFile, |
|
626 |
QColor color) |
|
627 |
{ |
|
628 |
QPixmap pixmap(50, 80); |
|
629 |
pixmap.fill(Qt::transparent); |
|
630 |
QPainter painter(&pixmap); |
|
631 |
QPixmap image(imageFile); |
|
632 |
QRect target(0, 0, 50, 60); |
|
633 |
QRect source(0, 0, 42, 42); |
|
634 |
painter.fillRect(QRect(0, 60, 50, 80), color); |
|
635 |
painter.drawPixmap(target, image, source); |
|
636 |
||
637 |
return QIcon(pixmap); |
|
638 |
} |
|
639 |
//! [31] |
|
640 |
||
641 |
//! [32] |
|
642 |
QIcon MainWindow::createColorIcon(QColor color) |
|
643 |
{ |
|
644 |
QPixmap pixmap(20, 20); |
|
645 |
QPainter painter(&pixmap); |
|
646 |
painter.setPen(Qt::NoPen); |
|
647 |
painter.fillRect(QRect(0, 0, 20, 20), color); |
|
648 |
||
649 |
return QIcon(pixmap); |
|
650 |
} |
|
651 |
//! [32] |