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

Private GIT Repository
added creation of control ifaces
[blast.git] / BoxItem.cpp
index 77e9533be3c44d7fd729bb678d8718ad51e74b1e..de22f19ce472c4ca8c651e23fe233f5fcb9a13c9 100644 (file)
@@ -12,6 +12,7 @@
 #include "ReferenceBlock.h"
 #include "ParametersWindow.h"
 #include "BlockParameter.h"
+#include "Graph.h"
 
 
 BoxItem::BoxItem(AbstractBlock *_refBlock,
@@ -33,7 +34,7 @@ BoxItem::BoxItem(AbstractBlock *_refBlock,
   setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
 
   initInterfaces();
-  updateGeometry();
+  updateGeometry(InterfaceMove);
   resetInterfacesPosition();
   QPointF initPos = QPointF(0.0,0.0) - originPoint;
   setPos(initPos);
@@ -41,6 +42,26 @@ BoxItem::BoxItem(AbstractBlock *_refBlock,
   //cout << "pos in group: " << x() << "," << y() << endl;
 }
 
+BoxItem::BoxItem(Dispatcher *_dispatcher, Parameters *_params, GroupItem *parent) throw(Exception) : AbstractBoxItem(_dispatcher, _params, parent) {
+
+  refBlock = NULL;
+  childGroupItem = NULL;
+  currentBorder = NoBorder;
+  selected = false;
+
+  setZValue(100);
+  setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
+
+  boxWidth = params->defaultBlockWidth;
+  boxHeight = params->defaultBlockHeight;
+  //initInterfaces();
+  //updateGeometry(InterfaceMove);
+  //resetInterfacesPosition();
+  //QPointF initPos = QPointF(0.0,0.0) - originPoint;
+  //setPos(initPos);
+  //cout << "total size of block: " << totalWidth << "," << totalHeight << endl;
+  //cout << "pos in group: " << x() << "," << y() << endl;
+}
 
 BoxItem::~BoxItem() {
 }
@@ -133,27 +154,29 @@ bool BoxItem::updateGeometry(ChangeType type) {
 
   bool boxSizeChanged = false;
 
-  if ((type == Resize) || (type == InterfaceMove)) {
-    updateMinimumSize();
-  }
+  // whatever the change, the minimum size may have changed
+  updateMinimumSize();
 
   if (type == Resize) {
-    prepareGeometryChange();
-    updateInterfacesAndConnections();
-    boxSizeChanged = true;
-  }
-  if (boxWidth < minimumBoxWidth) {
-    boxWidth = minimumBoxWidth;
+    // resize implies to move interfaces and to update connections
     boxSizeChanged = true;
   }
-  if (boxHeight < minimumBoxHeight) {
-    boxHeight = minimumBoxHeight;
-     boxSizeChanged = true;
+  else if (type == InterfaceMove) {
+    // if an interface moves, it may change the box size
+    if (boxWidth < minimumBoxWidth) {
+      boxWidth = minimumBoxWidth;
+      boxSizeChanged = true;
+    }
+    if (boxHeight < minimumBoxHeight) {
+      boxHeight = minimumBoxHeight;
+      boxSizeChanged = true;
+    }
   }
   if (boxSizeChanged) {
     updateInterfacesAndConnections();
   }
 
+
   double x = 0.0;
   double y = 0.0;
   totalWidth = boxWidth;
@@ -208,7 +231,7 @@ void BoxItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
     // update all connections from/to this block
     foreach(ConnectionItem *item, getScene()->getConnectionItems()){
       if ((item->getFromInterfaceItem()->getOwner() == this) || (item->getToInterfaceItem()->getOwner() == this)) {
-        item->setPathes();
+        item->setPath();
       }
     }
     cursorPosition = event->scenePos();
@@ -248,7 +271,7 @@ void BoxItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
       break;
     }
     // recompute the geometry of the block and possibly the group item
-    if (updateGeometry()) {
+    if (updateGeometry(Resize)) {
       (getScene()->getGroupItem())->updateShape();
     }
 
@@ -256,16 +279,16 @@ void BoxItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
   }
   else if(params->editState == Parameters::EditInterfaceMove) {
     prepareGeometryChange();
-    deplaceInterface(event->pos());
+    moveInterfaceTo(event->pos());
     // recompute the geometry of the block
-    if (updateGeometry()) {
+    if (updateGeometry(InterfaceMove)) {
       cout << "must recompute group item geometry" << endl;
       (getScene()->getGroupItem())->updateShape();
     }
     // update connection from/to the selected interface
     foreach(ConnectionItem *item, getScene()->getConnectionItems()){
       if ((item->getFromInterfaceItem() == currentInterface) || (item->getToInterfaceItem() == currentInterface)) {
-        item->setPathes();
+        item->setPath();
       }
     }    
   }
@@ -283,7 +306,7 @@ void BoxItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
 
   int mode = getScene()->getEditionMode();
 
-  dispatcher->setCurrentGroupWidget(getScene()->getGroupWindow());
+  dispatcher->setCurrentGroupWidget(getScene()->getGroupWidget());
 
   if ((mode == GroupScene::AddConnection) && (params->cursorState == Parameters::CursorOnInterface)) {
     InterfaceItem *inter = getInterfaceFromCursor(x,y);
@@ -366,6 +389,7 @@ void BoxItem::mouseReleaseEvent(QGraphicsSceneMouseEvent  *event) {
         params->setEditState(Parameters::EditNoOperation);
       }
       else {
+        //QMessageBox::warning(NULL,"Error","Cannot connect selected interfaces", QMessageBox::Ok);
         getScene()->setSelectedInterface(2,NULL);
         params->setEditState(Parameters::EditStartConnection);
       }
@@ -452,48 +476,98 @@ void BoxItem::hoverMoveEvent(QGraphicsSceneHoverEvent * event) {
 void BoxItem::contextMenuEvent(QGraphicsSceneContextMenuEvent * event) {
 
   QMenu menu;
-  QAction* removeAction = menu.addAction("Remove");
-  QAction* duplicateAction = menu.addAction("Duplicate");
-  QAction* renameAction = menu.addAction("Rename");
+  QAction* titleAction = NULL;
+  QAction* removeAction = NULL;
+  QAction* duplicateAction = NULL;
+  QAction* renameAction = NULL;
   QAction* connectToGroup = NULL;
   QAction* disconnectFromGroup = NULL;
   QAction* showProperties = NULL;
   QAction* cloneInterface = NULL;
   QAction* openWindow = NULL;
-  QAction* showRstClkInter = NULL;
+  QAction* showRstClkIface = NULL;
+  QAction* showWishboneIface = NULL;
   QAction* showParameters = NULL;
 
   InterfaceItem* ifaceItem = getInterfaceFromCursor(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");
 
     ConnectedInterface* iface = ifaceItem->refInter;
+    ConnectedInterface* ifaceGroup = NULL;
+    bool canRemove = true;
+
+
     if ((iface->getDirection() == AbstractInterface::Input) && (iface->getConnectedFrom() == NULL)) {
         connectToGroup = menu.addAction("Connect to group input");
     }
-    else if ((iface->getDirection() == AbstractInterface::Output) && (!iface->isConnectedTo())) {
+    else if ((iface->getDirection() == AbstractInterface::Output) && (iface->getConnectionToParentGroup() == NULL)) {
       connectToGroup = menu.addAction("Connect to group output");
     }
-    else if ((iface->getConnectionFromParentGroup() != NULL) || (iface->getConnectionToParentGroup() != NULL)) {
-      disconnectFromGroup = menu.addAction("Disconnect from group");
+    else if (iface->getConnectionFromParentGroup() != NULL) {
+      ifaceGroup = iface->getConnectionFromParentGroup();
+      //if ((!ifaceGroup->isConnectedFrom()) || (!ifaceGroup->isConnectedTo())) {
+      if (!ifaceGroup->isConnectedFrom()) {
+        disconnectFromGroup = menu.addAction("Disconnect from group");
+      }
+      else {
+        canRemove = false;
+      }
+    }
+    else if (iface->getConnectionToParentGroup() != NULL) {
+      ifaceGroup = iface->getConnectionToParentGroup();
+      //if ((!ifaceGroup->isConnectedFrom()) || (!ifaceGroup->isConnectedTo())) {
+      if (!ifaceGroup->isConnectedTo()) {
+        disconnectFromGroup = menu.addAction("Disconnect from group");
+      }
+      else {
+        canRemove = false;
+      }
     }
 
     if (iface->isFunctionalInterface()) {
       FunctionalInterface* fi = AI_TO_FUN(ifaceItem->refInter);
       ReferenceInterface* ri = (ReferenceInterface*)(fi->getReference());
       if(ri->getMultiplicity() == -1 || ri->getMultiplicity() > 1){
-        cloneInterface = menu.addAction("Clone interface");
-      }
+        cloneInterface = menu.addAction("Duplicate");
+        if ((canRemove) && (fi->getInterfaceMultiplicity() > 1)) {
+          removeAction = menu.addAction("Remove");
+        }
+      }      
     }
   }
-  if(refBlock->isGroupBlock()){
-    openWindow = menu.addAction("Open/show group window");
-  } else {
-    showRstClkInter = menu.addAction("Show reset/clock interfaces");
-    showRstClkInter->setCheckable(true);
-    showRstClkInter->setChecked(rstClkVisible);
+  // menu for blocks (group or func)
+  else {
+    titleAction = menu.addAction("Block operations");
+    titleAction->setEnabled(false);
+    menu.addSeparator();
+
+    if (refBlock->nbParameters() > 0) {
+      showParameters = menu.addAction("Show parameters");
+    }
+    renameAction = menu.addAction("Rename");
 
-    showParameters = menu.addAction("Show parameters");
+    if(refBlock->isGroupBlock()){
+      openWindow = menu.addAction("Open/show group window");
+    }
+    else {
+      duplicateAction = menu.addAction("Duplicate");
+      showRstClkIface = menu.addAction("Show reset/clock interfaces");
+      showRstClkIface->setCheckable(true);
+      showRstClkIface->setChecked(rstClkVisible);
+      showWishboneIface = menu.addAction("Show wishbone interfaces");
+      showWishboneIface->setCheckable(true);
+      showWishboneIface->setChecked(wishboneVisible);
+    }
+    removeAction = menu.addAction("Remove");
   }
 
   QAction* selectedAction = NULL;
@@ -502,16 +576,21 @@ void BoxItem::contextMenuEvent(QGraphicsSceneContextMenuEvent * event) {
   if(selectedAction == NULL) return ;
 
   if (selectedAction == removeAction) {
-    dispatcher->removeBlock(this);
+    if(ifaceItem != NULL) {
+     dispatcher->removeBlockInterface(ifaceItem);
+    }
+    else {
+      dispatcher->removeBlock(this);
+    }
   }
   else if (selectedAction == duplicateAction) {
     dispatcher->duplicateBlock(this);
   }
   else if(selectedAction == renameAction){
     if(ifaceItem != NULL)
-      dispatcher->rename(ifaceItem);
+      dispatcher->renameInterface(ifaceItem);
     else
-      dispatcher->rename(this);
+      dispatcher->renameBlockOrGroup(this);
   }
   else if(selectedAction == showProperties){
     dispatcher->showProperties(ifaceItem);
@@ -528,13 +607,142 @@ void BoxItem::contextMenuEvent(QGraphicsSceneContextMenuEvent * event) {
   else if (selectedAction == openWindow){
     dispatcher->showRaiseWindow(this);
   }
-  else if(selectedAction == showRstClkInter){
-    dispatcher->showRstClkInter(this);
+  else if(selectedAction == showRstClkIface) {
+    dispatcher->showRstClkIface(this);
+  }
+  else if(selectedAction == showWishboneIface) {
+    dispatcher->showWishboneIface(this);
   }
   else if(selectedAction == showParameters){
     new ParametersWindow(refBlock, params, NULL);
   }
 }
+void BoxItem::loadFunctional(QDomElement funcElement) throw(Exception) {
+
+  bool ok = false;
+
+  int id = funcElement.attribute("id","none").toInt(&ok);
+  if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
+
+  QString refXml = funcElement.attribute("ref_xml","none");
+  if(refXml == "none") throw(Exception(PROJECTFILE_CORRUPTED));
+
+  QString refMd5 = funcElement.attribute("ref_md5","none");
+  if(refMd5 == "none") throw(Exception(PROJECTFILE_CORRUPTED));
+
+  cout << "ref md5 : " << refMd5.toStdString() << "\nref xml : " << refXml.toStdString() << endl;
+
+  QString name = funcElement.attribute("name","none");
+  if(name == "none") throw(Exception(PROJECTFILE_CORRUPTED));
+
+  QStringList positionStr = funcElement.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 = funcElement.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));
+
+  ReferenceBlock *referenceMd5 = NULL;
+  ReferenceBlock *referenceXml = NULL;
+  ReferenceBlock *reference = NULL;
+  if(refMd5 != "none") {
+    referenceMd5 = params->searchBlockByMd5(refMd5);
+  }
+  if(refXml != "none"){
+    referenceXml = params->searchBlockByXml(refXml);
+  }
+  if ((referenceMd5 == NULL) && (referenceXml == NULL)) {
+    throw(Exception(PROJECTFILE_CORRUPTED));
+  }
+  if (referenceMd5 != referenceXml) {
+    throw(Exception(PROJECTFILE_CORRUPTED));
+  }
+  else {
+    reference = referenceMd5;
+  }
+  GroupBlock* parentGroupBlock = AB_TO_GRP(((GroupItem *)parentItem())->getRefBlock());
+  FunctionalBlock* functionalBlock = params->getGraph()->addFunctionalBlock(parentGroupBlock, reference);
+  /* NB: addFunctionalBlock creates all interfaces from the reference, which is annoying when
+    reading bif_iface tags. Thus interface are all removed.
+  */
+  functionalBlock->setName(name);
+  setRefBlock(functionalBlock);
+
+  setPos(posX,posY);
+  setDimension(dimX,dimY);
+  setId(id);
+
+
+  QDomNodeList blockParamNodes = funcElement.elementsByTagName("bif_parameter");
+  // setting parameters value
+  for(int i=0; i<blockParamNodes.length(); i++){
+    QDomElement currentBlockParamNode = blockParamNodes.at(i).toElement();
+
+    QString name = currentBlockParamNode.attribute("name","none");
+    if(name == "none") throw(Exception(PROJECTFILE_CORRUPTED));
+
+    QString value = currentBlockParamNode.attribute("value","none");
+    if(value == "none") throw(Exception(PROJECTFILE_CORRUPTED));
+
+    BlockParameter *blockParam = NULL;
+    blockParam = functionalBlock->getParameterFromName(name);
+    if (blockParam == NULL) throw(Exception(PROJECTFILE_CORRUPTED));
+    blockParam->setValue(value);
+  }
+
+  // recreate all interface because of some may have a multiplicity>1 with several examplars
+  functionalBlock->removeAllInterfaces();
+  QDomNodeList interfaceNodes = funcElement.elementsByTagName("bif_iface");
+  // setting interfaces (user name, and for multiplicity>1 may be create some new ones)
+  for(int i=0; i<interfaceNodes.length(); i++) {
+
+    QDomElement currentInterfaceNode = interfaceNodes.at(i).toElement();
+
+    QString name = currentInterfaceNode.attribute("name","none");
+    if(name == "none") throw(Exception(PROJECTFILE_CORRUPTED));
+
+    QString refName = currentInterfaceNode.attribute("ref_name","none");
+    if(refName == "none") throw(Exception(PROJECTFILE_CORRUPTED));
+
+    ReferenceInterface* refInter = AI_TO_REF(reference->getIfaceFromName(refName));
+    FunctionalInterface *functionalInterface = new FunctionalInterface(functionalBlock,refInter);
+    functionalInterface->setName(name);
+    functionalBlock->addInterface(functionalInterface);
+  }
+  // creating InterfaceItem
+  initInterfaces();
+  // setting them with saved values
+  for(int i=0; i<interfaceNodes.length(); i++){
+
+    QDomElement currentInterfaceNode = interfaceNodes.at(i).toElement();
+
+    int id = currentInterfaceNode.attribute("id","none").toInt(&ok);
+    if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
+
+    QString name = currentInterfaceNode.attribute("name","none");
+    if(name == "none") throw(Exception(PROJECTFILE_CORRUPTED));
+
+    QString orientationStr = currentInterfaceNode.attribute("orientation","none");
+    int orientation = InterfaceItem::getIntOrientation(orientationStr);
+    if(orientation == -1) throw(Exception(PROJECTFILE_CORRUPTED));
+
+    double position = currentInterfaceNode.attribute("position","none").toDouble(&ok);
+    if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
+
+    InterfaceItem *interfaceItem = searchInterfaceByName(name);
+    interfaceItem->setId(id);
+    interfaceItem->setOrientation(orientation);
+    interfaceItem->setPositionRatio(position);
+  }
+  updateGeometry(Resize);
+}
 
 void BoxItem::save(QXmlStreamWriter &writer) {
   if (refBlock->isFunctionalBlock()) {
@@ -628,7 +836,7 @@ QDataStream &operator <<(QDataStream &out, BoxItem &b) {
   for(int i=0; i<b.getInterfaces().length(); i++){
     InterfaceItem *inter = b.getInterfaces().at(i);
     toWrite << inter->getId();
-    toWrite << inter->getName();
+    //toWrite << inter->getName();
     toWrite << inter->getPositionRatio();
     toWrite << inter->getOrientation();
   }
@@ -638,8 +846,7 @@ QDataStream &operator <<(QDataStream &out, BoxItem &b) {
   return out;
 }
 
-QDataStream &operator >>(QDataStream &in, BoxItem &b)
-{
+QDataStream &operator >>(QDataStream &in, BoxItem &b) {
 
   in.setVersion(QDataStream::Qt_4_8);
 
@@ -669,8 +876,7 @@ QDataStream &operator >>(QDataStream &in, BoxItem &b)
     in >> positionRatio;
     in >> orientation;
 
-    inter->setId(id);
-    inter->setName(name);
+    inter->setId(id);    
     inter->setPositionRatio(positionRatio);
     inter->setOrientation(orientation);
     inter->updatePosition();