~kubuntu-members/perlkde/4.11

« back to all changes in this revision

Viewing changes to qtgui/examples/graphicsview/diagramscene/MainWindow.pm

  • Committer: Arno Rehn
  • Date: 2011-01-06 17:49:41 UTC
  • Revision ID: git-v1:ada9e4b459cf6fabd0f3b9b164387469cb19d9bc
A patch by Ian Monroe and myself to modularize the repository.
See README.MODULARIZATION for details.

svn path=/trunk/KDE/kdebindings/perl/; revision=1212365

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package MainWindow;
2
 
 
3
 
use strict;
4
 
use warnings;
5
 
use QtCore4;
6
 
use QtGui4;
7
 
use DiagramItem;
8
 
use DiagramScene;
9
 
use DiagramTextItem;
10
 
# [0]
11
 
use QtCore4::isa qw( Qt::MainWindow );
12
 
use QtCore4::slots
13
 
    backgroundButtonGroupClicked => ['QAbstractButton *'],
14
 
    buttonGroupClicked => ['int'],
15
 
    deleteItem => [],
16
 
    pointerGroupClicked => ['int'],
17
 
    bringToFront => [],
18
 
    sendToBack => [],
19
 
    itemInserted => ['QGraphicsPolygonItem *'],
20
 
    textInserted => ['QGraphicsTextItem *'],
21
 
    currentFontChanged => ['const QFont &'],
22
 
    fontSizeChanged => ['const QString &'],
23
 
    sceneScaleChanged => ['const QString &'],
24
 
    textColorChanged => [],
25
 
    itemColorChanged => [],
26
 
    lineColorChanged => [],
27
 
    textButtonTriggered => [],
28
 
    fillButtonTriggered => [],
29
 
    lineButtonTriggered => [],
30
 
    handleFontChange => [],
31
 
    itemSelected => ['QGraphicsItem *'],
32
 
    about => [];
33
 
 
34
 
sub scene() {
35
 
    return this->{scene};
36
 
}
37
 
 
38
 
sub view() {
39
 
    return this->{view};
40
 
}
41
 
 
42
 
sub exitAction() {
43
 
    return this->{exitAction};
44
 
}
45
 
 
46
 
sub addAction() {
47
 
    return this->{addAction};
48
 
}
49
 
 
50
 
sub deleteAction() {
51
 
    return this->{deleteAction};
52
 
}
53
 
 
54
 
sub toFrontAction() {
55
 
    return this->{toFrontAction};
56
 
}
57
 
 
58
 
sub sendBackAction() {
59
 
    return this->{sendBackAction};
60
 
}
61
 
 
62
 
sub aboutAction() {
63
 
    return this->{aboutAction};
64
 
}
65
 
 
66
 
sub fileMenu() {
67
 
    return this->{fileMenu};
68
 
}
69
 
 
70
 
sub itemMenu() {
71
 
    return this->{itemMenu};
72
 
}
73
 
 
74
 
sub aboutMenu() {
75
 
    return this->{aboutMenu};
76
 
}
77
 
 
78
 
sub textToolBar() {
79
 
    return this->{textToolBar};
80
 
}
81
 
 
82
 
sub editToolBar() {
83
 
    return this->{editToolBar};
84
 
}
85
 
 
86
 
sub colorToolBar() {
87
 
    return this->{colorToolBar};
88
 
}
89
 
 
90
 
sub pointerToolbar() {
91
 
    return this->{pointerToolbar};
92
 
}
93
 
 
94
 
sub sceneScaleCombo() {
95
 
    return this->{sceneScaleCombo};
96
 
}
97
 
 
98
 
sub itemColorCombo() {
99
 
    return this->{itemColorCombo};
100
 
}
101
 
 
102
 
sub textColorCombo() {
103
 
    return this->{textColorCombo};
104
 
}
105
 
 
106
 
sub fontSizeCombo() {
107
 
    return this->{fontSizeCombo};
108
 
}
109
 
 
110
 
sub fontCombo() {
111
 
    return this->{fontCombo};
112
 
}
113
 
 
114
 
sub toolBox() {
115
 
    return this->{toolBox};
116
 
}
117
 
 
118
 
sub buttonGroup() {
119
 
    return this->{buttonGroup};
120
 
}
121
 
 
122
 
sub pointerTypeGroup() {
123
 
    return this->{pointerTypeGroup};
124
 
}
125
 
 
126
 
sub backgroundButtonGroup() {
127
 
    return this->{backgroundButtonGroup};
128
 
}
129
 
 
130
 
sub fontColorToolButton() {
131
 
    return this->{fontColorToolButton};
132
 
}
133
 
 
134
 
sub fillColorToolButton() {
135
 
    return this->{fillColorToolButton};
136
 
}
137
 
 
138
 
sub lineColorToolButton() {
139
 
    return this->{lineColorToolButton};
140
 
}
141
 
 
142
 
sub boldAction() {
143
 
    return this->{boldAction};
144
 
}
145
 
 
146
 
sub underlineAction() {
147
 
    return this->{underlineAction};
148
 
}
149
 
 
150
 
sub italicAction() {
151
 
    return this->{italicAction};
152
 
}
153
 
 
154
 
sub textAction() {
155
 
    return this->{textAction};
156
 
}
157
 
 
158
 
sub fillAction() {
159
 
    return this->{fillAction};
160
 
}
161
 
 
162
 
sub lineAction() {
163
 
    return this->{lineAction};
164
 
}
165
 
 
166
 
my $InsertTextButton = 10;
167
 
 
168
 
# [0]
169
 
sub NEW
170
 
{
171
 
    my ($class) = @_;
172
 
    $class->SUPER::NEW();
173
 
    this->createActions();
174
 
    this->createToolBox();
175
 
    this->createMenus();
176
 
 
177
 
    this->{scene} = DiagramScene(this->itemMenu);
178
 
    this->scene->setSceneRect(Qt::RectF(0, 0, 5000, 5000));
179
 
    this->connect(this->scene, SIGNAL 'itemInserted(QGraphicsPolygonItem*)',
180
 
            this, SLOT 'itemInserted(QGraphicsPolygonItem*)');
181
 
    this->connect(this->scene, SIGNAL 'textInserted(QGraphicsTextItem*)',
182
 
        this, SLOT 'textInserted(QGraphicsTextItem*)');
183
 
    this->connect(this->scene, SIGNAL 'itemSelected(QGraphicsItem*)',
184
 
        this, SLOT 'itemSelected(QGraphicsItem*)');
185
 
    this->createToolbars();
186
 
 
187
 
    my $layout = Qt::HBoxLayout();
188
 
    $layout->addWidget(this->toolBox);
189
 
    this->{view} = Qt::GraphicsView(this->scene);
190
 
    $layout->addWidget(this->view);
191
 
 
192
 
    my $widget = Qt::Widget();
193
 
    $widget->setLayout($layout);
194
 
 
195
 
    this->setCentralWidget($widget);
196
 
    this->setWindowTitle(this->tr('Diagramscene'));
197
 
    this->setUnifiedTitleAndToolBarOnMac(1);
198
 
}
199
 
# [0]
200
 
 
201
 
# [1]
202
 
sub backgroundButtonGroupClicked
203
 
{
204
 
    my ($button) = @_;
205
 
    my $buttons = this->backgroundButtonGroup->buttons();
206
 
    foreach my $myButton ( @{$buttons} ) {
207
 
        if ($myButton != $button) {
208
 
            $button->setChecked(0);
209
 
        }
210
 
    }
211
 
    my $text = $button->text();
212
 
    if ($text eq this->tr('Blue Grid')) {
213
 
        this->scene->setBackgroundBrush(Qt::Brush(Qt::Pixmap(':/images/background1.png')));
214
 
    }
215
 
    elsif ($text eq this->tr('White Grid')) {
216
 
        this->scene->setBackgroundBrush(Qt::Brush(Qt::Pixmap(':/images/background2.png')));
217
 
    }
218
 
    elsif ($text eq this->tr('Gray Grid')) {
219
 
        this->scene->setBackgroundBrush(Qt::Brush(Qt::Pixmap(':/images/background3.png')));
220
 
    }
221
 
    else {
222
 
        this->scene->setBackgroundBrush(Qt::Brush(Qt::Pixmap(':/images/background4.png')));
223
 
    }
224
 
 
225
 
    this->scene->update();
226
 
    this->view->update();
227
 
}
228
 
# [1]
229
 
 
230
 
# [2]
231
 
sub buttonGroupClicked
232
 
{
233
 
    my ($id) = @_;
234
 
    my $buttons = this->buttonGroup->buttons();
235
 
    foreach my $button ( @{$buttons} ) {
236
 
        if (this->buttonGroup->button($id) != $button) {
237
 
            $button->setChecked(0);
238
 
        }
239
 
    }
240
 
    if ($id == $InsertTextButton) {
241
 
        this->scene->setMode(DiagramScene::InsertText);
242
 
    } else {
243
 
        this->scene->setItemType($id);
244
 
        this->scene->setMode(DiagramScene::InsertItem);
245
 
    }
246
 
}
247
 
# [2]
248
 
 
249
 
# [3]
250
 
sub deleteItem
251
 
{
252
 
    foreach my $item ( @{scene->selectedItems()} ) {
253
 
        if ($item->type() == DiagramItem::Type) {
254
 
            $item->removeArrows();
255
 
        }
256
 
        this->scene->removeItem($item);
257
 
    }
258
 
}
259
 
# [3]
260
 
 
261
 
# [4]
262
 
sub pointerGroupClicked
263
 
{
264
 
    this->scene->setMode(this->pointerTypeGroup->checkedId());
265
 
}
266
 
# [4]
267
 
 
268
 
# [5]
269
 
sub bringToFront
270
 
{
271
 
    if (scalar @{this->scene->selectedItems()} < 0) {
272
 
        return;
273
 
    }
274
 
 
275
 
    my $selectedItem = this->scene->selectedItems()->[0];
276
 
    my $overlapItems = $selectedItem->collidingItems();
277
 
 
278
 
    my $zValue = 0;
279
 
    foreach my $item ( @{$overlapItems} ) {
280
 
        if ($item->zValue() >= $zValue &&
281
 
            $item->type() == DiagramItem::Type) {
282
 
            $zValue = $item->zValue() + 0.1;
283
 
        }
284
 
    }
285
 
    $selectedItem->setZValue($zValue);
286
 
}
287
 
# [5]
288
 
 
289
 
# [6]
290
 
sub sendToBack
291
 
{
292
 
    if (scalar @{this->scene->selectedItems()} < 0) {
293
 
        return;
294
 
    }
295
 
 
296
 
    my $selectedItem = this->scene->selectedItems()->[0];
297
 
    my $overlapItems = $selectedItem->collidingItems();
298
 
 
299
 
    my $zValue = 0;
300
 
    foreach my $item ( @{$overlapItems} ) {
301
 
        if ($item->zValue() <= $zValue &&
302
 
            $item->type() == DiagramItem::Type) {
303
 
            $zValue = $item->zValue() - 0.1;
304
 
        }
305
 
    }
306
 
    $selectedItem->setZValue($zValue);
307
 
}
308
 
# [6]
309
 
 
310
 
# [7]
311
 
sub itemInserted
312
 
{
313
 
    my ($item) = @_;
314
 
    this->pointerTypeGroup->button(DiagramScene::MoveItem)->setChecked(1);
315
 
    this->scene->setMode(this->pointerTypeGroup->checkedId());
316
 
    if ( $item->isa( 'DiagramItem' ) ) {
317
 
        this->buttonGroup->button($item->diagramType())->setChecked(0);
318
 
    }
319
 
}
320
 
# [7]
321
 
 
322
 
# [8]
323
 
sub textInserted
324
 
{
325
 
    this->buttonGroup->button($InsertTextButton)->setChecked(0);
326
 
    this->scene->setMode(this->pointerTypeGroup->checkedId());
327
 
}
328
 
# [8]
329
 
 
330
 
# [9]
331
 
sub currentFontChanged
332
 
{
333
 
    this->handleFontChange();
334
 
}
335
 
# [9]
336
 
 
337
 
# [10]
338
 
sub fontSizeChanged
339
 
{
340
 
    this->handleFontChange();
341
 
}
342
 
# [10]
343
 
 
344
 
# [11]
345
 
sub sceneScaleChanged
346
 
{
347
 
    my ($scale) = @_;
348
 
    my $newScale = $scale;
349
 
    $newScale =~ s/%//g;
350
 
    $newScale /= 100.0;
351
 
    my $oldMatrix = this->view->matrix();
352
 
    this->view->resetMatrix();
353
 
    this->view->translate($oldMatrix->dx(), $oldMatrix->dy());
354
 
    this->view->scale($newScale, $newScale);
355
 
}
356
 
# [11]
357
 
 
358
 
# [12]
359
 
sub textColorChanged
360
 
{
361
 
    this->{textAction} = this->sender();
362
 
    this->fontColorToolButton->setIcon(this->createColorToolButtonIcon(
363
 
                ':/images/textpointer.png',
364
 
                this->textAction->data()->value()));
365
 
    this->textButtonTriggered();
366
 
}
367
 
# [12]
368
 
 
369
 
# [13]
370
 
sub itemColorChanged
371
 
{
372
 
    this->{fillAction} = this->sender();
373
 
    this->fillColorToolButton->setIcon(this->createColorToolButtonIcon(
374
 
                 ':/images/floodfill.png',
375
 
                 this->fillAction->data()->value()));
376
 
    this->fillButtonTriggered();
377
 
}
378
 
# [13]
379
 
 
380
 
# [14]
381
 
sub lineColorChanged
382
 
{
383
 
    this->{lineAction} = this->sender();
384
 
    this->lineColorToolButton->setIcon(this->createColorToolButtonIcon(
385
 
                 ':/images/linecolor.png',
386
 
                 this->lineAction->data()->value()));;
387
 
    this->lineButtonTriggered();
388
 
}
389
 
# [14]
390
 
 
391
 
# [15]
392
 
sub textButtonTriggered
393
 
{
394
 
    this->scene->setTextColor(this->textAction->data()->value());
395
 
}
396
 
# [15]
397
 
 
398
 
# [16]
399
 
sub fillButtonTriggered
400
 
{
401
 
    this->scene->setItemColor(this->fillAction->data()->value());
402
 
}
403
 
# [16]
404
 
 
405
 
# [17]
406
 
sub lineButtonTriggered
407
 
{
408
 
    this->scene->setLineColor(this->lineAction->data()->value());
409
 
}
410
 
# [17]
411
 
 
412
 
# [18]
413
 
sub handleFontChange
414
 
{
415
 
    my $font = this->fontCombo->currentFont();
416
 
    $font->setPointSize(this->fontSizeCombo->currentText());
417
 
    $font->setWeight(this->boldAction->isChecked() ? Qt::Font::Bold() : Qt::Font::Normal());
418
 
    $font->setItalic(this->italicAction->isChecked());
419
 
    $font->setUnderline(this->underlineAction->isChecked());
420
 
 
421
 
    this->scene->setFont($font);
422
 
}
423
 
# [18]
424
 
 
425
 
# [19]
426
 
sub itemSelected
427
 
{
428
 
    my ($item) = @_;
429
 
    my $textItem = $item;
430
 
 
431
 
    my $font = $textItem->font();
432
 
    my $color = $textItem->defaultTextColor();
433
 
    this->fontCombo->setCurrentFont($font);
434
 
    this->fontSizeCombo->setEditText($font->pointSize());
435
 
    this->boldAction->setChecked($font->weight() == Qt::Font::Bold());
436
 
    this->italicAction->setChecked($font->italic());
437
 
    this->underlineAction->setChecked($font->underline());
438
 
}
439
 
# [19]
440
 
 
441
 
# [20]
442
 
sub about
443
 
{
444
 
    Qt::MessageBox::about(this, this->tr('About Diagram Scene'),
445
 
                       this->tr('The <b>Diagram Scene</b> example shows ' .
446
 
                          'use of the graphics framework.'));
447
 
}
448
 
# [20]
449
 
 
450
 
# [21]
451
 
sub createToolBox
452
 
{
453
 
    this->{buttonGroup} = Qt::ButtonGroup();
454
 
    this->buttonGroup->setExclusive(0);
455
 
    this->connect(this->buttonGroup, SIGNAL 'buttonClicked(int)',
456
 
            this, SLOT 'buttonGroupClicked(int)');
457
 
    my $layout = Qt::GridLayout();
458
 
    $layout->addWidget(this->createCellWidget(this->tr('Conditional'),
459
 
                               DiagramItem::Conditional), 0, 0);
460
 
    $layout->addWidget(this->createCellWidget(this->tr('Process'),
461
 
                      DiagramItem::Step),0, 1);
462
 
    $layout->addWidget(this->createCellWidget(this->tr('Input/Output'),
463
 
                      DiagramItem::Io), 1, 0);
464
 
# [21]
465
 
 
466
 
    my $textButton = Qt::ToolButton();
467
 
    $textButton->setCheckable(1);
468
 
    this->buttonGroup->addButton($textButton, $InsertTextButton);
469
 
    $textButton->setIcon(Qt::Icon(Qt::Pixmap(':/images/textpointer.png')
470
 
                        ->scaled(30, 30)));
471
 
    $textButton->setIconSize(Qt::Size(50, 50));
472
 
    my $textLayout = Qt::GridLayout();
473
 
    $textLayout->addWidget($textButton, 0, 0, Qt::AlignHCenter());
474
 
    $textLayout->addWidget(Qt::Label(this->tr('Text')), 1, 0, Qt::AlignCenter());
475
 
    my $textWidget = Qt::Widget();
476
 
    $textWidget->setLayout($textLayout);
477
 
    $layout->addWidget($textWidget, 1, 1);
478
 
 
479
 
    $layout->setRowStretch(3, 10);
480
 
    $layout->setColumnStretch(2, 10);
481
 
 
482
 
    my $itemWidget = Qt::Widget();
483
 
    $itemWidget->setLayout($layout);
484
 
 
485
 
    this->{backgroundButtonGroup} = Qt::ButtonGroup();
486
 
    this->connect(this->backgroundButtonGroup, SIGNAL 'buttonClicked(QAbstractButton*)',
487
 
            this, SLOT 'backgroundButtonGroupClicked(QAbstractButton*)');
488
 
 
489
 
    my $backgroundLayout = Qt::GridLayout();
490
 
    $backgroundLayout->addWidget(this->createBackgroundCellWidget(this->tr('Blue Grid'),
491
 
                ':/images/background1.png'), 0, 0);
492
 
    $backgroundLayout->addWidget(this->createBackgroundCellWidget(this->tr('White Grid'),
493
 
                ':/images/background2.png'), 0, 1);
494
 
    $backgroundLayout->addWidget(this->createBackgroundCellWidget(this->tr('Gray Grid'),
495
 
                    ':/images/background3.png'), 1, 0);
496
 
    $backgroundLayout->addWidget(this->createBackgroundCellWidget(this->tr('No Grid'),
497
 
                ':/images/background4.png'), 1, 1);
498
 
 
499
 
    $backgroundLayout->setRowStretch(2, 10);
500
 
    $backgroundLayout->setColumnStretch(2, 10);
501
 
 
502
 
    my $backgroundWidget = Qt::Widget();
503
 
    $backgroundWidget->setLayout($backgroundLayout);
504
 
 
505
 
 
506
 
# [22]
507
 
    this->{toolBox} = Qt::ToolBox();
508
 
    this->toolBox->setSizePolicy(Qt::SizePolicy(Qt::SizePolicy::Maximum(), Qt::SizePolicy::Ignored()));
509
 
    this->toolBox->setMinimumWidth($itemWidget->sizeHint()->width());
510
 
    this->toolBox->addItem($itemWidget, this->tr('Basic Flowchart Shapes'));
511
 
    this->toolBox->addItem($backgroundWidget, this->tr('Backgrounds'));
512
 
}
513
 
# [22]
514
 
 
515
 
# [23]
516
 
sub createActions
517
 
{
518
 
    this->{toFrontAction} = Qt::Action(Qt::Icon(':/images/bringtofront.png'),
519
 
                                this->tr('Bring to &Front'), this);
520
 
    this->toFrontAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+F')));
521
 
    this->toFrontAction->setStatusTip(this->tr('Bring item to front'));
522
 
    this->connect(this->toFrontAction, SIGNAL 'triggered()',
523
 
            this, SLOT 'bringToFront()');
524
 
# [23]
525
 
 
526
 
    this->{sendBackAction} = Qt::Action(Qt::Icon(':/images/sendtoback.png'),
527
 
                                 this->tr('Send to &Back'), this);
528
 
    this->sendBackAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+B')));
529
 
    this->sendBackAction->setStatusTip(this->tr('Send item to back'));
530
 
    this->connect(this->sendBackAction, SIGNAL 'triggered()',
531
 
        this, SLOT 'sendToBack()');
532
 
 
533
 
    this->{deleteAction} = Qt::Action(Qt::Icon(':/images/delete.png'),
534
 
                               this->tr('&Delete'), this);
535
 
    this->deleteAction->setShortcut(Qt::KeySequence(this->tr('Delete')));
536
 
    this->deleteAction->setStatusTip(this->tr('Delete item from diagram'));
537
 
    this->connect(this->deleteAction, SIGNAL 'triggered()',
538
 
        this, SLOT 'deleteItem()');
539
 
 
540
 
    this->{exitAction} = Qt::Action(this->tr('E&xit'), this);
541
 
    this->exitAction->setShortcuts(Qt::KeySequence::Quit());
542
 
    this->exitAction->setStatusTip(this->tr('Quit Scenediagram example'));
543
 
    this->connect(this->exitAction, SIGNAL 'triggered()', this, SLOT 'close()');
544
 
 
545
 
    this->{boldAction} = Qt::Action(this->tr('Bold'), this);
546
 
    this->boldAction->setCheckable(1);
547
 
    my $pixmap = Qt::Pixmap(':/images/bold.png');
548
 
    this->boldAction->setIcon(Qt::Icon($pixmap));
549
 
    this->boldAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+B')));
550
 
    this->connect(this->boldAction, SIGNAL 'triggered()',
551
 
            this, SLOT 'handleFontChange()');
552
 
 
553
 
    this->{italicAction} = Qt::Action(Qt::Icon(':/images/italic.png'),
554
 
                               this->tr('Italic'), this);
555
 
    this->italicAction->setCheckable(1);
556
 
    this->italicAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+I')));
557
 
    this->connect(this->italicAction, SIGNAL 'triggered()',
558
 
            this, SLOT 'handleFontChange()');
559
 
 
560
 
    this->{underlineAction} = Qt::Action(Qt::Icon(':/images/underline.png'),
561
 
                                  this->tr('Underline'), this);
562
 
    this->underlineAction->setCheckable(1);
563
 
    this->underlineAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+U')));
564
 
    this->connect(this->underlineAction, SIGNAL 'triggered()',
565
 
            this, SLOT 'handleFontChange()');
566
 
 
567
 
    this->{aboutAction} = Qt::Action(this->tr('A&bout'), this);
568
 
    this->aboutAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+B')));
569
 
    this->connect(this->aboutAction, SIGNAL 'triggered()',
570
 
            this, SLOT 'about()');
571
 
}
572
 
 
573
 
# [24]
574
 
sub createMenus
575
 
{
576
 
    this->{fileMenu} = this->menuBar()->addMenu(this->tr('&File'));
577
 
    this->fileMenu->addAction(this->exitAction);
578
 
 
579
 
    this->{itemMenu} = this->menuBar()->addMenu(this->tr('&Item'));
580
 
    this->itemMenu->addAction(this->deleteAction);
581
 
    this->itemMenu->addSeparator();
582
 
    this->itemMenu->addAction(this->toFrontAction);
583
 
    this->itemMenu->addAction(this->sendBackAction);
584
 
 
585
 
    this->{aboutMenu} = this->menuBar()->addMenu(this->tr('&Help'));
586
 
    this->aboutMenu->addAction(this->aboutAction);
587
 
}
588
 
# [24]
589
 
 
590
 
# [25]
591
 
sub createToolbars
592
 
{
593
 
# [25]
594
 
    this->{editToolBar} = this->addToolBar(this->tr('Edit'));
595
 
    this->editToolBar->addAction(this->deleteAction);
596
 
    this->editToolBar->addAction(this->toFrontAction);
597
 
    this->editToolBar->addAction(this->sendBackAction);
598
 
 
599
 
    this->{fontCombo} = Qt::FontComboBox();
600
 
    this->{fontSizeCombo} = Qt::ComboBox();
601
 
    this->connect(this->fontCombo, SIGNAL 'currentFontChanged(QFont)',
602
 
            this, SLOT 'currentFontChanged(QFont)');
603
 
 
604
 
    this->{fontSizeCombo} = Qt::ComboBox();
605
 
    this->fontSizeCombo->setEditable(1);
606
 
    for (my $i = 8; $i < 30; $i = $i + 2) {
607
 
        fontSizeCombo->addItem($i);
608
 
    }
609
 
    my $validator = Qt::IntValidator(2, 64, this);
610
 
    this->fontSizeCombo->setValidator($validator);
611
 
    this->connect(this->fontSizeCombo, SIGNAL 'currentIndexChanged(QString)',
612
 
            this, SLOT 'fontSizeChanged(QString)');
613
 
 
614
 
    this->{fontColorToolButton} = Qt::ToolButton();
615
 
    this->fontColorToolButton->setPopupMode(Qt::ToolButton::MenuButtonPopup());
616
 
    this->fontColorToolButton->setMenu(this->createColorMenu(SLOT 'textColorChanged()',
617
 
                                                 Qt::black()));
618
 
    this->{textAction} = this->fontColorToolButton->menu()->defaultAction();
619
 
    this->fontColorToolButton->setIcon(this->createColorToolButtonIcon(
620
 
        ':/images/textpointer.png', Qt::black()));
621
 
    this->fontColorToolButton->setAutoFillBackground(1);
622
 
    this->connect(this->fontColorToolButton, SIGNAL 'clicked()',
623
 
            this, SLOT 'textButtonTriggered()');
624
 
 
625
 
# [26]
626
 
    this->{fillColorToolButton} = Qt::ToolButton();
627
 
    this->fillColorToolButton->setPopupMode(Qt::ToolButton::MenuButtonPopup());
628
 
    this->fillColorToolButton->setMenu(this->createColorMenu(SLOT 'itemColorChanged()',
629
 
                         Qt::white()));
630
 
    this->{fillAction} = this->fillColorToolButton->menu()->defaultAction();
631
 
    this->fillColorToolButton->setIcon(this->createColorToolButtonIcon(
632
 
        ':/images/floodfill.png', Qt::white()));
633
 
    this->connect(this->fillColorToolButton, SIGNAL 'clicked()',
634
 
            this, SLOT 'fillButtonTriggered()');
635
 
# [26]
636
 
 
637
 
    this->{lineColorToolButton} = Qt::ToolButton();
638
 
    this->lineColorToolButton->setPopupMode(Qt::ToolButton::MenuButtonPopup());
639
 
    this->lineColorToolButton->setMenu(this->createColorMenu(SLOT 'lineColorChanged()',
640
 
                                 Qt::black()));
641
 
    this->{lineAction} = this->lineColorToolButton->menu()->defaultAction();
642
 
    this->lineColorToolButton->setIcon(this->createColorToolButtonIcon(
643
 
        ':/images/linecolor.png', Qt::black()));
644
 
    this->connect(this->lineColorToolButton, SIGNAL 'clicked()',
645
 
            this, SLOT 'lineButtonTriggered()');
646
 
 
647
 
    this->{textToolBar} = this->addToolBar(this->tr('Font'));
648
 
    this->textToolBar->addWidget(this->fontCombo);
649
 
    this->textToolBar->addWidget(this->fontSizeCombo);
650
 
    this->textToolBar->addAction(this->boldAction);
651
 
    this->textToolBar->addAction(this->italicAction);
652
 
    this->textToolBar->addAction(this->underlineAction);
653
 
 
654
 
    this->{colorToolBar} = this->addToolBar(this->tr('Color'));
655
 
    this->colorToolBar->addWidget(this->fontColorToolButton);
656
 
    this->colorToolBar->addWidget(this->fillColorToolButton);
657
 
    this->colorToolBar->addWidget(this->lineColorToolButton);
658
 
 
659
 
    my $pointerButton = Qt::ToolButton();
660
 
    $pointerButton->setCheckable(1);
661
 
    $pointerButton->setChecked(1);
662
 
    $pointerButton->setIcon(Qt::Icon(':/images/pointer.png'));
663
 
    my $linePointerButton = Qt::ToolButton();
664
 
    $linePointerButton->setCheckable(1);
665
 
    $linePointerButton->setIcon(Qt::Icon(':/images/linepointer.png'));
666
 
 
667
 
    this->{pointerTypeGroup} = Qt::ButtonGroup();
668
 
    this->pointerTypeGroup->addButton($pointerButton, DiagramScene::MoveItem);
669
 
    this->pointerTypeGroup->addButton($linePointerButton,
670
 
                                DiagramScene::InsertLine);
671
 
    this->connect(this->pointerTypeGroup, SIGNAL 'buttonClicked(int)',
672
 
            this, SLOT 'pointerGroupClicked(int)');
673
 
 
674
 
    this->{sceneScaleCombo} = Qt::ComboBox();
675
 
    my @scales = (this->tr('50%'), this->tr('75%'), this->tr('100%'), this->tr('125%'), this->tr('150%'));
676
 
    this->sceneScaleCombo->addItems(\@scales);
677
 
    this->sceneScaleCombo->setCurrentIndex(2);
678
 
    this->connect(this->sceneScaleCombo, SIGNAL 'currentIndexChanged(QString)',
679
 
            this, SLOT 'sceneScaleChanged(QString)');
680
 
 
681
 
    this->{pointerToolbar} = this->addToolBar(this->tr('Pointer type'));
682
 
    this->pointerToolbar->addWidget($pointerButton);
683
 
    this->pointerToolbar->addWidget($linePointerButton);
684
 
    this->pointerToolbar->addWidget(this->sceneScaleCombo);
685
 
# [27]
686
 
}
687
 
# [27]
688
 
 
689
 
# [28]
690
 
sub createBackgroundCellWidget
691
 
{
692
 
    my ($text, $image) = @_;
693
 
    my $button = Qt::ToolButton();
694
 
    $button->setText($text);
695
 
    $button->setIcon(Qt::Icon($image));
696
 
    $button->setIconSize(Qt::Size(50, 50));
697
 
    $button->setCheckable(1);
698
 
    this->backgroundButtonGroup->addButton($button);
699
 
 
700
 
    my $layout = Qt::GridLayout();
701
 
    $layout->addWidget($button, 0, 0, Qt::AlignHCenter());
702
 
    $layout->addWidget(Qt::Label($text), 1, 0, Qt::AlignCenter());
703
 
 
704
 
    my $widget = Qt::Widget();
705
 
    $widget->setLayout($layout);
706
 
 
707
 
    return $widget;
708
 
}
709
 
# [28]
710
 
 
711
 
# [29]
712
 
sub createCellWidget
713
 
{
714
 
    my ($text, $type) = @_;
715
 
 
716
 
    my $item = DiagramItem($type, this->itemMenu);
717
 
    my $icon = Qt::Icon($item->image());
718
 
 
719
 
    my $button = Qt::ToolButton();
720
 
    $button->setIcon($icon);
721
 
    $button->setIconSize(Qt::Size(50, 50));
722
 
    $button->setCheckable(1);
723
 
    this->buttonGroup->addButton($button, $type);
724
 
 
725
 
    my $layout = Qt::GridLayout();
726
 
    $layout->addWidget($button, 0, 0, Qt::AlignHCenter());
727
 
    $layout->addWidget(Qt::Label($text), 1, 0, Qt::AlignCenter());
728
 
 
729
 
    my $widget = Qt::Widget();
730
 
    $widget->setLayout($layout);
731
 
 
732
 
    return $widget;
733
 
}
734
 
# [29]
735
 
 
736
 
# [30]
737
 
sub createColorMenu
738
 
{
739
 
    my ($slot, $defaultColor) = @_;
740
 
    my @colors = ( Qt::black(), Qt::white(), Qt::red(), Qt::blue(), Qt::yellow() );
741
 
    my @names = ( this->tr('black'), this->tr('white'), this->tr('red'), this->tr('blue'),
742
 
        this->tr('yellow') );
743
 
 
744
 
    my $colorMenu = Qt::Menu();
745
 
    for (my $i = 0; $i < @colors; ++$i) {
746
 
        my $action = Qt::Action($names[$i], this);
747
 
        $action->setData(Qt::Variant($colors[$i]));
748
 
        $action->setIcon(this->createColorIcon($colors[$i]));
749
 
        this->connect($action, SIGNAL 'triggered()',
750
 
                this, $slot);
751
 
        $colorMenu->addAction($action);
752
 
        if ($colors[$i] == $defaultColor) {
753
 
            $colorMenu->setDefaultAction($action);
754
 
        }
755
 
    }
756
 
    return $colorMenu;
757
 
}
758
 
# [30]
759
 
 
760
 
# [31]
761
 
sub createColorToolButtonIcon
762
 
{
763
 
    my ($imageFile, $color) = @_;
764
 
    my $pixmap = Qt::Pixmap(50, 80);
765
 
    $pixmap->fill(Qt::Color(Qt::transparent()));
766
 
    my $painter = Qt::Painter($pixmap);
767
 
    my $image = Qt::Pixmap($imageFile);
768
 
    my $target = Qt::Rect(0, 0, 50, 60);
769
 
    my $source = Qt::Rect(0, 0, 42, 42);
770
 
    $painter->fillRect(Qt::Rect(0, 60, 50, 80), $color);
771
 
    $painter->drawPixmap($target, $image, $source);
772
 
 
773
 
    return Qt::Icon($pixmap);
774
 
}
775
 
# [31]
776
 
 
777
 
# [32]
778
 
sub createColorIcon
779
 
{
780
 
    my ($color) = @_;
781
 
    my $pixmap = Qt::Pixmap(20, 20);
782
 
    my $painter = Qt::Painter($pixmap);
783
 
    $painter->setPen(Qt::NoPen());
784
 
    $painter->fillRect(Qt::Rect(0, 0, 20, 20), $color);
785
 
 
786
 
    return Qt::Icon($pixmap);
787
 
}
788
 
# [32]
789
 
 
790
 
1;