3 #include "ConnectionItem.h"
4 #include "InterfaceItem.h"
5 #include "Dispatcher.h"
6 #include "Parameters.h"
8 #include "AbstractBlock.h"
9 #include "AbstractInterface.h"
10 #include "ConnectedInterface.h"
11 #include "GroupScene.h"
12 #include "ParametersWindow.h"
13 #include "GroupBlock.h"
14 #include "GroupInterface.h"
17 GroupItem::GroupItem(BoxItem *_parentItem,
18 AbstractBlock *_refBlock,
19 Dispatcher *_dispatcher,
20 Parameters *_params) throw(Exception) :AbstractBoxItem( _refBlock, _dispatcher, _params) {
22 parentItem = _parentItem;
23 if (parentItem != NULL) {
24 parentItem->setChildGroupItem(this);
28 minimumBoxWidth = nameWidth+2*nameMargin;
29 minimumBoxHeight = 100;
30 boxHeight = minimumBoxHeight;
31 boxWidth = minimumBoxWidth;
33 rectTitle = QRectF(0,-(nameHeight+2*nameMargin),nameWidth+2*nameMargin,nameHeight+2*nameMargin);
35 totalHeight = boxHeight + rectTitle.height();
36 totalWidth = boxWidth;
43 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
46 updateGeometry(InterfaceMove);
47 QPointF initPos = QPointF(0.0,0.0) - originPoint;
49 cout << "total size of group: " << totalWidth << "," << totalHeight << endl;
50 cout << "pos in scene: " << x() << "," << y() << endl;
53 GroupItem::GroupItem(Dispatcher *_dispatcher,Parameters *_params) throw(Exception) :AbstractBoxItem(_dispatcher, _params) {
56 rectTitle = QRectF(0,-(nameHeight+2*nameMargin),nameWidth+2*nameMargin,nameHeight+2*nameMargin);
59 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
61 updateGeometry(InterfaceMove);
62 QPointF initPos = QPointF(0.0,0.0) - originPoint;
64 cout << "total size of group: " << totalWidth << "," << totalHeight << endl;
65 cout << "pos in scene: " << x() << "," << y() << endl;
68 GroupItem::~GroupItem() {
69 // since the reference block is nowhere referenced except in this class, it must be deleted here
73 bool GroupItem::isGroupItem() {
77 BoxItem* GroupItem::getParentItem() {
81 void GroupItem::setParentItem(BoxItem *_parentItem) {
82 parentItem = _parentItem;
83 if (parentItem != NULL) {
84 parentItem->setChildGroupItem(this);
88 void GroupItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
89 if(boxWidth > 0 && boxHeight > 0){
91 painter->setPen(Qt::red);
93 painter->setPen(Qt::black);
95 painter->drawRect(0,0,boxWidth,boxHeight);
96 painter->drawRect(rectTitle);
97 painter->drawText(rectTitle,Qt::AlignCenter | Qt::TextWordWrap,refBlock->getName());
99 foreach(InterfaceItem *item, interfaces){
100 item->paint(painter);
105 Each time a new block is added in a GroupItem, it is placed
106 at an absolute position of (0,0) within the GroupItem. (NB: the absolute
107 position is computed by item.pos()+item.getOriginPoint()).
108 Thus, there are 3 cases :
109 - a single block is within the GroupItem
110 - several blocks already placed and a new one
111 - several blocks already placed and one is moving.
114 void GroupItem::updateMinimumSize() {
116 // compute place taken by blocks.
117 int marginConn = 2*(params->arrowWidth+params->arrowLineLength);
118 if (rectTitle.width() > 2*marginConn) {
119 minimumBoxWidth = rectTitle.width();
122 minimumBoxWidth = 2*marginConn;
124 minimumBoxHeight = 2*marginConn;
126 if (getScene() == NULL) return;
128 QList<BoxItem *> blocks = getScene()->getBlockItems();
129 if(blocks.length() > 0) {
130 // first, search for blocks that are at (0,0)
135 bool isZeroBlocks = false;
136 bool isOtherBlocks = false;
137 foreach(BoxItem* item, blocks) {
138 QPointF p = item->pos() + item->getOriginPoint();
139 if ((p.x()==0.0) && (p.y()==0.0)) {
141 if (item->getTotalWidth() > xMaxZero) {
142 xMaxZero = item->getTotalWidth();
144 if (item->getTotalHeight() > yMaxZero) {
145 yMaxZero = item->getTotalHeight();
149 isOtherBlocks = true;
150 if(p.x()+item->getTotalWidth() > xMax) {
151 xMax = p.x()+item->getTotalWidth();
153 if(p.y()+item->getTotalHeight() > yMax) {
154 yMax = p.y()+item->getTotalHeight();
159 if (!isOtherBlocks) {
160 minimumBoxWidth = xMaxZero+2*marginConn;
161 minimumBoxHeight = yMaxZero+2*marginConn;
164 if (xMaxZero+marginConn > xMax) {
165 minimumBoxWidth = xMaxZero+2*marginConn;
168 minimumBoxWidth = xMax+marginConn;
170 if (yMaxZero+marginConn > yMax) {
171 minimumBoxHeight = yMaxZero+2*marginConn;
174 minimumBoxHeight = yMax+marginConn;
179 minimumBoxWidth = xMax+marginConn;
180 minimumBoxHeight = yMax+marginConn;
183 //cout << "min group size: " << minimumBoxWidth << "," << minimumBoxHeight << endl;
186 void GroupItem::updateShape() {
187 updateGeometry(InterfaceMove);
190 bool GroupItem::updateGeometry(ChangeType type) {
192 QPointF oldOrigin = originPoint;
193 QSize oldSize(totalWidth,totalHeight);
195 bool boxSizeChanged = false;
197 // whatever the change, the minimum size may have changed
200 if (type == Resize) {
201 boxSizeChanged = true;
203 // if an internal block has moved, the actual box size may be inadequate
204 if (boxWidth < minimumBoxWidth) {
205 boxWidth = minimumBoxWidth;
206 boxSizeChanged = true;
208 if (boxHeight < minimumBoxHeight) {
209 boxHeight = minimumBoxHeight;
210 boxSizeChanged = true;
213 if (boxSizeChanged) {
214 updateInterfacesAndConnections();
218 // compute the max. width of interfaces' name for 4 orientations.
225 foreach(InterfaceItem* iface, interfaces) {
226 ifaceWidth = iface->getNameWidth();
227 if (iface->getOrientation() == Parameters::South) {
228 if (ifaceWidth > maxSouth) maxSouth = ifaceWidth+ifaceMargin+params->arrowWidth+params->arrowLineLength;
230 else if (iface->getOrientation() == Parameters::North) {
231 if (ifaceWidth > maxNorth) maxNorth = ifaceWidth+ifaceMargin+params->arrowWidth+params->arrowLineLength;
233 else if (iface->getOrientation() == Parameters::East) {
234 if (ifaceWidth > maxEast) maxEast = ifaceWidth+ifaceMargin+params->arrowWidth+params->arrowLineLength;
236 else if (iface->getOrientation() == Parameters::West) {
237 if (ifaceWidth > maxWest) maxWest = ifaceWidth+ifaceMargin+params->arrowWidth+params->arrowLineLength;
242 totalWidth = boxWidth+maxEast;
243 totalHeight = boxHeight+maxSouth;
247 totalWidth += maxWest;
249 if (maxNorth > (nameHeight+2*nameMargin)) {
251 totalHeight += maxNorth;
254 y -= nameHeight+2*nameMargin;
255 totalHeight += nameHeight+2*nameMargin;
257 QSizeF newSize(totalWidth,totalHeight);
261 if ((boxSizeChanged) || (newSize != oldSize) || (originPoint != oldOrigin)) {
262 cout << "must change group item shape" << endl;
263 prepareGeometryChange();
269 void GroupItem::interfaceRenamed() {
270 updateGeometry(InterfaceMove);
274 void GroupItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
276 if(params->editState == Parameters::EditGroupMove) {
277 QPointF absPos = currentPosition + originPoint;
278 int gapX = event->scenePos().x() - cursorPosition.x();
279 int gapY = event->scenePos().y() - cursorPosition.y();
281 //cout << "block abs. pos: " << absPos.x() << "," << absPos.y() << " | ";
282 //cout << "block current. pos: " << currentPosition.x() << "," << currentPosition.y() << " | ";
284 if (absPos.x()+gapX < 0) {
287 if (absPos.y()+gapY < 0) {
291 //cout << "gap: " << gapX << "," << gapY << " | ";
292 //cout << "scene: " << getScene()->sceneRect().x() << "," << getScene()->sceneRect().y() << endl;
293 QPointF gap(gapX,gapY);
294 currentPosition = currentPosition+gap;
295 setPos(currentPosition);
296 cursorPosition = event->scenePos();
298 else if(params->editState == Parameters::EditGroupResize) {
300 int gapX = event->scenePos().x() - cursorPosition.x();
301 int gapY = event->scenePos().y() - cursorPosition.y();
302 //cout << "gap: " << gapX << "," << gapY << endl;
303 switch(currentBorder){
305 if(boxWidth+gapX > minimumBoxWidth){
311 if(boxHeight+gapY > minimumBoxHeight){
316 case CornerSouthEast: {
317 if(boxWidth+gapX > minimumBoxWidth){
320 if(boxHeight+gapY > minimumBoxHeight){
326 cout << "abnormal case while resizing block" << endl;
329 updateGeometry(Resize);
331 // recompute the geometry of the block
333 // update all interfaces positions
334 foreach(InterfaceItem *item, interfaces){
335 item->updatePosition();
337 // update all connections from/to this block
338 foreach(ConnectionItem *item, getScene()->getConnectionItems()){
339 if ((item->getFromInterfaceItem()->getOwner() == this) || (item->getToInterfaceItem()->getOwner() == this)) {
344 cursorPosition = event->scenePos();
346 else if(params->editState == Parameters::EditInterfaceMove) {
347 prepareGeometryChange();
348 moveInterfaceTo(event->pos());
349 // recompute the geometry of the block
350 updateGeometry(InterfaceMove);
351 // update connection from/to the selected interface
352 foreach(ConnectionItem *item, getScene()->getConnectionItems()){
353 if ((item->getFromInterfaceItem() == currentInterface) || (item->getToInterfaceItem() == currentInterface)) {
361 void GroupItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
363 QPointF pos = event->pos();
367 QGraphicsItem::mousePressEvent(event);
369 if(event->button() == Qt::RightButton) return;
371 int mode = getScene()->getEditionMode();
373 dispatcher->setCurrentGroupWidget(getScene()->getGroupWidget());
375 /* NOTE : commneted because group interface are normally
376 created and the connected directly to a block within
377 the group. Furthermore, there can be a single connection
378 from a groupe interface.
380 if ((mode == GroupScene::AddConnection) && (params->cursorState == Parameters::CursorOnInterface)) {
381 InterfaceItem *inter = getInterfaceFromCursor(x,y);
384 if (params->editState == Parameters::EditNoOperation) {
385 getScene()->setSelectedInterface(1,inter);
386 params->setEditState(Parameters::EditStartConnection);
388 else if (params->editState == Parameters::EditStartConnection) {
389 if (inter == getScene()->getSelectedInterface(1)) {
390 params->setEditState(Parameters::EditAbortConnection);
393 getScene()->setSelectedInterface(2,inter);
394 params->setEditState(Parameters::EditCloseConnection);
400 if (mode == GroupScene::ItemEdition) {
402 if (params->cursorState == Parameters::CursorOnInterface) {
403 InterfaceItem *inter = getInterfaceFromCursor(x,y);
405 currentInterface = inter;
406 params->setEditState(Parameters::EditInterfaceMove);
409 else if (params->cursorState == Parameters::CursorInGroupTitle) {
410 params->setEditState(Parameters::EditGroupMove);
411 cursorPosition = event->scenePos();
413 else if (params->cursorState == Parameters::CursorOnBorder) {
414 setFlag(ItemIsMovable, false);
415 cursorPosition = event->scenePos();
416 params->setEditState(Parameters::EditGroupResize);
421 void GroupItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
423 int mode = getScene()->getEditionMode();
425 /* NOTE : commneted because group interface are normally
426 created and the connected directly to a block within
427 the group. Furthermore, there can be a single connection
428 from a groupe interface.
430 if (mode == GroupScene::AddConnection) {
432 if (params->editState == Parameters::EditStartConnection) {
433 params->setEditState(Parameters::EditStartConnection);
434 InterfaceItem* iface = getScene()->getSelectedInterface(1);
435 iface->selected = true;
436 update(iface->boundingRect());
438 else if (params->editState == Parameters::EditAbortConnection) {
439 InterfaceItem* iface = getScene()->getSelectedInterface(1);
440 iface->selected = false;
441 update(iface->boundingRect());
442 getScene()->setSelectedInterface(1,NULL);
443 params->setEditState(Parameters::EditNoOperation);
445 else if (params->editState == Parameters::EditCloseConnection) {
446 InterfaceItem* iface1 = getScene()->getSelectedInterface(1);
447 InterfaceItem* iface2 = getScene()->getSelectedInterface(2);
448 bool ok = dispatcher->connect(iface1,iface2);
450 iface1->selected = false;
451 update(iface1->boundingRect());
452 getScene()->setSelectedInterface(1,NULL);
453 getScene()->setSelectedInterface(2,NULL);
454 params->setEditState(Parameters::EditNoOperation);
459 if (mode == GroupScene::ItemEdition) {
460 currentInterface = NULL;
461 setFlag(ItemIsMovable, true);
462 params->editState = Parameters::EditNoOperation;
464 QGraphicsItem::mouseReleaseEvent(event);
467 void GroupItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
468 QPointF pos = event->pos();
471 currentBorder = NoBorder;
472 int mode = getScene()->getEditionMode();
474 if (mode == GroupScene::AddConnection) {
475 InterfaceItem* iface = getInterfaceFromCursor(x,y);
477 params->cursorState = Parameters::CursorOnInterface;
478 setCursor(Qt::PointingHandCursor);
481 params->cursorState = Parameters::CursorNowhere;
482 setCursor(Qt::ArrowCursor);
485 else if (mode == GroupScene::ItemEdition) {
489 InterfaceItem* iface = getInterfaceFromCursor(x,y);
491 params->cursorState = Parameters::CursorOnInterface;
492 setCursor(Qt::PointingHandCursor);
494 else if ((x>boxWidth-marginE)&&(x<boxWidth)) {
496 params->cursorState = Parameters::CursorOnBorder;
498 if ((y>boxHeight-2*marginS)&&(y<boxHeight)) {
499 currentBorder = CornerSouthEast;
500 setCursor(Qt::SizeFDiagCursor);
503 currentBorder = BorderEast;
504 setCursor(Qt::SizeHorCursor);
507 else if ((y>boxHeight-marginS)&&(y<boxHeight)) {
509 params->cursorState = Parameters::CursorOnBorder;
511 if ((x>boxWidth-2*marginE)&&(x<boxWidth)) {
512 currentBorder = CornerSouthEast;
513 setCursor(Qt::SizeFDiagCursor);
516 currentBorder = BorderSouth;
517 setCursor(Qt::SizeVerCursor);
521 if (rectTitle.contains(x,y)) {
522 params->cursorState = Parameters::CursorInGroupTitle;
523 setCursor(Qt::OpenHandCursor);
526 params->cursorState = Parameters::CursorNowhere;
527 setCursor(Qt::ArrowCursor);
531 QGraphicsItem::hoverMoveEvent(event);
534 void GroupItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
536 QAction* titleAction = NULL;
537 QAction* showProperties = NULL;
538 QAction* removeAction = NULL;
539 QAction* renameAction = NULL;
540 QAction* showParameters = NULL;
542 InterfaceItem* ifaceItem = getInterfaceFromCursor(event->pos().x(), event->pos().y());
544 // menu for interface
545 if( ifaceItem != NULL) {
546 titleAction = menu.addAction("Interface operations");
547 titleAction->setEnabled(false);
549 showProperties = menu.addAction("Show properties");
550 renameAction = menu.addAction("Rename");
552 /* CAUTION : the interface can be removed only if its
553 connected to only one side, i.e. connectedFrom is null
554 or connectedTo is empty.
557 ConnectedInterface* ref = ifaceItem->refInter;
558 if ((!ref->isConnectedFrom()) || (!ref->isConnectedTo())) {
559 removeAction = menu.addAction("Remove");
563 titleAction = menu.addAction("Block operations");
564 titleAction->setEnabled(false);
567 if (refBlock->nbParameters() > 0) {
568 showParameters = menu.addAction("Show parameters");
570 renameAction = menu.addAction("Rename");
572 QAction* selectedAction = menu.exec(event->screenPos());
574 if(selectedAction == NULL) return;
576 if(selectedAction == renameAction){
577 if(ifaceItem != NULL)
578 dispatcher->renameInterface(ifaceItem);
580 dispatcher->renameBlockOrGroup(this);
582 else if(selectedAction == showProperties){
583 dispatcher->showProperties(ifaceItem);
585 else if (selectedAction == removeAction) {
586 dispatcher->removeGroupInterface(ifaceItem);
588 else if(selectedAction == showParameters) {
589 new ParametersWindow(refBlock, params, NULL);
593 InterfaceItem* GroupItem::isHoverInterface(QPointF point) {
594 foreach(InterfaceItem *inter, interfaces){
595 if(inter->boundingRect().contains(point))
601 void GroupItem::load(QDomElement groupElement) throw(Exception) {
603 GroupBlock* groupBlock = AB_TO_GRP(refBlock);
607 int id = groupElement.attribute("id","none").toInt(&ok);
608 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
610 QString nameStr = groupElement.attribute("name","none");
611 if(nameStr == "none") throw(Exception(PROJECTFILE_CORRUPTED));
613 QStringList positionStr = groupElement.attribute("position","none").split(",");
614 if(positionStr.length() != 2) throw(Exception(PROJECTFILE_CORRUPTED));
615 int posX = positionStr.at(0).toInt(&ok);
616 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
617 int posY = positionStr.at(1).toInt(&ok);
618 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
620 QStringList dimensionStr = groupElement.attribute("dimension","none").split(",");
621 if(dimensionStr.length() != 2) throw(Exception(PROJECTFILE_CORRUPTED));
622 int dimX = dimensionStr.at(0).toInt(&ok);
623 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
624 int dimY = dimensionStr.at(1).toInt(&ok);
625 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
629 setDimension(dimX,dimY);
630 groupBlock->setName(nameStr);
632 cout << "group info : \n-id : " << id << "\n-pos : " << posX << ", " << posY << "\n-dim : " << dimX << ", " << dimY << "\n-name : " << nameStr.toStdString() << endl;
634 QDomNodeList interfaces = groupElement.elementsByTagName("group_iface");
635 for(int j=0; j<interfaces.length(); j++){
636 QDomElement currentInterfaceNode = interfaces.at(j).toElement();
638 int id = currentInterfaceNode.attribute("id","none").toInt(&ok);
639 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
641 QString name = currentInterfaceNode.attribute("name","none");
642 if(name == "none") throw(Exception(PROJECTFILE_CORRUPTED));
644 QString directionStr = currentInterfaceNode.attribute("direction","none");
645 int direction = AbstractInterface::getIntDirection(directionStr);
646 if(direction == -1) throw(Exception(PROJECTFILE_CORRUPTED));
648 QString orientationStr = currentInterfaceNode.attribute("orientation","none");
649 int orientation = InterfaceItem::getIntOrientation(orientationStr);
650 if(orientation == -1) throw(Exception(PROJECTFILE_CORRUPTED));
652 double position = currentInterfaceNode.attribute("position","none").toDouble(&ok);
653 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
655 GroupInterface *groupInterface = new GroupInterface(groupBlock,name,direction,AbstractInterface::Data);
657 InterfaceItem *interfaceItem = new InterfaceItem(position,orientation,groupInterface,this,params);
658 interfaceItem->setId(id);
660 groupBlock->addInterface(groupInterface);
661 addInterface(interfaceItem, false);
662 cout << "interface add to " << groupBlock->getName().toStdString() << endl;
667 void GroupItem::save(QXmlStreamWriter &writer) {
669 writer.writeStartElement("group_item");
671 QString attrId = QString::number(id);
672 QString attrName(getRefBlock()->getName());
673 QString attrUpperItem = QString::number(-1);
674 if(parentItem != NULL){
675 attrUpperItem = QString::number(parentItem->getId());
677 QString attrPos = QString::number((int)(pos().x())).append(",").append(QString::number((int)(pos().y())));
678 QString attrDim = QString::number(getWidth()).append(",").append(QString::number(getHeight()));
681 writer.writeAttribute("id",attrId);
682 writer.writeAttribute("upper_item",attrUpperItem);
683 writer.writeAttribute("name",attrName);
684 writer.writeAttribute("position", attrPos);
685 writer.writeAttribute("dimension", attrDim);
687 writer.writeStartElement("group_ifaces");
689 writer.writeAttribute("count",QString::number(interfaces.length()));
691 foreach(InterfaceItem *item, interfaces){
692 writer.writeStartElement("group_iface");
694 writer.writeAttribute("id",QString::number(item->getId()));
695 writer.writeAttribute("name",item->getName());
696 writer.writeAttribute("direction",QString(item->refInter->getDirectionString()));
697 writer.writeAttribute("orientation",item->getStrOrientation());
698 writer.writeAttribute("position",QString::number(item->getPositionRatio()));
700 writer.writeEndElement();
703 writer.writeEndElement();//</interfaces>
705 writer.writeEndElement();//</group_item>