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

Private GIT Repository
added source items
[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->getDirection() == AbstractInterface::Input) {
122       painter->drawPath(params->inArrow);
123     }
124     else if(refInter->getDirection() == AbstractInterface::Output) {
125       painter->drawPath(params->outArrow);
126     } else if(refInter->getDirection() == AbstractInterface::InOut) {
127       painter->drawPath(params->inArrow);
128       painter->drawPath(params->outArrow);
129     }
130
131     // draw names
132
133     // reset to normal if at west
134     if(orientation == Parameters::West){
135       painter->rotate(180);
136     }
137
138     painter->setFont(params->defaultIfaceFont);
139
140     QFontMetrics fm = painter->fontMetrics();
141     int w = nameWidth + owner->getIfaceMargin();
142     int h = nameHeight;
143
144     if(orientation == Parameters::West){
145
146       if(owner->isGroupItem()){
147         painter->drawText(-(w+params->arrowWidth+params->arrowLineLength),-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
148       }
149       else if((owner->isBoxItem()) || (owner->isSourceItem())){
150         painter->drawText(0,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
151       }
152     }
153     else  {
154
155       if(owner->isGroupItem()) {
156         painter->drawText(params->arrowWidth+params->arrowLineLength,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
157       }
158       else if((owner->isBoxItem()) || (owner->isSourceItem())){     
159         painter->drawText(-w,-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
160       }
161     }    
162
163     painter->restore();
164   }
165 }
166
167 QPointF InterfaceItem::getEndPointInGroup() {
168   QPointF p;
169
170   if (owner->isGroupItem()) {
171     p = originPoint;
172   }
173   else {
174     double x = owner->x() + originPoint.x();
175     double y = owner->y() + originPoint.y();
176     switch(orientation){
177     case Parameters::East:
178       x += params->arrowWidth+params->arrowLineLength;
179       break;
180     case Parameters::North:
181       y -= params->arrowWidth+params->arrowLineLength;
182       break;
183     case Parameters::West:
184       x -= params->arrowWidth+params->arrowLineLength;
185       break;
186     case Parameters::South:
187       y += params->arrowWidth+params->arrowLineLength;
188       break;
189     }
190     p = QPointF(x,y);
191   }
192
193   //cout << "iface end point in group item: " << p.x() << "," << p.y() << endl;
194   return p;
195 }
196
197 void InterfaceItem::setOriginPoint() {
198   switch(orientation){
199   case Parameters::East:
200     originPoint = QPointF(owner->getWidth(),position);
201     break;
202   case Parameters::North:
203     originPoint = QPointF(position,0);
204     break;
205   case Parameters::West:
206     originPoint = QPointF(0,position);
207     break;
208   case Parameters::South:
209     originPoint = QPointF(position,owner->getHeight());
210     break;
211   }
212 }
213
214 QString InterfaceItem::getStrOrientation() {
215   QString str = NULL;
216   switch(orientation){
217   case Parameters::North :
218     str = QString("north");
219     break;
220   case Parameters::South :
221     str = QString("south");
222     break;
223   case Parameters::East :
224     str = QString("east");
225     break;
226   case Parameters::West :
227     str = QString("west");
228     break;
229   }
230
231   return str;
232 }
233
234 int InterfaceItem::getIntOrientation(QString str) {
235   if(str == "west") return Parameters::West;
236   if(str == "east") return Parameters::East;
237   if(str == "south") return Parameters::South;
238   if(str == "north") return Parameters::North;
239   return -1;
240 }
241
242
243 void InterfaceItem::updatePosition() {
244   if(orientation == Parameters::North || orientation == Parameters::South){
245     position = positionRatio * owner->getWidth();
246   } else {
247     position = positionRatio * owner->getHeight();
248   }
249   setOriginPoint();
250 }
251
252 void InterfaceItem::addConnectionItem(ConnectionItem* item) {
253   connections.append(item);
254 }
255
256 void InterfaceItem::removeConnectionItem(ConnectionItem* item) {
257   connections.removeOne(item);
258 }
259
260 QDataStream &operator <<(QDataStream &out, InterfaceItem *i) {
261
262 #ifdef DEBUG_INCLFUN
263   out.setVersion(QDataStream::Qt_5);
264
265   QByteArray interfaceData;
266   QDataStream toWrite(&interfaceData, QIODevice::WriteOnly);
267
268   toWrite << i->getId();
269   toWrite << i->getName();
270   toWrite << i->getPositionRatio();
271   toWrite << i->getOrientation();
272
273   foreach(QGraphicsItem* item, i->params->getCurrentScene()->items()){
274     if(item->data(0) == "connection"){
275       ConnectionItem *conn = dynamic_cast<ConnectionItem*>(item);
276       if(conn->getFromInterfaceItem() == i || conn->getToInterfaceItem() == i){
277         toWrite << conn->getId();
278         cout << "id connection : " << conn->getId() << endl;
279       }
280     }
281   }
282   out << interfaceData;
283 #endif
284   return out;
285 }
286
287 QDataStream &operator >>(QDataStream &in, InterfaceItem &i) {
288
289 #ifdef DEBUG_INCLFUN
290   in.setVersion(QDataStream::Qt_5);
291
292   int id, orientation;
293   double positionRatio;
294   QString name;
295
296   in >> id;
297   in >> name;
298   in >> positionRatio;
299   in >> orientation;
300
301   i.setId(id);
302   i.setName(name);
303   i.setPositionRatio(positionRatio);
304   i.setOrientation(orientation);
305   i.updatePosition();
306 #endif
307   return in;
308 }