/
BitmapAnnotation.cpp
216 lines (203 loc) · 7.93 KB
/
BitmapAnnotation.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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, 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 "BitmapAnnotation.h"
#include "Modeling/Commands.h"
BitmapAnnotation::BitmapAnnotation(QString classFileName, QString annotation, GraphicsView *pGraphicsView)
: ShapeAnnotation(false, pGraphicsView, 0)
{
mpComponent = 0;
mClassFileName = classFileName;
// set the default values
GraphicItem::setDefaults();
ShapeAnnotation::setDefaults();
// set users default value by reading the settings file.
ShapeAnnotation::setUserDefaults();
parseShapeAnnotation(annotation);
setShapeFlags(true);
}
BitmapAnnotation::BitmapAnnotation(ShapeAnnotation *pShapeAnnotation, Component *pParent)
: ShapeAnnotation(pParent), mpComponent(pParent)
{
updateShape(pShapeAnnotation);
initUpdateVisible(); // DynamicSelect for visible attribute
setPos(mOrigin);
setRotation(mRotation);
connect(pShapeAnnotation, SIGNAL(updateReferenceShapes()), pShapeAnnotation, SIGNAL(changed()));
connect(pShapeAnnotation, SIGNAL(added()), this, SLOT(referenceShapeAdded()));
connect(pShapeAnnotation, SIGNAL(changed()), this, SLOT(referenceShapeChanged()));
connect(pShapeAnnotation, SIGNAL(deleted()), this, SLOT(referenceShapeDeleted()));
}
BitmapAnnotation::BitmapAnnotation(ShapeAnnotation *pShapeAnnotation, GraphicsView *pGraphicsView)
: ShapeAnnotation(true, pGraphicsView, 0)
{
mpComponent = 0;
updateShape(pShapeAnnotation);
setShapeFlags(true);
mpGraphicsView->addItem(this);
connect(pShapeAnnotation, SIGNAL(updateReferenceShapes()), pShapeAnnotation, SIGNAL(changed()));
connect(pShapeAnnotation, SIGNAL(added()), this, SLOT(referenceShapeAdded()));
connect(pShapeAnnotation, SIGNAL(changed()), this, SLOT(referenceShapeChanged()));
connect(pShapeAnnotation, SIGNAL(deleted()), this, SLOT(referenceShapeDeleted()));
}
void BitmapAnnotation::parseShapeAnnotation(QString annotation)
{
GraphicItem::parseShapeAnnotation(annotation);
// parse the shape to get the list of attributes of Bitmap.
QStringList list = StringHandler::getStrings(annotation);
if (list.size() < 5) {
return;
}
// 4th item is the extent points
QStringList extentsList = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(3)));
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()));
}
// 5th item is the fileName
setFileName(StringHandler::removeFirstLastQuotes(list.at(4)));
// 6th item is the imageSource
if (list.size() >= 6) {
mImageSource = StringHandler::removeFirstLastQuotes(list.at(5));
}
if (!mImageSource.isEmpty()) {
mImage.loadFromData(QByteArray::fromBase64(mImageSource.toLatin1()));
} else if (!mFileName.isEmpty()) {
mImage.load(mFileName);
} else {
mImage = QImage(":/Resources/icons/bitmap-shape.svg");
}
}
QRectF BitmapAnnotation::boundingRect() const
{
return shape().boundingRect();
}
QPainterPath BitmapAnnotation::shape() const
{
QPainterPath path;
path.addRect(getBoundingRect());
return path;
}
void BitmapAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
if (mVisible || !mDynamicVisible.isEmpty())
drawBitmapAnnotaion(painter);
}
void BitmapAnnotation::drawBitmapAnnotaion(QPainter *painter)
{
painter->drawImage(getBoundingRect(), mImage.mirrored());
}
/*!
* \brief BitmapAnnotation::getOMCShapeAnnotation
* Returns Bitmap annotation in format as returned by OMC.
* \return
*/
QString BitmapAnnotation::getOMCShapeAnnotation()
{
QStringList annotationString;
annotationString.append(GraphicItem::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 file name
annotationString.append(QString("\"").append(mOriginalFileName).append("\""));
// get the image source
annotationString.append(QString("\"").append(mImageSource).append("\""));
return annotationString.join(",");
}
/*!
* \brief BitmapAnnotation::getShapeAnnotation
* Returns Bitmap annotation.
* \return
*/
QString BitmapAnnotation::getShapeAnnotation()
{
QStringList annotationString;
annotationString.append(GraphicItem::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 file name
if (!mOriginalFileName.isEmpty()) {
annotationString.append(QString("fileName=\"").append(mOriginalFileName).append("\""));
}
// get the image source
if (!mImageSource.isEmpty()) {
annotationString.append(QString("imageSource=\"").append(mImageSource).append("\""));
}
return QString("Bitmap(").append(annotationString.join(",")).append(")");
}
void BitmapAnnotation::updateShape(ShapeAnnotation *pShapeAnnotation)
{
// set the default values
GraphicItem::setDefaults(pShapeAnnotation);
FilledShape::setDefaults(pShapeAnnotation);
ShapeAnnotation::setDefaults(pShapeAnnotation);
}
/*!
* \brief BitmapAnnotation::duplicate
* Duplicates the shape.
*/
void BitmapAnnotation::duplicate()
{
BitmapAnnotation *pBitmapAnnotation = new BitmapAnnotation(mClassFileName, "", mpGraphicsView);
pBitmapAnnotation->updateShape(this);
QPointF gridStep(mpGraphicsView->mCoOrdinateSystem.getHorizontalGridStep() * 5,
mpGraphicsView->mCoOrdinateSystem.getVerticalGridStep() * 5);
pBitmapAnnotation->setOrigin(mOrigin + gridStep);
pBitmapAnnotation->initializeTransformation();
pBitmapAnnotation->drawCornerItems();
pBitmapAnnotation->setCornerItemsActiveOrPassive();
pBitmapAnnotation->update();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new AddShapeCommand(pBitmapAnnotation));
mpGraphicsView->getModelWidget()->getLibraryTreeItem()->emitShapeAdded(pBitmapAnnotation, mpGraphicsView);
setSelected(false);
pBitmapAnnotation->setSelected(true);
}