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

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