-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
1310 lines (1134 loc) · 51.9 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QPushButton>
#include <QListWidget>
#include <QStackedWidget>
#include <QFileDialog>
#include <QGraphicsPixmapItem>
#include "arrow.h"
#include "diagramitem.h"
#include "diagramscene.h"
#include "diagramtextitem.h"
#include "diagramimage.h"
#include "mainwindow.h"
#include <iostream>
#include <QtWidgets>
#include <QPen>
#include <QLineF>
#include <fstream>
#include <sstream>
const int InsertTextButton = 10;
//! [0]
MainWindow::MainWindow():ModelPositions(0,std::vector<double>(0,0)),ModelTypes(0,0),ImageVec(0,NULL)
{
// In the constructor we call methods to create the widgets and layouts of the example before we create the diagram scene.
// All the things right below the toolbar
createActions();
// The things which can be chosen to be drawn into the scene
createToolBox();
//
createMenus();
SceneWidth = 800;
SceneHeight = 3000;
scene = new DiagramScene(itemMenu, this);
scene->setSceneRect(QRectF(0, 0, SceneWidth, SceneHeight));
// Draw Boundaries
penBoundaries = new QPen(Qt::black);
penBoundaries->setStyle(Qt::SolidLine);
verticalLineLeft = scene->addLine(QLine(0,0,0,SceneHeight),*penBoundaries);
verticalLineRight = scene->addLine(QLine(SceneWidth,0,SceneWidth,SceneHeight),*penBoundaries);
horizontalLineTop = scene->addLine(QLine(0,0,SceneWidth,0),*penBoundaries);
horizontalLineBottom = scene->addLine(QLine(0,SceneHeight,SceneWidth,SceneHeight),*penBoundaries);
// We connect to the itemInserted() and textInserted() slots of the diagram scenes as we want to uncheck the buttons in the
// tool box when an item is inserted. When an item is selected in the scene we receive the itemSelected() signal. We use
// this to update the widgets that display font properties if the item selected is a DiagramTextItem.
connect(scene, SIGNAL(itemInserted(DiagramItem*)),
this, SLOT(itemInserted(DiagramItem*)));
connect(scene, SIGNAL(itemsInserted(DiagramItem*,std::size_t,const std::vector<std::vector<double>>&)),
this, SLOT(itemsInserted(DiagramItem*,std::size_t,const std::vector<std::vector<double>>&)));
connect(scene, SIGNAL(itemsInserted(diagramImage*,DiagramItem::DiagramType)),
this, SLOT(itemsInserted(diagramImage*,DiagramItem::DiagramType)));
// connect(scene, SIGNAL(itemsInserted(DiagramItem *item, std::size_t nModels)),
// this, SLOT(itemsInserted(DiagramItem *item, std::size_t nModels)));
connect(scene, SIGNAL(textInserted(QGraphicsTextItem*)),
this, SLOT(textInserted(QGraphicsTextItem*)));
connect(scene, SIGNAL(itemSelected(QGraphicsItem*)),
this, SLOT(itemSelected(QGraphicsItem*)));
connect(scene, SIGNAL(checkItemPosition()),
this, SLOT(checkItemPosition()));
// The toolbars must be created after the scene as they connect to its signals.
// (all the things that can be chosen and stay clicked afterwards)
createToolbars();
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(toolBox);
view = new QGraphicsView(scene);
view->setMaximumSize(QSize(SceneWidth+10, SceneHeight+10));
layout->addWidget(view);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
setWindowTitle(tr("Diagramscene"));
setUnifiedTitleAndToolBarOnMac(true);
}
//! [0]
void MainWindow::setBounds(){
// delete this->grid
verticalLineLeft->setLine(0,0,0,SceneHeight);
// verticalLineLeft = new QLine(0,0,0,SceneHeight);
verticalLineRight->setLine(SceneWidth,0,SceneWidth,SceneHeight);
horizontalLineTop->setLine(0,0,SceneWidth,0);
horizontalLineBottom->setLine(0,SceneHeight,SceneWidth,SceneHeight);
}
// In this function we set the QBrush that is used to draw the background of the diagramscene.
// The background can be a grid of squares of blue, gray, or white tiles, or no grid at all.
// We have QPixmaps of the tiles from png files that we create the brush with.
// When one of the buttons in the background tabbed widget item is clicked we change the brush;
// we find out which button it is by checking its text.
void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button)
{
QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons();
foreach (QAbstractButton *myButton, buttons) {
if (myButton != button)
button->setChecked(false);
}
QString text = button->text();
if (text == tr("Blue Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background1.png"));
else if (text == tr("White Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background2.png"));
else if (text == tr("Gray Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background3.png"));
else
scene->setBackgroundBrush(QPixmap(":/images/background4.png"));
scene->update();
view->update();
}
//! [1]
//! [2]
// This slot is called when a button in buttonGroup is checked. When a button is checked
// the user can click on the graphics view and a DiagramItem of the selected type will be inserted into the DiagramScene.
void MainWindow::buttonGroupClicked(int id)
{
QList<QAbstractButton *> buttons = buttonGroup->buttons();
// We must loop through the buttons in the group to uncheck other buttons as only one button is allowed to be checked at a time.
foreach (QAbstractButton *button, buttons) {
if (buttonGroup->button(id) != button)
button->setChecked(false);
}
// QButtonGroup assigns an id to each button. We have set the id of each button to the diagram type,
// as given by DiagramItem::DiagramType that will be inserted into the scene when it is clicked.
// We can then use the button id when we set the diagram type with setItemType(). In the case of text we assigned
// an id that has a value that is not in the DiagramType enum.
if (id == InsertTextButton) {
scene->setMode(DiagramScene::InsertText);
} else {
scene->setItemType(DiagramItem::DiagramType(id));
scene->setMode(DiagramScene::InsertItem);
}
}
//! [2]
//! [3]
// This slot deletes the selected item, if any, from the scene. It deletes the arrows first in order to avoid to delete
// them twice. If the item to be deleted is a DiagramItem, we also need to delete arrows connected to it; we don't want
// arrows in the scene that aren't connected to items in both ends.
void MainWindow::deleteItem()
{
// Initialization
QList<QGraphicsItem *> itemList = scene->items();
std::vector<std::size_t> indices(scene->selectedItems().size(),0);
std::size_t posVec = 0;
foreach (QGraphicsItem *item, scene->selectedItems()) {
// Save indices of deleted item in the list. Used further below
for(std::size_t iItem=0;iItem<std::size_t(itemList.size()-4);iItem++){
// if(itemList[iItem]==item){
// indices[posVec] =iItem;
// posVec++;
// }
if(ImageVec[iItem]->getItemImage()==item){
indices[posVec] =iItem;
posVec++;
}
}
// if (item->type() == DiagramItem::Type)
// qgraphicsitem_cast<DiagramItem *>(item)->removeArrows();
scene->removeItem(item);
delete item;
}
// Bring indices in a defined order
std::sort (indices.begin(),indices.end());
// Go in the inverted direction for deleting otherwise you end up messing up the indices
// std::size_t maxSize = ModelTypes.size();
for(std::size_t iItem=0;iItem<indices.size();iItem++){
std::size_t indexUsed = indices[iItem];
ImageVec.erase(ImageVec.begin()+indexUsed);
}
// // Test
// for(std::size_t iItem=0;iItem<ImageVec.size();iItem++){
// std::cout<<"Type: "<<ImageVec[iItem]->getModelType()<<std::endl;
// }
}
//! [3]
// This function creates a text-file with all necesaary information in order to recreate the map
void MainWindow::saveFileAs()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Open File"), "/home", tr("text files (*.txt)"));
std::ofstream myfile;
myfile.open (fileName.toStdString(), std::ios::out | std::ios::trunc);
// Save Size of view
myfile<<SceneWidth<<"\t"<<SceneHeight<<"\t"<<std::endl;
QList<QGraphicsItem *> itemList = scene->items();
// -4 since the last four item are the boundary lines
for(std::size_t iItem=0;iItem<ImageVec.size();iItem++){
myfile<<ImageVec[iItem]->getModelType()<<"\t"<<ImageVec[iItem]->getItemImage()->x()<<"\t"<<ImageVec[iItem]->getItemImage()->y()
<<"\t"<<ImageVec[iItem]->getMovementType()<<"\t"<<ImageVec[iItem]->getMovementDetail()<<"\t"<<ImageVec[iItem]->getGravity()
<<"\t"<<ImageVec[iItem]->getRelationToMainChara()<<"\t"<<ImageVec[iItem]->getEventIndex()
<<"\t"<<std::endl;
}
myfile.close();
}
void MainWindow::loadFile(){
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), "/home", tr("text files (*.txt)"));
if(fileName.length()==0){
return;
}
// Clear old Scene
QList<QGraphicsItem *> itemProto = scene->items();
for(std::size_t iItem=0;iItem<std::size_t(itemProto.size()-4);iItem++) {
delete itemProto[iItem];
}
std::string line;
std::size_t nModels = 0;
// Get number of objects which are saved in the file
std::ifstream infile(fileName.toStdString());
while ( std::getline(infile, line) )
++nModels;
nModels=nModels-1;
std::cout<<"Lines: "<<nModels<<std::endl;
infile.close();
ModelTypes.resize(nModels,0);
ModelPositions.resize(nModels,std::vector<double>(2,0));
MovementType.resize(nModels,0);
MovementDetails.resize(nModels,0);
Gravity.resize(nModels,0);
RelationToMainChara.resize(nModels,0);
EventIndex.resize(nModels,0);
// Empty this vec since items are added after they are created in addItemsFromList
ImageVec.resize(0,NULL);
std::size_t lineIndex=0;
infile.open(fileName.toStdString());
// First the size of the scene
std::getline(infile, line);
std::istringstream iss1(line);
iss1>> SceneWidth >> SceneHeight;
std::cout<<"SceneWidth: "<<SceneWidth<<"\tSceneHeight: "<<SceneHeight<<std::endl;
while (std::getline(infile, line))
{
std::istringstream iss(line);
// std::istringstream iss("1 2.1 3.0 4 5 6 7 8");
// double val;
// iss >> val;
// std::cout<<val<<std::endl;
// iss >> val;
// std::cout<<val<<std::endl;
// iss >> val;
// std::cout<<val<<std::endl;
// iss >> val;
// std::cout<<val<<std::endl;
// iss >> val;
// std::cout<<val<<std::endl;
bool testBool=false;
testBool=testBool||!(iss >> ModelTypes[lineIndex]);
std::cout<<"ModelType: "<<ModelTypes[lineIndex];
testBool=testBool||!(iss >> ModelPositions[lineIndex][0]);
std::cout<<"\tModelPositions: "<<ModelPositions[lineIndex][0];
testBool=testBool||!(iss >> ModelPositions[lineIndex][1]);
testBool=testBool||!(iss >> MovementType[lineIndex]);
std::cout<<"MovementType: "<<MovementType[lineIndex];
testBool=testBool||!(iss >> MovementDetails[lineIndex]);
std::cout<<"MovementDetails: "<<MovementDetails[lineIndex];
testBool=testBool||!(iss >> Gravity[lineIndex]);
testBool=testBool||!(iss >> RelationToMainChara[lineIndex]);
testBool=testBool||!(iss >> EventIndex[lineIndex]);
std::cout<<"\ttestBool: "<<testBool<<std::endl;
if(testBool)
{
std::cout<<"break"<<std::endl;
break;
} // error
lineIndex++;
}
infile.close();
scene->addItemsFromList(ModelTypes,ModelPositions);
std::cout<<"Before ImageVec For: "<<ImageVec.size()<<std::endl;
for(std::size_t iModel=0;iModel<nModels;iModel++){
ImageVec[iModel]->setModelType(ModelTypes[iModel]);
ImageVec[iModel]->setMovementType(MovementType[iModel]);
ImageVec[iModel]->setMovementDetail(MovementDetails[iModel]);
ImageVec[iModel]->setGravity(Gravity[iModel]);
ImageVec[iModel]->setRelationToMainChara(RelationToMainChara[iModel]);
ImageVec[iModel]->setEventIndex(EventIndex[iModel]);
}
view->setSceneRect(QRectF(0, 0, SceneWidth+10, SceneHeight+10));
scene->setSceneRect(QRectF(0, 0, SceneWidth, SceneHeight));
setBounds();
scene->setMode(DiagramScene::MoveItem);
}
void MainWindow::editModelProperties(){
QList<QGraphicsItem *> selectedItems = scene->selectedItems();
std::vector<std::size_t> indicesOfSelectedItems(selectedItems.size(),0);
std::size_t posItem = 0;
std::cout<<"nSelected Items: "<<selectedItems.size()<<std::endl;
for (std::size_t iSelected=0;iSelected<std::size_t(selectedItems.size());iSelected++) {
for(std::size_t iItem=0;iItem<ImageVec.size();iItem++){
if(ImageVec[iItem]->getItemImage()==selectedItems[iSelected]){
std::cout<<"Selected Items: "<<iItem<<std::endl;
indicesOfSelectedItems[posItem]=iItem;
posItem++;
}
}
}
QWidget* window = new QWidget;
QGridLayout *layout = new QGridLayout;
QSpinBox *spinBoxMovementType = new QSpinBox();
QLabel *spinBoxLabelMovementType = new QLabel(tr("Movement Type"));
spinBoxMovementType->setValue(ImageVec[indicesOfSelectedItems[0]]->getMovementType());
layout->addWidget(spinBoxLabelMovementType,6,0);
layout->addWidget(spinBoxMovementType,6,1);
QDoubleSpinBox *spinBoxMovementDetail = new QDoubleSpinBox();
QLabel *spinBoxLabelMovementDetail = new QLabel(tr("Movement Details"));
spinBoxMovementDetail->setValue(ImageVec[indicesOfSelectedItems[0]]->getMovementDetail());
layout->addWidget(spinBoxLabelMovementDetail,7,0);
layout->addWidget(spinBoxMovementDetail,7,1);
QCheckBox *checkBoxGravity = new QCheckBox();
QLabel *checkBoxLabelGravity = new QLabel(tr("Gravity"));
checkBoxGravity->setChecked(ImageVec[indicesOfSelectedItems[0]]->getGravity()==1);
layout->addWidget(checkBoxLabelGravity,8,0);
layout->addWidget(checkBoxGravity,8,1);
QSpinBox *spinBoxRelationToMainChara = new QSpinBox();
QLabel *spinBoxLabelRelationToMainChara = new QLabel(tr("Relation to Main Chara"));
spinBoxRelationToMainChara->setValue(ImageVec[indicesOfSelectedItems[0]]->getRelationToMainChara());
layout->addWidget(spinBoxLabelRelationToMainChara,9,0);
layout->addWidget(spinBoxRelationToMainChara,9,1);
QSpinBox *spinBoxEventIndex = new QSpinBox();
QLabel *spinBoxLabelEventIndex = new QLabel(tr("Event Type"));
spinBoxEventIndex->setValue(ImageVec[indicesOfSelectedItems[0]]->getEventIndex());
layout->addWidget(spinBoxLabelEventIndex,10,0);
layout->addWidget(spinBoxEventIndex,10,1);
window->setLayout(layout);
window->show();
connect(spinBoxMovementType,SIGNAL(valueChanged(int)),this,SLOT(setMovementType(int)));
connect(spinBoxMovementDetail,SIGNAL(valueChanged(double)),this,SLOT(setMovementDetails(double)));
connect(checkBoxGravity,SIGNAL(stateChanged(int)),this,SLOT(setGravity(int)));
connect(spinBoxRelationToMainChara,SIGNAL(valueChanged(int)),this,SLOT(setRelationToMainChara(int)));
connect(spinBoxEventIndex,SIGNAL(valueChanged(int)),this,SLOT(setEventIndex(int)));
}
void MainWindow::setMovementType(int newValue){
QList<QGraphicsItem *> selectedItems = scene->selectedItems();
for (std::size_t iSelected=0;iSelected<std::size_t(selectedItems.size());iSelected++) {
for(std::size_t iItem=0;iItem<ImageVec.size();iItem++){
if(ImageVec[iItem]->getItemImage()==selectedItems[iSelected]){
ImageVec[iItem]->setMovementType(newValue);
}
}
}
}
void MainWindow::setMovementDetails(double newValue){
QList<QGraphicsItem *> selectedItems = scene->selectedItems();
for (std::size_t iSelected=0;iSelected<std::size_t(selectedItems.size());iSelected++) {
for(std::size_t iItem=0;iItem<ImageVec.size();iItem++){
if(ImageVec[iItem]->getItemImage()==selectedItems[iSelected]){
ImageVec[iItem]->setMovementDetail(newValue);
}
}
}
}
void MainWindow::setGravity(int newValue){
// std::cout<<"newValue: "<<newValue<<std::endl;
QList<QGraphicsItem *> selectedItems = scene->selectedItems();
for (std::size_t iSelected=0;iSelected<std::size_t(selectedItems.size());iSelected++) {
for(std::size_t iItem=0;iItem<ImageVec.size();iItem++){
if(ImageVec[iItem]->getItemImage()==selectedItems[iSelected]){
ImageVec[iItem]->setGravity(newValue==Qt::Checked);
}
}
}
}
void MainWindow::setRelationToMainChara(int newValue){
QList<QGraphicsItem *> selectedItems = scene->selectedItems();
for (std::size_t iSelected=0;iSelected<std::size_t(selectedItems.size());iSelected++) {
for(std::size_t iItem=0;iItem<ImageVec.size();iItem++){
if(ImageVec[iItem]->getItemImage()==selectedItems[iSelected]){
ImageVec[iItem]->setRelationToMainChara(newValue);
}
}
}
}
void MainWindow::setEventIndex(int newValue){
QList<QGraphicsItem *> selectedItems = scene->selectedItems();
for (std::size_t iSelected=0;iSelected<std::size_t(selectedItems.size());iSelected++) {
for(std::size_t iItem=0;iItem<ImageVec.size();iItem++){
if(ImageVec[iItem]->getItemImage()==selectedItems[iSelected]){
ImageVec[iItem]->setEventIndex(newValue);
}
}
}
}
void MainWindow::setMapSize()
{
bool ok;
double sceneWidth = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
tr("Amount:"), SceneWidth, 0, 10000, 2, &ok);
if (ok)
SceneWidth=sceneWidth;
double sceneHeight = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
tr("Amount:"), SceneHeight, 0, 10000, 2, &ok);
if (ok)
SceneHeight=sceneHeight;
view->setSceneRect(QRectF(0, 0, SceneWidth+10, SceneHeight+10));
scene->setSceneRect(QRectF(0, 0, SceneWidth, SceneHeight));
setBounds();
}
//! [4]
// The pointerTypeGroup decides whether the scene is in ItemMove or InsertLine mode. This button group is exclusive,
// i.e., only one button is checked at any time. As with the buttonGroup above we have assigned an id to the buttons
// that matches values of the DiagramScene::Mode enum, so that we can use the id to set the correct mode.
void MainWindow::pointerGroupClicked(int)
{
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
if(DiagramScene::Mode(pointerTypeGroup->checkedId())==DiagramScene::InsertHorizontalLine||
DiagramScene::Mode(pointerTypeGroup->checkedId())==DiagramScene::InsertVerticalLine){
scene->setEndOfLine(false);
};
}
//! [4]
//! [5]
// Several items may collide, i.e., overlap, with each other in the scene. This slot is called when the user requests
// that an item should be placed on top of the items it collides with. QGrapicsItems have a z-value that decides the
// order in which items are stacked in the scene; you can think of it as the z-axis in a 3D coordinate system.
// When items collide the items with higher z-values will be drawn on top of items with lower values. When we bring an
// item to the front we can loop through the items it collides with and set a z-value that is higher than all of them.
void MainWindow::bringToFront()
{
if (scene->selectedItems().isEmpty())
return;
QGraphicsItem *selectedItem = scene->selectedItems().first();
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
qreal zValue = 0;
foreach (QGraphicsItem *item, overlapItems) {
if (item->zValue() >= zValue && item->type() == DiagramItem::Type)
zValue = item->zValue() + 0.1;
}
selectedItem->setZValue(zValue);
}
//! [5]
//! [6]
// This slot works in the same way as bringToFront() described above, but sets a z-value that is lower than items the
// item that should be send to the back collides with.
void MainWindow::sendToBack()
{
if (scene->selectedItems().isEmpty())
return;
QGraphicsItem *selectedItem = scene->selectedItems().first();
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
qreal zValue = 0;
foreach (QGraphicsItem *item, overlapItems) {
if (item->zValue() <= zValue && item->type() == DiagramItem::Type)
zValue = item->zValue() - 0.1;
}
selectedItem->setZValue(zValue);
}
//! [6]
void MainWindow::checkItemPosition(){
// std::cout<<"ItemMoved"<<std::endl;
bool tooFarRight = true;
bool tooFarLeft = true;
bool tooFarUp = true;
bool tooFarDown = true;
QList<QGraphicsItem *> list = scene->items();
// Initialization
std::vector<std::size_t> indicesInImageVec(0,0);
QGraphicsItem *item;
for(int iItem=0;iItem<list.size()-4;iItem++) {
item = list[iItem];
if (item->type() == DiagramItem::Type)
qgraphicsitem_cast<DiagramItem *>(item)->removeArrows();
tooFarRight = (item->x())<0;
tooFarLeft = (item->x()+item->sceneBoundingRect().width())>SceneWidth;
tooFarUp = (item->y())<0;
tooFarDown = (item->y()+item->sceneBoundingRect().height())>SceneHeight;
if(tooFarRight||tooFarLeft||tooFarUp||tooFarDown){
// Save indices of deleted item in the imageVec. Used further below
for(std::size_t iItem2=0;iItem2<std::size_t(list.size()-4);iItem2++){
if(ImageVec[iItem2]->getItemImage()==item){
indicesInImageVec.push_back(iItem2);
}
}
scene->removeItem(item);
delete item;
}
}
// Bring indices in a defined order
std::sort(indicesInImageVec.begin(),indicesInImageVec.end());
// Go in the inverted direction for deleting otherwise you end up messing up the indices
for(std::size_t iItem=0;iItem<indicesInImageVec.size();iItem++){
std::cout<<"iItem: "<<indicesInImageVec[indicesInImageVec.size()-1-iItem]<<std::endl;
ImageVec.erase(ImageVec.begin()+indicesInImageVec[indicesInImageVec.size()-1-iItem]);
}
}
//! [7]
// This slot is called from the DiagramScene when an item has been added to the scene. We set the mode of the scene back
// to the mode before the item was inserted, which is ItemMove or InsertText depending on which button is checked in the
// pointerTypeGroup. We must also uncheck the button in the in the buttonGroup.
void MainWindow::itemInserted(DiagramItem *item)
{
scene->setMode(DiagramScene::Mode(3));
buttonGroup->button(int(item->diagramType()))->setChecked(false);
switch (item->diagramType()) {
// Archer
case DiagramItem::DiagramType(0):
MovementType.push_back(1);
MovementDetails.push_back(0);
Gravity.push_back(1);
RelationToMainChara.push_back(2);
EventIndex.push_back(0);
break;
// Cat Girl
case DiagramItem::DiagramType(1):
MovementType.push_back(1);
MovementDetails.push_back(2);
Gravity.push_back(1);
RelationToMainChara.push_back(2);
EventIndex.push_back(0);
break;
// Monster
case DiagramItem::DiagramType(2):
MovementType.push_back(1);
MovementDetails.push_back(2);
Gravity.push_back(1);
RelationToMainChara.push_back(1);
EventIndex.push_back(0);
break;
// Tree
case DiagramItem::DiagramType(3):
MovementType.push_back(0);
MovementDetails.push_back(0);
Gravity.push_back(0);
RelationToMainChara.push_back(0);
EventIndex.push_back(0);
default:
break;
}
// Update lists
ModelTypes.push_back(item->diagramType());
}
void MainWindow::itemsInserted(DiagramItem *item, std::size_t nModels,const std::vector<std::vector<double>>& modelPositions){
// scene->setMode(DiagramScene::Mode(3));
// buttonGroup->button(int(item->diagramType()))->setChecked(false);
for(std::size_t iModel=0;iModel<nModels;iModel++){
ModelPositions.push_back(std::vector<double>(2,0));
ModelPositions[ModelPositions.size()-1][0] = modelPositions[iModel][0];
ModelPositions[ModelPositions.size()-1][1] = modelPositions[iModel][1];
switch (item->diagramType()) {
// Archer
case DiagramItem::DiagramType(0):
MovementType.push_back(1);
MovementDetails.push_back(0);
Gravity.push_back(1);
RelationToMainChara.push_back(2);
EventIndex.push_back(0);
break;
// Cat Girl
case DiagramItem::DiagramType(1):
MovementType.push_back(1);
MovementDetails.push_back(2);
Gravity.push_back(1);
RelationToMainChara.push_back(2);
EventIndex.push_back(0);
break;
// Monster
case DiagramItem::DiagramType(2):
MovementType.push_back(1);
MovementDetails.push_back(2);
Gravity.push_back(1);
RelationToMainChara.push_back(1);
EventIndex.push_back(0);
break;
// Tree
case DiagramItem::DiagramType(3):
MovementType.push_back(0);
MovementDetails.push_back(0);
Gravity.push_back(0);
RelationToMainChara.push_back(0);
EventIndex.push_back(0);
default:
break;
}
// Update lists
ModelTypes.push_back(item->diagramType());
}
}
//! [7]
void MainWindow::itemsInserted(diagramImage *item,DiagramItem::DiagramType type){
scene->setMode(DiagramScene::MoveItem);
buttonGroup->button(int(type))->setChecked(false);
// ModelPositions.push_back(std::vector<double>(2,0));
// ModelPositions[ModelPositions.size()-1][0] = modelPositions[iModel][0];
// ModelPositions[ModelPositions.size()-1][1] = modelPositions[iModel][1];
switch (type) {
// Archer
case DiagramItem::DiagramType(0):
item->setModelType(0);
item->setMovementType(1);
item->setMovementDetail(0);
item->setGravity(1);
item->setRelationToMainChara(2);
item->setEventIndex(0);
break;
// Cat Girl
case DiagramItem::DiagramType(1):
item->setModelType(1);
item->setMovementType(1);
item->setMovementDetail(0);
item->setGravity(1);
item->setRelationToMainChara(2);
item->setEventIndex(0);
break;
// Monster
case DiagramItem::DiagramType(2):
item->setModelType(2);
item->setMovementType(1);
item->setMovementDetail(0);
item->setGravity(1);
item->setRelationToMainChara(1);
item->setEventIndex(0);
break;
// Tree
case DiagramItem::DiagramType(3):
item->setModelType(3);
item->setMovementType(0);
item->setMovementDetail(0);
item->setGravity(0);
item->setRelationToMainChara(0);
item->setEventIndex(0);
default:
break;
}
// Update lists
ImageVec.push_back(item);
}
//! [8]
// We simply set the mode of the scene back to the mode it had before the text was inserted.
void MainWindow::textInserted(QGraphicsTextItem *)
{
// buttonGroup->button(InsertTextButton)->setChecked(false);
scene->setMode(DiagramScene::Mode(3));
}
//! [8]
//! [9]
// When the user requests a font change, by using one of the widgets in the fontToolBar, we create a new QFont object and set
// its properties to match the state of the widgets. This is done in handleFontChange(), so we simply call that slot.
void MainWindow::currentFontChanged(const QFont &)
{
handleFontChange();
}
//! [9]
//! [10] When the user requests a font change, by using one of the widgets in
//the fontToolBar, we create a new QFont object and set its properties to match
//the state of the widgets. This is done in handleFontChange(), so we simply
//call that slot.
void MainWindow::fontSizeChanged(const QString &)
{
handleFontChange();
}
//! [10]
//! [11]
// The user can increase or decrease the scale, with the sceneScaleCombo, the scene is drawn in. It is not the scene itself
// that changes its scale, but only the view.
void MainWindow::sceneScaleChanged(const QString &scale)
{
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
QMatrix oldMatrix = view->matrix();
view->resetMatrix();
view->translate(oldMatrix.dx(), oldMatrix.dy());
view->scale(newScale, newScale);
}
//! [11]
//! [12]
// This slot is called when an item in the drop-down menu of the fontColorToolButton is pressed. We need to change the icon
// on the button to the color of the selected QAction. We keep a pointer to the selected action in textAction.
// It is in textButtonTriggered() we change the text color to the color of textAction, so we call that slot.
void MainWindow::textColorChanged()
{
textAction = qobject_cast<QAction *>(sender());
fontColorToolButton->setIcon(createColorToolButtonIcon(
":/images/textpointer.png",
qvariant_cast<QColor>(textAction->data())));
textButtonTriggered();
}
//! [12]
//! [13]
// This slot handles requests for changing the color of DiagramItems in the same manner as textColorChanged() does for DiagramTextItems.
void MainWindow::itemColorChanged()
{
fillAction = qobject_cast<QAction *>(sender());
fillColorToolButton->setIcon(createColorToolButtonIcon(
":/images/floodfill.png",
qvariant_cast<QColor>(fillAction->data())));
fillButtonTriggered();
}
//! [13]
//! [14]
// This slot handles requests for changing the color of Arrows in the same manner that textColorChanged() does it for DiagramTextItems.
void MainWindow::lineColorChanged()
{
lineAction = qobject_cast<QAction *>(sender());
lineColorToolButton->setIcon(createColorToolButtonIcon(
":/images/linecolor.png",
qvariant_cast<QColor>(lineAction->data())));
lineButtonTriggered();
}
//! [14]
//! [15]
// textAction points to the QAction of the currently selected menu item in the fontColorToolButton's color drop-down menu.
// We have set the data of the action to the QColor the action represents, so we can simply fetch this when we set the color
// of text with setTextColor().
void MainWindow::textButtonTriggered()
{
scene->setTextColor(qvariant_cast<QColor>(textAction->data()));
}
//! [15]
//! [16]
// fillAction points to the selected menu item in the drop-down menu of fillColorToolButton().
// We can therefore use the data of this action when we set the item color with setItemColor().
void MainWindow::fillButtonTriggered()
{
scene->setItemColor(qvariant_cast<QColor>(fillAction->data()));
}
//! [16]
//! [17]
// lineAction point to the selected item in the drop-down menu of lineColorToolButton.
// We use its data when we set the arrow color with setLineColor().
void MainWindow::lineButtonTriggered()
{
scene->setLineColor(qvariant_cast<QColor>(lineAction->data()));
}
//! [17]
//! [18]
// handleFontChange() is called when any of the widgets that show font properties changes. We create a new QFont object and
// set its properties based on the widgets. We then call the setFont() function of DiagramScene; it is the scene that set the
// font of the DiagramTextItems it manages.
void MainWindow::handleFontChange()
{
QFont font = fontCombo->currentFont();
font.setPointSize(fontSizeCombo->currentText().toInt());
font.setWeight(boldAction->isChecked() ? QFont::Bold : QFont::Normal);
font.setItalic(italicAction->isChecked());
font.setUnderline(underlineAction->isChecked());
scene->setFont(font);
}
//! [18]
//! [19]
// This slot is called when an item in the DiagramScene is selected. In the case of this example it is only text items
// that emit signals when they are selected, so we do not need to check what kind of graphics item is.
// We set the state of the widgets to match the properties of the font of the selected text item.
void MainWindow::itemSelected(QGraphicsItem *item)
{
DiagramTextItem *textItem =
qgraphicsitem_cast<DiagramTextItem *>(item);
QFont font = textItem->font();
fontCombo->setCurrentFont(font);
fontSizeCombo->setEditText(QString().setNum(font.pointSize()));
boldAction->setChecked(font.weight() == QFont::Bold);
italicAction->setChecked(font.italic());
underlineAction->setChecked(font.underline());
}
//! [19]
//! [20]
// This slot displays an about box for the example when the user selects the about menu item from the help menu.
void MainWindow::about()
{
QMessageBox::about(this, tr("About Diagram Scene"),
tr("The <b>Diagram Scene</b> example shows "
"use of the graphics framework."));
}
//! [20]
//! [21]
void MainWindow::createToolBox()
{
// This part of the function sets up the tabbed widget item that contains the flowchart shapes. An exclusive QButtonGroup
// always keeps one button checked; we want the group to allow all buttons to be unchecked. We still use a button group
// since we can associate user data, which we use to store the diagram type, with each button.
buttonGroup = new QButtonGroup(this);
buttonGroup->setExclusive(false);
connect(buttonGroup, SIGNAL(buttonClicked(int)),
this, SLOT(buttonGroupClicked(int)));
QGridLayout *layout = new QGridLayout;
// The createCellWidget() function sets up the buttons in the tabbed widget item and is examined later.
layout->addWidget(createCellWidget(tr("MainChara"), DiagramItem::MainChara), 0, 0);
layout->addWidget(createCellWidget(tr("CatGirl"), DiagramItem::CatGirl), 0, 1);
layout->addWidget(createCellWidget(tr("Monster"), DiagramItem::Monster),1, 0);
layout->addWidget(createCellWidget(tr("Tree"), DiagramItem::Tree), 1, 1);
// Not Commented -----------------------------------------------------
// QToolButton *textButton = new QToolButton;
// textButton->setCheckable(true);
// buttonGroup->addButton(textButton, InsertTextButton);
// textButton->setIcon(QIcon(QPixmap(":/images/Rechts_Laufen_Bogen.png")));
// textButton->setIconSize(QSize(50, 50));
// QGridLayout *textLayout = new QGridLayout;
// textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter);
// textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter);
// QWidget *textWidget = new QWidget;
// textWidget->setLayout(textLayout);
// layout->addWidget(textWidget, 1, 1);
layout->setRowStretch(3, 10);
layout->setColumnStretch(2, 10);
QWidget *itemWidget = new QWidget;
itemWidget->setLayout(layout);
// #########################
QToolButton *mainCharaButton = new QToolButton;
mainCharaButton->setCheckable(true);
buttonGroup->addButton(mainCharaButton, InsertTextButton);
mainCharaButton->setIcon(QIcon(QPixmap(":/images/Rechts_Laufen_Bogen.png")));
// mainCharaButton->setIconSize(QSize(50, 50));
// QGridLayout *mainCharaLayout = new QGridLayout;
// mainCharaLayout->addWidget(mainCharaButton, 0, 0, Qt::AlignHCenter);
// mainCharaLayout->addWidget(new QLabel(tr("MainCharacter")), 1, 0, Qt::AlignCenter);
// QWidget *mainCharaWidget = new QWidget;
// mainCharaWidget->setLayout(mainCharaLayout);
// layout->addWidget(mainCharaWidget, 1, 1);
// layout->setRowStretch(3, 10);
// layout->setColumnStretch(2, 10);
// QWidget *itemMainCharaWidget = new QWidget;
// itemMainCharaWidget->setLayout(layout);
// #########################
backgroundButtonGroup = new QButtonGroup(this);
connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton*)),
this, SLOT(backgroundButtonGroupClicked(QAbstractButton*)));
QGridLayout *backgroundLayout = new QGridLayout;
backgroundLayout->addWidget(createBackgroundCellWidget(tr("Blue Grid"),
":/images/background1.png"), 0, 0);
backgroundLayout->addWidget(createBackgroundCellWidget(tr("White Grid"),
":/images/background2.png"), 0, 1);
backgroundLayout->addWidget(createBackgroundCellWidget(tr("Gray Grid"),
":/images/background3.png"), 1, 0);
backgroundLayout->addWidget(createBackgroundCellWidget(tr("No Grid"),
":/images/background4.png"), 1, 1);
backgroundLayout->setRowStretch(2, 10);
backgroundLayout->setColumnStretch(2, 10);
QWidget *backgroundWidget = new QWidget;
backgroundWidget->setLayout(backgroundLayout);
// Not Commented -----------------------------------------------------
// We set the preferred size of the toolbox as its maximum. This way, more space is given to the graphics view.
toolBox = new QToolBox;
toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored));
toolBox->setMinimumWidth(itemWidget->sizeHint().width());
toolBox->addItem(itemWidget, tr("Basic Flowchart Shapes"));
toolBox->addItem(backgroundWidget, tr("Backgrounds"));
}
//! [22]
//! [23]
void MainWindow::createActions()
{
// Create actions