1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
42
#include "diagramitem.h"
43
#include "diagramscene.h"
44
#include "diagramtextitem.h"
45
#include "mainwindow.h"
49
const int InsertTextButton = 10;
52
MainWindow::MainWindow()
58
scene = new DiagramScene(itemMenu, this);
59
scene->setSceneRect(QRectF(0, 0, 5000, 5000));
60
connect(scene, SIGNAL(itemInserted(DiagramItem*)),
61
this, SLOT(itemInserted(DiagramItem*)));
62
connect(scene, SIGNAL(textInserted(QGraphicsTextItem*)),
63
this, SLOT(textInserted(QGraphicsTextItem*)));
64
connect(scene, SIGNAL(itemSelected(QGraphicsItem*)),
65
this, SLOT(itemSelected(QGraphicsItem*)));
68
QHBoxLayout *layout = new QHBoxLayout;
69
layout->addWidget(toolBox);
70
view = new QGraphicsView(scene);
71
layout->addWidget(view);
73
QWidget *widget = new QWidget;
74
widget->setLayout(layout);
76
setCentralWidget(widget);
77
setWindowTitle(tr("Diagramscene"));
78
setUnifiedTitleAndToolBarOnMac(true);
83
void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button)
85
QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons();
86
foreach (QAbstractButton *myButton, buttons) {
87
if (myButton != button)
88
button->setChecked(false);
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"));
98
scene->setBackgroundBrush(QPixmap(":/images/background4.png"));
106
void MainWindow::buttonGroupClicked(int id)
108
QList<QAbstractButton *> buttons = buttonGroup->buttons();
109
foreach (QAbstractButton *button, buttons) {
110
if (buttonGroup->button(id) != button)
111
button->setChecked(false);
113
if (id == InsertTextButton) {
114
scene->setMode(DiagramScene::InsertText);
116
scene->setItemType(DiagramItem::DiagramType(id));
117
scene->setMode(DiagramScene::InsertItem);
123
void MainWindow::deleteItem()
125
foreach (QGraphicsItem *item, scene->selectedItems()) {
126
if (item->type() == Arrow::Type) {
127
scene->removeItem(item);
128
Arrow *arrow = qgraphicsitem_cast<Arrow *>(item);
129
arrow->startItem()->removeArrow(arrow);
130
arrow->endItem()->removeArrow(arrow);
135
foreach (QGraphicsItem *item, scene->selectedItems()) {
136
if (item->type() == DiagramItem::Type)
137
qgraphicsitem_cast<DiagramItem *>(item)->removeArrows();
138
scene->removeItem(item);
145
void MainWindow::pointerGroupClicked(int)
147
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
152
void MainWindow::bringToFront()
154
if (scene->selectedItems().isEmpty())
157
QGraphicsItem *selectedItem = scene->selectedItems().first();
158
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
161
foreach (QGraphicsItem *item, overlapItems) {
162
if (item->zValue() >= zValue && item->type() == DiagramItem::Type)
163
zValue = item->zValue() + 0.1;
165
selectedItem->setZValue(zValue);
170
void MainWindow::sendToBack()
172
if (scene->selectedItems().isEmpty())
175
QGraphicsItem *selectedItem = scene->selectedItems().first();
176
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
179
foreach (QGraphicsItem *item, overlapItems) {
180
if (item->zValue() <= zValue && item->type() == DiagramItem::Type)
181
zValue = item->zValue() - 0.1;
183
selectedItem->setZValue(zValue);
188
void MainWindow::itemInserted(DiagramItem *item)
190
pointerTypeGroup->button(int(DiagramScene::MoveItem))->setChecked(true);
191
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
192
buttonGroup->button(int(item->diagramType()))->setChecked(false);
197
void MainWindow::textInserted(QGraphicsTextItem *)
199
buttonGroup->button(InsertTextButton)->setChecked(false);
200
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
205
void MainWindow::currentFontChanged(const QFont &)
212
void MainWindow::fontSizeChanged(const QString &)
219
void MainWindow::sceneScaleChanged(const QString &scale)
221
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
222
QMatrix oldMatrix = view->matrix();
224
view->translate(oldMatrix.dx(), oldMatrix.dy());
225
view->scale(newScale, newScale);
230
void MainWindow::textColorChanged()
232
textAction = qobject_cast<QAction *>(sender());
233
fontColorToolButton->setIcon(createColorToolButtonIcon(
234
":/images/textpointer.png",
235
qvariant_cast<QColor>(textAction->data())));
236
textButtonTriggered();
241
void MainWindow::itemColorChanged()
243
fillAction = qobject_cast<QAction *>(sender());
244
fillColorToolButton->setIcon(createColorToolButtonIcon(
245
":/images/floodfill.png",
246
qvariant_cast<QColor>(fillAction->data())));
247
fillButtonTriggered();
252
void MainWindow::lineColorChanged()
254
lineAction = qobject_cast<QAction *>(sender());
255
lineColorToolButton->setIcon(createColorToolButtonIcon(
256
":/images/linecolor.png",
257
qvariant_cast<QColor>(lineAction->data())));
258
lineButtonTriggered();
263
void MainWindow::textButtonTriggered()
265
scene->setTextColor(qvariant_cast<QColor>(textAction->data()));
270
void MainWindow::fillButtonTriggered()
272
scene->setItemColor(qvariant_cast<QColor>(fillAction->data()));
277
void MainWindow::lineButtonTriggered()
279
scene->setLineColor(qvariant_cast<QColor>(lineAction->data()));
284
void MainWindow::handleFontChange()
286
QFont font = fontCombo->currentFont();
287
font.setPointSize(fontSizeCombo->currentText().toInt());
288
font.setWeight(boldAction->isChecked() ? QFont::Bold : QFont::Normal);
289
font.setItalic(italicAction->isChecked());
290
font.setUnderline(underlineAction->isChecked());
292
scene->setFont(font);
297
void MainWindow::itemSelected(QGraphicsItem *item)
299
DiagramTextItem *textItem =
300
qgraphicsitem_cast<DiagramTextItem *>(item);
302
QFont font = textItem->font();
303
fontCombo->setCurrentFont(font);
304
fontSizeCombo->setEditText(QString().setNum(font.pointSize()));
305
boldAction->setChecked(font.weight() == QFont::Bold);
306
italicAction->setChecked(font.italic());
307
underlineAction->setChecked(font.underline());
312
void MainWindow::about()
314
QMessageBox::about(this, tr("About Diagram Scene"),
315
tr("The <b>Diagram Scene</b> example shows "
316
"use of the graphics framework."));
321
void MainWindow::createToolBox()
323
buttonGroup = new QButtonGroup(this);
324
buttonGroup->setExclusive(false);
325
connect(buttonGroup, SIGNAL(buttonClicked(int)),
326
this, SLOT(buttonGroupClicked(int)));
327
QGridLayout *layout = new QGridLayout;
328
layout->addWidget(createCellWidget(tr("Conditional"), DiagramItem::Conditional), 0, 0);
329
layout->addWidget(createCellWidget(tr("Process"), DiagramItem::Step),0, 1);
330
layout->addWidget(createCellWidget(tr("Input/Output"), DiagramItem::Io), 1, 0);
333
QToolButton *textButton = new QToolButton;
334
textButton->setCheckable(true);
335
buttonGroup->addButton(textButton, InsertTextButton);
336
textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png")));
337
textButton->setIconSize(QSize(50, 50));
338
QGridLayout *textLayout = new QGridLayout;
339
textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter);
340
textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter);
341
QWidget *textWidget = new QWidget;
342
textWidget->setLayout(textLayout);
343
layout->addWidget(textWidget, 1, 1);
345
layout->setRowStretch(3, 10);
346
layout->setColumnStretch(2, 10);
348
QWidget *itemWidget = new QWidget;
349
itemWidget->setLayout(layout);
351
backgroundButtonGroup = new QButtonGroup(this);
352
connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton*)),
353
this, SLOT(backgroundButtonGroupClicked(QAbstractButton*)));
355
QGridLayout *backgroundLayout = new QGridLayout;
356
backgroundLayout->addWidget(createBackgroundCellWidget(tr("Blue Grid"),
357
":/images/background1.png"), 0, 0);
358
backgroundLayout->addWidget(createBackgroundCellWidget(tr("White Grid"),
359
":/images/background2.png"), 0, 1);
360
backgroundLayout->addWidget(createBackgroundCellWidget(tr("Gray Grid"),
361
":/images/background3.png"), 1, 0);
362
backgroundLayout->addWidget(createBackgroundCellWidget(tr("No Grid"),
363
":/images/background4.png"), 1, 1);
365
backgroundLayout->setRowStretch(2, 10);
366
backgroundLayout->setColumnStretch(2, 10);
368
QWidget *backgroundWidget = new QWidget;
369
backgroundWidget->setLayout(backgroundLayout);
373
toolBox = new QToolBox;
374
toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored));
375
toolBox->setMinimumWidth(itemWidget->sizeHint().width());
376
toolBox->addItem(itemWidget, tr("Basic Flowchart Shapes"));
377
toolBox->addItem(backgroundWidget, tr("Backgrounds"));
382
void MainWindow::createActions()
384
toFrontAction = new QAction(QIcon(":/images/bringtofront.png"),
385
tr("Bring to &Front"), this);
386
toFrontAction->setShortcut(tr("Ctrl+F"));
387
toFrontAction->setStatusTip(tr("Bring item to front"));
388
connect(toFrontAction, SIGNAL(triggered()), this, SLOT(bringToFront()));
391
sendBackAction = new QAction(QIcon(":/images/sendtoback.png"), tr("Send to &Back"), this);
392
sendBackAction->setShortcut(tr("Ctrl+B"));
393
sendBackAction->setStatusTip(tr("Send item to back"));
394
connect(sendBackAction, SIGNAL(triggered()), this, SLOT(sendToBack()));
396
deleteAction = new QAction(QIcon(":/images/delete.png"), tr("&Delete"), this);
397
deleteAction->setShortcut(tr("Delete"));
398
deleteAction->setStatusTip(tr("Delete item from diagram"));
399
connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
401
exitAction = new QAction(tr("E&xit"), this);
402
exitAction->setShortcuts(QKeySequence::Quit);
403
exitAction->setStatusTip(tr("Quit Scenediagram example"));
404
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
406
boldAction = new QAction(tr("Bold"), this);
407
boldAction->setCheckable(true);
408
QPixmap pixmap(":/images/bold.png");
409
boldAction->setIcon(QIcon(pixmap));
410
boldAction->setShortcut(tr("Ctrl+B"));
411
connect(boldAction, SIGNAL(triggered()), this, SLOT(handleFontChange()));
413
italicAction = new QAction(QIcon(":/images/italic.png"), tr("Italic"), this);
414
italicAction->setCheckable(true);
415
italicAction->setShortcut(tr("Ctrl+I"));
416
connect(italicAction, SIGNAL(triggered()), this, SLOT(handleFontChange()));
418
underlineAction = new QAction(QIcon(":/images/underline.png"), tr("Underline"), this);
419
underlineAction->setCheckable(true);
420
underlineAction->setShortcut(tr("Ctrl+U"));
421
connect(underlineAction, SIGNAL(triggered()), this, SLOT(handleFontChange()));
423
aboutAction = new QAction(tr("A&bout"), this);
424
aboutAction->setShortcut(tr("Ctrl+B"));
425
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
429
void MainWindow::createMenus()
431
fileMenu = menuBar()->addMenu(tr("&File"));
432
fileMenu->addAction(exitAction);
434
itemMenu = menuBar()->addMenu(tr("&Item"));
435
itemMenu->addAction(deleteAction);
436
itemMenu->addSeparator();
437
itemMenu->addAction(toFrontAction);
438
itemMenu->addAction(sendBackAction);
440
aboutMenu = menuBar()->addMenu(tr("&Help"));
441
aboutMenu->addAction(aboutAction);
446
void MainWindow::createToolbars()
449
editToolBar = addToolBar(tr("Edit"));
450
editToolBar->addAction(deleteAction);
451
editToolBar->addAction(toFrontAction);
452
editToolBar->addAction(sendBackAction);
454
fontCombo = new QFontComboBox();
455
connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
456
this, SLOT(currentFontChanged(QFont)));
458
fontSizeCombo = new QComboBox;
459
fontSizeCombo->setEditable(true);
460
for (int i = 8; i < 30; i = i + 2)
461
fontSizeCombo->addItem(QString().setNum(i));
462
QIntValidator *validator = new QIntValidator(2, 64, this);
463
fontSizeCombo->setValidator(validator);
464
connect(fontSizeCombo, SIGNAL(currentIndexChanged(QString)),
465
this, SLOT(fontSizeChanged(QString)));
467
fontColorToolButton = new QToolButton;
468
fontColorToolButton->setPopupMode(QToolButton::MenuButtonPopup);
469
fontColorToolButton->setMenu(createColorMenu(SLOT(textColorChanged()), Qt::black));
470
textAction = fontColorToolButton->menu()->defaultAction();
471
fontColorToolButton->setIcon(createColorToolButtonIcon(":/images/textpointer.png", Qt::black));
472
fontColorToolButton->setAutoFillBackground(true);
473
connect(fontColorToolButton, SIGNAL(clicked()),
474
this, SLOT(textButtonTriggered()));
477
fillColorToolButton = new QToolButton;
478
fillColorToolButton->setPopupMode(QToolButton::MenuButtonPopup);
479
fillColorToolButton->setMenu(createColorMenu(SLOT(itemColorChanged()), Qt::white));
480
fillAction = fillColorToolButton->menu()->defaultAction();
481
fillColorToolButton->setIcon(createColorToolButtonIcon(
482
":/images/floodfill.png", Qt::white));
483
connect(fillColorToolButton, SIGNAL(clicked()),
484
this, SLOT(fillButtonTriggered()));
487
lineColorToolButton = new QToolButton;
488
lineColorToolButton->setPopupMode(QToolButton::MenuButtonPopup);
489
lineColorToolButton->setMenu(createColorMenu(SLOT(lineColorChanged()), Qt::black));
490
lineAction = lineColorToolButton->menu()->defaultAction();
491
lineColorToolButton->setIcon(createColorToolButtonIcon(
492
":/images/linecolor.png", Qt::black));
493
connect(lineColorToolButton, SIGNAL(clicked()),
494
this, SLOT(lineButtonTriggered()));
496
textToolBar = addToolBar(tr("Font"));
497
textToolBar->addWidget(fontCombo);
498
textToolBar->addWidget(fontSizeCombo);
499
textToolBar->addAction(boldAction);
500
textToolBar->addAction(italicAction);
501
textToolBar->addAction(underlineAction);
503
colorToolBar = addToolBar(tr("Color"));
504
colorToolBar->addWidget(fontColorToolButton);
505
colorToolBar->addWidget(fillColorToolButton);
506
colorToolBar->addWidget(lineColorToolButton);
508
QToolButton *pointerButton = new QToolButton;
509
pointerButton->setCheckable(true);
510
pointerButton->setChecked(true);
511
pointerButton->setIcon(QIcon(":/images/pointer.png"));
512
QToolButton *linePointerButton = new QToolButton;
513
linePointerButton->setCheckable(true);
514
linePointerButton->setIcon(QIcon(":/images/linepointer.png"));
516
pointerTypeGroup = new QButtonGroup(this);
517
pointerTypeGroup->addButton(pointerButton, int(DiagramScene::MoveItem));
518
pointerTypeGroup->addButton(linePointerButton, int(DiagramScene::InsertLine));
519
connect(pointerTypeGroup, SIGNAL(buttonClicked(int)),
520
this, SLOT(pointerGroupClicked(int)));
522
sceneScaleCombo = new QComboBox;
524
scales << tr("50%") << tr("75%") << tr("100%") << tr("125%") << tr("150%");
525
sceneScaleCombo->addItems(scales);
526
sceneScaleCombo->setCurrentIndex(2);
527
connect(sceneScaleCombo, SIGNAL(currentIndexChanged(QString)),
528
this, SLOT(sceneScaleChanged(QString)));
530
pointerToolbar = addToolBar(tr("Pointer type"));
531
pointerToolbar->addWidget(pointerButton);
532
pointerToolbar->addWidget(linePointerButton);
533
pointerToolbar->addWidget(sceneScaleCombo);
539
QWidget *MainWindow::createBackgroundCellWidget(const QString &text, const QString &image)
541
QToolButton *button = new QToolButton;
542
button->setText(text);
543
button->setIcon(QIcon(image));
544
button->setIconSize(QSize(50, 50));
545
button->setCheckable(true);
546
backgroundButtonGroup->addButton(button);
548
QGridLayout *layout = new QGridLayout;
549
layout->addWidget(button, 0, 0, Qt::AlignHCenter);
550
layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter);
552
QWidget *widget = new QWidget;
553
widget->setLayout(layout);
560
QWidget *MainWindow::createCellWidget(const QString &text, DiagramItem::DiagramType type)
563
DiagramItem item(type, itemMenu);
564
QIcon icon(item.image());
566
QToolButton *button = new QToolButton;
567
button->setIcon(icon);
568
button->setIconSize(QSize(50, 50));
569
button->setCheckable(true);
570
buttonGroup->addButton(button, int(type));
572
QGridLayout *layout = new QGridLayout;
573
layout->addWidget(button, 0, 0, Qt::AlignHCenter);
574
layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter);
576
QWidget *widget = new QWidget;
577
widget->setLayout(layout);
584
QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor)
586
QList<QColor> colors;
587
colors << Qt::black << Qt::white << Qt::red << Qt::blue << Qt::yellow;
589
names << tr("black") << tr("white") << tr("red") << tr("blue")
592
QMenu *colorMenu = new QMenu(this);
593
for (int i = 0; i < colors.count(); ++i) {
594
QAction *action = new QAction(names.at(i), this);
595
action->setData(colors.at(i));
596
action->setIcon(createColorIcon(colors.at(i)));
597
connect(action, SIGNAL(triggered()), this, slot);
598
colorMenu->addAction(action);
599
if (colors.at(i) == defaultColor)
600
colorMenu->setDefaultAction(action);
607
QIcon MainWindow::createColorToolButtonIcon(const QString &imageFile, QColor color)
609
QPixmap pixmap(50, 80);
610
pixmap.fill(Qt::transparent);
611
QPainter painter(&pixmap);
612
QPixmap image(imageFile);
613
// Draw icon centred horizontally on button.
614
QRect target(4, 0, 42, 43);
615
QRect source(0, 0, 42, 43);
616
painter.fillRect(QRect(0, 60, 50, 80), color);
617
painter.drawPixmap(target, image, source);
619
return QIcon(pixmap);
624
QIcon MainWindow::createColorIcon(QColor color)
626
QPixmap pixmap(20, 20);
627
QPainter painter(&pixmap);
628
painter.setPen(Qt::NoPen);
629
painter.fillRect(QRect(0, 0, 20, 20), color);
631
return QIcon(pixmap);