]> AND Private Git Repository - blast.git/blob - InterfaceItem.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
finished testbench generation
[blast.git] / InterfaceItem.cpp
1 #include "InterfaceItem.h"
2
3 #include "Parameters.h"
4 #include "GroupInterface.h"
5 #include "FunctionalInterface.h"
6 #include "BoxItem.h"
7 #include "ReferenceInterface.h"
8
9 int InterfaceItem::counter = 0;
10
11 InterfaceItem::InterfaceItem(double _position,
12                              int _orientation,
13                              ConnectedInterface *_refInter,
14                              AbstractBoxItem* _owner,
15                              Parameters* _params, bool forceVisible) {
16   positionRatio = _position;
17   orientation = _orientation;
18   refInter = _refInter;
19
20   // CAUTION : the owner must add explicitely this item to its interface, calling addInterface()
21   owner = _owner;
22   params = _params;
23   selected = false;  
24   QFontMetrics fmName(params->defaultIfaceFont);
25   nameWidth = fmName.width(refInter->getName());
26   nameHeight = fmName.height();
27   // by default, only data interface are visible
28   if ((forceVisible) || (refInter->getPurpose() == AbstractInterface::Data)) {
29     visible = true;
30   }
31   else {
32     visible = false;
33   }
34
35   this->id = InterfaceItem::counter++;
36
37   updatePosition();
38 }
39
40
41 InterfaceItem::InterfaceItem(){
42   this->id = counter++;
43 }
44
45 QString InterfaceItem::getName() {
46   return refInter->getName();
47 }
48
49 QString InterfaceItem::getReferenceName() {
50   if (!refInter->isFunctionalInterface()) {
51     return "no_refname_for_groupiface";
52   }
53   FunctionalInterface* iface = AI_TO_FUN(refInter);
54   ReferenceInterface* refIface = iface->getReference();
55   return refIface->getName();
56 }
57
58 void InterfaceItem::updateName(QString name) {  
59   QFontMetrics fmName(params->defaultIfaceFont);
60   nameWidth = fmName.width(refInter->getName());
61   nameHeight = fmName.height();
62   updatePosition();
63 }
64
65
66 /* boundingRect() : give the bounding rect in the blockitem coord. system */
67 QRectF InterfaceItem::boundingRect() const {
68
69   QPointF pointHG;
70   QSizeF s;
71
72   switch(orientation){
73   case Parameters::East :
74     pointHG = QPointF(originPoint.x(),originPoint.y()-(params->arrowHeight/2.0));
75     s = QSizeF(params->arrowWidth+params->arrowLineLength, params->arrowHeight);
76     break;
77   case Parameters::North :
78     pointHG = QPointF(originPoint.x()-(params->arrowHeight/2.0),originPoint.y()-params->arrowWidth-params->arrowLineLength);
79     s = QSizeF(params->arrowHeight,params->arrowWidth+params->arrowLineLength);
80     break;
81   case Parameters::West :
82     pointHG = QPointF(originPoint.x()-params->arrowLineLength-params->arrowWidth,originPoint.y()-(params->arrowHeight/2.0));
83     s = QSizeF(params->arrowWidth+params->arrowLineLength, params->arrowHeight);
84     break;
85   case Parameters::South :
86     pointHG = QPointF(originPoint.x()-(params->arrowHeight/2.0),originPoint.y());
87     s = QSizeF(params->arrowHeight, params->arrowWidth+params->arrowLineLength);
88     break;
89   default :
90     pointHG = QPointF();
91     s = QSizeF();
92     break;
93   }  
94
95   return QRectF(pointHG,s);
96 }
97
98 void InterfaceItem::paint(QPainter *painter) {
99
100   if(visible) {
101
102     painter->save();
103
104     if(selected) {
105       painter->setPen(QPen(Qt::red,2));
106     }    
107     else  {
108       if(owner->isBoxItem()) {
109         painter->setPen(QPen(Qt::black,1));
110       }
111       else if(owner->isStimuliItem()) {
112         painter->setPen(QPen(Qt::darkCyan,1));
113       }
114     }
115
116     painter->translate(originPoint);
117
118     switch(orientation) {
119     case Parameters::North:
120       painter->rotate(-90);
121       break;
122     case Parameters::West:
123       painter->rotate(180);
124       break;
125     case Parameters::South:
126       painter->rotate(90);
127       break;
128     }
129
130     // draw arrows
131     if ( (refInter->getPurpose() == AbstractInterface::Clock) || (refInter->getPurpose() == AbstractInterface::Reset)) {
132       painter->drawPath(params->clkrstArrow);
133     }
134     else if(refInter->getDirection() == AbstractInterface::Input) {
135       painter->drawPath(params->dataArrowIn);
136     }
137     else if(refInter->getDirection() == AbstractInterface::Output) {
138       painter->drawPath(params->dataArrowOut);
139     } else if(refInter->getDirection() == AbstractInterface::InOut) {
140       painter->drawPath(params->dataArrowIn);
141       painter->drawPath(params->dataArrowOut);
142     }
143
144     // paint modifier box if needed
145     ConnectedInterface* connIface = AI_TO_CON(refInter->getAssociatedIface());
146     if ((connIface != NULL) && (connIface->getInputModifier() != NULL)) {
147       painter->save();
148       painter->translate(params->arrowWidth+params->arrowLineLength,0);
149       painter->drawRect(0,-5,10,10);
150       painter->restore();
151     }
152
153
154     // draw names
155     if(selected) {
156       painter->setPen(QPen(Qt::red,2));
157     }    
158     else  {
159       painter->setPen(QPen(Qt::black,1));      
160     }
161
162     // reset to normal if at west
163     if(orientation == Parameters::West){
164       painter->rotate(180);
165     }
166
167     painter->setFont(params->defaultIfaceFont);
168
169     QFontMetrics fm = painter->fontMetrics();
170     int w = nameWidth + owner->getIfaceMargin();
171     int h = nameHeight;
172
173     if(orientation == Parameters::West){
174
175       if(owner->isGroupItem()){
176         painter->drawText(-(w+params->arrowWidth+params->arrowLineLength),-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
177       }
178       else if((owner->isBoxItem()) || (owner->isStimuliItem())){
179         painter->drawText(0,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
180       }
181     }
182     else  {
183
184       if(owner->isGroupItem()) {
185         painter->drawText(params->arrowWidth+params->arrowLineLength,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
186       }
187       else if((owner->isBoxItem()) || (owner->isStimuliItem())){     
188         painter->drawText(-w,-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
189       }
190     }
191
192
193
194     painter->restore();
195   }
196 }
197
198 QPointF InterfaceItem::getStartPosition() {
199   QPointF p = originPoint;
200   QPointF ownerPos = owner->scenePos();  
201   p += ownerPos;
202   return p;
203 }
204
205 QPointF InterfaceItem::getEndPosition() {
206   QPointF p = getStartPosition();
207   
208   switch(orientation){
209   case Parameters::East:
210     p.setX(p.x()+params->arrowWidth+params->arrowLineLength);
211     break;
212   case Parameters::North:
213     p.setY(p.y() - params->arrowWidth-params->arrowLineLength);
214     break;
215   case Parameters::West:
216     p.setX(p.x() - params->arrowWidth-params->arrowLineLength);
217     break;
218   case Parameters::South:
219     p.setY(p.y() + params->arrowWidth+params->arrowLineLength);
220     break;
221   }
222   return p;
223 }
224
225 void InterfaceItem::setOriginPoint() {
226   switch(orientation){
227   case Parameters::East:
228     originPoint = QPointF(owner->getWidth(),position);
229     break;
230   case Parameters::North:
231     originPoint = QPointF(position,0);
232     break;
233   case Parameters::West:
234     originPoint = QPointF(0,position);
235     break;
236   case Parameters::South:
237     originPoint = QPointF(position,owner->getHeight());
238     break;
239   }
240 }
241
242 QString InterfaceItem::getStrOrientation() {
243   QString str = NULL;
244   switch(orientation){
245   case Parameters::North :
246     str = QString("north");
247     break;
248   case Parameters::South :
249     str = QString("south");
250     break;
251   case Parameters::East :
252     str = QString("east");
253     break;
254   case Parameters::West :
255     str = QString("west");
256     break;
257   }
258
259   return str;
260 }
261
262 int InterfaceItem::getIntOrientation(QString str) {
263   if(str == "west") return Parameters::West;
264   if(str == "east") return Parameters::East;
265   if(str == "south") return Parameters::South;
266   if(str == "north") return Parameters::North;
267   return -1;
268 }
269
270
271 void InterfaceItem::updatePosition() {
272   if(orientation == Parameters::North || orientation == Parameters::South){
273     position = positionRatio * owner->getWidth();
274   } else {
275     position = positionRatio * owner->getHeight();
276   }
277   setOriginPoint();
278 }
279
280 void InterfaceItem::addConnectionItem(ConnectionItem* item) {
281   connections.append(item);
282 }
283
284 void InterfaceItem::removeConnectionItem(ConnectionItem* item) {
285   connections.removeAll(item);
286 }
287
288 QDataStream &operator <<(QDataStream &out, InterfaceItem *i) {
289
290 #ifdef DEBUG_INCLFUN
291   out.setVersion(QDataStream::Qt_5);
292
293   QByteArray interfaceData;
294   QDataStream toWrite(&interfaceData, QIODevice::WriteOnly);
295
296   toWrite << i->getId();
297   toWrite << i->getName();
298   toWrite << i->getPositionRatio();
299   toWrite << i->getOrientation();
300
301   foreach(QGraphicsItem* item, i->params->getCurrentScene()->items()){
302     if(item->data(0) == "connection"){
303       ConnectionItem *conn = dynamic_cast<ConnectionItem*>(item);
304       if(conn->getFromInterfaceItem() == i || conn->getToInterfaceItem() == i){
305         toWrite << conn->getId();
306         cout << "id connection : " << conn->getId() << endl;
307       }
308     }
309   }
310   out << interfaceData;
311 #endif
312   return out;
313 }
314
315 QDataStream &operator >>(QDataStream &in, InterfaceItem &i) {
316
317 #ifdef DEBUG_INCLFUN
318   in.setVersion(QDataStream::Qt_5);
319
320   int id, orientation;
321   double positionRatio;
322   QString name;
323
324   in >> id;
325   in >> name;
326   in >> positionRatio;
327   in >> orientation;
328
329   i.setId(id);
330   i.setName(name);
331   i.setPositionRatio(positionRatio);
332   i.setOrientation(orientation);
333   i.updatePosition();
334 #endif
335   return in;
336 }