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