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 if(owner->isBoxItem()) {
99 painter->setPen(QPen(Qt::black,1));
101 else if(owner->isSourceItem()) {
102 painter->setPen(QPen(Qt::darkCyan,1));
106 painter->translate(originPoint);
108 switch(orientation) {
109 case Parameters::North:
110 painter->rotate(-90);
112 case Parameters::West:
113 painter->rotate(180);
115 case Parameters::South:
121 if(refInter->getDirection() == AbstractInterface::Input) {
122 painter->drawPath(params->inArrow);
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);
133 // reset to normal if at west
134 if(orientation == Parameters::West){
135 painter->rotate(180);
138 painter->setFont(params->defaultIfaceFont);
140 QFontMetrics fm = painter->fontMetrics();
141 int w = nameWidth + owner->getIfaceMargin();
144 if(orientation == Parameters::West){
146 if(owner->isGroupItem()){
147 painter->drawText(-(w+params->arrowWidth+params->arrowLineLength),-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
149 else if((owner->isBoxItem()) || (owner->isSourceItem())){
150 painter->drawText(0,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
155 if(owner->isGroupItem()) {
156 painter->drawText(params->arrowWidth+params->arrowLineLength,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
158 else if((owner->isBoxItem()) || (owner->isSourceItem())){
159 painter->drawText(-w,-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
167 QPointF InterfaceItem::getEndPointInGroup() {
170 if (owner->isGroupItem()) {
174 double x = owner->x() + originPoint.x();
175 double y = owner->y() + originPoint.y();
177 case Parameters::East:
178 x += params->arrowWidth+params->arrowLineLength;
180 case Parameters::North:
181 y -= params->arrowWidth+params->arrowLineLength;
183 case Parameters::West:
184 x -= params->arrowWidth+params->arrowLineLength;
186 case Parameters::South:
187 y += params->arrowWidth+params->arrowLineLength;
193 //cout << "iface end point in group item: " << p.x() << "," << p.y() << endl;
197 void InterfaceItem::setOriginPoint() {
199 case Parameters::East:
200 originPoint = QPointF(owner->getWidth(),position);
202 case Parameters::North:
203 originPoint = QPointF(position,0);
205 case Parameters::West:
206 originPoint = QPointF(0,position);
208 case Parameters::South:
209 originPoint = QPointF(position,owner->getHeight());
214 QString InterfaceItem::getStrOrientation() {
217 case Parameters::North :
218 str = QString("north");
220 case Parameters::South :
221 str = QString("south");
223 case Parameters::East :
224 str = QString("east");
226 case Parameters::West :
227 str = QString("west");
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;
243 void InterfaceItem::updatePosition() {
244 if(orientation == Parameters::North || orientation == Parameters::South){
245 position = positionRatio * owner->getWidth();
247 position = positionRatio * owner->getHeight();
252 void InterfaceItem::addConnectionItem(ConnectionItem* item) {
253 connections.append(item);
256 void InterfaceItem::removeConnectionItem(ConnectionItem* item) {
257 connections.removeOne(item);
260 QDataStream &operator <<(QDataStream &out, InterfaceItem *i) {
263 out.setVersion(QDataStream::Qt_5);
265 QByteArray interfaceData;
266 QDataStream toWrite(&interfaceData, QIODevice::WriteOnly);
268 toWrite << i->getId();
269 toWrite << i->getName();
270 toWrite << i->getPositionRatio();
271 toWrite << i->getOrientation();
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;
282 out << interfaceData;
287 QDataStream &operator >>(QDataStream &in, InterfaceItem &i) {
290 in.setVersion(QDataStream::Qt_5);
293 double positionRatio;
303 i.setPositionRatio(positionRatio);
304 i.setOrientation(orientation);