1 #include "InterfaceItem.h"
3 #include "Parameters.h"
4 #include "GroupInterface.h"
5 #include "FunctionalInterface.h"
7 #include "ReferenceInterface.h"
9 int InterfaceItem::counter = 0;
11 InterfaceItem::InterfaceItem(double _position,
13 ConnectedInterface *_refInter,
14 AbstractBoxItem* _owner,
15 Parameters* _params, bool forceVisible) {
16 positionRatio = _position;
17 orientation = _orientation;
20 // CAUTION : the owner must add explicitely this item to its interface, calling addInterface()
24 QFontMetrics fmName(params->defaultIfaceFont);
25 nameWidth = fmName.width(refInter->getName());
26 nameHeight = fmName.height();
27 // by default, only data interface are visible
28 if ((forceVisible) || (refInter->getPurpose() == AbstractInterface::Data)) {
35 this->id = InterfaceItem::counter++;
41 InterfaceItem::InterfaceItem(){
45 QString InterfaceItem::getName() {
46 return refInter->getName();
49 QString InterfaceItem::getReferenceName() {
50 if (!refInter->isFunctionalInterface()) {
51 return "no_refname_for_groupiface";
53 FunctionalInterface* iface = AI_TO_FUN(refInter);
54 ReferenceInterface* refIface = iface->getReference();
55 return refIface->getName();
58 void InterfaceItem::updateName(QString name) {
59 QFontMetrics fmName(params->defaultIfaceFont);
60 nameWidth = fmName.width(refInter->getName());
61 nameHeight = fmName.height();
66 /* boundingRect() : give the bounding rect in the blockitem coord. system */
67 QRectF InterfaceItem::boundingRect() const {
73 case Parameters::East :
74 pointHG = QPointF(originPoint.x(),originPoint.y()-(params->arrowHeight/2.0));
75 s = QSizeF(params->arrowWidth+params->arrowLineLength, params->arrowHeight);
77 case Parameters::North :
78 pointHG = QPointF(originPoint.x()-(params->arrowHeight/2.0),originPoint.y()-params->arrowWidth-params->arrowLineLength);
79 s = QSizeF(params->arrowHeight,params->arrowWidth+params->arrowLineLength);
81 case Parameters::West :
82 pointHG = QPointF(originPoint.x()-params->arrowLineLength-params->arrowWidth,originPoint.y()-(params->arrowHeight/2.0));
83 s = QSizeF(params->arrowWidth+params->arrowLineLength, params->arrowHeight);
85 case Parameters::South :
86 pointHG = QPointF(originPoint.x()-(params->arrowHeight/2.0),originPoint.y());
87 s = QSizeF(params->arrowHeight, params->arrowWidth+params->arrowLineLength);
95 return QRectF(pointHG,s);
98 void InterfaceItem::paint(QPainter *painter) {
105 painter->setPen(QPen(Qt::red,2));
108 if(owner->isBoxItem()) {
109 painter->setPen(QPen(Qt::black,1));
111 else if(owner->isStimuliItem()) {
112 painter->setPen(QPen(Qt::darkCyan,1));
116 painter->translate(originPoint);
118 switch(orientation) {
119 case Parameters::North:
120 painter->rotate(-90);
122 case Parameters::West:
123 painter->rotate(180);
125 case Parameters::South:
131 if ( (refInter->getPurpose() == AbstractInterface::Clock) || (refInter->getPurpose() == AbstractInterface::Reset)) {
132 painter->drawPath(params->clkrstArrow);
134 else if(refInter->getDirection() == AbstractInterface::Input) {
135 painter->drawPath(params->dataArrowIn);
137 else if(refInter->getDirection() == AbstractInterface::Output) {
138 painter->drawPath(params->dataArrowOut);
139 } else if(refInter->getDirection() == AbstractInterface::InOut) {
140 painter->drawPath(params->dataArrowIn);
141 painter->drawPath(params->dataArrowOut);
144 // paint modifier box if needed
145 ConnectedInterface* connIface = AI_TO_CON(refInter->getAssociatedIface());
146 if ((connIface != NULL) && (connIface->getInputModifier() != NULL)) {
148 painter->translate(params->arrowWidth+params->arrowLineLength,0);
149 painter->drawRect(0,-5,10,10);
156 painter->setPen(QPen(Qt::red,2));
159 painter->setPen(QPen(Qt::black,1));
162 // reset to normal if at west
163 if(orientation == Parameters::West){
164 painter->rotate(180);
167 painter->setFont(params->defaultIfaceFont);
169 QFontMetrics fm = painter->fontMetrics();
170 int w = nameWidth + owner->getIfaceMargin();
173 if(orientation == Parameters::West){
175 if(owner->isGroupItem()){
176 painter->drawText(-(w+params->arrowWidth+params->arrowLineLength),-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
178 else if((owner->isBoxItem()) || (owner->isStimuliItem())){
179 painter->drawText(0,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
184 if(owner->isGroupItem()) {
185 painter->drawText(params->arrowWidth+params->arrowLineLength,-h/2,w,h,Qt::AlignRight | Qt::TextWordWrap, refInter->getName());
187 else if((owner->isBoxItem()) || (owner->isStimuliItem())){
188 painter->drawText(-w,-h/2,w,h,Qt::AlignLeft | Qt::TextWordWrap, refInter->getName());
198 QPointF InterfaceItem::getStartPosition() {
199 QPointF p = originPoint;
200 QPointF ownerPos = owner->scenePos();
205 QPointF InterfaceItem::getEndPosition() {
206 QPointF p = getStartPosition();
209 case Parameters::East:
210 p.setX(p.x()+params->arrowWidth+params->arrowLineLength);
212 case Parameters::North:
213 p.setY(p.y() - params->arrowWidth-params->arrowLineLength);
215 case Parameters::West:
216 p.setX(p.x() - params->arrowWidth-params->arrowLineLength);
218 case Parameters::South:
219 p.setY(p.y() + params->arrowWidth+params->arrowLineLength);
225 void InterfaceItem::setOriginPoint() {
227 case Parameters::East:
228 originPoint = QPointF(owner->getWidth(),position);
230 case Parameters::North:
231 originPoint = QPointF(position,0);
233 case Parameters::West:
234 originPoint = QPointF(0,position);
236 case Parameters::South:
237 originPoint = QPointF(position,owner->getHeight());
242 QString InterfaceItem::getStrOrientation() {
245 case Parameters::North :
246 str = QString("north");
248 case Parameters::South :
249 str = QString("south");
251 case Parameters::East :
252 str = QString("east");
254 case Parameters::West :
255 str = QString("west");
262 int InterfaceItem::getIntOrientation(QString str) {
263 if(str == "west") return Parameters::West;
264 if(str == "east") return Parameters::East;
265 if(str == "south") return Parameters::South;
266 if(str == "north") return Parameters::North;
271 void InterfaceItem::updatePosition() {
272 if(orientation == Parameters::North || orientation == Parameters::South){
273 position = positionRatio * owner->getWidth();
275 position = positionRatio * owner->getHeight();
280 void InterfaceItem::addConnectionItem(ConnectionItem* item) {
281 connections.append(item);
284 void InterfaceItem::removeConnectionItem(ConnectionItem* item) {
285 connections.removeAll(item);
288 QDataStream &operator <<(QDataStream &out, InterfaceItem *i) {
291 out.setVersion(QDataStream::Qt_5);
293 QByteArray interfaceData;
294 QDataStream toWrite(&interfaceData, QIODevice::WriteOnly);
296 toWrite << i->getId();
297 toWrite << i->getName();
298 toWrite << i->getPositionRatio();
299 toWrite << i->getOrientation();
301 foreach(QGraphicsItem* item, i->params->getCurrentScene()->items()){
302 if(item->data(0) == "connection"){
303 ConnectionItem *conn = dynamic_cast<ConnectionItem*>(item);
304 if(conn->getFromInterfaceItem() == i || conn->getToInterfaceItem() == i){
305 toWrite << conn->getId();
306 cout << "id connection : " << conn->getId() << endl;
310 out << interfaceData;
315 QDataStream &operator >>(QDataStream &in, InterfaceItem &i) {
318 in.setVersion(QDataStream::Qt_5);
321 double positionRatio;
331 i.setPositionRatio(positionRatio);
332 i.setOrientation(orientation);