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()->getBoxItems();
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 << "GroupItem: must change group item shape" << endl;
263 prepareGeometryChange();
269 void GroupItem::nameChanged() {
272 QFontMetrics fmId(params->defaultBlockFont);
273 nameWidth = fmId.width(refBlock->getName());
274 nameHeight = fmId.height();
275 // changing the BoxItem in the upperscene
276 if (parentItem != NULL) {
277 parentItem->nameChanged();
279 updateGeometry(InterfaceMove);
280 // force the update in case of the size has not changed
285 void GroupItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
287 if(params->editState == Parameters::EditGroupMove) {
288 QPointF absPos = currentPosition + originPoint;
289 int gapX = event->scenePos().x() - cursorPosition.x();
290 int gapY = event->scenePos().y() - cursorPosition.y();
292 //cout << "block abs. pos: " << absPos.x() << "," << absPos.y() << " | ";
293 //cout << "block current. pos: " << currentPosition.x() << "," << currentPosition.y() << " | ";
295 if (absPos.x()+gapX < 0) {
298 if (absPos.y()+gapY < 0) {
302 //cout << "gap: " << gapX << "," << gapY << " | ";
303 //cout << "scene: " << getScene()->sceneRect().x() << "," << getScene()->sceneRect().y() << endl;
304 QPointF gap(gapX,gapY);
305 currentPosition = currentPosition+gap;
306 setPos(currentPosition);
307 cursorPosition = event->scenePos();
309 else if(params->editState == Parameters::EditGroupResize) {
311 int gapX = event->scenePos().x() - cursorPosition.x();
312 int gapY = event->scenePos().y() - cursorPosition.y();
313 //cout << "gap: " << gapX << "," << gapY << endl;
314 switch(currentBorder){
316 if(boxWidth+gapX > minimumBoxWidth){
322 if(boxHeight+gapY > minimumBoxHeight){
327 case CornerSouthEast: {
328 if(boxWidth+gapX > minimumBoxWidth){
331 if(boxHeight+gapY > minimumBoxHeight){
337 cout << "abnormal case while resizing block" << endl;
340 updateGeometry(Resize);
342 // recompute the geometry of the block
344 // update all interfaces positions
345 foreach(InterfaceItem *item, interfaces){
346 item->updatePosition();
348 // update all connections from/to this block
349 foreach(ConnectionItem *item, getScene()->getConnectionItems()){
350 if ((item->getFromInterfaceItem()->getOwner() == this) || (item->getToInterfaceItem()->getOwner() == this)) {
355 cursorPosition = event->scenePos();
357 else if(params->editState == Parameters::EditInterfaceMove) {
358 prepareGeometryChange();
359 moveInterfaceTo(event->pos());
360 // recompute the geometry of the block
361 updateGeometry(InterfaceMove);
362 // update connection from/to the selected interface
363 foreach(ConnectionItem *item, getScene()->getConnectionItems()){
364 if ((item->getFromInterfaceItem() == currentInterface) || (item->getToInterfaceItem() == currentInterface)) {
372 void GroupItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
374 QPointF pos = event->pos();
378 QGraphicsItem::mousePressEvent(event);
380 if(event->button() == Qt::RightButton) return;
382 int mode = getScene()->getEditionMode();
384 dispatcher->setCurrentGroupWidget(getScene()->getGroupWidget());
386 if ((refBlock->isTopGroupBlock()) && (mode == GroupScene::AddConnection) && (params->cursorState == Parameters::CursorOnInterface)) {
387 InterfaceItem *inter = getInterfaceFromCursor(x,y);
390 if (params->editState == Parameters::EditNoOperation) {
391 getScene()->setSelectedInterface(1,inter);
392 params->setEditState(Parameters::EditStartConnection);
394 else if (params->editState == Parameters::EditStartConnection) {
395 if (inter == getScene()->getSelectedInterface(1)) {
396 params->setEditState(Parameters::EditAbortConnection);
399 getScene()->setSelectedInterface(2,inter);
400 params->setEditState(Parameters::EditCloseConnection);
405 else if (mode == GroupScene::ItemEdition) {
407 if (params->cursorState == Parameters::CursorOnInterface) {
408 InterfaceItem *inter = getInterfaceFromCursor(x,y);
410 currentInterface = inter;
411 params->setEditState(Parameters::EditInterfaceMove);
414 else if (params->cursorState == Parameters::CursorInGroupTitle) {
415 params->setEditState(Parameters::EditGroupMove);
416 cursorPosition = event->scenePos();
418 else if (params->cursorState == Parameters::CursorOnBorder) {
419 setFlag(ItemIsMovable, false);
420 cursorPosition = event->scenePos();
421 params->setEditState(Parameters::EditGroupResize);
426 void GroupItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
428 int mode = getScene()->getEditionMode();
430 if ((refBlock->isTopGroupBlock()) && (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->createConnectionItem(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);
458 else if (mode == GroupScene::ItemEdition) {
459 currentInterface = NULL;
460 setFlag(ItemIsMovable, true);
461 params->editState = Parameters::EditNoOperation;
463 QGraphicsItem::mouseReleaseEvent(event);
466 void GroupItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
467 QPointF pos = event->pos();
470 currentBorder = NoBorder;
471 int mode = getScene()->getEditionMode();
473 if (mode == GroupScene::AddConnection) {
474 InterfaceItem* iface = getInterfaceFromCursor(x,y);
476 params->cursorState = Parameters::CursorOnInterface;
477 setCursor(Qt::PointingHandCursor);
480 params->cursorState = Parameters::CursorNowhere;
481 setCursor(Qt::ArrowCursor);
484 else if (mode == GroupScene::ItemEdition) {
488 InterfaceItem* iface = getInterfaceFromCursor(x,y);
490 params->cursorState = Parameters::CursorOnInterface;
491 setCursor(Qt::PointingHandCursor);
493 else if ((x>boxWidth-marginE)&&(x<boxWidth)) {
495 params->cursorState = Parameters::CursorOnBorder;
497 if ((y>boxHeight-2*marginS)&&(y<boxHeight)) {
498 currentBorder = CornerSouthEast;
499 setCursor(Qt::SizeFDiagCursor);
502 currentBorder = BorderEast;
503 setCursor(Qt::SizeHorCursor);
506 else if ((y>boxHeight-marginS)&&(y<boxHeight)) {
508 params->cursorState = Parameters::CursorOnBorder;
510 if ((x>boxWidth-2*marginE)&&(x<boxWidth)) {
511 currentBorder = CornerSouthEast;
512 setCursor(Qt::SizeFDiagCursor);
515 currentBorder = BorderSouth;
516 setCursor(Qt::SizeVerCursor);
520 if (rectTitle.contains(x,y)) {
521 params->cursorState = Parameters::CursorInGroupTitle;
522 setCursor(Qt::OpenHandCursor);
525 params->cursorState = Parameters::CursorNowhere;
526 setCursor(Qt::ArrowCursor);
530 QGraphicsItem::hoverMoveEvent(event);
533 void GroupItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
535 QAction* titleAction = NULL;
536 QAction* showProperties = NULL;
537 QAction* removeAction = NULL;
538 QAction* renameAction = NULL;
539 QAction* showParameters = NULL;
541 InterfaceItem* ifaceItem = getInterfaceFromCursor(event->pos().x(), event->pos().y());
543 // menu for interface
544 if( ifaceItem != NULL) {
545 titleAction = menu.addAction("Interface operations");
546 titleAction->setEnabled(false);
548 showProperties = menu.addAction("Show properties");
549 renameAction = menu.addAction("Rename");
551 /* CAUTION : the interface can be removed only if its
552 connected to only one side, i.e. connectedFrom is null
553 or connectedTo is empty.
556 ConnectedInterface* ref = ifaceItem->refInter;
557 if ((!ref->isConnectedFrom()) || (!ref->isConnectedTo())) {
558 removeAction = menu.addAction("Remove");
562 titleAction = menu.addAction("Block operations");
563 titleAction->setEnabled(false);
566 if (refBlock->nbParameters() > 0) {
567 showParameters = menu.addAction("Show parameters");
569 renameAction = menu.addAction("Rename");
571 QAction* selectedAction = menu.exec(event->screenPos());
573 if(selectedAction == NULL) return;
575 if(selectedAction == renameAction){
576 if(ifaceItem != NULL)
577 dispatcher->renameInterface(ifaceItem);
579 dispatcher->renameGroupBlock(this);
581 else if(selectedAction == showProperties){
582 dispatcher->showProperties(ifaceItem);
584 else if (selectedAction == removeAction) {
585 dispatcher->removeGroupInterface(ifaceItem);
587 else if(selectedAction == showParameters) {
588 new ParametersWindow(refBlock, params, NULL);
592 InterfaceItem* GroupItem::isHoverInterface(QPointF point) {
593 foreach(InterfaceItem *inter, interfaces){
594 if(inter->boundingRect().contains(point))
600 void GroupItem::load(QDomElement groupElement) throw(Exception) {
602 GroupBlock* groupBlock = AB_TO_GRP(refBlock);
606 int id = groupElement.attribute("id","none").toInt(&ok);
607 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
609 QString nameStr = groupElement.attribute("name","none");
610 if(nameStr == "none") throw(Exception(PROJECTFILE_CORRUPTED));
612 QStringList positionStr = groupElement.attribute("position","none").split(",");
613 if(positionStr.length() != 2) throw(Exception(PROJECTFILE_CORRUPTED));
614 int posX = positionStr.at(0).toInt(&ok);
615 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
616 int posY = positionStr.at(1).toInt(&ok);
617 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
619 QStringList dimensionStr = groupElement.attribute("dimension","none").split(",");
620 if(dimensionStr.length() != 2) throw(Exception(PROJECTFILE_CORRUPTED));
621 int dimX = dimensionStr.at(0).toInt(&ok);
622 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
623 int dimY = dimensionStr.at(1).toInt(&ok);
624 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
628 setDimension(dimX,dimY);
629 groupBlock->setName(nameStr);
631 cout << "group info : \n-id : " << id << "\n-pos : " << posX << ", " << posY << "\n-dim : " << dimX << ", " << dimY << "\n-name : " << nameStr.toStdString() << endl;
633 QDomNodeList interfaces = groupElement.elementsByTagName("group_iface");
634 for(int j=0; j<interfaces.length(); j++){
635 QDomElement currentInterfaceNode = interfaces.at(j).toElement();
637 int id = currentInterfaceNode.attribute("id","none").toInt(&ok);
638 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
640 QString name = currentInterfaceNode.attribute("name","none");
641 if(name == "none") throw(Exception(PROJECTFILE_CORRUPTED));
643 QString directionStr = currentInterfaceNode.attribute("direction","none");
644 int direction = AbstractInterface::getIntDirection(directionStr);
645 if(direction == -1) throw(Exception(PROJECTFILE_CORRUPTED));
647 QString orientationStr = currentInterfaceNode.attribute("orientation","none");
648 int orientation = InterfaceItem::getIntOrientation(orientationStr);
649 if(orientation == -1) throw(Exception(PROJECTFILE_CORRUPTED));
651 double position = currentInterfaceNode.attribute("position","none").toDouble(&ok);
652 if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
654 GroupInterface *groupInterface = new GroupInterface(groupBlock,name,direction,AbstractInterface::Data);
656 InterfaceItem *interfaceItem = new InterfaceItem(position,orientation,groupInterface,this,params);
657 interfaceItem->setId(id);
659 groupBlock->addInterface(groupInterface);
660 addInterface(interfaceItem, false);
661 cout << "interface add to " << groupBlock->getName().toStdString() << endl;
666 void GroupItem::save(QXmlStreamWriter &writer) {
668 writer.writeStartElement("group_item");
670 QString attrId = QString::number(id);
671 QString attrName(getRefBlock()->getName());
672 QString attrUpperItem = QString::number(-1);
673 if(parentItem != NULL){
674 attrUpperItem = QString::number(parentItem->getId());
676 QString attrPos = QString::number((int)(pos().x())).append(",").append(QString::number((int)(pos().y())));
677 QString attrDim = QString::number(getWidth()).append(",").append(QString::number(getHeight()));
680 writer.writeAttribute("id",attrId);
681 writer.writeAttribute("upper_item",attrUpperItem);
682 writer.writeAttribute("name",attrName);
683 writer.writeAttribute("position", attrPos);
684 writer.writeAttribute("dimension", attrDim);
686 writer.writeStartElement("group_ifaces");
688 writer.writeAttribute("count",QString::number(interfaces.length()));
690 foreach(InterfaceItem *item, interfaces){
691 writer.writeStartElement("group_iface");
693 writer.writeAttribute("id",QString::number(item->getId()));
694 writer.writeAttribute("name",item->getName());
695 writer.writeAttribute("direction",QString(item->refInter->getDirectionString()));
696 writer.writeAttribute("orientation",item->getStrOrientation());
697 writer.writeAttribute("position",QString::number(item->getPositionRatio()));
699 writer.writeEndElement();
702 writer.writeEndElement();//</interfaces>
704 writer.writeEndElement();//</group_item>