/
TextAnnotation.cpp
650 lines (630 loc) · 25.5 KB
/
TextAnnotation.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
/*
* 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 "TextAnnotation.h"
#include "Modeling/Commands.h"
/*!
* \class TextAnnotation
* \brief Draws the text shapes.
*/
/*!
* \brief TextAnnotation::TextAnnotation
* \param annotation - text annotation string.
* \param inheritedShape
* \param pGraphicsView - pointer to GraphicsView
*/
TextAnnotation::TextAnnotation(QString annotation, GraphicsView *pGraphicsView)
: ShapeAnnotation(false, pGraphicsView, 0, 0)
{
mpComponent = 0;
mpOriginItem = new OriginItem(this);
mpOriginItem->setPassive();
// set the default values
GraphicItem::setDefaults();
FilledShape::setDefaults();
ShapeAnnotation::setDefaults();
// set users default value by reading the settings file.
ShapeAnnotation::setUserDefaults();
parseShapeAnnotation(annotation);
setShapeFlags(true);
}
TextAnnotation::TextAnnotation(ShapeAnnotation *pShapeAnnotation, Element *pParent)
: ShapeAnnotation(pShapeAnnotation, pParent), mpComponent(pParent)
{
mpOriginItem = 0;
updateShape(pShapeAnnotation);
initUpdateTextString();
applyTransformation();
}
TextAnnotation::TextAnnotation(ShapeAnnotation *pShapeAnnotation, GraphicsView *pGraphicsView)
: ShapeAnnotation(true, pGraphicsView, pShapeAnnotation, 0)
{
mpComponent = 0;
mpOriginItem = new OriginItem(this);
mpOriginItem->setPassive();
updateShape(pShapeAnnotation);
setShapeFlags(true);
mpGraphicsView->addItem(this);
mpGraphicsView->addItem(mpOriginItem);
}
TextAnnotation::TextAnnotation(Element *pParent)
: ShapeAnnotation(0, pParent), mpComponent(pParent)
{
mpOriginItem = 0;
// set the default values
GraphicItem::setDefaults();
FilledShape::setDefaults();
ShapeAnnotation::setDefaults();
// give a reasonable size to default component text
mExtents.replace(0, QPointF(-100, -50));
mExtents.replace(1, QPointF(100, 50));
setTextString("%name");
initUpdateTextString();
setPos(mOrigin);
setRotation(mRotation);
}
TextAnnotation::TextAnnotation(QString annotation, LineAnnotation *pLineAnnotation)
: ShapeAnnotation(0, pLineAnnotation)
{
mpComponent = 0;
mpOriginItem = 0;
// set the default values
GraphicItem::setDefaults();
FilledShape::setDefaults();
ShapeAnnotation::setDefaults();
parseShapeAnnotation(annotation);
updateTextString();
/* From Modelica Spec 33revision1,
* The extent of the Text is interpreted relative to either the first point of the Line, in the case of immediate=false,
* or the last point (immediate=true).
*/
if (pLineAnnotation->getPoints().size() > 0) {
if (pLineAnnotation->getImmediate()) {
setPos(pLineAnnotation->getPoints().last());
} else {
setPos(pLineAnnotation->getPoints().first());
}
}
}
/*!
* \brief TextAnnotation::TextAnnotation
* Used by OMSimulator FMU ModelWidget\n
* We always make this shape as inherited shape since its not allowed to be modified.
* \param pGraphicsView
*/
TextAnnotation::TextAnnotation(GraphicsView *pGraphicsView)
: ShapeAnnotation(true, pGraphicsView, 0, 0)
{
mpComponent = 0;
mpOriginItem = 0;
// set the default values
GraphicItem::setDefaults();
FilledShape::setDefaults();
ShapeAnnotation::setDefaults();
// give a reasonable size
mExtents.replace(0, QPointF(-100, 20));
mExtents.replace(1, QPointF(100, -20));
setTextString("%name");
initUpdateTextString();
setPos(mOrigin);
setRotation(mRotation);
setShapeFlags(true);
}
/*!
* \brief TextAnnotation::parseShapeAnnotation
* Parses the text annotation string
* \param annotation - text annotation string.
*/
void TextAnnotation::parseShapeAnnotation(QString annotation)
{
GraphicItem::parseShapeAnnotation(annotation);
FilledShape::parseShapeAnnotation(annotation);
// parse the shape to get the list of attributes of Text.
QStringList list = StringHandler::getStrings(annotation);
if (list.size() < 15) {
return;
}
// 9th item of the list contains the extent points
QStringList extentsList = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(8)));
for (int i = 0 ; i < qMin(extentsList.size(), 2) ; i++) {
QStringList extentPoints = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(extentsList[i]));
if (extentPoints.size() >= 2)
mExtents.replace(i, QPointF(extentPoints.at(0).toFloat(), extentPoints.at(1).toFloat()));
}
// 10th item of the list contains the textString.
if (list.at(9).startsWith("{")) {
// DynamicSelect
QStringList args = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(9)));
if (args.count() > 0) {
mOriginalTextString = StringHandler::removeFirstLastQuotes(args.at(0));
}
if (args.count() > 1) {
mDynamicTextString << args.at(1); // variable name
}
if (args.count() > 2) {
mDynamicTextString << args.at(2); // significantDigits
}
} else {
mOriginalTextString = StringHandler::removeFirstLastQuotes(list.at(9));
}
mTextString = mOriginalTextString;
initUpdateTextString();
// 11th item of the list contains the fontSize.
mFontSize = list.at(10).toFloat();
// 12th item of the list contains the optional textColor, {-1, -1, -1} if not set
QStringList textColorList = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(11)));
if (textColorList.size() >= 3) {
int red, green, blue = 0;
red = textColorList.at(0).toInt();
green = textColorList.at(1).toInt();
blue = textColorList.at(2).toInt();
if (red >= 0 && green >= 0 && blue >= 0) {
mLineColor = QColor (red, green, blue);
}
}
// 13th item of the list contains the font name.
QString fontName = StringHandler::removeFirstLastQuotes(list.at(12));
if (!fontName.isEmpty()) {
mFontName = fontName;
}
// 14th item of the list contains the text styles.
QStringList textStyles = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(13)));
foreach (QString textStyle, textStyles) {
if (textStyle == "TextStyle.Bold") {
mTextStyles.append(StringHandler::TextStyleBold);
} else if (textStyle == "TextStyle.Italic") {
mTextStyles.append(StringHandler::TextStyleItalic);
} else if (textStyle == "TextStyle.UnderLine") {
mTextStyles.append(StringHandler::TextStyleUnderLine);
}
}
// 15th item of the list contains the text alignment.
QString horizontalAlignment = StringHandler::removeFirstLastQuotes(list.at(14));
if (horizontalAlignment == "TextAlignment.Left") {
mHorizontalAlignment = StringHandler::TextAlignmentLeft;
} else if (horizontalAlignment == "TextAlignment.Center") {
mHorizontalAlignment = StringHandler::TextAlignmentCenter;
} else if (horizontalAlignment == "TextAlignment.Right") {
mHorizontalAlignment = StringHandler::TextAlignmentRight;
}
}
/*!
* \brief TextAnnotation::boundingRect
* Defines the bounding rectangle of the shape.
* \return bounding rectangle
*/
QRectF TextAnnotation::boundingRect() const
{
return shape().boundingRect();
}
/*!
* \brief TextAnnotation::shape
* Defines the shape path
* \return shape path
*/
QPainterPath TextAnnotation::shape() const
{
QPainterPath path;
path.addRect(getBoundingRect());
return path;
}
/*!
* \brief TextAnnotation::paint
* Reimplementation of QGraphicsItem::paint.
* \param painter
* \param option
* \param widget
*/
void TextAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
//! @note We don't show text annotation that contains % for Library Icons or if it is too long.
if (mpGraphicsView && mpGraphicsView->isRenderingLibraryPixmap()) {
if (mOriginalTextString.contains("%") || mOriginalTextString.length() > maxTextLengthToShowOnLibraryIcon) {
return;
}
} else if (mpComponent && mpComponent->getGraphicsView()->isRenderingLibraryPixmap()) {
return;
}
if (mVisible || !mDynamicVisible.isEmpty()) {
// state machine visualization
// text annotation on a component
if (mpComponent && mpComponent->getLibraryTreeItem() && mpComponent->getLibraryTreeItem()->isState()
&& mpComponent->getGraphicsView()->isVisualizationView()) {
if (mpComponent->isActiveState()) {
painter->setOpacity(1.0);
} else {
painter->setOpacity(0.2);
}
}
// text annotation on a transition
LineAnnotation *pTransitionLineAnnotation = dynamic_cast<LineAnnotation*>(parentItem());
if (pTransitionLineAnnotation && pTransitionLineAnnotation->getLineType() == LineAnnotation::TransitionType
&& pTransitionLineAnnotation->getGraphicsView() && pTransitionLineAnnotation->getGraphicsView()->isVisualizationView()) {
if (pTransitionLineAnnotation->isActiveState()) {
painter->setOpacity(1.0);
} else {
painter->setOpacity(0.2);
}
}
if (!mDynamicVisibleValue && ((mpGraphicsView && mpGraphicsView->isVisualizationView())
|| (mpParentComponent && mpParentComponent->getGraphicsView()->isVisualizationView()))) {
return;
}
drawTextAnnotaion(painter);
}
}
/*!
* \brief TextAnnotation::drawTextAnnotaion
* Draws the Text annotation
* \param painter
*/
void TextAnnotation::drawTextAnnotaion(QPainter *painter)
{
applyLinePattern(painter);
/* Don't apply the fill patterns on Text shapes. */
/*applyFillPattern(painter);*/
// store the existing transformations
const QTransform painterTransform = painter->transform();
const qreal scaleX = painterTransform.m11();
const qreal scaleY = painterTransform.m22();
const qreal shearX = painterTransform.m21();
const qreal shearY = painterTransform.m12();
// set new transformation for the text based on rotation or scale.
qreal sx, sy;
if (painterTransform.type() == QTransform::TxRotate) {
// if no flip or XY flip
if ((shearX >= 0 && shearY >= 0) || (shearX < 0 && shearY < 0)) {
painter->setTransform(QTransform(painterTransform.m11(), (shearY < 0 ? -1.0 : 1.0), painterTransform.m13(),
(shearX >= 0 ? -1.0 : 1.0), painterTransform.m22(), painterTransform.m23(),
painterTransform.m31(), painterTransform.m32(), painterTransform.m33()));
sx = shearX * (shearX < 0 ? -1.0 : 1.0);
sy = shearY * (shearY >= 0 ? -1.0 : 1.0);
} else { // if x or y flip
painter->setTransform(QTransform(painterTransform.m11(), (shearY < 0 ? 1.0 : -1.0), painterTransform.m13(),
(shearX >= 0 ? -1.0 : 1.0), painterTransform.m22(), painterTransform.m23(),
painterTransform.m31(), painterTransform.m32(), painterTransform.m33()));
sx = shearX * (shearX < 0 ? 1.0 : -1.0);
sy = shearY * (shearY >= 0 ? -1.0 : 1.0);
}
} else {
painter->setTransform(QTransform(1.0, painterTransform.m12(), painterTransform.m13(),
painterTransform.m21(), 1.0, painterTransform.m23(),
painterTransform.m31(), painterTransform.m32(), painterTransform.m33()));
sx = scaleX;
sy = scaleY;
}
// map the existing bounding rect to new transformation
QRectF mappedBoundingRect = QRectF(boundingRect().x() * sx, boundingRect().y() * sy, boundingRect().width() * sx, boundingRect().height() * sy);
// map the existing bounding rect to new transformation but with positive width and height so that font metrics can work
QRectF absMappedBoundingRect = QRectF(boundingRect().x() * sx, boundingRect().y() * sy, qAbs(boundingRect().width() * sx), qAbs(boundingRect().height() * sy));
// normalize the text for drawing
mTextString = StringHandler::removeFirstLastQuotes(mTextString);
mTextString = StringHandler::unparse(QString("\"").append(mTextString).append("\""));
// Don't create new QFont instead get a font from painter and set the values on it and set it back.
QFont font = painter->font();
font.setFamily(mFontName);
if (mFontSize > 0) {
font.setPointSizeF(mFontSize);
}
font.setWeight(StringHandler::getFontWeight(mTextStyles));
font.setItalic(StringHandler::getFontItalic(mTextStyles));
// set font underline
if (StringHandler::getFontUnderline(mTextStyles)) {
font.setUnderline(true);
}
painter->setFont(font);
/* From Modelica specification version 3.5-dev
* "The style attribute fontSize specifies the font size. If the fontSize attribute is 0 the text is scaled to fit its extent. Otherwise, the size specifies the absolute size."
*/
// if absolute font size is defined and is greater than 0 then we don't need to calculate the font size.
if (mFontSize <= 0) {
QFontMetrics fontMetrics(painter->font());
QRect fontBoundRect = fontMetrics.boundingRect(absMappedBoundingRect.toRect(), Qt::TextDontClip, mTextString);
const qreal xFactor = absMappedBoundingRect.width() / fontBoundRect.width();
const qreal yFactor = absMappedBoundingRect.height() / fontBoundRect.height();
/* Ticket:4256
* Text aspect when x1=x2 i.e, width is 0.
* Use height.
*/
const qreal factor = (absMappedBoundingRect.width() != 0 && xFactor < yFactor) ? xFactor : yFactor;
qreal fontSizeFactor = font.pointSizeF() * factor;
// Yes we don't go below Helper::minimumTextFontSize font pt.
font.setPointSizeF(qMax(fontSizeFactor, Helper::minimumTextFontSize));
painter->setFont(font);
}
/* Try to get the elided text if calculated font size <= Helper::minimumTextFontSize
* OR if font size is absolute.
*/
QString textToDraw = mTextString;
if (absMappedBoundingRect.width() > 1 && ((mFontSize <= 0 && painter->font().pointSizeF() <= Helper::minimumTextFontSize) || mFontSize > 0)) {
QFontMetrics fontMetrics(painter->font());
textToDraw = fontMetrics.elidedText(mTextString, Qt::ElideRight, absMappedBoundingRect.width());
// if we get "..." i.e., QChar(0x2026) as textToDraw then don't draw anything
if (textToDraw.compare(QChar(0x2026)) == 0) {
textToDraw = "";
}
}
// draw the font
if (mpComponent || mappedBoundingRect.width() != 0 || mappedBoundingRect.height() != 0) {
painter->drawText(mappedBoundingRect, StringHandler::getTextAlignment(mHorizontalAlignment) | Qt::AlignVCenter | Qt::TextDontClip, textToDraw);
}
}
/*!
* \brief TextAnnotation::getOMCShapeAnnotation
* \return the shape annotation in format as returned by OMC.
*/
QString TextAnnotation::getOMCShapeAnnotation()
{
QStringList annotationString;
annotationString.append(GraphicItem::getOMCShapeAnnotation());
annotationString.append(FilledShape::getOMCShapeAnnotation());
// get the extents
QString extentString;
extentString.append("{");
extentString.append("{").append(QString::number(mExtents.at(0).x())).append(",");
extentString.append(QString::number(mExtents.at(0).y())).append("},");
extentString.append("{").append(QString::number(mExtents.at(1).x())).append(",");
extentString.append(QString::number(mExtents.at(1).y())).append("}");
extentString.append("}");
annotationString.append(extentString);
// get the text string
annotationString.append(QString("\"").append(mOriginalTextString).append("\""));
// get the font size
annotationString.append(QString::number(mFontSize));
// get the text color
QString textColorString;
textColorString.append("{");
textColorString.append(QString::number(mLineColor.red())).append(",");
textColorString.append(QString::number(mLineColor.green())).append(",");
textColorString.append(QString::number(mLineColor.blue()));
textColorString.append("}");
annotationString.append(textColorString);
// get the font name
if (!mFontName.isEmpty() && mFontName.compare(Helper::systemFontInfo.family()) != 0) {
annotationString.append(QString("\"").append(mFontName).append("\""));
} else {
annotationString.append(QString("\"\""));
}
// get the font styles
QString textStylesString;
QStringList stylesList;
textStylesString.append("{");
for (int i = 0 ; i < mTextStyles.size() ; i++) {
stylesList.append(StringHandler::getTextStyleString(mTextStyles[i]));
}
textStylesString.append(stylesList.join(","));
textStylesString.append("}");
annotationString.append(textStylesString);
// get the font horizontal alignment
annotationString.append(StringHandler::getTextAlignmentString(mHorizontalAlignment));
return annotationString.join(",");
}
/*!
* \brief TextAnnotation::getOMCShapeAnnotationWithShapeName
* \return the shape annotation in format as returned by OMC wrapped in Text keyword.
*/
QString TextAnnotation::getOMCShapeAnnotationWithShapeName()
{
return QString("Text(%1)").arg(getOMCShapeAnnotation());
}
/*!
* \brief TextAnnotation::getShapeAnnotation
* \return the shape annotation in Modelica syntax.
*/
QString TextAnnotation::getShapeAnnotation()
{
QStringList annotationString;
annotationString.append(GraphicItem::getShapeAnnotation());
annotationString.append(FilledShape::getShapeAnnotation());
// get the extents
if (mExtents.size() > 1) {
QString extentString;
extentString.append("extent={");
extentString.append("{").append(QString::number(mExtents.at(0).x())).append(",");
extentString.append(QString::number(mExtents.at(0).y())).append("},");
extentString.append("{").append(QString::number(mExtents.at(1).x())).append(",");
extentString.append(QString::number(mExtents.at(1).y())).append("}");
extentString.append("}");
annotationString.append(extentString);
}
// get the text string
annotationString.append(QString("textString=\"").append(mOriginalTextString).append("\""));
// get the font size
if (mFontSize != 0) {
annotationString.append(QString("fontSize=").append(QString::number(mFontSize)));
}
// get the font name
/* Ticket:4204
* Don't insert the default font name as it might be operating system specific.
*/
if (!mFontName.isEmpty() && mFontName.compare(Helper::systemFontInfo.family()) != 0) {
annotationString.append(QString("fontName=\"").append(mFontName).append("\""));
}
// get the font styles
QString textStylesString;
QStringList stylesList;
if (mTextStyles.size() > 0) {
textStylesString.append("textStyle={");
}
for (int i = 0 ; i < mTextStyles.size() ; i++) {
stylesList.append(StringHandler::getTextStyleString(mTextStyles[i]));
}
if (mTextStyles.size() > 0) {
textStylesString.append(stylesList.join(","));
textStylesString.append("}");
annotationString.append(textStylesString);
}
// get the font horizontal alignment
if (mHorizontalAlignment != StringHandler::TextAlignmentCenter) {
annotationString.append(QString("horizontalAlignment=").append(StringHandler::getTextAlignmentString(mHorizontalAlignment)));
}
return QString("Text(").append(annotationString.join(",")).append(")");
}
void TextAnnotation::updateShape(ShapeAnnotation *pShapeAnnotation)
{
// set the default values
GraphicItem::setDefaults(pShapeAnnotation);
FilledShape::setDefaults(pShapeAnnotation);
ShapeAnnotation::setDefaults(pShapeAnnotation);
}
void TextAnnotation::initUpdateTextString()
{
if (mpComponent) {
if (mOriginalTextString.contains("%")) {
updateTextString();
connect(mpComponent, SIGNAL(displayTextChanged()), SLOT(updateTextString()), Qt::UniqueConnection);
}
}
}
/*!
* \brief TextAnnotation::updateTextStringHelper
* Helper function for TextAnnotation::updateTextString()
* \param regExp
*/
void TextAnnotation::updateTextStringHelper(QRegExp regExp)
{
int pos = 0;
while ((pos = regExp.indexIn(mTextString, pos)) != -1) {
QString variable = regExp.cap(0).trimmed();
if ((!variable.isEmpty()) && (variable.compare("%%") != 0) && (variable.compare("%name") != 0) && (variable.compare("%class") != 0)) {
variable.remove("%");
variable = StringHandler::removeFirstLastCurlBrackets(variable);
if (!variable.isEmpty()) {
QString textValue;
/* Ticket:4204
* If we have extend component then call Element::getParameterDisplayString from root component.
*/
textValue = mpComponent->getRootParentComponent()->getParameterDisplayString(variable);
if (!textValue.isEmpty()) {
QString unit = "";
QString displaytUnit = "";
Element *pElement = mpComponent->getRootParentComponent()->getElementByName(variable);
if (pElement) {
displaytUnit = pElement->getDerivedClassModifierValue("displaytUnit");
if (displaytUnit.isEmpty()) {
unit = pElement->getDerivedClassModifierValue("unit");
displaytUnit = unit;
}
}
if (displaytUnit.isEmpty()) {
mTextString.replace(pos, regExp.matchedLength(), textValue);
pos += textValue.length();
} else {
displaytUnit = Utilities::convertUnitToSymbol(displaytUnit);
QString textValueWithDisplayUnit = QString("%1 %2").arg(textValue, displaytUnit);
mTextString.replace(pos, regExp.matchedLength(), textValueWithDisplayUnit);
pos += textValueWithDisplayUnit.length();
}
} else { /* if the value of %\\W* is empty then remove the % sign. */
mTextString.replace(pos, 1, "");
}
} else { /* if there is just alone % then remove it. Because if you want to print % then use %%. */
mTextString.replace(pos, 1, "");
}
} else if (variable.compare("%%") == 0) { /* if string is %% then just move over it. We replace it with % in TextAnnotation::updateTextString(). */
pos += regExp.matchedLength();
}
}
}
/*!
* \brief TextAnnotation::updateTextString
* Updates the text to display.
*/
void TextAnnotation::updateTextString()
{
/* From Modelica Spec 32revision2,
* There are a number of common macros that can be used in the text, and they should be replaced when displaying
* the text as follows:
* - %par replaced by the value of the parameter par. The intent is that the text is easily readable, thus if par is
* of an enumeration type, replace %par by the item name, not by the full name.
* [Example: if par="Modelica.Blocks.Types.Enumeration.Periodic", then %par should be displayed as "Periodic"]
* - %% replaced by %
* - %name replaced by the name of the component (i.e. the identifier for it in in the enclosing class).
* - %class replaced by the name of the class.
*/
LineAnnotation *pLineAnnotation = dynamic_cast<LineAnnotation*>(parentItem());
if (pLineAnnotation) {
if (mOriginalTextString.toLower().contains("%condition")) {
if (!pLineAnnotation->getCondition().isEmpty()) {
mTextString.replace(QRegExp("%condition"), pLineAnnotation->getCondition());
}
if (pLineAnnotation->getPriority() > 1) {
mTextString.prepend(QString("%1: ").arg(pLineAnnotation->getPriority()));
}
}
} else if (mpComponent) {
mTextString = mOriginalTextString;
if (!mTextString.contains("%")) {
return;
}
if (mOriginalTextString.toLower().contains("%name")) {
mTextString.replace(QRegExp("%name"), mpComponent->getName());
}
if (mOriginalTextString.toLower().contains("%class") && mpComponent->getLibraryTreeItem()) {
mTextString.replace(QRegExp("%class"), mpComponent->getLibraryTreeItem()->getNameStructure());
}
if (!mTextString.contains("%")) {
return;
}
/* handle variables now */
updateTextStringHelper(QRegExp("(%%|%\\{?\\w+(\\.\\w+)*\\}?)"));
/* call again with non-word characters so invalid % can be removed. */
updateTextStringHelper(QRegExp("(%%|%\\{?\\W+(\\.\\W+)*\\}?)"));
/* handle %% */
if (mOriginalTextString.toLower().contains("%%")) {
mTextString.replace(QRegExp("%%"), "%");
}
}
}
/*!
* \brief TextAnnotation::duplicate
* Duplicates the shape.
*/
void TextAnnotation::duplicate()
{
TextAnnotation *pTextAnnotation = new TextAnnotation("", mpGraphicsView);
pTextAnnotation->updateShape(this);
QPointF gridStep(mpGraphicsView->mMergedCoOrdinateSystem.getHorizontalGridStep() * 5,
mpGraphicsView->mMergedCoOrdinateSystem.getVerticalGridStep() * 5);
pTextAnnotation->setOrigin(mOrigin + gridStep);
pTextAnnotation->drawCornerItems();
pTextAnnotation->setCornerItemsActiveOrPassive();
pTextAnnotation->applyTransformation();
pTextAnnotation->update();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new AddShapeCommand(pTextAnnotation));
mpGraphicsView->getModelWidget()->getLibraryTreeItem()->emitShapeAdded(pTextAnnotation, mpGraphicsView);
setSelected(false);
pTextAnnotation->setSelected(true);
}