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