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, bool forceVisible) {
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 ((forceVisible) || (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->getPurpose() == AbstractInterface::Clock) || (refInter->getPurpose() == AbstractInterface::Reset)) {
122 painter->drawPath(params->clkrstArrow);
124 else if(refInter->getDirection() == AbstractInterface::Input) {
125 painter->drawPath(params->dataArrowIn);
127 else if(refInter->getDirection() == AbstractInterface::Output) {
128 painter->drawPath(params->dataArrowOut);
129 } else if(refInter->getDirection() == AbstractInterface::InOut) {
130 painter->drawPath(params->dataArrowIn);
131 painter->drawPath(params->dataArrowOut);
134 // paint modifier box if needed
135 ConnectedInterface* connIface = AI_TO_CON(refInter->getAssociatedIface());
136 if ((connIface != NULL) && (connIface->getInputModifier() != NULL)) {
138 painter->translate(params->arrowWidth+params->arrowLineLength,0);
139 painter->drawRect(0,-5,10,10);
146 painter->setPen(QPen(Qt::red,2));
149 painter->setPen(QPen(Qt::black,1));
152 // reset to normal if at west
153 if(orientation == Parameters::West){
154 painter->rotate(180);
157 painter->setFont(params->defaultIfaceFont);
159 QFontMetrics fm = painter->fontMetrics();
160 int w = nameWidth + owner->getIfaceMargin();
163 if(orientation == Parameters::West){
165 if(owner->isGroupItem()){
166 painter->drawText(-(w+params->arrowWidth+params->arrowLineLength),-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
168 else if((owner->isBoxItem()) || (owner->isSourceItem())){
169 painter->drawText(0,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
174 if(owner->isGroupItem()) {
175 painter->drawText(params->arrowWidth+params->arrowLineLength,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
177 else if((owner->isBoxItem()) || (owner->isSourceItem())){
178 painter->drawText(-w,-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
188 QPointF InterfaceItem::getStartPosition() {
189 QPointF p = originPoint;
190 QPointF ownerPos = owner->scenePos();
195 QPointF InterfaceItem::getEndPosition() {
196 QPointF p = getStartPosition();
199 case Parameters::East:
200 p.setX(p.x()+params->arrowWidth+params->arrowLineLength);
202 case Parameters::North:
203 p.setY(p.y() - params->arrowWidth-params->arrowLineLength);
205 case Parameters::West:
206 p.setX(p.x() - params->arrowWidth-params->arrowLineLength);
208 case Parameters::South:
209 p.setY(p.y() + params->arrowWidth+params->arrowLineLength);
215 void InterfaceItem::setOriginPoint() {
217 case Parameters::East:
218 originPoint = QPointF(owner->getWidth(),position);
220 case Parameters::North:
221 originPoint = QPointF(position,0);
223 case Parameters::West:
224 originPoint = QPointF(0,position);
226 case Parameters::South:
227 originPoint = QPointF(position,owner->getHeight());
232 QString InterfaceItem::getStrOrientation() {
235 case Parameters::North :
236 str = QString("north");
238 case Parameters::South :
239 str = QString("south");
241 case Parameters::East :
242 str = QString("east");
244 case Parameters::West :
245 str = QString("west");
252 int InterfaceItem::getIntOrientation(QString str) {
253 if(str == "west") return Parameters::West;
254 if(str == "east") return Parameters::East;
255 if(str == "south") return Parameters::South;
256 if(str == "north") return Parameters::North;
261 void InterfaceItem::updatePosition() {
262 if(orientation == Parameters::North || orientation == Parameters::South){
263 position = positionRatio * owner->getWidth();
265 position = positionRatio * owner->getHeight();
270 void InterfaceItem::addConnectionItem(ConnectionItem* item) {
271 connections.append(item);
274 void InterfaceItem::removeConnectionItem(ConnectionItem* item) {
275 connections.removeAll(item);
278 QDataStream &operator <<(QDataStream &out, InterfaceItem *i) {
281 out.setVersion(QDataStream::Qt_5);
283 QByteArray interfaceData;
284 QDataStream toWrite(&interfaceData, QIODevice::WriteOnly);
286 toWrite << i->getId();
287 toWrite << i->getName();
288 toWrite << i->getPositionRatio();
289 toWrite << i->getOrientation();
291 foreach(QGraphicsItem* item, i->params->getCurrentScene()->items()){
292 if(item->data(0) == "connection"){
293 ConnectionItem *conn = dynamic_cast<ConnectionItem*>(item);
294 if(conn->getFromInterfaceItem() == i || conn->getToInterfaceItem() == i){
295 toWrite << conn->getId();
296 cout << "id connection : " << conn->getId() << endl;
300 out << interfaceData;
305 QDataStream &operator >>(QDataStream &in, InterfaceItem &i) {
308 in.setVersion(QDataStream::Qt_5);
311 double positionRatio;
321 i.setPositionRatio(positionRatio);
322 i.setOrientation(orientation);