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

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