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

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