1 #include "InterfaceItem.h"
3 #include "Parameters.h"
4 #include "GroupInterface.h"
5 #include "FunctionalInterface.h"
8 int InterfaceItem::counter = 0;
10 InterfaceItem::InterfaceItem(double _position,
12 ConnectedInterface *_refInter,
13 AbstractBoxItem* _owner,
15 positionRatio = _position;
16 orientation = _orientation;
19 // CAUTION : the owner must add explicitely this item to its interface, calling addInterface()
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) {
34 this->id = InterfaceItem::counter++;
40 InterfaceItem::InterfaceItem(){
44 QString InterfaceItem::getName() {
45 return refInter->getName();
48 void InterfaceItem::updateName(QString name) {
49 QFontMetrics fmName(params->defaultIfaceFont);
50 nameWidth = fmName.width(refInter->getName());
51 nameHeight = fmName.height();
56 /* boundingRect() : give the bounding rect in the blockitem coord. system */
57 QRectF InterfaceItem::boundingRect() const {
63 case Parameters::East :
64 pointHG = QPointF(originPoint.x(),originPoint.y()-(params->arrowHeight/2.0));
65 s = QSizeF(params->arrowWidth+params->arrowLineLength, params->arrowHeight);
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);
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);
75 case Parameters::South :
76 pointHG = QPointF(originPoint.x()-(params->arrowHeight/2.0),originPoint.y());
77 s = QSizeF(params->arrowHeight, params->arrowWidth+params->arrowLineLength);
85 return QRectF(pointHG,s);
88 void InterfaceItem::paint(QPainter *painter) {
95 painter->setPen(QPen(Qt::red,2));
98 painter->setPen(QPen(Qt::black,1));
101 painter->translate(originPoint);
103 switch(orientation) {
104 case Parameters::North:
105 painter->rotate(-90);
107 case Parameters::West:
108 painter->rotate(180);
110 case Parameters::South:
116 if(refInter->getDirection() == AbstractInterface::Input) {
117 painter->drawPath(params->inArrow);
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);
128 // reset to normal if at west
129 if(orientation == Parameters::West){
130 painter->rotate(180);
133 painter->setFont(params->defaultIfaceFont);
135 QFontMetrics fm = painter->fontMetrics();
136 int w = nameWidth + owner->getIfaceMargin();
139 if(orientation == Parameters::West){
141 if(owner->isGroupItem()){
142 painter->drawText(-(w+params->arrowWidth+params->arrowLineLength),-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
144 else if(owner->isBoxItem()){
145 painter->drawText(0,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
150 if(owner->isGroupItem()) {
151 painter->drawText(params->arrowWidth+params->arrowLineLength,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
153 else if(owner->isBoxItem()) {
154 painter->drawText(-w,-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
162 QPointF InterfaceItem::getEndPointInGroup() {
165 if (owner->isGroupItem()) {
169 double x = owner->x() + originPoint.x();
170 double y = owner->y() + originPoint.y();
172 case Parameters::East:
173 x += params->arrowWidth+params->arrowLineLength;
175 case Parameters::North:
176 y -= params->arrowWidth+params->arrowLineLength;
178 case Parameters::West:
179 x -= params->arrowWidth+params->arrowLineLength;
181 case Parameters::South:
182 y += params->arrowWidth+params->arrowLineLength;
188 //cout << "iface end point in group item: " << p.x() << "," << p.y() << endl;
192 void InterfaceItem::setOriginPoint() {
194 case Parameters::East:
195 originPoint = QPointF(owner->getWidth(),position);
197 case Parameters::North:
198 originPoint = QPointF(position,0);
200 case Parameters::West:
201 originPoint = QPointF(0,position);
203 case Parameters::South:
204 originPoint = QPointF(position,owner->getHeight());
209 QString InterfaceItem::getStrOrientation() {
212 case Parameters::North :
213 str = QString("north");
215 case Parameters::South :
216 str = QString("south");
218 case Parameters::East :
219 str = QString("east");
221 case Parameters::West :
222 str = QString("west");
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;
238 void InterfaceItem::updatePosition() {
239 if(orientation == Parameters::North || orientation == Parameters::South){
240 position = positionRatio * owner->getWidth();
242 position = positionRatio * owner->getHeight();
247 void InterfaceItem::addConnectionItem(ConnectionItem* item) {
248 connections.append(item);
251 void InterfaceItem::removeConnectionItem(ConnectionItem* item) {
252 connections.removeOne(item);
255 QDataStream &operator <<(QDataStream &out, InterfaceItem *i) {
258 out.setVersion(QDataStream::Qt_5);
260 QByteArray interfaceData;
261 QDataStream toWrite(&interfaceData, QIODevice::WriteOnly);
263 toWrite << i->getId();
264 toWrite << i->getName();
265 toWrite << i->getPositionRatio();
266 toWrite << i->getOrientation();
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;
277 out << interfaceData;
282 QDataStream &operator >>(QDataStream &in, InterfaceItem &i) {
285 in.setVersion(QDataStream::Qt_5);
288 double positionRatio;
298 i.setPositionRatio(positionRatio);
299 i.setOrientation(orientation);