]> AND Private Git Repository - blast.git/blobdiff - GroupItem.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
added sources managment
[blast.git] / GroupItem.cpp
index 5d843811941a6bbd3334858beab637d8f6ac5232..3286c14a9ae5e9d74863cab1c4f6e92ea7e05d40 100644 (file)
@@ -5,6 +5,7 @@
 #include "Dispatcher.h"
 #include "Parameters.h"
 #include "BoxItem.h"
+#include "SourceItem.h"
 #include "AbstractBlock.h"
 #include "AbstractInterface.h"
 #include "ConnectedInterface.h"
@@ -21,7 +22,7 @@ GroupItem::GroupItem(BoxItem *_parentItem,
 
   parentItem = _parentItem;
   if (parentItem != NULL) {
-    parentItem->setChildGroupItem(this);
+    parentItem->setChildGroupItem(this);    
   }
 
   /*
@@ -38,7 +39,7 @@ GroupItem::GroupItem(BoxItem *_parentItem,
   selected = false;
 
 
-  setZValue(-100);
+  setZValue(100);
 
   setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);  
 
@@ -55,7 +56,7 @@ GroupItem::GroupItem(Dispatcher *_dispatcher,Parameters *_params) throw(Exceptio
   parentItem = NULL;
   rectTitle = QRectF(0,-(nameHeight+2*nameMargin),nameWidth+2*nameMargin,nameHeight+2*nameMargin);
   selected = false;
-  setZValue(-100);
+  setZValue(100);
   setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
 
   updateGeometry(InterfaceMove);
@@ -125,7 +126,7 @@ void GroupItem::updateMinimumSize() {
 
   if (getScene() == NULL) return;
 
-  QList<BoxItem *> blocks = getScene()->getBlockItems();
+  QList<BoxItem *> blocks = getScene()->getBoxItems();
   if(blocks.length() > 0) {
     // first, search for blocks that are at (0,0)
     int xMaxZero = 0;
@@ -211,7 +212,7 @@ bool GroupItem::updateGeometry(ChangeType type) {
   }
 
   if (boxSizeChanged) {
-    updateInterfacesAndConnections();
+    updateInterfaceAndConnectionItems();
   }
 
 
@@ -259,40 +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::interfaceRenamed() {
-  updateGeometry(InterfaceMove);     
+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) {
@@ -345,7 +359,7 @@ void GroupItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
   }
   else if(params->editState == Parameters::EditInterfaceMove) {
     prepareGeometryChange();
-    moveInterfaceTo(event->pos());
+    moveInterfaceItemTo(event->pos());
     // recompute the geometry of the block
     updateGeometry(InterfaceMove);
     // update connection from/to the selected interface
@@ -372,13 +386,8 @@ void GroupItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
 
   dispatcher->setCurrentGroupWidget(getScene()->getGroupWidget());
 
-  /* 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) && (params->cursorState == Parameters::CursorOnInterface)) {
-    InterfaceItem *inter = getInterfaceFromCursor(x,y);
+    InterfaceItem *inter = getInterfaceItemFromCursor(x,y);
     if (inter != NULL) {
 
       if (params->editState == Parameters::EditNoOperation) {
@@ -395,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);
@@ -422,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) {
@@ -444,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;
@@ -472,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);
@@ -486,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);
@@ -528,7 +537,8 @@ void GroupItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
       }
     }
   }
-  QGraphicsItem::hoverMoveEvent(event);
+  //QGraphicsItem::hoverMoveEvent(event);
+  event->ignore();
 }
 
 void GroupItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
@@ -539,7 +549,7 @@ void GroupItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
   QAction* renameAction = NULL;
   QAction* showParameters = NULL;
 
-  InterfaceItem* ifaceItem = getInterfaceFromCursor(event->pos().x(), event->pos().y());
+  InterfaceItem* ifaceItem = getInterfaceItemFromCursor(event->pos().x(), event->pos().y());
 
   // menu for interface
   if( ifaceItem != NULL) {
@@ -577,7 +587,7 @@ void GroupItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
     if(ifaceItem != NULL)
       dispatcher->renameInterface(ifaceItem);
     else
-      dispatcher->renameBlockOrGroup(this);
+      dispatcher->renameGroupBlock(this);
   }
   else if(selectedAction == showProperties){
     dispatcher->showProperties(ifaceItem);
@@ -587,7 +597,7 @@ void GroupItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
   }
   else if(selectedAction == showParameters) {
     new ParametersWindow(refBlock, params, NULL);
-  }
+  }   
 }
 
 InterfaceItem* GroupItem::isHoverInterface(QPointF point) {
@@ -608,7 +618,7 @@ void GroupItem::load(QDomElement groupElement) throw(Exception) {
   if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
 
   QString nameStr = groupElement.attribute("name","none");
-  if(nameStr == "none") throw(Exception(PROJECTFILE_CORRUPTED));
+  if(nameStr == "none") throw(Exception(PROJECTFILE_CORRUPTED));    
 
   QStringList positionStr = groupElement.attribute("position","none").split(",");
   if(positionStr.length() != 2) throw(Exception(PROJECTFILE_CORRUPTED));
@@ -641,6 +651,10 @@ void GroupItem::load(QDomElement groupElement) throw(Exception) {
     QString name = currentInterfaceNode.attribute("name","none");
     if(name == "none") throw(Exception(PROJECTFILE_CORRUPTED));
 
+    QString purposeStr = currentInterfaceNode.attribute("purpose","none");
+    int purpose = AbstractInterface::getIntPurpose(purposeStr);
+    if(purpose == -1) throw(Exception(PROJECTFILE_CORRUPTED));
+    
     QString directionStr = currentInterfaceNode.attribute("direction","none");
     int direction = AbstractInterface::getIntDirection(directionStr);
     if(direction == -1) throw(Exception(PROJECTFILE_CORRUPTED));
@@ -651,14 +665,17 @@ void GroupItem::load(QDomElement groupElement) throw(Exception) {
 
     double position = currentInterfaceNode.attribute("position","none").toDouble(&ok);
     if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
-
-    GroupInterface *groupInterface = new GroupInterface(groupBlock,name,direction,AbstractInterface::Data);
-
-    InterfaceItem *interfaceItem = new InterfaceItem(position,orientation,groupInterface,this,params);
+    
+    GroupInterface *groupIface = new GroupInterface(groupBlock,name,direction,purpose);
+    GroupInterface *groupCtlIface = new GroupInterface(groupBlock,name+"_enb",direction,AbstractInterface::Control);
+    groupCtlIface->setAssociatedIface(groupIface);
+    
+    InterfaceItem *interfaceItem = new InterfaceItem(position,orientation,groupIface,this,params);
     interfaceItem->setId(id);
 
-    groupBlock->addInterface(groupInterface);
-    addInterface(interfaceItem, false);
+    groupBlock->addInterface(groupIface);
+    groupBlock->addInterface(groupCtlIface);
+    addInterfaceItem(interfaceItem, false);
     cout << "interface add to " << groupBlock->getName().toStdString() << endl;
   }
 
@@ -693,6 +710,7 @@ void GroupItem::save(QXmlStreamWriter &writer) {
 
     writer.writeAttribute("id",QString::number(item->getId()));
     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()));