-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathComponentShape.cpp
More file actions
1515 lines (1299 loc) · 46.4 KB
/
Copy pathComponentShape.cpp
File metadata and controls
1515 lines (1299 loc) · 46.4 KB
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
#include "ComponentShape.h"
#include <cmath>
#include <cassert>
#include <QBrush>
#include <QPainter>
#include <QMenu>
#include <QAction>
#include <QGraphicsScene>
#include <QGraphicsSceneContextMenuEvent>
#include <QDebug>
#include <QColorDialog>
#include <QWidgetAction>
#include <QLabel>
#include <QTime>
#include <QListWidget>
#include <QGraphicsProxyWidget>
#include <QVBoxLayout>
#include <QSlider>
#include <QLineEdit>
#include <QAbstractSlider>
#include <QComboBox>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QHeaderView>
#include <QLinkedList>
#include <QPair>
#include "View/Utility/Tracer.h"
/*
* This is the cpp file of the ComponentShape class
* Collapse/Fold all methods to have a better overview.
*/
ComponentShape::ComponentShape(QGraphicsObject* parent,
IModelTrackedTrajectory* trajectory,
int id)
: QGraphicsObject(parent)
, m_trajectory(trajectory)
, m_id(id)
, m_parent(parent)
{
setData(0, m_id);
setData(1, "point");
m_polygons = QList<QPolygonF>();
m_useDefaultDimensions = true;
m_penWidth = 2;
m_transparency = 255;
m_penStyle = Qt::SolidLine;
m_penStylePrev = Qt::SolidLine;
m_marked = false;
// set default permissions
m_pMovable = true;
m_pRemovable = true;
m_pSwappable = true;
m_pRotatable = true;
m_fixed = false;
m_currentFramenumber = 0;
m_rotation = 0;
// m_trajectoryWasActiveOnce = false;
// create tracing layer
m_tracingLayer = new QGraphicsRectItem();
m_tracingLayer->setZValue(3);
this->scene()->addItem(m_tracingLayer);
// create orientation line
m_rotationLine = QLineF();
// set flags
setFlag(ItemIsMovable);
setFlag(ItemIsSelectable);
setAcceptedMouseButtons(Qt::LeftButton);
setVisible(true);
}
ComponentShape::~ComponentShape()
{
delete m_tracingLayer;
}
//*****************************QGraphicsItem
// interface***************************************
QRectF ComponentShape::boundingRect() const
{
if (this->data(1) == "ellipse" || this->data(1) == "point" ||
this->data(1) == "rectangle") {
return QRectF(0, 0, m_w, m_h);
} else if (this->data(1) == "polygon") {
// outer polygon bounding rect
return m_polygons[0].boundingRect();
} else {
// Note: This log might not appear (on console) as the application dies
// of assert before the buffer is served
qDebug() << "Could not create a bounding rect for current track "
<< m_id;
assert(0);
}
}
QPainterPath ComponentShape::shape() const
{
QPainterPath path;
if (this->data(1) == "ellipse") {
path.addEllipse(0, 0, m_w, m_h);
} else if (this->data(1) == "point") {
int dim = m_w <= m_h ? m_w : m_h;
// path.addEllipse(0, 0, dim , dim);
QRectF ellipse;
// ellipse = QRectF(0, m_h - m_w , m_w, m_w);
qreal origin_x = m_rotationLine.p1().x() - dim / 2;
qreal origin_y = m_rotationLine.p1().y() - dim / 2;
ellipse = QRectF(origin_x, origin_y, dim, dim);
path.addEllipse(ellipse);
} else if (this->data(1) == "rectangle") {
path.addRect(0, 0, m_w, m_h);
} else if (this->data(1) == "polygon") {
// outer polygon
path.addPolygon(m_polygons[0]);
} else {
qDebug() << "Could not create a interaction area for trajectory "
<< m_id;
assert(0);
}
return path;
}
void ComponentShape::paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
QWidget* widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
if (m_currentFramenumber < 0)
return;
// Antialiasing
if (m_antialiasing) {
painter->setRenderHint(QPainter::Antialiasing);
}
// check if scene is set
if (!(this->scene())) {
qDebug() << "componentscene is null\n";
}
QPen pen = QPen(m_penColor, m_penWidth, m_penStyle);
QBrush brush = QBrush(m_brushColor);
painter->setPen(pen);
painter->setBrush(brush);
// draw orientation line
if (m_orientationLine && !m_rotationLine.isNull()) {
painter->drawLine(m_rotationLine);
}
// TODO enums for types?
// draw ellipse
if (this->data(1) == "ellipse") {
QRectF ellipse = QRectF(0, 0, m_w, m_h);
painter->drawEllipse(ellipse);
}
// draw rectangle
if (this->data(1) == "rectangle") {
QRect rectangle = QRect(0, 0, m_w, m_h);
painter->drawRect(rectangle);
}
// draw point
// take the smaller of width and height
// a point should always have the same height and width
if (this->data(1) == "point") {
QRectF ellipse;
if (m_w <= m_h) {
// ellipse = QRectF(0, m_h - m_w , m_w, m_w);
qreal origin_x = m_rotationLine.p1().x() - m_w / 2;
qreal origin_y = m_rotationLine.p1().y() - m_w / 2;
ellipse = QRectF(origin_x, origin_y, m_w, m_w);
} else {
qreal origin_x = m_rotationLine.p1().x() - m_h / 2;
qreal origin_y = m_rotationLine.p1().y() - m_h / 2;
ellipse = QRectF(origin_x, origin_y, m_h, m_h);
}
painter->drawEllipse(ellipse);
}
// draw polygon
if (this->data(1) == "polygon") {
foreach (QPolygonF polygonF, m_polygons) {
painter->drawPolygon(polygonF);
}
}
// draw id in center
if (m_showId) {
painter->drawText(this->boundingRect(),
Qt::AlignCenter,
QString::number(m_id));
}
}
bool ComponentShape::advance()
{
return false;
}
void ComponentShape::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
m_mousePressTime = QTime::currentTime();
m_mousePressTime.start();
m_mousePressPos = pos().toPoint();
if (event->button() == Qt::LeftButton) {
// handle left mouse button here
setCursor(Qt::ClosedHandCursor);
update();
}
// pass on
QGraphicsItem::mousePressEvent(event);
}
void ComponentShape::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
setCursor(Qt::ArrowCursor);
QPoint currentPos = this->pos().toPoint();
int manhattanLength = (currentPos - m_mousePressPos).manhattanLength();
int moveTime = m_mousePressTime.elapsed();
if (manhattanLength > 5 || moveTime > 200) {
m_dragged = true;
}
if (m_dragged) {
// broadcast move so other selected elements get moved too
// maybe unconventional and slow but couldn't find another way;
// Dropevents in view and dropevents in shape didn't seem to work
broadcastMove();
} else {
this->setPos(m_mousePressPos);
this->update();
}
m_dragged = false;
update();
}
// pass on
QGraphicsItem::mouseReleaseEvent(event);
}
void ComponentShape::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
// pass on
QGraphicsItem::mouseMoveEvent(event);
}
QVariant ComponentShape::itemChange(GraphicsItemChange change,
const QVariant& value)
{
if (change == ItemSelectedHasChanged && scene()) {
if (this->isSelected()) {
m_penColorLast = m_penColor;
m_penColor = Qt::red;
m_penStylePrev = m_penStyle;
if (m_fixed) {
m_penStyle = Qt::DashDotLine;
} else {
m_penStyle = Qt::DashLine;
}
this->setZValue(2);
trace();
update();
} else {
m_penColor = m_penColorLast;
m_penStyle = m_penStylePrev;
this->setZValue(0);
trace();
update();
}
}
return QGraphicsItem::itemChange(change, value);
}
void ComponentShape::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
{
QMenu menu;
/*
create the info box
*/
QWidgetAction* infoBox = new QWidgetAction(this);
QString info = QString("ID: ");
info.append(QString::number(m_id));
QLabel* infoLabel = new QLabel(info);
infoLabel->setWordWrap(true);
infoLabel->setStyleSheet("QLabel {font-weight: bold; text-align: center}");
infoLabel->setAlignment(Qt::AlignCenter);
infoBox->setDefaultWidget(infoLabel);
menu.addAction(infoBox);
//
menu.addSeparator();
/*
create set object name - line edit
*/
QWidgetAction* objectNameAction = new QWidgetAction(this);
QLineEdit* objectEdit = new QLineEdit();
if (objectName() == "") {
objectEdit->setPlaceholderText("no object name set");
} else {
objectEdit->setText(objectName());
}
objectEdit->setAlignment(Qt::AlignHCenter);
objectEdit->setFrame(false);
objectEdit->setToolTip(
"Change the trajectory's name. This will not be saved in the data "
"output (yet)!");
QObject::connect(objectEdit,
&QLineEdit::textEdited,
this,
&ComponentShape::setObjectNameContext);
objectNameAction->setDefaultWidget(objectEdit);
menu.addAction(objectNameAction);
/*
show info window for current frame
*/
menu.addSeparator();
QAction* showInfoAction = menu.addAction(
"Show full info",
dynamic_cast<ComponentShape*>(this),
SLOT(createInfoWindow()));
menu.addSeparator();
/*
coloring
*/
QAction* changeBrushColorAction = menu.addAction(
"Change fill color",
dynamic_cast<ComponentShape*>(this),
SLOT(changeBrushColor()));
QAction* changePenColorAction = menu.addAction(
"Change border color",
dynamic_cast<ComponentShape*>(this),
SLOT(changePenColor()));
/*
transparency slider
*/
QMenu* transparencyMenu = new QMenu("Transparency");
QWidgetAction* sliderBox = new QWidgetAction(this);
QSlider* transparencySlider = new QSlider(Qt::Horizontal);
transparencySlider->setMinimum(0);
transparencySlider->setMaximum(255);
transparencySlider->setSingleStep(1);
transparencySlider->setTickPosition(QSlider::TicksBothSides);
transparencySlider->setTickInterval(64);
transparencySlider->setValue(m_transparency);
QObject::connect(transparencySlider,
&QSlider::sliderMoved,
this,
&ComponentShape::receiveTransparency);
sliderBox->setDefaultWidget(transparencySlider);
transparencyMenu->addAction(sliderBox);
menu.addMenu(transparencyMenu);
//
menu.addSeparator();
/*
dimension menu
*/
QMenu* dimensionMenu = new QMenu("Dimensions");
// Width
QWidgetAction* widthBox = new QWidgetAction(this);
QSpinBox* widthSpin = new QSpinBox;
widthSpin->setPrefix("Width: ");
widthSpin->setMinimum(1);
widthSpin->setMaximum(100000);
widthSpin->setValue(m_w);
QObject::connect(widthSpin,
QOverload<int>::of(&QSpinBox::valueChanged),
this,
&ComponentShape::receiveWidth);
widthBox->setDefaultWidget(widthSpin);
dimensionMenu->addAction(widthBox);
// Height
QWidgetAction* heightBox = new QWidgetAction(this);
QSpinBox* heightSpin = new QSpinBox;
heightSpin->setPrefix("Height: ");
heightSpin->setMinimum(1);
heightSpin->setMaximum(100000);
heightSpin->setValue(m_h);
QObject::connect(heightSpin,
QOverload<int>::of(&QSpinBox::valueChanged),
this,
&ComponentShape::receiveHeight);
heightBox->setDefaultWidget(heightSpin);
dimensionMenu->addAction(heightBox);
//
menu.addMenu(dimensionMenu);
/*
toggle orientation line
*/
QAction* showOrientationAction = menu.addAction(
"Show orientation line",
dynamic_cast<ComponentShape*>(this),
SLOT(receiveToggleOrientationLine(bool)));
showOrientationAction->setCheckable(true);
showOrientationAction->setChecked(m_orientationLine);
/*
toggle id
*/
QAction* showIDAction = menu.addAction("Show ID",
dynamic_cast<ComponentShape*>(this),
SLOT(receiveShowId(bool)));
showIDAction->setCheckable(true);
showIDAction->setChecked(m_showId);
/*
tracing menu
*/
QMenu* tracingMenu = new QMenu("Tracing");
// tracing type
QWidgetAction* typeBox = new QWidgetAction(this);
QComboBox* typeCombo = new QComboBox;
QStringList types;
types << "No tracing"
<< "Shape"
<< "Path"
<< "Arrow Path";
typeCombo->addItems(types);
typeCombo->setCurrentText(m_tracingStyle);
QObject::connect(
typeCombo,
QOverload<const QString&>::of(&QComboBox::currentIndexChanged),
this,
&ComponentShape::receiveTracingStyle);
typeBox->setDefaultWidget(typeCombo);
QMenu* tracingTypeMenu = new QMenu("Type");
tracingTypeMenu->addAction(typeBox);
tracingMenu->addMenu(tracingTypeMenu);
// tracingHistory
QWidgetAction* historyBox = new QWidgetAction(this);
QSpinBox* historySpinBox = new QSpinBox;
historySpinBox->setPrefix("History: ");
historySpinBox->setMinimum(1);
historySpinBox->setMaximum(100000);
historySpinBox->setValue(m_tracingLength);
QObject::connect(historySpinBox,
QOverload<int>::of(&QSpinBox::valueChanged),
this,
&ComponentShape::receiveTracingLength);
historyBox->setDefaultWidget(historySpinBox);
tracingMenu->addAction(historyBox);
// tracingSteps
QWidgetAction* stepsBox = new QWidgetAction(this);
QSpinBox* stepsSpinBox = new QSpinBox;
stepsSpinBox->setPrefix("Steps: ");
stepsSpinBox->setMinimum(1);
stepsSpinBox->setMaximum(100000);
stepsSpinBox->setValue(m_tracingSteps);
QObject::connect(stepsSpinBox,
QOverload<int>::of(&QSpinBox::valueChanged),
this,
&ComponentShape::receiveTracingSteps);
stepsBox->setDefaultWidget(stepsSpinBox);
tracingMenu->addAction(stepsBox);
// tracingDegradation
QWidgetAction* degrBox = new QWidgetAction(this);
QComboBox* degrCombo = new QComboBox;
QStringList degrTypes;
degrTypes << "None"
<< "Transparency"
<< "False color";
degrCombo->addItems(degrTypes);
degrCombo->setCurrentText(m_tracingTimeDegradation);
QObject::connect(
degrCombo,
QOverload<const QString&>::of(&QComboBox::currentIndexChanged),
this,
&ComponentShape::receiveTracingTimeDegradation);
degrBox->setDefaultWidget(degrCombo);
QMenu* tracingTimeDegrMenu = new QMenu("Time degradation");
tracingTimeDegrMenu->addAction(degrBox);
tracingMenu->addMenu(tracingTimeDegrMenu);
// toggle orientation line
QAction* showTrOrientationAction = tracingMenu->addAction(
"Show tracer orientation line",
dynamic_cast<ComponentShape*>(this),
SLOT(receiveTracerOrientationLine(bool)));
showTrOrientationAction->setCheckable(true);
showTrOrientationAction->setChecked(m_tracingOrientationLine);
// tracer number
QAction* showTrNumberAction = tracingMenu->addAction(
"Show framenumber on tracers",
dynamic_cast<ComponentShape*>(this),
SLOT(receiveTracerFrameNumber(bool)));
showTrNumberAction->setCheckable(true);
showTrNumberAction->setChecked(m_tracerFrameNumber);
// tracer proportions
QWidgetAction* propBox = new QWidgetAction(this);
QDoubleSpinBox* propSpinBox = new QDoubleSpinBox;
propSpinBox->setPrefix("Proportions: ");
propSpinBox->setDecimals(2);
propSpinBox->setSingleStep(0.001f);
propSpinBox->setMinimum(0.01f);
propSpinBox->setMaximum(99.99f);
propSpinBox->setValue(m_tracerProportions);
QObject::connect(propSpinBox,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this,
&ComponentShape::receiveTracerProportions);
propBox->setDefaultWidget(propSpinBox);
tracingMenu->addAction(propBox);
menu.addMenu(tracingMenu);
//
menu.addSeparator();
/*
removing
*/
QAction* removeTrackAction = menu.addAction(
"Remove track",
dynamic_cast<ComponentShape*>(this),
SLOT(removeTrack()));
QAction* removeTrackEntityAction = menu.addAction(
"Remove track entity",
dynamic_cast<ComponentShape*>(this),
SLOT(removeTrackEntity()));
if (!m_pRemovable) {
removeTrackAction->setEnabled(false);
removeTrackEntityAction->setEnabled(false);
}
/*
fixing
*/
QString fixText = m_fixed ? "Unfix track" : "Fix Track";
QAction* fixTrackAction = menu.addAction(
fixText,
dynamic_cast<ComponentShape*>(this),
SLOT(toggleFixTrack()));
/*
marking
*/
QString markText = m_marked ? "Unmark" : "Mark";
QAction* markAction = menu.addAction(markText,
dynamic_cast<ComponentShape*>(this),
SLOT(markShape()));
QAction* unmarkAction = menu.addAction(markText,
dynamic_cast<ComponentShape*>(this),
SLOT(unmarkShape()));
markAction->setVisible(!m_marked);
unmarkAction->setVisible(m_marked);
//
menu.addSeparator();
/*
morphing
*/
QMenu* morphMenu = new QMenu("Morph into...");
if (data(1) == "rectangle" || data(1) == "ellipse" || data(1) == "point" ||
data(1) == "polygon") {
QAction* morphPoint = morphMenu->addAction(
"Point",
dynamic_cast<ComponentShape*>(this),
SLOT(morphIntoPoint()));
QAction* morphEllipse = morphMenu->addAction(
"Ellipse",
dynamic_cast<ComponentShape*>(this),
SLOT(morphIntoEllipse()));
QAction* morphRect = morphMenu->addAction(
"Rectangle",
dynamic_cast<ComponentShape*>(this),
SLOT(morphIntoRect()));
QAction* morphPoylgon = morphMenu->addAction(
"Polygon",
dynamic_cast<ComponentShape*>(this),
SLOT(morphIntoPolygon()));
} else {
morphMenu->setEnabled(false);
}
menu.addMenu(morphMenu);
//
QAction* selectedAction = menu.exec(event->screenPos());
}
//*************************************************************************
/// updates this with data from new current entity
bool ComponentShape::updateAttributes(uint frameNumber)
{
m_currentFramenumber = frameNumber;
// if trajectory does not exist anymore, delete the shape
if (!m_trajectory) {
this->hide();
m_tracingLayer->hide();
this->deleteLater();
return false;
}
// check if trajectory is valid and current entity exists
if (m_trajectory->size() != 0 && m_trajectory->getValid() &&
m_trajectory->getChild(frameNumber)) {
m_id = m_trajectory->getId();
// update m_fixed
m_fixed = m_trajectory->getFixed();
if (!m_fixed) {
if (this->isSelected()) {
m_penStyle = Qt::DashLine;
} else {
m_penStyle = Qt::SolidLine;
}
} else {
if (this->isSelected()) {
m_penStyle = Qt::DashDotLine;
} else {
m_penStyle = Qt::DotLine;
}
}
// update dimensions
prepareGeometryChange();
// type checker
bool hasType = false;
// if point, ellipse, rectangle and valid
IModelTrackedPoint* pointLike = dynamic_cast<IModelTrackedPoint*>(
m_trajectory->getChild(m_currentFramenumber));
if (pointLike && (pointLike->getValid())) {
hasType = true;
// update width and height or use defaults
if (m_useDefaultDimensions) {
if (pointLike->hasW()) {
m_w = pointLike->getW();
} else {
m_w = m_wDefault;
}
if (pointLike->hasH()) {
m_h = pointLike->getH();
} else {
m_h = m_hDefault;
}
}
// update rotation
if (pointLike->hasDeg()) {
this->setTransformOriginPoint(m_w / 2, m_h / 2);
if (m_h > m_w || data(1) == "polygon") {
m_rotation = -90 - pointLike->getDeg();
this->setRotation(m_rotation);
} else {
m_rotation = -pointLike->getDeg();
this->setRotation(m_rotation);
}
// update rotation line
m_rotationLine.setP1(QPointF(m_w / 2, m_h / 2));
if (m_h > m_w || data(1) == "polygon") {
m_rotationLine.setAngle(-90);
} else {
m_rotationLine.setAngle(0);
}
qreal length = (m_w + m_h) / 2 * 3;
m_rotationLine.setLength(length);
// update rotation handle
m_rotationHandleLayer->setTransformOriginPoint(m_w / 2,
m_h / 2);
m_rotationHandleLayer->setRotation(0);
m_rotationHandle->setPos(m_rotationLine.p2());
} else {
m_rotationLine = QLineF();
}
// update Position
this->setPos(pointLike->getXpx() - m_w / 2,
pointLike->getYpx() - m_h / 2);
m_oldPos = this->pos().toPoint();
/*
-when morphing into polygon:
-construct polygon from pos and width,height
-pentagon
x / \
/ \
| |
\_ _/
*/
m_polygons = QList<QPolygonF>();
QPolygonF polygon;
QPointF startPoint = QPointF(m_w / 2, 0);
polygon << startPoint;
polygon << QPointF(m_w, m_h / 2);
polygon << QPointF(3 * m_w / 4, m_h);
polygon << QPointF(m_w / 4, m_h);
polygon << QPointF(0, m_h / 2);
polygon << startPoint;
if (polygon.isClosed()) {
m_polygons.append(polygon);
}
// create tracers
trace();
this->show();
update();
return true;
} else {
this->hide();
return true;
}
// if polygon
IModelTrackedPolygon* polygons = dynamic_cast<IModelTrackedPolygon*>(
m_trajectory->getChild(frameNumber));
if (polygons && (polygons->getValid() || m_currentFramenumber == 0)) {
hasType = true;
// update polygon
if (polygons->hasPolygon()) {
m_polygons = polygons->getPolygon();
}
// also update position
this->setPos(polygons->getXpx() - m_w / 2,
polygons->getYpx() - m_h / 2);
m_oldPos = this->pos().toPoint();
// update width and height or use defaults --> for morphing
if (m_useDefaultDimensions) {
m_w = m_polygons[0].boundingRect().width();
m_h = m_polygons[0].boundingRect().height();
}
// create tracers
trace();
this->show();
update();
return true;
}
// has no type
if (!hasType) {
qDebug() << "The current entity has no known type";
}
} else {
// trajectory is empty or null or invald or current entity is null
this->hide();
m_tracingLayer->hide();
// delete this;
return false;
}
}
/// attemps to draw tracers, if _tracingStyle is set tracers are drawn
void ComponentShape::trace()
{
// TRACING
IModelTrackedPoint* currentChild = dynamic_cast<IModelTrackedPoint*>(
m_trajectory->getChild(m_currentFramenumber));
// return if current entity is not existant
if (!currentChild)
return;
QPointF currentPoint = QPointF(currentChild->getXpx(),
currentChild->getYpx());
// update the tracing layer
m_tracingLayer->setPos(this->pos());
m_tracingLayer->setFlag(QGraphicsItem::ItemHasNoContents);
m_tracingLayer->show();
// really unefficient to flush each time
// flush tracing shape history, open up the memory --> thats slow
QList<QGraphicsItem*> tracers = m_tracingLayer->childItems();
foreach (QGraphicsItem* tracer, tracers) {
delete tracer;
}
// return if tracing disabled
if (m_trajectory->size() == 0 || m_tracingLength <= 0 ||
m_tracingStyle == "No tracing") {
return;
}
QPointF lastPointDifference = QPointF(0,
0); // + QPointF(m_h / 2, m_w / 2);
// create each n'th (m_tracingSteps) tracer of tracing history
// (m_tracingLength)
for (int i = 1; i <= m_tracingLength && i <= m_currentFramenumber;
i += m_tracingSteps) {
IModelTrackedPoint* historyChild = dynamic_cast<IModelTrackedPoint*>(
m_trajectory->getChild(m_currentFramenumber - i));
if (historyChild && historyChild->getValid()) {
// positioning
QPointF historyPoint = QPointF(historyChild->getXpx(),
historyChild->getYpx());
QPointF historyPointDifference = historyPoint - currentPoint;
QPointF adjustedHistoryPointDifference = historyPointDifference;
// time degradation colors
QPen timeDegradationPen = QPen(m_penColor, m_penWidth, m_penStyle);
QBrush timeDegradationBrush = QBrush(m_brushColor);
QColor timeDegradationBrushColor;
QColor timeDegradationPenColor;
if (m_tracingTimeDegradation == "Transparency") {
float tr = (float) m_transparency;
float trForThis = tr != 0
? tr - (tr / (float) m_tracingLength) *
(i - 1)
: 0.0f;
timeDegradationPenColor = QColor(m_penColor.red(),
m_penColor.green(),
m_penColor.blue(),
trForThis);
timeDegradationPen = QPen(timeDegradationPenColor,
m_penWidth,
Qt::SolidLine);
timeDegradationBrushColor = QColor(m_brushColor.red(),
m_brushColor.green(),
m_brushColor.blue(),
trForThis);
timeDegradationBrush = QBrush(timeDegradationBrushColor);
} else if (m_tracingTimeDegradation == "False color") {
float hue = (240.0f -
((240.0f / (float) m_tracingLength) * i));
timeDegradationPenColor = QColor::fromHsv((int) hue,
255.0f,
255.0f);
timeDegradationPenColor.setAlpha(m_transparency);
timeDegradationBrushColor = QColor::fromHsv((int) hue,
255.0f,
255.0f);
timeDegradationBrushColor.setAlpha(m_transparency);
timeDegradationPen = QPen(m_penColor, m_penWidth, m_penStyle);
timeDegradationBrush = QBrush(timeDegradationBrushColor);
}
// check tracing type
// SHAPE
if (m_tracingStyle == "Shape") {
// orientation line
if (m_tracingOrientationLine) {
QLineF orientationLine = QLineF();
orientationLine.setP1(adjustedHistoryPointDifference);
orientationLine.setAngle(historyChild->getDeg());
qreal length = (m_w + m_h) / 2 / m_tracerProportions * 3;
orientationLine.setLength(15);
QGraphicsLineItem* orientationItem = new QGraphicsLineItem(
orientationLine,
m_tracingLayer);
}
float tracerDeg = historyChild->hasDeg()
? historyChild->getDeg()
: 0.0;
float tracerW = m_w * m_tracerProportions;
float tracerH = m_h * m_tracerProportions;
int tracerNumber = m_currentFramenumber - i;
Tracer* tracer = new Tracer(this->data(1),
tracerNumber,
tracerDeg,
adjustedHistoryPointDifference,
tracerW,
tracerH,
timeDegradationPen,
timeDegradationBrush,
m_tracingLayer);
QObject::connect(tracer,
&Tracer::emitGoToFrame,
this,
&ComponentShape::emitGoToFrame);
}
// PATH
else if (m_tracingStyle == "Path") {
if (lastPointDifference != adjustedHistoryPointDifference) {
QLineF base = QLineF(lastPointDifference,
adjustedHistoryPointDifference);
QGraphicsLineItem* lineItem = new QGraphicsLineItem(
base,
m_tracingLayer);
lineItem->setPen(
QPen(timeDegradationPenColor, m_penWidth, m_penStyle));
lastPointDifference = adjustedHistoryPointDifference;
}
}
// ARROWPATH
else if (m_tracingStyle == "Arrow path") {
if (lastPointDifference != adjustedHistoryPointDifference) {
QLineF base = QLineF(lastPointDifference,
adjustedHistoryPointDifference);
int armLength = std::floor(base.length() / 9) + 2;
QLineF arm0 = base.normalVector();
arm0.setLength(armLength);
arm0.setAngle(base.angle() + 20);
QLineF arm1 = base.normalVector();
arm1.setLength(armLength);
arm1.setAngle(base.angle() - 20);
QGraphicsLineItem* baseItem = new QGraphicsLineItem(
base,
m_tracingLayer);
baseItem->setPen(QPen(timeDegradationBrushColor,
m_penWidth,
m_penStyle));
QGraphicsLineItem* arm0Item = new QGraphicsLineItem(
arm0,
m_tracingLayer);
arm0Item->setPen(timeDegradationPen);
QGraphicsLineItem* arm1Item = new QGraphicsLineItem(
arm1,
m_tracingLayer);
arm1Item->setPen(timeDegradationPen);
lastPointDifference = adjustedHistoryPointDifference;
}
}
// add framenumber to each tracer
if (m_tracerFrameNumber) {
uint tracerNumber = m_currentFramenumber - i;
QFont font = QFont();
int fontPixelSize = (int) ((m_w + m_h) / 5) *
m_tracerProportions;
font.setPixelSize(fontPixelSize);
// font.setBold(true);
QGraphicsSimpleTextItem* tracerNumberText =
new QGraphicsSimpleTextItem(QString::number(tracerNumber),
m_tracingLayer);