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,
14 Parameters* _params) {
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 painter->setPen(QPen(Qt::red,2));
136 painter->setPen(QPen(Qt::black,1));
139 // reset to normal if at west
140 if(orientation == Parameters::West){
141 painter->rotate(180);
144 painter->setFont(params->defaultIfaceFont);
146 QFontMetrics fm = painter->fontMetrics();
147 int w = nameWidth + owner->getIfaceMargin();
150 if(orientation == Parameters::West){
152 if(owner->isGroupItem()){
153 painter->drawText(-(w+params->arrowWidth+params->arrowLineLength),-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
155 else if((owner->isBoxItem()) || (owner->isSourceItem())){
156 painter->drawText(0,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
161 if(owner->isGroupItem()) {
162 painter->drawText(params->arrowWidth+params->arrowLineLength,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
164 else if((owner->isBoxItem()) || (owner->isSourceItem())){
165 painter->drawText(-w,-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
173 QPointF InterfaceItem::getStartPosition() {
174 QPointF p = originPoint;
175 QPointF ownerPos = owner->scenePos();
180 QPointF InterfaceItem::getEndPosition() {
181 QPointF p = getStartPosition();
184 case Parameters::East:
185 p.setX(p.x()+params->arrowWidth+params->arrowLineLength);
187 case Parameters::North:
188 p.setY(p.y() - params->arrowWidth-params->arrowLineLength);
190 case Parameters::West:
191 p.setX(p.x() - params->arrowWidth-params->arrowLineLength);
193 case Parameters::South:
194 p.setY(p.y() + params->arrowWidth+params->arrowLineLength);
200 void InterfaceItem::setOriginPoint() {
202 case Parameters::East:
203 originPoint = QPointF(owner->getWidth(),position);
205 case Parameters::North:
206 originPoint = QPointF(position,0);
208 case Parameters::West:
209 originPoint = QPointF(0,position);
211 case Parameters::South:
212 originPoint = QPointF(position,owner->getHeight());
217 QString InterfaceItem::getStrOrientation() {
220 case Parameters::North :
221 str = QString("north");
223 case Parameters::South :
224 str = QString("south");
226 case Parameters::East :
227 str = QString("east");
229 case Parameters::West :
230 str = QString("west");
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;
246 void InterfaceItem::updatePosition() {
247 if(orientation == Parameters::North || orientation == Parameters::South){
248 position = positionRatio * owner->getWidth();
250 position = positionRatio * owner->getHeight();
255 void InterfaceItem::addConnectionItem(ConnectionItem* item) {
256 connections.append(item);
259 void InterfaceItem::removeConnectionItem(ConnectionItem* item) {
260 connections.removeAll(item);
263 QDataStream &operator <<(QDataStream &out, InterfaceItem *i) {
266 out.setVersion(QDataStream::Qt_5);
268 QByteArray interfaceData;
269 QDataStream toWrite(&interfaceData, QIODevice::WriteOnly);
271 toWrite << i->getId();
272 toWrite << i->getName();
273 toWrite << i->getPositionRatio();
274 toWrite << i->getOrientation();
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;
285 out << interfaceData;
290 QDataStream &operator >>(QDataStream &in, InterfaceItem &i) {
293 in.setVersion(QDataStream::Qt_5);
296 double positionRatio;
306 i.setPositionRatio(positionRatio);
307 i.setOrientation(orientation);