~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/widgets/graphicsview/diagramscene/mainwindow.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
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
 
19
**     distribution.
 
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.
 
23
**
 
24
**
 
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."
 
36
**
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
#include "arrow.h"
 
42
#include "diagramitem.h"
 
43
#include "diagramscene.h"
 
44
#include "diagramtextitem.h"
 
45
#include "mainwindow.h"
 
46
 
 
47
#include <QtWidgets>
 
48
 
 
49
const int InsertTextButton = 10;
 
50
 
 
51
//! [0]
 
52
MainWindow::MainWindow()
 
53
{
 
54
    createActions();
 
55
    createToolBox();
 
56
    createMenus();
 
57
 
 
58
    scene = new DiagramScene(itemMenu, 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*)));
 
66
    createToolbars();
 
67
 
 
68
    QHBoxLayout *layout = new QHBoxLayout;
 
69
    layout->addWidget(toolBox);
 
70
    view = new QGraphicsView(scene);
 
71
    layout->addWidget(view);
 
72
 
 
73
    QWidget *widget = new QWidget;
 
74
    widget->setLayout(layout);
 
75
 
 
76
    setCentralWidget(widget);
 
77
    setWindowTitle(tr("Diagramscene"));
 
78
    setUnifiedTitleAndToolBarOnMac(true);
 
79
}
 
80
//! [0]
 
81
 
 
82
//! [1]
 
83
void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button)
 
84
{
 
85
    QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons();
 
86
    foreach (QAbstractButton *myButton, buttons) {
 
87
        if (myButton != button)
 
88
            button->setChecked(false);
 
89
    }
 
90
    QString text = button->text();
 
91
    if (text == tr("Blue Grid"))
 
92
        scene->setBackgroundBrush(QPixmap(":/images/background1.png"));
 
93
    else if (text == tr("White Grid"))
 
94
        scene->setBackgroundBrush(QPixmap(":/images/background2.png"));
 
95
    else if (text == tr("Gray Grid"))
 
96
        scene->setBackgroundBrush(QPixmap(":/images/background3.png"));
 
97
    else
 
98
        scene->setBackgroundBrush(QPixmap(":/images/background4.png"));
 
99
 
 
100
    scene->update();
 
101
    view->update();
 
102
}
 
103
//! [1]
 
104
 
 
105
//! [2]
 
106
void MainWindow::buttonGroupClicked(int id)
 
107
{
 
108
    QList<QAbstractButton *> buttons = buttonGroup->buttons();
 
109
    foreach (QAbstractButton *button, buttons) {
 
110
        if (buttonGroup->button(id) != button)
 
111
            button->setChecked(false);
 
112
    }
 
113
    if (id == InsertTextButton) {
 
114
        scene->setMode(DiagramScene::InsertText);
 
115
    } else {
 
116
        scene->setItemType(DiagramItem::DiagramType(id));
 
117
        scene->setMode(DiagramScene::InsertItem);
 
118
    }
 
119
}
 
120
//! [2]
 
121
 
 
122
//! [3]
 
123
void MainWindow::deleteItem()
 
124
{
 
125
    foreach (QGraphicsItem *item, scene->selectedItems()) {
 
126
        if (item->type() == Arrow::Type) {
 
127
            scene->removeItem(item);
 
128
            Arrow *arrow = qgraphicsitem_cast<Arrow *>(item);
 
129
            arrow->startItem()->removeArrow(arrow);
 
130
            arrow->endItem()->removeArrow(arrow);
 
131
            delete item;
 
132
        }
 
133
    }
 
134
 
 
135
    foreach (QGraphicsItem *item, scene->selectedItems()) {
 
136
         if (item->type() == DiagramItem::Type)
 
137
             qgraphicsitem_cast<DiagramItem *>(item)->removeArrows();
 
138
         scene->removeItem(item);
 
139
         delete item;
 
140
     }
 
141
}
 
142
//! [3]
 
143
 
 
144
//! [4]
 
145
void MainWindow::pointerGroupClicked(int)
 
146
{
 
147
    scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
 
148
}
 
149
//! [4]
 
150
 
 
151
//! [5]
 
152
void MainWindow::bringToFront()
 
153
{
 
154
    if (scene->selectedItems().isEmpty())
 
155
        return;
 
156
 
 
157
    QGraphicsItem *selectedItem = scene->selectedItems().first();
 
158
    QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
 
159
 
 
160
    qreal zValue = 0;
 
161
    foreach (QGraphicsItem *item, overlapItems) {
 
162
        if (item->zValue() >= zValue && item->type() == DiagramItem::Type)
 
163
            zValue = item->zValue() + 0.1;
 
164
    }
 
165
    selectedItem->setZValue(zValue);
 
166
}
 
167
//! [5]
 
168
 
 
169
//! [6]
 
170
void MainWindow::sendToBack()
 
171
{
 
172
    if (scene->selectedItems().isEmpty())
 
173
        return;
 
174
 
 
175
    QGraphicsItem *selectedItem = scene->selectedItems().first();
 
176
    QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
 
177
 
 
178
    qreal zValue = 0;
 
179
    foreach (QGraphicsItem *item, overlapItems) {
 
180
        if (item->zValue() <= zValue && item->type() == DiagramItem::Type)
 
181
            zValue = item->zValue() - 0.1;
 
182
    }
 
183
    selectedItem->setZValue(zValue);
 
184
}
 
185
//! [6]
 
186
 
 
187
//! [7]
 
188
void MainWindow::itemInserted(DiagramItem *item)
 
189
{
 
190
    pointerTypeGroup->button(int(DiagramScene::MoveItem))->setChecked(true);
 
191
    scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
 
192
    buttonGroup->button(int(item->diagramType()))->setChecked(false);
 
193
}
 
194
//! [7]
 
195
 
 
196
//! [8]
 
197
void MainWindow::textInserted(QGraphicsTextItem *)
 
198
{
 
199
    buttonGroup->button(InsertTextButton)->setChecked(false);
 
200
    scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
 
201
}
 
202
//! [8]
 
203
 
 
204
//! [9]
 
205
void MainWindow::currentFontChanged(const QFont &)
 
206
{
 
207
    handleFontChange();
 
208
}
 
209
//! [9]
 
210
 
 
211
//! [10]
 
212
void MainWindow::fontSizeChanged(const QString &)
 
213
{
 
214
    handleFontChange();
 
215
}
 
216
//! [10]
 
217
 
 
218
//! [11]
 
219
void MainWindow::sceneScaleChanged(const QString &scale)
 
220
{
 
221
    double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
 
222
    QMatrix oldMatrix = view->matrix();
 
223
    view->resetMatrix();
 
224
    view->translate(oldMatrix.dx(), oldMatrix.dy());
 
225
    view->scale(newScale, newScale);
 
226
}
 
227
//! [11]
 
228
 
 
229
//! [12]
 
230
void MainWindow::textColorChanged()
 
231
{
 
232
    textAction = qobject_cast<QAction *>(sender());
 
233
    fontColorToolButton->setIcon(createColorToolButtonIcon(
 
234
                                     ":/images/textpointer.png",
 
235
                                     qvariant_cast<QColor>(textAction->data())));
 
236
    textButtonTriggered();
 
237
}
 
238
//! [12]
 
239
 
 
240
//! [13]
 
241
void MainWindow::itemColorChanged()
 
242
{
 
243
    fillAction = qobject_cast<QAction *>(sender());
 
244
    fillColorToolButton->setIcon(createColorToolButtonIcon(
 
245
                                     ":/images/floodfill.png",
 
246
                                     qvariant_cast<QColor>(fillAction->data())));
 
247
    fillButtonTriggered();
 
248
}
 
249
//! [13]
 
250
 
 
251
//! [14]
 
252
void MainWindow::lineColorChanged()
 
253
{
 
254
    lineAction = qobject_cast<QAction *>(sender());
 
255
    lineColorToolButton->setIcon(createColorToolButtonIcon(
 
256
                                     ":/images/linecolor.png",
 
257
                                     qvariant_cast<QColor>(lineAction->data())));
 
258
    lineButtonTriggered();
 
259
}
 
260
//! [14]
 
261
 
 
262
//! [15]
 
263
void MainWindow::textButtonTriggered()
 
264
{
 
265
    scene->setTextColor(qvariant_cast<QColor>(textAction->data()));
 
266
}
 
267
//! [15]
 
268
 
 
269
//! [16]
 
270
void MainWindow::fillButtonTriggered()
 
271
{
 
272
    scene->setItemColor(qvariant_cast<QColor>(fillAction->data()));
 
273
}
 
274
//! [16]
 
275
 
 
276
//! [17]
 
277
void MainWindow::lineButtonTriggered()
 
278
{
 
279
    scene->setLineColor(qvariant_cast<QColor>(lineAction->data()));
 
280
}
 
281
//! [17]
 
282
 
 
283
//! [18]
 
284
void MainWindow::handleFontChange()
 
285
{
 
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());
 
291
 
 
292
    scene->setFont(font);
 
293
}
 
294
//! [18]
 
295
 
 
296
//! [19]
 
297
void MainWindow::itemSelected(QGraphicsItem *item)
 
298
{
 
299
    DiagramTextItem *textItem =
 
300
    qgraphicsitem_cast<DiagramTextItem *>(item);
 
301
 
 
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());
 
308
}
 
309
//! [19]
 
310
 
 
311
//! [20]
 
312
void MainWindow::about()
 
313
{
 
314
    QMessageBox::about(this, tr("About Diagram Scene"),
 
315
                       tr("The <b>Diagram Scene</b> example shows "
 
316
                          "use of the graphics framework."));
 
317
}
 
318
//! [20]
 
319
 
 
320
//! [21]
 
321
void MainWindow::createToolBox()
 
322
{
 
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);
 
331
//! [21]
 
332
 
 
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);
 
344
 
 
345
    layout->setRowStretch(3, 10);
 
346
    layout->setColumnStretch(2, 10);
 
347
 
 
348
    QWidget *itemWidget = new QWidget;
 
349
    itemWidget->setLayout(layout);
 
350
 
 
351
    backgroundButtonGroup = new QButtonGroup(this);
 
352
    connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton*)),
 
353
            this, SLOT(backgroundButtonGroupClicked(QAbstractButton*)));
 
354
 
 
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);
 
364
 
 
365
    backgroundLayout->setRowStretch(2, 10);
 
366
    backgroundLayout->setColumnStretch(2, 10);
 
367
 
 
368
    QWidget *backgroundWidget = new QWidget;
 
369
    backgroundWidget->setLayout(backgroundLayout);
 
370
 
 
371
 
 
372
//! [22]
 
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"));
 
378
}
 
379
//! [22]
 
380
 
 
381
//! [23]
 
382
void MainWindow::createActions()
 
383
{
 
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()));
 
389
//! [23]
 
390
 
 
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()));
 
395
 
 
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()));
 
400
 
 
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()));
 
405
 
 
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()));
 
412
 
 
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()));
 
417
 
 
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()));
 
422
 
 
423
    aboutAction = new QAction(tr("A&bout"), this);
 
424
    aboutAction->setShortcut(tr("Ctrl+B"));
 
425
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
 
426
}
 
427
 
 
428
//! [24]
 
429
void MainWindow::createMenus()
 
430
{
 
431
    fileMenu = menuBar()->addMenu(tr("&File"));
 
432
    fileMenu->addAction(exitAction);
 
433
 
 
434
    itemMenu = menuBar()->addMenu(tr("&Item"));
 
435
    itemMenu->addAction(deleteAction);
 
436
    itemMenu->addSeparator();
 
437
    itemMenu->addAction(toFrontAction);
 
438
    itemMenu->addAction(sendBackAction);
 
439
 
 
440
    aboutMenu = menuBar()->addMenu(tr("&Help"));
 
441
    aboutMenu->addAction(aboutAction);
 
442
}
 
443
//! [24]
 
444
 
 
445
//! [25]
 
446
void MainWindow::createToolbars()
 
447
{
 
448
//! [25]
 
449
    editToolBar = addToolBar(tr("Edit"));
 
450
    editToolBar->addAction(deleteAction);
 
451
    editToolBar->addAction(toFrontAction);
 
452
    editToolBar->addAction(sendBackAction);
 
453
 
 
454
    fontCombo = new QFontComboBox();
 
455
    connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
 
456
            this, SLOT(currentFontChanged(QFont)));
 
457
 
 
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)));
 
466
 
 
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()));
 
475
 
 
476
//! [26]
 
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()));
 
485
//! [26]
 
486
 
 
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()));
 
495
 
 
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);
 
502
 
 
503
    colorToolBar = addToolBar(tr("Color"));
 
504
    colorToolBar->addWidget(fontColorToolButton);
 
505
    colorToolBar->addWidget(fillColorToolButton);
 
506
    colorToolBar->addWidget(lineColorToolButton);
 
507
 
 
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"));
 
515
 
 
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)));
 
521
 
 
522
    sceneScaleCombo = new QComboBox;
 
523
    QStringList scales;
 
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)));
 
529
 
 
530
    pointerToolbar = addToolBar(tr("Pointer type"));
 
531
    pointerToolbar->addWidget(pointerButton);
 
532
    pointerToolbar->addWidget(linePointerButton);
 
533
    pointerToolbar->addWidget(sceneScaleCombo);
 
534
//! [27]
 
535
}
 
536
//! [27]
 
537
 
 
538
//! [28]
 
539
QWidget *MainWindow::createBackgroundCellWidget(const QString &text, const QString &image)
 
540
{
 
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);
 
547
 
 
548
    QGridLayout *layout = new QGridLayout;
 
549
    layout->addWidget(button, 0, 0, Qt::AlignHCenter);
 
550
    layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter);
 
551
 
 
552
    QWidget *widget = new QWidget;
 
553
    widget->setLayout(layout);
 
554
 
 
555
    return widget;
 
556
}
 
557
//! [28]
 
558
 
 
559
//! [29]
 
560
QWidget *MainWindow::createCellWidget(const QString &text, DiagramItem::DiagramType type)
 
561
{
 
562
 
 
563
    DiagramItem item(type, itemMenu);
 
564
    QIcon icon(item.image());
 
565
 
 
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));
 
571
 
 
572
    QGridLayout *layout = new QGridLayout;
 
573
    layout->addWidget(button, 0, 0, Qt::AlignHCenter);
 
574
    layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter);
 
575
 
 
576
    QWidget *widget = new QWidget;
 
577
    widget->setLayout(layout);
 
578
 
 
579
    return widget;
 
580
}
 
581
//! [29]
 
582
 
 
583
//! [30]
 
584
QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor)
 
585
{
 
586
    QList<QColor> colors;
 
587
    colors << Qt::black << Qt::white << Qt::red << Qt::blue << Qt::yellow;
 
588
    QStringList names;
 
589
    names << tr("black") << tr("white") << tr("red") << tr("blue")
 
590
          << tr("yellow");
 
591
 
 
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);
 
601
    }
 
602
    return colorMenu;
 
603
}
 
604
//! [30]
 
605
 
 
606
//! [31]
 
607
QIcon MainWindow::createColorToolButtonIcon(const QString &imageFile, QColor color)
 
608
{
 
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);
 
618
 
 
619
    return QIcon(pixmap);
 
620
}
 
621
//! [31]
 
622
 
 
623
//! [32]
 
624
QIcon MainWindow::createColorIcon(QColor color)
 
625
{
 
626
    QPixmap pixmap(20, 20);
 
627
    QPainter painter(&pixmap);
 
628
    painter.setPen(Qt::NoPen);
 
629
    painter.fillRect(QRect(0, 0, 20, 20), color);
 
630
 
 
631
    return QIcon(pixmap);
 
632
}
 
633
//! [32]