X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/blast.git/blobdiff_plain/abbc64cf04a35ab3549d5c516f44c7c5921baa63..4cf57e6db08da791233d75237f62e74bc88dd427:/GroupItem.cpp?ds=inline diff --git a/GroupItem.cpp b/GroupItem.cpp index 04d7bca..3286c14 100644 --- a/GroupItem.cpp +++ b/GroupItem.cpp @@ -5,10 +5,14 @@ #include "Dispatcher.h" #include "Parameters.h" #include "BoxItem.h" +#include "SourceItem.h" #include "AbstractBlock.h" #include "AbstractInterface.h" #include "ConnectedInterface.h" #include "GroupScene.h" +#include "ParametersWindow.h" +#include "GroupBlock.h" +#include "GroupInterface.h" GroupItem::GroupItem(BoxItem *_parentItem, @@ -17,6 +21,9 @@ GroupItem::GroupItem(BoxItem *_parentItem, Parameters *_params) throw(Exception) :AbstractBoxItem( _refBlock, _dispatcher, _params) { parentItem = _parentItem; + if (parentItem != NULL) { + parentItem->setChildGroupItem(this); + } /* minimumBoxWidth = nameWidth+2*nameMargin; @@ -32,11 +39,27 @@ GroupItem::GroupItem(BoxItem *_parentItem, selected = false; - setZValue(-100); + setZValue(100); setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges); - updateGeometry(); + + updateGeometry(InterfaceMove); + QPointF initPos = QPointF(0.0,0.0) - originPoint; + setPos(initPos); + cout << "total size of group: " << totalWidth << "," << totalHeight << endl; + cout << "pos in scene: " << x() << "," << y() << endl; +} + +GroupItem::GroupItem(Dispatcher *_dispatcher,Parameters *_params) throw(Exception) :AbstractBoxItem(_dispatcher, _params) { + + parentItem = NULL; + rectTitle = QRectF(0,-(nameHeight+2*nameMargin),nameWidth+2*nameMargin,nameHeight+2*nameMargin); + selected = false; + setZValue(100); + setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges); + + updateGeometry(InterfaceMove); QPointF initPos = QPointF(0.0,0.0) - originPoint; setPos(initPos); cout << "total size of group: " << totalWidth << "," << totalHeight << endl; @@ -56,6 +79,13 @@ BoxItem* GroupItem::getParentItem() { return parentItem; } +void GroupItem::setParentItem(BoxItem *_parentItem) { + parentItem = _parentItem; + if (parentItem != NULL) { + parentItem->setChildGroupItem(this); + } +} + void GroupItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { if(boxWidth > 0 && boxHeight > 0){ if(selected) @@ -96,7 +126,7 @@ void GroupItem::updateMinimumSize() { if (getScene() == NULL) return; - QList blocks = getScene()->getBlockItems(); + QList blocks = getScene()->getBoxItems(); if(blocks.length() > 0) { // first, search for blocks that are at (0,0) int xMaxZero = 0; @@ -155,7 +185,7 @@ void GroupItem::updateMinimumSize() { } void GroupItem::updateShape() { - updateGeometry(); + updateGeometry(InterfaceMove); } bool GroupItem::updateGeometry(ChangeType type) { @@ -163,20 +193,29 @@ bool GroupItem::updateGeometry(ChangeType type) { QPointF oldOrigin = originPoint; QSize oldSize(totalWidth,totalHeight); - updateMinimumSize(); bool boxSizeChanged = false; + + // whatever the change, the minimum size may have changed + updateMinimumSize(); + + if (type == Resize) { + boxSizeChanged = true; + } + // if an internal block has moved, the actual box size may be inadequate if (boxWidth < minimumBoxWidth) { boxWidth = minimumBoxWidth; boxSizeChanged = true; } if (boxHeight < minimumBoxHeight) { boxHeight = minimumBoxHeight; - boxSizeChanged = true; + boxSizeChanged = true; } + if (boxSizeChanged) { - updateInterfacesAndConnections(); + updateInterfaceAndConnectionItems(); } + // compute the max. width of interfaces' name for 4 orientations. int maxSouth = 0; int maxNorth = 0; @@ -221,35 +260,53 @@ bool GroupItem::updateGeometry(ChangeType type) { originPoint.setY(y); if ((boxSizeChanged) || (newSize != oldSize) || (originPoint != oldOrigin)) { - //cout << "must change group item shape" << endl; + //cout << "GroupItem: must change group item shape" << endl; prepareGeometryChange(); return true; } return false; } +void GroupItem::nameChanged() { + + + QFontMetrics fmId(params->defaultBlockFont); + nameWidth = fmId.width(refBlock->getName()); + nameHeight = fmId.height(); + // changing the BoxItem in the upperscene + if (parentItem != NULL) { + parentItem->nameChanged(); + } + updateGeometry(InterfaceMove); + // force the update in case of the size has not changed + update(); +} + + void GroupItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - if(params->editState == Parameters::EditGroupMove) { - QPointF absPos = currentPosition + originPoint; + if(params->editState == Parameters::EditGroupMove) { int gapX = event->scenePos().x() - cursorPosition.x(); int gapY = event->scenePos().y() - cursorPosition.y(); - //cout << "block abs. pos: " << absPos.x() << "," << absPos.y() << " | "; - //cout << "block current. pos: " << currentPosition.x() << "," << currentPosition.y() << " | "; -/* - if (absPos.x()+gapX < 0) { - gapX = -absPos.x(); + bool canMove = true; + if (refBlock->isTopGroupBlock()) { + QRectF rectGroup = boundingRectInScene(); + rectGroup.moveTo(rectGroup.x()+gapX,rectGroup.y()+gapY); + foreach(SourceItem* source, getScene()->getSourceItems()) { + QRectF rectSource = source->boundingRectInScene(); + if (rectGroup.intersects(rectSource)) canMove = false; + } } - if (absPos.y()+gapY < 0) { - gapY = -absPos.y(); + + if (canMove) { + QPointF gap(gapX,gapY); + currentPosition = currentPosition+gap; + setPos(currentPosition); + + // updating all connections of the scene. + getScene()->updateConnectionItemsShape(); } - */ - //cout << "gap: " << gapX << "," << gapY << " | "; - //cout << "scene: " << getScene()->sceneRect().x() << "," << getScene()->sceneRect().y() << endl; - QPointF gap(gapX,gapY); - currentPosition = currentPosition+gap; - setPos(currentPosition); cursorPosition = event->scenePos(); } else if(params->editState == Parameters::EditGroupResize) { @@ -283,7 +340,7 @@ void GroupItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { cout << "abnormal case while resizing block" << endl; break; } - updateGeometry(); + updateGeometry(Resize); /* // recompute the geometry of the block updateGeometry(); @@ -302,16 +359,16 @@ void GroupItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { } else if(params->editState == Parameters::EditInterfaceMove) { prepareGeometryChange(); - deplaceInterface(event->pos()); + moveInterfaceItemTo(event->pos()); // recompute the geometry of the block - updateGeometry(); + updateGeometry(InterfaceMove); // update connection from/to the selected interface foreach(ConnectionItem *item, getScene()->getConnectionItems()){ if ((item->getFromInterfaceItem() == currentInterface) || (item->getToInterfaceItem() == currentInterface)) { - item->setPathes(); + item->setPath(); } } - update(); + //update(); } } @@ -327,15 +384,10 @@ void GroupItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { int mode = getScene()->getEditionMode(); - dispatcher->setCurrentGroupWidget(getScene()->getGroupWindow()); - - /* NOTE : commneted because group interface are normally - created and the connected directly to a block within - the group. Furthermore, there can be a single connection - from a groupe interface. + dispatcher->setCurrentGroupWidget(getScene()->getGroupWidget()); if ((mode == GroupScene::AddConnection) && (params->cursorState == Parameters::CursorOnInterface)) { - InterfaceItem *inter = getInterfaceFromCursor(x,y); + InterfaceItem *inter = getInterfaceItemFromCursor(x,y); if (inter != NULL) { if (params->editState == Parameters::EditNoOperation) { @@ -352,12 +404,11 @@ void GroupItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { } } } - } - */ - if (mode == GroupScene::ItemEdition) { + } + else if (mode == GroupScene::ItemEdition) { if (params->cursorState == Parameters::CursorOnInterface) { - InterfaceItem *inter = getInterfaceFromCursor(x,y); + InterfaceItem *inter = getInterfaceItemFromCursor(x,y); if (inter != NULL) { currentInterface = inter; params->setEditState(Parameters::EditInterfaceMove); @@ -379,11 +430,6 @@ void GroupItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { int mode = getScene()->getEditionMode(); - /* NOTE : commneted because group interface are normally - created and the connected directly to a block within - the group. Furthermore, there can be a single connection - from a groupe interface. - if (mode == GroupScene::AddConnection) { if (params->editState == Parameters::EditStartConnection) { @@ -401,19 +447,25 @@ void GroupItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { } else if (params->editState == Parameters::EditCloseConnection) { InterfaceItem* iface1 = getScene()->getSelectedInterface(1); - InterfaceItem* iface2 = getScene()->getSelectedInterface(2); - bool ok = dispatcher->connect(iface1,iface2); + InterfaceItem* iface2 = getScene()->getSelectedInterface(2); + bool ok = dispatcher->createConnection(iface1,iface2); if (ok) { iface1->selected = false; update(iface1->boundingRect()); + iface2->selected = false; + update(iface2->boundingRect()); getScene()->setSelectedInterface(1,NULL); getScene()->setSelectedInterface(2,NULL); params->setEditState(Parameters::EditNoOperation); } + else { + //QMessageBox::warning(NULL,"Error","Cannot connect selected interfaces", QMessageBox::Ok); + getScene()->setSelectedInterface(2,NULL); + params->setEditState(Parameters::EditStartConnection); + } } - } - */ - if (mode == GroupScene::ItemEdition) { + } + else if (mode == GroupScene::ItemEdition) { currentInterface = NULL; setFlag(ItemIsMovable, true); params->editState = Parameters::EditNoOperation; @@ -429,7 +481,7 @@ void GroupItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { int mode = getScene()->getEditionMode(); if (mode == GroupScene::AddConnection) { - InterfaceItem* iface = getInterfaceFromCursor(x,y); + InterfaceItem* iface = getInterfaceItemFromCursor(x,y); if (iface != NULL) { params->cursorState = Parameters::CursorOnInterface; setCursor(Qt::PointingHandCursor); @@ -443,7 +495,7 @@ void GroupItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { int marginE = 5; int marginS = 5; - InterfaceItem* iface = getInterfaceFromCursor(x,y); + InterfaceItem* iface = getInterfaceItemFromCursor(x,y); if (iface != NULL) { params->cursorState = Parameters::CursorOnInterface; setCursor(Qt::PointingHandCursor); @@ -485,17 +537,25 @@ void GroupItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { } } } - QGraphicsItem::hoverMoveEvent(event); + //QGraphicsItem::hoverMoveEvent(event); + event->ignore(); } void GroupItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { QMenu menu; + QAction* titleAction = NULL; QAction* showProperties = NULL; QAction* removeAction = NULL; QAction* renameAction = NULL; + QAction* showParameters = NULL; - InterfaceItem* ifaceItem = getInterfaceFromCursor(event->pos().x(), event->pos().y()); - if( ifaceItem != NULL){ + InterfaceItem* ifaceItem = getInterfaceItemFromCursor(event->pos().x(), event->pos().y()); + + // menu for interface + if( ifaceItem != NULL) { + titleAction = menu.addAction("Interface operations"); + titleAction->setEnabled(false); + menu.addSeparator(); showProperties = menu.addAction("Show properties"); renameAction = menu.addAction("Rename"); menu.addSeparator(); @@ -510,6 +570,13 @@ void GroupItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { } } else { + titleAction = menu.addAction("Block operations"); + titleAction->setEnabled(false); + menu.addSeparator(); + + if (refBlock->nbParameters() > 0) { + showParameters = menu.addAction("Show parameters"); + } renameAction = menu.addAction("Rename"); } QAction* selectedAction = menu.exec(event->screenPos()); @@ -518,16 +585,19 @@ void GroupItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { if(selectedAction == renameAction){ if(ifaceItem != NULL) - dispatcher->rename(ifaceItem); + dispatcher->renameInterface(ifaceItem); else - dispatcher->rename(this); + dispatcher->renameGroupBlock(this); } else if(selectedAction == showProperties){ dispatcher->showProperties(ifaceItem); - } + } else if (selectedAction == removeAction) { dispatcher->removeGroupInterface(ifaceItem); } + else if(selectedAction == showParameters) { + new ParametersWindow(refBlock, params, NULL); + } } InterfaceItem* GroupItem::isHoverInterface(QPointF point) { @@ -538,6 +608,79 @@ InterfaceItem* GroupItem::isHoverInterface(QPointF point) { return NULL; } +void GroupItem::load(QDomElement groupElement) throw(Exception) { + + GroupBlock* groupBlock = AB_TO_GRP(refBlock); + + bool ok = false; + + int id = groupElement.attribute("id","none").toInt(&ok); + if(!ok) throw(Exception(PROJECTFILE_CORRUPTED)); + + QString nameStr = groupElement.attribute("name","none"); + if(nameStr == "none") throw(Exception(PROJECTFILE_CORRUPTED)); + + QStringList positionStr = groupElement.attribute("position","none").split(","); + if(positionStr.length() != 2) throw(Exception(PROJECTFILE_CORRUPTED)); + int posX = positionStr.at(0).toInt(&ok); + if(!ok) throw(Exception(PROJECTFILE_CORRUPTED)); + int posY = positionStr.at(1).toInt(&ok); + if(!ok) throw(Exception(PROJECTFILE_CORRUPTED)); + + QStringList dimensionStr = groupElement.attribute("dimension","none").split(","); + if(dimensionStr.length() != 2) throw(Exception(PROJECTFILE_CORRUPTED)); + int dimX = dimensionStr.at(0).toInt(&ok); + if(!ok) throw(Exception(PROJECTFILE_CORRUPTED)); + int dimY = dimensionStr.at(1).toInt(&ok); + if(!ok) throw(Exception(PROJECTFILE_CORRUPTED)); + + setId(id); + setPos(posX,posY); + setDimension(dimX,dimY); + groupBlock->setName(nameStr); + + cout << "group info : \n-id : " << id << "\n-pos : " << posX << ", " << posY << "\n-dim : " << dimX << ", " << dimY << "\n-name : " << nameStr.toStdString() << endl; + + QDomNodeList interfaces = groupElement.elementsByTagName("group_iface"); + for(int j=0; jsetAssociatedIface(groupIface); + + InterfaceItem *interfaceItem = new InterfaceItem(position,orientation,groupIface,this,params); + interfaceItem->setId(id); + + groupBlock->addInterface(groupIface); + groupBlock->addInterface(groupCtlIface); + addInterfaceItem(interfaceItem, false); + cout << "interface add to " << groupBlock->getName().toStdString() << endl; + } + +} + void GroupItem::save(QXmlStreamWriter &writer) { writer.writeStartElement("group_item"); @@ -548,7 +691,7 @@ void GroupItem::save(QXmlStreamWriter &writer) { if(parentItem != NULL){ attrUpperItem = QString::number(parentItem->getId()); } - QString attrPos = QString::number(pos().x()).append(",").append(QString::number(pos().y())); + QString attrPos = QString::number((int)(pos().x())).append(",").append(QString::number((int)(pos().y()))); QString attrDim = QString::number(getWidth()).append(",").append(QString::number(getHeight())); @@ -566,8 +709,8 @@ void GroupItem::save(QXmlStreamWriter &writer) { writer.writeStartElement("group_iface"); writer.writeAttribute("id",QString::number(item->getId())); - writer.writeAttribute("name",item->getName()); - writer.writeAttribute("level",QString(item->refInter->getLevelString())); + writer.writeAttribute("name",item->getName()); + writer.writeAttribute("purpose",QString(item->refInter->getPurposeString())); writer.writeAttribute("direction",QString(item->refInter->getDirectionString())); writer.writeAttribute("orientation",item->getStrOrientation()); writer.writeAttribute("position",QString::number(item->getPositionRatio()));