source: trunk/examples/graphicsview/diagramscene/mainwindow.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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