-
Notifications
You must be signed in to change notification settings - Fork 298
/
ShapeAnnotation.cpp
1943 lines (1833 loc) · 72.9 KB
/
ShapeAnnotation.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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/
#include "ShapeAnnotation.h"
#include "Util/Helper.h"
#include "MainWindow.h"
#include "Options/OptionsDialog.h"
#include "ShapePropertiesDialog.h"
#include "Modeling/Commands.h"
#include "Element/ElementProperties.h"
#include "TLM/FetchInterfaceDataDialog.h"
#include "Plotting/VariablesWidget.h"
#include "Util/ResourceCache.h"
/*!
* \brief GraphicItem::setDefaults
* Sets the default value.
*/
void GraphicItem::setDefaults()
{
mVisible = true;
mDynamicVisible = "";
mDynamicVisibleValue = true;
mOrigin = QPointF(0, 0);
mDynamicOrigin = "";
mDynamicOriginValue = QPointF(0, 0);
mRotation = 0;
mDynamicRotation = "";
mDynamicRotationValue = 0;
}
/*!
* \brief GraphicItem::setDefaults
* Sets the default value from ShapeAnnotation.
* \param pShapeAnnotation
*/
void GraphicItem::setDefaults(ShapeAnnotation *pShapeAnnotation)
{
mVisible = pShapeAnnotation->mVisible;
mDynamicVisible = pShapeAnnotation->mDynamicVisible;
mDynamicVisibleValue = pShapeAnnotation->mDynamicVisibleValue;
mOrigin = pShapeAnnotation->mOrigin;
mDynamicOrigin = pShapeAnnotation->mDynamicOrigin;
mDynamicOriginValue = pShapeAnnotation->mDynamicOriginValue;
mRotation = pShapeAnnotation->mRotation;
mDynamicRotation = pShapeAnnotation->mDynamicRotation;
mDynamicRotationValue = pShapeAnnotation->mDynamicRotationValue;
}
/*!
Parses the GraphicItem annotation values.
\param annotation - the annotation string.
*/
void GraphicItem::parseShapeAnnotation(QString annotation)
{
// parse the shape to get the list of attributes
QStringList list = StringHandler::getStrings(annotation);
if (list.size() < 3)
return;
// if first item of list is true then the shape should be visible.
if (list.at(0).startsWith("{")) { // DynamicSelect
QStringList args = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(0)));
if (args.count() > 0) {
mVisible = args.at(0).contains("true");
}
if (args.count() > 1) {
mDynamicVisible = args.at(1); // variable name
}
} else {
mVisible = list.at(0).contains("true");
}
// 2nd item is the origin
QStringList originList = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(1)));
if (originList.size() >= 2) {
setOrigin(QPointF(originList.at(0).toFloat(), originList.at(1).toFloat()));
}
// 3rd item is the rotation
if (list.at(2).startsWith("{")) { // DynamicSelect
QStringList args = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(2)));
if (args.count() > 0) {
mRotation = args.at(0).toFloat();
}
if (args.count() > 1) {
mDynamicRotation = args.at(1); // variable name
}
} else {
mRotation = list.at(2).toFloat();
}
}
/*!
* \brief GraphicItem::getOMCShapeAnnotation
* Returns the annotation values of the GraphicItem in format as returned by OMC.
* \return the annotation values as a list.
*/
QStringList GraphicItem::getOMCShapeAnnotation()
{
QStringList annotationString;
/* get visible */
annotationString.append(mVisible ? "true" : "false");
/* get origin */
QString originString;
originString.append("{").append(QString::number(mOrigin.x())).append(",");
originString.append(QString::number(mOrigin.y())).append("}");
annotationString.append(originString);
/* get rotation */
annotationString.append(QString::number(mRotation));
return annotationString;
}
/*!
* \brief GraphicItem::getShapeAnnotation
* Returns the annotation values of the GraphicItem.
* \return the annotation values as a list.
*/
QStringList GraphicItem::getShapeAnnotation()
{
QStringList annotationString;
/* get visible */
if (!mVisible) {
annotationString.append("visible=false");
}
/* get origin */
if (mOrigin != QPointF(0, 0)) {
QString originString;
originString.append("origin=");
originString.append("{").append(QString::number(mOrigin.x())).append(",");
originString.append(QString::number(mOrigin.y())).append("}");
annotationString.append(originString);
}
/* get rotation */
if (mRotation != 0) {
annotationString.append(QString("rotation=").append(QString::number(mRotation)));
}
return annotationString;
}
/*!
* \brief FilledShape::setDefaults
* Sets the default values.
*/
void FilledShape::setDefaults()
{
mLineColor = QColor(0, 0, 0);
mFillColor = QColor(0, 0, 0);
mLinePattern = StringHandler::LineSolid;
mFillPattern = StringHandler::FillNone;
mLineThickness = 0.25;
}
/*!
* \brief FilledShape::setDefaults
* Sets the default value from ShapeAnnotation.
* \param pShapeAnnotation
*/
void FilledShape::setDefaults(ShapeAnnotation *pShapeAnnotation)
{
mLineColor = pShapeAnnotation->mLineColor;
mFillColor = pShapeAnnotation->mFillColor;
mLinePattern = pShapeAnnotation->mLinePattern;
mFillPattern = pShapeAnnotation->mFillPattern;
mLineThickness = pShapeAnnotation->mLineThickness;
}
/*!
Parses the FilledShape annotation values.
\param annotation - the annotation string.
*/
void FilledShape::parseShapeAnnotation(QString annotation)
{
// parse the shape to get the list of attributes
QStringList list = StringHandler::getStrings(annotation);
if (list.size() < 8) {
return;
}
// 4th item of the list is the line color
QStringList colorList = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(3)));
if (colorList.size() >= 3) {
int red, green, blue = 0;
red = colorList.at(0).toInt();
green = colorList.at(1).toInt();
blue = colorList.at(2).toInt();
mLineColor = QColor (red, green, blue);
}
// 5th item of list contains the fill color.
QStringList fillColorList = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(4)));
if (fillColorList.size() >= 3) {
int red, green, blue = 0;
red = fillColorList.at(0).toInt();
green = fillColorList.at(1).toInt();
blue = fillColorList.at(2).toInt();
mFillColor = QColor (red, green, blue);
}
// 6th item of list contains the Line Pattern.
mLinePattern = StringHandler::getLinePatternType(list.at(5));
// 7th item of list contains the Fill Pattern.
mFillPattern = StringHandler::getFillPatternType(list.at(6));
// 8th item of list contains the thickness.
mLineThickness = list.at(7).toFloat();
}
/*!
* \brief FilledShape::getOMCShapeAnnotation
* Returns the annotation values of the FilledShape in format as returned by OMC.
* \return the annotation values as a list.
*/
QStringList FilledShape::getOMCShapeAnnotation()
{
QStringList annotationString;
/* get the line color */
QString lineColorString;
lineColorString.append("{");
lineColorString.append(QString::number(mLineColor.red())).append(",");
lineColorString.append(QString::number(mLineColor.green())).append(",");
lineColorString.append(QString::number(mLineColor.blue()));
lineColorString.append("}");
annotationString.append(lineColorString);
/* get the fill color */
QString fillColorString;
fillColorString.append("{");
fillColorString.append(QString::number(mFillColor.red())).append(",");
fillColorString.append(QString::number(mFillColor.green())).append(",");
fillColorString.append(QString::number(mFillColor.blue()));
fillColorString.append("}");
annotationString.append(fillColorString);
/* get the line pattern */
annotationString.append(StringHandler::getLinePatternString(mLinePattern));
/* get the fill pattern */
annotationString.append(StringHandler::getFillPatternString(mFillPattern));
// get the thickness
annotationString.append(QString::number(mLineThickness));
return annotationString;
}
/*!
* \brief FilledShape::getShapeAnnotation
* Returns the annotation values of the FilledShape.
* \return the annotation values as a list.
*/
QStringList FilledShape::getShapeAnnotation()
{
QStringList annotationString;
/* get the line color */
if (mLineColor != Qt::black) {
QString lineColorString;
lineColorString.append("lineColor={");
lineColorString.append(QString::number(mLineColor.red())).append(",");
lineColorString.append(QString::number(mLineColor.green())).append(",");
lineColorString.append(QString::number(mLineColor.blue()));
lineColorString.append("}");
annotationString.append(lineColorString);
}
/* get the fill color */
if (mFillColor != Qt::black) {
QString fillColorString;
fillColorString.append("fillColor={");
fillColorString.append(QString::number(mFillColor.red())).append(",");
fillColorString.append(QString::number(mFillColor.green())).append(",");
fillColorString.append(QString::number(mFillColor.blue()));
fillColorString.append("}");
annotationString.append(fillColorString);
}
/* get the line pattern */
if (mLinePattern != StringHandler::LineSolid) {
annotationString.append(QString("pattern=").append(StringHandler::getLinePatternString(mLinePattern)));
}
/* get the fill pattern */
if (mFillPattern != StringHandler::FillNone) {
annotationString.append(QString("fillPattern=").append(StringHandler::getFillPatternString(mFillPattern)));
}
// get the thickness
if (mLineThickness != 0.25) {
annotationString.append(QString("lineThickness=").append(QString::number(mLineThickness)));
}
return annotationString;
}
/*!
* \class ShapeAnnotation
* \brief The base class for all shapes LineAnnotation, PolygonAnnotation, RectangleAnnotation, EllipseAnnotation, TextAnnotation, BitmapAnnotation.
*/
/*!
* \brief ShapeAnnotation::ShapeAnnotation
* \param pShapeAnnotation
* \param pParent
*/
ShapeAnnotation::ShapeAnnotation(ShapeAnnotation *pShapeAnnotation, QGraphicsItem *pParent)
: QGraphicsItem(pParent)
{
mpGraphicsView = 0;
mpParentComponent = dynamic_cast<Element*>(pParent);
//mTransformation = 0;
mpReferenceShapeAnnotation = pShapeAnnotation;
mIsInheritedShape = false;
setOldScenePosition(QPointF(0, 0));
mIsCornerItemClicked = false;
mOldAnnotation = "";
if (pShapeAnnotation) {
connect(pShapeAnnotation, SIGNAL(added()), this, SLOT(referenceShapeAdded()));
connect(pShapeAnnotation, SIGNAL(changed()), this, SLOT(referenceShapeChanged()));
connect(pShapeAnnotation, SIGNAL(deleted()), this, SLOT(referenceShapeDeleted()));
}
}
/*!
* \brief ShapeAnnotation::ShapeAnnotation
* \param inheritedShape
* \param pGraphicsView - pointer to GraphicsView
* \param pShapeAnnotation
* \param pParent - pointer to QGraphicsItem
*/
ShapeAnnotation::ShapeAnnotation(bool inheritedShape, GraphicsView *pGraphicsView, ShapeAnnotation *pShapeAnnotation, QGraphicsItem *pParent)
: QGraphicsItem(pParent)
{
mpGraphicsView = pGraphicsView;
mpParentComponent = 0;
mTransformation = Transformation(StringHandler::Diagram);
mpReferenceShapeAnnotation = pShapeAnnotation;
mIsInheritedShape = inheritedShape;
setOldScenePosition(QPointF(0, 0));
mIsCornerItemClicked = false;
mOldAnnotation = "";
createActions();
if (pShapeAnnotation) {
connect(pShapeAnnotation, SIGNAL(added()), this, SLOT(referenceShapeAdded()));
connect(pShapeAnnotation, SIGNAL(changed()), this, SLOT(referenceShapeChanged()));
connect(pShapeAnnotation, SIGNAL(deleted()), this, SLOT(referenceShapeDeleted()));
}
}
int ShapeAnnotation::maxTextLengthToShowOnLibraryIcon = 2;
/*!
* \brief ShapeAnnotation::setDefaults
* Sets the default values for the shape annotations. Defaults valued as defined in Modelica specification 3.2 are used.
* \sa setUserDefaults()
*/
void ShapeAnnotation::setDefaults()
{
mLineColor = QColor(0, 0, 0);
mLinePattern = StringHandler::LineSolid;
mLineThickness = 0.25;
mPoints.clear();
mGeometries.clear();
mArrow.clear();
mArrow.append(StringHandler::ArrowNone);
mArrow.append(StringHandler::ArrowNone);
mArrowSize = 3;
mSmooth = StringHandler::SmoothNone;
mExtents.clear();
mExtents.append(QPointF(0, 0));
mExtents.append(QPointF(0, 0));
mBorderPattern = StringHandler::BorderNone;
mRadius = 0;
mStartAngle = 0;
mEndAngle = 360;
mOriginalTextString = "";
mTextString = "";
mFontSize = 0;
mFontName = Helper::systemFontInfo.family();
mTextStyles.clear();
mHorizontalAlignment = StringHandler::TextAlignmentCenter;
mOriginalFileName = "";
mFileName = "";
mImageSource = "";
mImage = ResourceCache::getImage(":/Resources/icons/bitmap-shape.svg");
mDynamicTextString.clear();
}
/*!
* \brief ShapeAnnotation::setDefaults
* Sets the default values for the shape annotations using another ShapeAnnotation.
* \param pShapeAnnotation
* \sa setUserDefaults()
*/
void ShapeAnnotation::setDefaults(ShapeAnnotation *pShapeAnnotation)
{
mLineColor = pShapeAnnotation->mLineColor;
mLinePattern = pShapeAnnotation->mLinePattern;
mLineThickness = pShapeAnnotation->mLineThickness;
mArrow.append(StringHandler::ArrowNone);
mArrow.append(StringHandler::ArrowNone);
setStartArrow(pShapeAnnotation->getStartArrow());
setEndArrow(pShapeAnnotation->getEndArrow());
mArrowSize = pShapeAnnotation->mArrowSize;
mSmooth = pShapeAnnotation->mSmooth;
setExtents(pShapeAnnotation->getExtents());
mBorderPattern = pShapeAnnotation->mBorderPattern;
mRadius = pShapeAnnotation->mRadius;
mStartAngle = pShapeAnnotation->mStartAngle;
mEndAngle = pShapeAnnotation->mEndAngle;
mOriginalTextString = pShapeAnnotation->mOriginalTextString;
mTextString = pShapeAnnotation->mTextString;
mFontSize = pShapeAnnotation->mFontSize;
mFontName = pShapeAnnotation->mFontName;
mTextStyles = pShapeAnnotation->mTextStyles;
mHorizontalAlignment = pShapeAnnotation->mHorizontalAlignment;
mOriginalFileName = mOriginalFileName;
mFileName = pShapeAnnotation->mFileName;
mClassFileName = pShapeAnnotation->mClassFileName;
mImageSource = pShapeAnnotation->mImageSource;
mImage = pShapeAnnotation->mImage;
mDynamicTextString = pShapeAnnotation->mDynamicTextString;
}
/*!
Reads the user defined line and fill style values. Overrides the Modelica specification 3.2 default values.
\sa setDefaults()
*/
void ShapeAnnotation::setUserDefaults()
{
OptionsDialog *pOptionsDialog = OptionsDialog::instance();
/* Set user Line Style settings */
if (pOptionsDialog->getLineStylePage()->getLineColor().isValid()) {
mLineColor = pOptionsDialog->getLineStylePage()->getLineColor();
}
mLinePattern = StringHandler::getLinePatternType(pOptionsDialog->getLineStylePage()->getLinePattern());
mLineThickness = pOptionsDialog->getLineStylePage()->getLineThickness();
mArrow.replace(0, StringHandler::getArrowType(pOptionsDialog->getLineStylePage()->getLineStartArrow()));
mArrow.replace(1, StringHandler::getArrowType(pOptionsDialog->getLineStylePage()->getLineEndArrow()));
mArrowSize = pOptionsDialog->getLineStylePage()->getLineArrowSize();
if (pOptionsDialog->getLineStylePage()->getLineSmooth()) {
mSmooth = StringHandler::SmoothBezier;
} else {
mSmooth = StringHandler::SmoothNone;
}
/* Set user Fill Style settings */
if (pOptionsDialog->getFillStylePage()->getFillColor().isValid()) {
mFillColor = pOptionsDialog->getFillStylePage()->getFillColor();
}
mFillPattern = StringHandler::getFillPatternType(pOptionsDialog->getFillStylePage()->getFillPattern());
}
bool ShapeAnnotation::isInheritedShape()
{
return mIsInheritedShape;
}
/*!
* \brief ShapeAnnotation::createActions
* Defines the actions used by the shape's context menu.
*/
void ShapeAnnotation::createActions()
{
// shape properties
mpShapePropertiesAction = new QAction(Helper::properties, mpGraphicsView);
mpShapePropertiesAction->setStatusTip(tr("Shows the shape properties"));
connect(mpShapePropertiesAction, SIGNAL(triggered()), SLOT(showShapeProperties()));
// shape attributes
mpAlignInterfacesAction = new QAction(ResourceCache::getIcon(":/Resources/icons/align-interfaces.svg"), Helper::alignInterfaces, mpGraphicsView);
mpAlignInterfacesAction->setStatusTip(Helper::alignInterfacesTip);
connect(mpAlignInterfacesAction, SIGNAL(triggered()), SLOT(alignInterfaces()));
// shape attributes
mpShapeAttributesAction = new QAction(Helper::attributes, mpGraphicsView);
mpShapeAttributesAction->setStatusTip(tr("Shows the shape attributes"));
connect(mpShapeAttributesAction, SIGNAL(triggered()), SLOT(showShapeAttributes()));
// edit transition action
mpEditTransitionAction = new QAction(Helper::editTransition, mpGraphicsView);
mpEditTransitionAction->setStatusTip(tr("Edits the transition"));
connect(mpEditTransitionAction, SIGNAL(triggered()), SLOT(editTransition()));
}
/*!
Adds a transparent path around the shape so that shape selection becomes easy.\n
Otherwise the shape selection can be very difficult because the shape drawing lines can be very small in thickness.
\param path - reference to QPainterPath
\return the path object.
*/
QPainterPath ShapeAnnotation::addPathStroker(QPainterPath &path) const
{
QPainterPathStroker stroker;
stroker.setWidth(Helper::shapesStrokeWidth);
return stroker.createStroke(path);
}
/*!
Returns the bounding rectangle of the shape.
\return the bounding rectangle.
*/
QRectF ShapeAnnotation::getBoundingRect() const
{
QPointF p1 = mExtents.size() > 0 ? mExtents.at(0) : QPointF(-100.0, -100.0);
QPointF p2 = mExtents.size() > 1 ? mExtents.at(1) : QPointF(100.0, 100.0);
return QRectF(p1, p2);
}
/*!
* \brief ShapeAnnotation::applyLinePattern
* Applies the shape line pattern.
* \param painter - pointer to QPainter
*/
void ShapeAnnotation::applyLinePattern(QPainter *painter)
{
qreal thickness = Utilities::convertMMToPixel(mLineThickness);
/* Ticket #4490
* The specification doesn't say anything about it.
* But just to keep this consist with Dymola set a default line thickness for border patterns raised & sunken.
* We need better handling of border patterns.
*/
if (mBorderPattern == StringHandler::BorderRaised || mBorderPattern == StringHandler::BorderSunken) {
thickness = Utilities::convertMMToPixel(0.25);
}
QPen pen(mLineColor, thickness, StringHandler::getLinePatternType(mLinePattern), Qt::SquareCap, Qt::MiterJoin);
/* The specification doesn't say anything about it.
* But just to keep this consist with Dymola we use Qt::BevelJoin for Line shapes.
* All other shapes use Qt::MiterJoin
*/
if (dynamic_cast<LineAnnotation*>(this)) {
pen.setJoinStyle(Qt::BevelJoin);
}
/* Ticket #3222
* Make all the shapes use cosmetic pens so that they perserve their pen width when scaled i.e zoomed in/out.
*/
pen.setCosmetic(true);
/* Ticket #2272, Ticket #2268.
* If thickness is greater than 2 then don't make the pen cosmetic since cosmetic pens don't change the width with respect to zoom.
* Use non cosmetic pens for Libraries Browser and shapes inside component when thickness is greater than 2.
*/
if (thickness > 2
&& ((mpGraphicsView && mpGraphicsView->isRenderingLibraryPixmap())
|| mpParentComponent)) {
pen.setCosmetic(false);
}
// if thickness is greater than 1 pixel then use antialiasing.
if (thickness > 1) {
painter->setRenderHint(QPainter::Antialiasing);
}
painter->setPen(pen);
}
/*!
Applies the shape fill pattern.
\param painter - pointer to QPainter
*/
void ShapeAnnotation::applyFillPattern(QPainter *painter)
{
QRectF boundingRectangle;
if (dynamic_cast<PolygonAnnotation*>(this)) {
boundingRectangle = boundingRect();
} else {
boundingRectangle = getBoundingRect();
}
QLinearGradient linearGradient;
QRadialGradient radialGradient;
switch (mFillPattern) {
case StringHandler::FillHorizontalCylinder:
linearGradient = QLinearGradient(boundingRectangle.center().x(), boundingRectangle.top(), boundingRectangle.center().x(), boundingRectangle.bottom());
linearGradient.setColorAt(0.0, mLineColor);
linearGradient.setColorAt(0.5, mFillColor);
linearGradient.setColorAt(1.0, mLineColor);
painter->setBrush(linearGradient);
break;
case StringHandler::FillVerticalCylinder:
linearGradient = QLinearGradient(boundingRectangle.left(), boundingRectangle.center().y(), boundingRectangle.right(), boundingRectangle.center().y());
linearGradient.setColorAt(0.0, mLineColor);
linearGradient.setColorAt(0.5, mFillColor);
linearGradient.setColorAt(1.0, mLineColor);
painter->setBrush(linearGradient);
break;
case StringHandler::FillSphere:
radialGradient = QRadialGradient(boundingRectangle.center().x(), boundingRectangle.center().y(), boundingRectangle.width());
radialGradient.setColorAt(0.0, mFillColor);
radialGradient.setColorAt(1.0, mLineColor);
painter->setBrush(radialGradient);
break;
case StringHandler::FillSolid:
painter->setBrush(QBrush(mFillColor, StringHandler::getFillPatternType(mFillPattern)));
break;
case StringHandler::FillNone:
break;
default:
painter->setBackgroundMode(Qt::OpaqueMode);
painter->setBackground(mFillColor);
QBrush brush(mLineColor, StringHandler::getFillPatternType(mFillPattern));
brush.setTransform(QTransform(1, 0, 0, 0, 1, 0, 0, 0, 0));
painter->setBrush(brush);
break;
}
}
QList<QPointF> ShapeAnnotation::getExtentsForInheritedShapeFromIconDiagramMap(GraphicsView *pGraphicsView, ShapeAnnotation *pReferenceShapeAnnotation)
{
QPointF defaultPoint1 = QPointF(pGraphicsView->mMergedCoOrdinateSystem.getLeft(), pGraphicsView->mMergedCoOrdinateSystem.getBottom());
QPointF defaultPoint2 = QPointF(pGraphicsView->mMergedCoOrdinateSystem.getRight(), pGraphicsView->mMergedCoOrdinateSystem.getTop());
QPointF point1 = defaultPoint1;
QPointF point2 = defaultPoint2;
int index = pGraphicsView->getModelWidget()->getInheritedClassesList().indexOf(pReferenceShapeAnnotation->getGraphicsView()->getModelWidget()->getLibraryTreeItem()) + 1;
if (index > 0) {
QList<QPointF> extent;
if (pGraphicsView->getViewType() == StringHandler::Icon) {
extent = pGraphicsView->getModelWidget()->getInheritedClassIconMap().value(index).mExtent;
} else {
extent = pGraphicsView->getModelWidget()->getInheritedClassDiagramMap().value(index).mExtent;
}
point1 = extent.size() > 0 ? extent.at(0) : defaultPoint1;
point2 = extent.size() > 1 ? extent.at(1) : defaultPoint2;
// find the width and height
qreal width = qFabs(point1.x() - point2.x());
qreal height = qFabs(point1.y() - point2.y());
if (width < 1 || height < 1) {
point1 = defaultPoint1;
point2 = defaultPoint2;
} else {
/* if preserveAspectRatio of the base class is true
* Take x if width is lesser than height otherwise take y
*/
if (pReferenceShapeAnnotation->getGraphicsView() && pReferenceShapeAnnotation->getGraphicsView()->mMergedCoOrdinateSystem.getPreserveAspectRatio()) {
if (width < height) {
point1.setY(point1.x());
point2.setY(point2.x());
} else {
point1.setX(point1.y());
point2.setX(point2.y());
}
}
}
}
return QList<QPointF>() << point1 << point2;
}
/*!
* \brief ShapeAnnotation::applyTransformation
* Applies the transformation by setting a transformation matrix.
*/
void ShapeAnnotation::applyTransformation()
{
resetTransform();
const bool state = flags().testFlag(QGraphicsItem::ItemSendsGeometryChanges);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
setPos(0, 0);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, state);
mTransformation.setWidth(qFabs(mExtents.at(0).x() - mExtents.at(1).x()));
mTransformation.setHeight(qFabs(mExtents.at(0).y() - mExtents.at(1).y()));
mTransformation.setOrigin(mOrigin);
mTransformation.setRotateAngle(mRotation);
mTransformation.setExtent1(mExtents.at(0));
mTransformation.setExtent2(mExtents.at(1));
setTransform(mTransformation.getTransformationMatrix());
QPointF origin = mOrigin;
// Only apply the extends coordinate extents on the shapes and not on connection, transition etc.
// Don't apply it also on shapes inside Element
// if the extends have some new coordinate extents then use it to scale the shape
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(this);
GraphicsView *pGraphicsView = mpGraphicsView ? mpGraphicsView : mpReferenceShapeAnnotation->getGraphicsView();
if (!mpParentComponent && pGraphicsView
&& !(pLineAnnotation && pLineAnnotation->getLineType() != LineAnnotation::ShapeType)
&& mpReferenceShapeAnnotation && mpReferenceShapeAnnotation->getGraphicsView()) {
QList<QPointF> extendsCoOrdinateExtents = getExtentsForInheritedShapeFromIconDiagramMap(pGraphicsView, mpReferenceShapeAnnotation);
qreal left = pGraphicsView->mMergedCoOrdinateSystem.getLeft();
qreal bottom = pGraphicsView->mMergedCoOrdinateSystem.getBottom();
qreal right = pGraphicsView->mMergedCoOrdinateSystem.getRight();
qreal top = pGraphicsView->mMergedCoOrdinateSystem.getTop();
// map the origin to extends CoOrdinateSystem
origin.setX(Utilities::mapToCoOrdinateSystem(mOrigin.x(), left, right, extendsCoOrdinateExtents.at(0).x(), extendsCoOrdinateExtents.at(1).x()));
origin.setY(Utilities::mapToCoOrdinateSystem(mOrigin.y(), bottom, top, extendsCoOrdinateExtents.at(0).y(), extendsCoOrdinateExtents.at(1).y()));
// scale the shape to new CoOrdinateSystem
const qreal coOrdinateWidth = qFabs(left - right);
const qreal extendsCoOrdinateWidth = qFabs(extendsCoOrdinateExtents.at(0).x() - extendsCoOrdinateExtents.at(1).x());
const qreal sx = extendsCoOrdinateWidth / coOrdinateWidth;
const qreal coOrdinateHeight = qFabs(bottom - top);
const qreal extendsCoOrdinateHeight = qFabs(extendsCoOrdinateExtents.at(0).y() - extendsCoOrdinateExtents.at(1).y());
const qreal sy = extendsCoOrdinateHeight / coOrdinateHeight;
const QTransform scaledTransform = transform() * QTransform::fromScale(sx, sy);
// map the position of shape to new CoOrdinateSystem
const qreal x = Utilities::mapToCoOrdinateSystem(scenePos().x(), left, right, extendsCoOrdinateExtents.at(0).x(), extendsCoOrdinateExtents.at(1).x());
const qreal y = Utilities::mapToCoOrdinateSystem(scenePos().y(), bottom, top, extendsCoOrdinateExtents.at(0).y(), extendsCoOrdinateExtents.at(1).y());
QTransform finalTransform(scaledTransform.m11(), scaledTransform.m12(), scaledTransform.m13(),
scaledTransform.m21(), scaledTransform.m22(), scaledTransform.m23(),
x, y);
setTransform(finalTransform);
}
updateCornerItems();
setOriginItemPos(origin);
}
/*!
* \brief ShapeAnnotation::drawCornerItems
* Draws the CornerItem around the shape.\n
* If the shape is LineAnnotation or PolygonAnnotation then their points are used to draw CornerItem's.\n
* If the shape is RectangleAnnotation, EllipseAnnotation, TextAnnotation or BitmapAnnotation
* then their extents are used to draw CornerItem's.
*/
void ShapeAnnotation::drawCornerItems()
{
if (dynamic_cast<LineAnnotation*>(this) || dynamic_cast<PolygonAnnotation*>(this)) {
for (int i = 0 ; i < mPoints.size() ; i++) {
QPointF point = mPoints.at(i);
CornerItem *pCornerItem = new CornerItem(point.x(), point.y(), i, this);
mCornerItemsList.append(pCornerItem);
}
} else {
QPointF extent1 = QPointF(qMin(mExtents.at(0).x(), mExtents.at(1).x()), qMin(mExtents.at(0).y(), mExtents.at(1).y()));
QPointF extent2 = QPointF(qMax(mExtents.at(0).x(), mExtents.at(1).x()), qMax(mExtents.at(0).y(), mExtents.at(1).y()));
mCornerItemsList.append(new CornerItem(extent1.x(), extent1.y(), 0, this));
mCornerItemsList.append(new CornerItem(extent2.x(), extent2.y(), 1, this));
}
}
/*!
* \brief ShapeAnnotation::setCornerItemsActiveOrPassive
* Makes the corner points of the shape active/passive.
*/
void ShapeAnnotation::setCornerItemsActiveOrPassive()
{
foreach (CornerItem *pCornerItem, mCornerItemsList) {
if (isSelected()) {
pCornerItem->setToolTip(Helper::clickAndDragToResize);
pCornerItem->setVisible(true);
} else {
pCornerItem->setToolTip("");
pCornerItem->setVisible(false);
}
}
if (mpOriginItem) {
if (isSelected()) {
mpOriginItem->setActive();
} else {
mpOriginItem->setPassive();
}
}
}
/*!
* \brief ShapeAnnotation::updateCornerItems
*/
void ShapeAnnotation::updateCornerItems()
{
if (dynamic_cast<LineAnnotation*>(this) || dynamic_cast<PolygonAnnotation*>(this)) {
for (int i = 0 ; i < mCornerItemsList.size() ; i++) {
assert(mPoints.size() > i);
mCornerItemsList.at(i)->setPos(QPointF(mPoints.at(i).x(), mPoints.at(i).y()));
}
} else {
assert(mExtents.size() > 1);
QPointF extent1 = QPointF(qMin(mExtents.at(0).x(), mExtents.at(1).x()), qMin(mExtents.at(0).y(), mExtents.at(1).y()));
QPointF extent2 = QPointF(qMax(mExtents.at(0).x(), mExtents.at(1).x()), qMax(mExtents.at(0).y(), mExtents.at(1).y()));
if (mCornerItemsList.size() > 1) {
mCornerItemsList.at(0)->setPos(QPointF(extent1.x(), extent1.y()));
mCornerItemsList.at(1)->setPos(QPointF(extent2.x(), extent2.y()));
}
}
}
/*!
* \brief ShapeAnnotation::removeCornerItems
* Removes the CornerItem's around the shape.
*/
void ShapeAnnotation::removeCornerItems()
{
foreach (CornerItem *pCornerItem, mCornerItemsList) {
pCornerItem->deleteLater();
}
mCornerItemsList.clear();
}
/*!
* \brief ShapeAnnotation::replaceExtent
* Adds the extent point value.
* \param index - the index of extent point.
* \param point - the point value to add.
*/
void ShapeAnnotation::replaceExtent(const int index, const QPointF point)
{
assert(mExtents.size() > 1);
assert(index >= 0 && index <= 1);
prepareGeometryChange();
mExtents.replace(index, point);
}
/*!
* \brief ShapeAnnotation::updateExtent
* Updates the extent point.
* \param index
* \param point
*/
void ShapeAnnotation::updateExtent(const int index, const QPointF point)
{
assert(mExtents.size() > 1);
assert(index >= 0 && index <= 1);
prepareGeometryChange();
mExtents.replace(index, point);
applyTransformation();
}
void ShapeAnnotation::setOriginItemPos(const QPointF point)
{
if (mpOriginItem) {
mpOriginItem->setPos(point);
}
}
/*!
Sets the text string.
\return textString - the string to set.
*/
void ShapeAnnotation::setTextString(QString textString)
{
mOriginalTextString = textString;
mTextString = textString;
}
/*!
* \brief ShapeAnnotation::setFileName
* Sets the file name.
* \param fileName
*/
void ShapeAnnotation::setFileName(QString fileName)
{
if (fileName.isEmpty()) {
mOriginalFileName = fileName;
mFileName = fileName;
return;
}
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
mOriginalFileName = fileName;
QUrl fileUrl(mOriginalFileName);
QFileInfo fileInfo(mOriginalFileName);
QFileInfo classFileInfo(mClassFileName);
/* if its a modelica:// link then make it absolute path */
if (fileUrl.scheme().toLower().compare("modelica") == 0) {
mFileName = pOMCProxy->uriToFilename(mOriginalFileName);
} else if (fileInfo.isRelative()) {
mFileName = QString(classFileInfo.absoluteDir().absolutePath()).append("/").append(mOriginalFileName);
} else if (fileInfo.isAbsolute()) {
mFileName = mOriginalFileName;
} else {
mFileName = "";
}
}
/*!
Returns the file name.
\return the file name.
*/
QString ShapeAnnotation::getFileName()
{
return mOriginalFileName;
}
/*!
Sets the image source.
\return image - the image source.
*/
void ShapeAnnotation::setImageSource(QString imageSource)
{
mImageSource = imageSource;
}
/*!
Returns the base 64 image source.
\return the image source.
*/
QString ShapeAnnotation::getImageSource()
{
return mImageSource;
}
/*!
Sets the image.
\return image - the QImage object.
*/
void ShapeAnnotation::setImage(QImage image)
{
mImage = image;
}
/*!
Returns the image.
\return the image.
*/
QImage ShapeAnnotation::getImage()
{
return mImage;
}
/*!
* \brief ShapeAnnotation::applyRotation
* Applies the rotation on the shape and sets the shape transformation matrix accordingly.
* \param angle - the rotation angle to apply.
* \sa rotateClockwise() and rotateAntiClockwise()
*/
void ShapeAnnotation::applyRotation(qreal angle)
{
if (angle == 360) {
angle = 0;
}
QString oldAnnotation = getOMCShapeAnnotation();
setRotationAngle(angle);
QString newAnnotation = getOMCShapeAnnotation();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldAnnotation, newAnnotation));
}
/*!
Adjusts the points according to the origin.
*/
void ShapeAnnotation::adjustPointsWithOrigin()
{
QList<QPointF> points;
foreach (QPointF point, mPoints) {
points.append(point - mOrigin);
}
mPoints = points;
}
/*!
Adjusts the extents according to the origin.
*/
void ShapeAnnotation::adjustExtentsWithOrigin()
{
QList<QPointF> extents;
foreach (QPointF extent, mExtents) {
extents.append(extent - mOrigin);
}
mExtents = extents;
}
/*!
Returns the CornerItem located at index.
\param index
\return CornerItem
*/
CornerItem* ShapeAnnotation::getCornerItem(int index)
{
for (int i = 0 ; i < mCornerItemsList.size() ; i++) {
if (mCornerItemsList[i]->getConnectetPointIndex() == index) {
return mCornerItemsList[i];
}
}
return 0;
}
/*!
Updates the position of the CornerItem located at index.
\param index
*/
void ShapeAnnotation::updateCornerItem(int index)
{
CornerItem *pCornerItem = getCornerItem(index);
if (pCornerItem) {
bool signalsState = pCornerItem->blockSignals(true);
bool flagState = pCornerItem->flags().testFlag(QGraphicsItem::ItemSendsGeometryChanges);
pCornerItem->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
if (dynamic_cast<LineAnnotation*>(this) || dynamic_cast<PolygonAnnotation*>(this)) {
pCornerItem->setPos(mPoints.at(index));
} else {
pCornerItem->setPos(mExtents.at(index));
}
pCornerItem->setFlag(QGraphicsItem::ItemSendsGeometryChanges, flagState);
pCornerItem->blockSignals(signalsState);
}
}
/*!
Adds new points, geometries & CornerItems at index. \n
This function is called when resizing the connection lines and new points are needed to keep the lines manhattanized.
\param index
*/
void ShapeAnnotation::insertPointsGeometriesAndCornerItems(int index)
{
QPointF point = (mPoints[index - 1] + mPoints[index]) / 2;