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);
131 // paint modifier box if needed
133 if (refInter->getInputModifier() != NULL) {
135 painter->translate(params->arrowWidth+params->arrowLineLength,0);
136 painter->drawRect(0,-5,10,10);
143 painter->setPen(QPen(Qt::red,2));
146 painter->setPen(QPen(Qt::black,1));
149 // reset to normal if at west
150 if(orientation == Parameters::West){
151 painter->rotate(180);
154 painter->setFont(params->defaultIfaceFont);
156 QFontMetrics fm = painter->fontMetrics();
157 int w = nameWidth + owner->getIfaceMargin();
160 if(orientation == Parameters::West){
162 if(owner->isGroupItem()){
163 painter->drawText(-(w+params->arrowWidth+params->arrowLineLength),-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
165 else if((owner->isBoxItem()) || (owner->isSourceItem())){
166 painter->drawText(0,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
171 if(owner->isGroupItem()) {
172 painter->drawText(params->arrowWidth+params->arrowLineLength,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
174 else if((owner->isBoxItem()) || (owner->isSourceItem())){
175 painter->drawText(-w,-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
185 QPointF InterfaceItem::getStartPosition() {
186 QPointF p = originPoint;
187 QPointF ownerPos = owner->scenePos();
192 QPointF InterfaceItem::getEndPosition() {
193 QPointF p = getStartPosition();
196 case Parameters::East:
197 p.setX(p.x()+params->arrowWidth+params->arrowLineLength);
199 case Parameters::North:
200 p.setY(p.y() - params->arrowWidth-params->arrowLineLength);
202 case Parameters::West:
203 p.setX(p.x() - params->arrowWidth-params->arrowLineLength);
205 case Parameters::South:
206 p.setY(p.y() + params->arrowWidth+params->arrowLineLength);
212 void InterfaceItem::setOriginPoint() {
214 case Parameters::East:
215 originPoint = QPointF(owner->getWidth(),position);
217 case Parameters::North:
218 originPoint = QPointF(position,0);
220 case Parameters::West:
221 originPoint = QPointF(0,position);
223 case Parameters::South:
224 originPoint = QPointF(position,owner->getHeight());
229 QString InterfaceItem::getStrOrientation() {
232 case Parameters::North :
233 str = QString("north");
235 case Parameters::South :
236 str = QString("south");
238 case Parameters::East :
239 str = QString("east");
241 case Parameters::West :
242 str = QString("west");
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;
258 void InterfaceItem::updatePosition() {
259 if(orientation == Parameters::North || orientation == Parameters::South){
260 position = positionRatio * owner->getWidth();
262 position = positionRatio * owner->getHeight();
267 void InterfaceItem::addConnectionItem(ConnectionItem* item) {
268 connections.append(item);
271 void InterfaceItem::removeConnectionItem(ConnectionItem* item) {
272 connections.removeAll(item);
275 QDataStream &operator <<(QDataStream &out, InterfaceItem *i) {
278 out.setVersion(QDataStream::Qt_5);
280 QByteArray interfaceData;
281 QDataStream toWrite(&interfaceData, QIODevice::WriteOnly);
283 toWrite << i->getId();
284 toWrite << i->getName();
285 toWrite << i->getPositionRatio();
286 toWrite << i->getOrientation();
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;
297 out << interfaceData;
302 QDataStream &operator >>(QDataStream &in, InterfaceItem &i) {
305 in.setVersion(QDataStream::Qt_5);
308 double positionRatio;
318 i.setPositionRatio(positionRatio);
319 i.setOrientation(orientation);