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

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