11
use QtCore4::isa qw( Qt::MainWindow );
13
backgroundButtonGroupClicked => ['QAbstractButton *'],
14
buttonGroupClicked => ['int'],
16
pointerGroupClicked => ['int'],
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 *'],
43
return this->{exitAction};
47
return this->{addAction};
51
return this->{deleteAction};
55
return this->{toFrontAction};
58
sub sendBackAction() {
59
return this->{sendBackAction};
63
return this->{aboutAction};
67
return this->{fileMenu};
71
return this->{itemMenu};
75
return this->{aboutMenu};
79
return this->{textToolBar};
83
return this->{editToolBar};
87
return this->{colorToolBar};
90
sub pointerToolbar() {
91
return this->{pointerToolbar};
94
sub sceneScaleCombo() {
95
return this->{sceneScaleCombo};
98
sub itemColorCombo() {
99
return this->{itemColorCombo};
102
sub textColorCombo() {
103
return this->{textColorCombo};
106
sub fontSizeCombo() {
107
return this->{fontSizeCombo};
111
return this->{fontCombo};
115
return this->{toolBox};
119
return this->{buttonGroup};
122
sub pointerTypeGroup() {
123
return this->{pointerTypeGroup};
126
sub backgroundButtonGroup() {
127
return this->{backgroundButtonGroup};
130
sub fontColorToolButton() {
131
return this->{fontColorToolButton};
134
sub fillColorToolButton() {
135
return this->{fillColorToolButton};
138
sub lineColorToolButton() {
139
return this->{lineColorToolButton};
143
return this->{boldAction};
146
sub underlineAction() {
147
return this->{underlineAction};
151
return this->{italicAction};
155
return this->{textAction};
159
return this->{fillAction};
163
return this->{lineAction};
166
my $InsertTextButton = 10;
172
$class->SUPER::NEW();
173
this->createActions();
174
this->createToolBox();
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();
187
my $layout = Qt::HBoxLayout();
188
$layout->addWidget(this->toolBox);
189
this->{view} = Qt::GraphicsView(this->scene);
190
$layout->addWidget(this->view);
192
my $widget = Qt::Widget();
193
$widget->setLayout($layout);
195
this->setCentralWidget($widget);
196
this->setWindowTitle(this->tr('Diagramscene'));
197
this->setUnifiedTitleAndToolBarOnMac(1);
202
sub backgroundButtonGroupClicked
205
my $buttons = this->backgroundButtonGroup->buttons();
206
foreach my $myButton ( @{$buttons} ) {
207
if ($myButton != $button) {
208
$button->setChecked(0);
211
my $text = $button->text();
212
if ($text eq this->tr('Blue Grid')) {
213
this->scene->setBackgroundBrush(Qt::Brush(Qt::Pixmap(':/images/background1.png')));
215
elsif ($text eq this->tr('White Grid')) {
216
this->scene->setBackgroundBrush(Qt::Brush(Qt::Pixmap(':/images/background2.png')));
218
elsif ($text eq this->tr('Gray Grid')) {
219
this->scene->setBackgroundBrush(Qt::Brush(Qt::Pixmap(':/images/background3.png')));
222
this->scene->setBackgroundBrush(Qt::Brush(Qt::Pixmap(':/images/background4.png')));
225
this->scene->update();
226
this->view->update();
231
sub buttonGroupClicked
234
my $buttons = this->buttonGroup->buttons();
235
foreach my $button ( @{$buttons} ) {
236
if (this->buttonGroup->button($id) != $button) {
237
$button->setChecked(0);
240
if ($id == $InsertTextButton) {
241
this->scene->setMode(DiagramScene::InsertText);
243
this->scene->setItemType($id);
244
this->scene->setMode(DiagramScene::InsertItem);
252
foreach my $item ( @{scene->selectedItems()} ) {
253
if ($item->type() == DiagramItem::Type) {
254
$item->removeArrows();
256
this->scene->removeItem($item);
262
sub pointerGroupClicked
264
this->scene->setMode(this->pointerTypeGroup->checkedId());
271
if (scalar @{this->scene->selectedItems()} < 0) {
275
my $selectedItem = this->scene->selectedItems()->[0];
276
my $overlapItems = $selectedItem->collidingItems();
279
foreach my $item ( @{$overlapItems} ) {
280
if ($item->zValue() >= $zValue &&
281
$item->type() == DiagramItem::Type) {
282
$zValue = $item->zValue() + 0.1;
285
$selectedItem->setZValue($zValue);
292
if (scalar @{this->scene->selectedItems()} < 0) {
296
my $selectedItem = this->scene->selectedItems()->[0];
297
my $overlapItems = $selectedItem->collidingItems();
300
foreach my $item ( @{$overlapItems} ) {
301
if ($item->zValue() <= $zValue &&
302
$item->type() == DiagramItem::Type) {
303
$zValue = $item->zValue() - 0.1;
306
$selectedItem->setZValue($zValue);
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);
325
this->buttonGroup->button($InsertTextButton)->setChecked(0);
326
this->scene->setMode(this->pointerTypeGroup->checkedId());
331
sub currentFontChanged
333
this->handleFontChange();
340
this->handleFontChange();
345
sub sceneScaleChanged
348
my $newScale = $scale;
351
my $oldMatrix = this->view->matrix();
352
this->view->resetMatrix();
353
this->view->translate($oldMatrix->dx(), $oldMatrix->dy());
354
this->view->scale($newScale, $newScale);
361
this->{textAction} = this->sender();
362
this->fontColorToolButton->setIcon(this->createColorToolButtonIcon(
363
':/images/textpointer.png',
364
this->textAction->data()->value()));
365
this->textButtonTriggered();
372
this->{fillAction} = this->sender();
373
this->fillColorToolButton->setIcon(this->createColorToolButtonIcon(
374
':/images/floodfill.png',
375
this->fillAction->data()->value()));
376
this->fillButtonTriggered();
383
this->{lineAction} = this->sender();
384
this->lineColorToolButton->setIcon(this->createColorToolButtonIcon(
385
':/images/linecolor.png',
386
this->lineAction->data()->value()));;
387
this->lineButtonTriggered();
392
sub textButtonTriggered
394
this->scene->setTextColor(this->textAction->data()->value());
399
sub fillButtonTriggered
401
this->scene->setItemColor(this->fillAction->data()->value());
406
sub lineButtonTriggered
408
this->scene->setLineColor(this->lineAction->data()->value());
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());
421
this->scene->setFont($font);
429
my $textItem = $item;
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());
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.'));
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);
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')
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);
479
$layout->setRowStretch(3, 10);
480
$layout->setColumnStretch(2, 10);
482
my $itemWidget = Qt::Widget();
483
$itemWidget->setLayout($layout);
485
this->{backgroundButtonGroup} = Qt::ButtonGroup();
486
this->connect(this->backgroundButtonGroup, SIGNAL 'buttonClicked(QAbstractButton*)',
487
this, SLOT 'backgroundButtonGroupClicked(QAbstractButton*)');
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);
499
$backgroundLayout->setRowStretch(2, 10);
500
$backgroundLayout->setColumnStretch(2, 10);
502
my $backgroundWidget = Qt::Widget();
503
$backgroundWidget->setLayout($backgroundLayout);
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'));
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()');
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()');
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()');
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()');
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()');
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()');
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()');
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()');
576
this->{fileMenu} = this->menuBar()->addMenu(this->tr('&File'));
577
this->fileMenu->addAction(this->exitAction);
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);
585
this->{aboutMenu} = this->menuBar()->addMenu(this->tr('&Help'));
586
this->aboutMenu->addAction(this->aboutAction);
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);
599
this->{fontCombo} = Qt::FontComboBox();
600
this->{fontSizeCombo} = Qt::ComboBox();
601
this->connect(this->fontCombo, SIGNAL 'currentFontChanged(QFont)',
602
this, SLOT 'currentFontChanged(QFont)');
604
this->{fontSizeCombo} = Qt::ComboBox();
605
this->fontSizeCombo->setEditable(1);
606
for (my $i = 8; $i < 30; $i = $i + 2) {
607
fontSizeCombo->addItem($i);
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)');
614
this->{fontColorToolButton} = Qt::ToolButton();
615
this->fontColorToolButton->setPopupMode(Qt::ToolButton::MenuButtonPopup());
616
this->fontColorToolButton->setMenu(this->createColorMenu(SLOT 'textColorChanged()',
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()');
626
this->{fillColorToolButton} = Qt::ToolButton();
627
this->fillColorToolButton->setPopupMode(Qt::ToolButton::MenuButtonPopup());
628
this->fillColorToolButton->setMenu(this->createColorMenu(SLOT 'itemColorChanged()',
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()');
637
this->{lineColorToolButton} = Qt::ToolButton();
638
this->lineColorToolButton->setPopupMode(Qt::ToolButton::MenuButtonPopup());
639
this->lineColorToolButton->setMenu(this->createColorMenu(SLOT 'lineColorChanged()',
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()');
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);
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);
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'));
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)');
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)');
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);
690
sub createBackgroundCellWidget
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);
700
my $layout = Qt::GridLayout();
701
$layout->addWidget($button, 0, 0, Qt::AlignHCenter());
702
$layout->addWidget(Qt::Label($text), 1, 0, Qt::AlignCenter());
704
my $widget = Qt::Widget();
705
$widget->setLayout($layout);
714
my ($text, $type) = @_;
716
my $item = DiagramItem($type, this->itemMenu);
717
my $icon = Qt::Icon($item->image());
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);
725
my $layout = Qt::GridLayout();
726
$layout->addWidget($button, 0, 0, Qt::AlignHCenter());
727
$layout->addWidget(Qt::Label($text), 1, 0, Qt::AlignCenter());
729
my $widget = Qt::Widget();
730
$widget->setLayout($layout);
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') );
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()',
751
$colorMenu->addAction($action);
752
if ($colors[$i] == $defaultColor) {
753
$colorMenu->setDefaultAction($action);
761
sub createColorToolButtonIcon
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);
773
return Qt::Icon($pixmap);
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);
786
return Qt::Icon($pixmap);