]> Creatis software - bbtkGEditor.git/blobdiff - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
The actual version works with complex box automatic generated from selected boxes...
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsKernelEditorGraphic / wxVtkSceneManager.cxx
index 784c3f667f9fd4aa1d301e716449145e37ead717..50991d12c27689408f839e8cede7bfd1cac94c71 100644 (file)
@@ -182,13 +182,7 @@ namespace bbtk
                //Resgiter change to the observers of the actual model
                model->notifyObservers(_idManager);
                
-               //Register the controller of the new object
-               registerController((InteractorStyleMaracas*) controller);
-
-               //Add the object to the objects list 
-               int newId = _controllers.size();
-               controller->setId(newId);
-               _controllers[newId] = controller;
+               int newId = addObjectController(controller);
                return newId;
 
        }
@@ -242,13 +236,7 @@ namespace bbtk
                //Resgiter change to the observers of the actual model
                model->notifyObservers(_idManager);
                
-               //Register the controller of the new object
-               registerController((InteractorStyleMaracas*) controller);
-
-               //Add the object to the objects list 
-               int newId = _controllers.size();
-               controller->setId(newId);
-               _controllers[newId] = controller;
+               int newId = addObjectController(controller);
                return newId;
        }
 
@@ -301,13 +289,7 @@ namespace bbtk
                //Resgiter change to the observers of the actual model
                model->notifyObservers(_idManager);
                
-               //Register the controller of the new object
-               registerController((InteractorStyleMaracas*) controller);
-
-               //Add the object to the objects list 
-               int newId = _controllers.size();
-               controller->setId(newId);
-               _controllers[newId] = controller;
+               int newId = addObjectController(controller);
                return newId;
        }
 
@@ -359,12 +341,7 @@ namespace bbtk
 
                model->notifyObservers(_idManager);
 
-               //Register the controller of the new object
-               registerController((InteractorStyleMaracas*) controller);
-               
-               int newId = _controllers.size();
-               controller->setId(newId);
-               _controllers[newId] = controller;
+               int newId = addObjectController(controller);
 
                return (GPortController*)controller;
        }
@@ -423,22 +400,20 @@ namespace bbtk
 
                manContourControl->SetMoving( false );
 
-               registerController((InteractorStyleMaracas*) connectorcontroller);
+               connectorcontroller->setModelAndView(connectorModel,connectorView);
+
+               int newId = addObjectController(connectorcontroller);
 
                connectorcontroller->setManualContourController(manContourControl);             
                connectorModel->setManualContourModel(manContourModel);
                connectorView->setManualContourView(manContourView);
                connectorView->setModel(connectorModel);
                connectorView->setBaseView(_baseView);
-               connectorcontroller->setModelAndView(connectorModel,connectorView);
+               
 
                connectorModel->addObserver(connectorView);
                connectorModel->addObserver(this);
 
-               int newId = _controllers.size();
-               connectorcontroller->setId(newId);
-               _controllers[newId] = connectorcontroller;
-
                return newId;
        }
 
@@ -631,9 +606,6 @@ namespace bbtk
                        }
                }
                
-               
-
-
                return true;
        }
 
@@ -830,9 +802,9 @@ namespace bbtk
                GObjectController *control = _controllers[id];
                std::vector<int> controllersToRemove;
 
-               if(control->getGObjectType()==GBLACKBOX)
+               if(control->getGObjectType()==GBLACKBOX || control->getGObjectType()==GCOMPLEXINPUTPORT || control->getGObjectType()==GCOMPLEXOUTPUTPORT)
                {
-                       GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
+                       GBoxModel *bbmod = (GBoxModel*)control->getModel();
                        std::vector<GPortModel*> inputs = bbmod->getInputPorts();
                        
                        bool boxConnected = false;
@@ -1283,10 +1255,52 @@ namespace bbtk
        //=========================================================================
 
        void wxVtkSceneManager::saveDiagram(std::string &content)
-       {
-               //Print boxes
+       {               
                char buffer [50];
-               std::vector<int> boxes = getBoxes();
+
+               //Print info IF COMPLEX BOX
+               content+="COMPLEXBOX:";
+               if(_isComplexBox)
+               {
+                       content+="TRUE\n";
+
+                       //Print info complex input ports
+                       std::vector<int> inputs = getComplexInputPorts();
+                       int insize = inputs.size();
+                       content+="COMPLEXINPUTS:";
+                       sprintf (buffer, "%d", insize);
+                       content+=buffer;
+                       content+="\n";
+                       
+                       for(int i = 0;i<insize;i++)
+                       {
+                               int id = inputs[i];
+                               GObjectController *cont = _controllers[id];
+                               cont->getModel()->save(content);
+                       }
+
+                       //Print info complex output ports
+                       std::vector<int> outputs = getComplexOutputPorts();
+                       int outsize = outputs.size();
+                       content+="COMPLEXOUTPUTS:";
+                       sprintf (buffer, "%d", outsize);
+                       content+=buffer;
+                       content+="\n";
+                       
+                       for(int i = 0;i<outsize;i++)
+                       {
+                               int id = outputs[i];
+                               GObjectController *cont = _controllers[id];
+                               cont->getModel()->save(content);
+                       }
+               }
+               else
+               {
+                       content+="FALSE\n";
+               }
+
+               //Print boxes
+               std::vector<int> boxes = getBlackBoxes();
                int bsize = boxes.size();
                content+="BOXES:";
                sprintf (buffer, "%d", bsize);
@@ -1343,6 +1357,119 @@ namespace bbtk
 
                if(start)
                {
+
+                       //----------
+                       getline(inputStream,line);//COMPLEX_BOX:TRUE|FALSE
+                       char complex[15];
+                       strcpy( complex, line.c_str() );
+                       result = strtok( complex, delims );
+                       result = strtok( NULL, delims );
+                       std::string isComplexBox(result);
+
+                       if(isComplexBox=="TRUE")
+                       {
+                               _isComplexBox=true;
+
+                               //-----------------------
+                               //- COMPLEX INPUT PORTS
+                               //-----------------------
+                               getline(inputStream,line);//COMPLEXINPUTS:num
+                               char inputs[18];
+                               strcpy( inputs, line.c_str() );
+                               result = strtok( inputs, delims );
+                               result = strtok( NULL, delims );
+                                               
+                               int numInputs;
+                               std::istringstream inps(result);
+                               inps >> numInputs;
+                               
+                               for(int i = 0;i<numInputs;i++)
+                               {
+                                       //----------
+                                       getline(inputStream,line);//COMPLEX_PORT
+                                       getline(inputStream,line);//name
+                                       std::string inputPortName(line);
+
+                                       //----------
+                                       getline(inputStream,line);//xInic:yInic:zInic
+                                       char coord[80];
+                                       strcpy( coord, line.c_str() );
+                                       result = strtok( coord, delims );//xInic
+                                       std::string xInic(result);
+                                       result = strtok( NULL, delims );//yInic
+                                       std::string yInic(result);
+                                       result = strtok( NULL, delims );//zInic
+                                       std::string zInic(result);
+
+                                       double xIn, yIn, zIn;
+                                       std::istringstream xSt(xInic);
+                                       xSt >> xIn;
+                                       std::istringstream ySt(yInic);
+                                       ySt >> yIn;
+                                       std::istringstream zSt(zInic);
+                                       zSt >> zIn;
+
+                                       getline(inputStream,line);//FIN_COMPLEX_PORT
+
+                                       int idInputPort = createGComplexBoxInputPort(inputPortName);
+                                       GObjectController *cont = _controllers[idInputPort];
+                                       GBoxModel *cbmod = (GBoxModel*)cont->getModel();
+                                       cbmod->setInicPoint(xIn,yIn,zIn);
+                                       cbmod->notifyObservers(_idManager);
+                               }
+
+
+                               //-----------------------
+                               //- COMPLEX OUTPUT PORTS
+                               //-----------------------
+
+                               getline(inputStream,line);//COMPLEXOUTPUTS:num
+                               char outputs[18];
+                               strcpy( outputs, line.c_str() );
+                               result = strtok( outputs, delims );
+                               result = strtok( NULL, delims );
+                                               
+                               int numOutputs;
+                               std::istringstream outps(result);
+                               outps >> numOutputs;
+                               
+                               for(int i = 0;i<numOutputs;i++)
+                               {
+                                       //----------
+                                       getline(inputStream,line);//COMPLEX_PORT
+                                       getline(inputStream,line);//name
+                                       std::string outputPortName(line);
+
+                                       //----------
+                                       getline(inputStream,line);//xInic:yInic:zInic
+                                       char coord[80];
+                                       strcpy( coord, line.c_str() );
+                                       result = strtok( coord, delims );//xInic
+                                       std::string xInic(result);
+                                       result = strtok( NULL, delims );//yInic
+                                       std::string yInic(result);
+                                       result = strtok( NULL, delims );//zInic
+                                       std::string zInic(result);
+
+                                       double xIn, yIn, zIn;
+                                       std::istringstream xSt(xInic);
+                                       xSt >> xIn;
+                                       std::istringstream ySt(yInic);
+                                       ySt >> yIn;
+                                       std::istringstream zSt(zInic);
+                                       zSt >> zIn;
+
+                                       getline(inputStream,line);//FIN_COMPLEX_PORT
+
+                                       int idOutputPort = createGComplexBoxOutputPort(outputPortName);
+                                       GObjectController *cont = _controllers[idOutputPort];
+                                       GBoxModel *cbmod = (GBoxModel*)cont->getModel();
+                                       cbmod->setInicPoint(xIn,yIn,zIn);
+                                       cbmod->notifyObservers(_idManager);
+                               }
+
+                       }
+
                        //----------
                        getline(inputStream,line);//BOXES:num
                        char boxes[9];
@@ -1478,7 +1605,7 @@ namespace bbtk
                                result = strtok( NULL, delims );
                                std::string nameEndPort(result);
 
-                               std::vector<int> lstB = getBoxes();
+                               std::vector<int> lstB = getBlackBoxes();
                                
                                GPortModel *startP=NULL;
                                GPortModel *endP=NULL;                          
@@ -1495,7 +1622,38 @@ namespace bbtk
                                                endP = bbMod->getInputPort(nameEndPort);
                                        }
                                }
-                               
+
+                               if(_isComplexBox)
+                               {
+                                       if(startP==NULL)
+                                       {
+                                               std::vector<int> lstInputs = getComplexInputPorts();
+                                               for(int j = 0;j<lstInputs.size();j++)
+                                               {
+                                                       int idB = lstInputs[j];
+                                                       GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
+                                                       if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
+                                                       {                                               
+                                                               startP = cbMod->getOutputPort(nameStartPort);
+                                                       }
+                                               }
+                                       }
+
+                                       if(endP==NULL)
+                                       {
+                                               std::vector<int> lstOutputs = getComplexOutputPorts();
+                                               for(int j = 0;j<lstOutputs.size();j++)
+                                               {
+                                                       int idB = lstOutputs[j];
+                                                       GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
+                                                       if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
+                                                       {                                               
+                                                               endP = cbMod->getInputPort(nameEndPort);
+                                                       }
+                                               }
+                                       }
+                               }
+                                                               
                                int idCon = createGConnector(startP);
                                _worldState = NOTHING_HAPPENS;
                                GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
@@ -1513,7 +1671,7 @@ namespace bbtk
 
        //=========================================================================
 
-       std::vector<int> wxVtkSceneManager::getBoxes()
+       std::vector<int> wxVtkSceneManager::getBlackBoxes()
        {
                std::vector<int> vect;
                std::map<int, GObjectController*>::iterator it;
@@ -1530,6 +1688,40 @@ namespace bbtk
 
        //=========================================================================
 
+       std::vector<int> wxVtkSceneManager::getComplexInputPorts()
+       {
+               std::vector<int> vect;
+               std::map<int, GObjectController*>::iterator it;
+               for(it = _controllers.begin(); it != _controllers.end(); ++it)
+               {
+                       GObjectController *cont = it->second;           
+                       if(cont->getGObjectType()==GCOMPLEXINPUTPORT)
+                       {
+                               vect.push_back(cont->getId());
+                       }
+               }
+               return vect;
+       }
+
+       //=========================================================================
+
+       std::vector<int> wxVtkSceneManager::getComplexOutputPorts()
+       {
+               std::vector<int> vect;
+               std::map<int, GObjectController*>::iterator it;
+               for(it = _controllers.begin(); it != _controllers.end(); ++it)
+               {
+                       GObjectController *cont = it->second;           
+                       if(cont->getGObjectType()==GCOMPLEXOUTPUTPORT)
+                       {
+                               vect.push_back(cont->getId());
+                       }
+               }
+               return vect;
+       }
+
+       //=========================================================================
+
        std::vector<int> wxVtkSceneManager::getConnections()
        {
                std::vector<int> vect;
@@ -1561,6 +1753,246 @@ namespace bbtk
 
        //=========================================================================
 
+       int wxVtkSceneManager::addObjectController(GObjectController* objController)
+       {
+               //Register the controller of the new object
+               registerController((InteractorStyleMaracas*) objController);
+
+               //Add the object to the objects list 
+               int newId = _controllers.size();
+               objController->setId(newId);
+               _controllers[newId] = objController;
+               return newId;
+       }
+
+       //=========================================================================
+
+       int wxVtkSceneManager::getNumSelectedObjects()
+       {
+               return _selectedObjects.size();
+       }
+
+       //=========================================================================
+
+       std::map<int,GObjectController*> wxVtkSceneManager::getSelectedObjects()
+       {
+               std::map<int,GObjectController*> mapSelected;
+
+               std::map<int, GObjectController*>::iterator it;
+               for(it = _controllers.begin(); it != _controllers.end(); ++it)
+               {
+                       GObjectController *cont = it->second;           
+                       if(cont->getGObjectType()==GBLACKBOX && cont->getView()->getState()==SELECTED)
+                       {
+                               mapSelected[cont->getId()]=cont;
+                       }
+               }
+
+               std::map<int, GObjectController*>::iterator it2;
+               for(it2 = _controllers.begin(); it2 != _controllers.end(); ++it2)
+               {
+                       GObjectController *cont = it2->second;          
+                       if(cont->getGObjectType()==GCONNECTOR)
+                       {
+                               GConnectorModel* cmod = (GConnectorModel*)cont->getModel();
+                               GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
+                               GBoxModel* startPortParentBox = cmod->getStartPort()->getParentBox();
+
+                               std::map<int, GObjectController*>::iterator iterOBJ1 = mapSelected.find(startPortParentBox->getObjectId());
+                               std::map<int, GObjectController*>::iterator iterOBJ2 = mapSelected.find(endPortParentBox->getObjectId());
+                               
+                               if(iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end())
+                               {                                       
+                                       int ID = cont->getId();
+                                       mapSelected[ID]=cont;
+                               }
+                               
+                       }
+               }
+               return mapSelected;
+       }
+
+       //=========================================================================
+
+       void wxVtkSceneManager::addObjects(std::map<int,GObjectController*> objectsMap)
+       {
+               
+               std::map<int,int> oldIdNewIdBoxes;
+               std::vector<int> connections;
+
+               std::map<int, GObjectController*>::iterator it;         
+               for(it = objectsMap.begin(); it != objectsMap.end(); ++it)
+               {
+                       GObjectController *cont = it->second;
+                       int type = cont->getGObjectType();
+
+                       if(type==GBLACKBOX)
+                       {
+                               // Copy black box
+                               double xInic, yInic,zInic;
+                               GBlackBoxModel* copyBox = (GBlackBoxModel*)cont->getModel();
+                               copyBox->getInicPoint(xInic,yInic,zInic);
+                               int idBox = createGBlackBox(0,0,copyBox->getBBTKPackage(),copyBox->getBBTKType());
+
+                               int idcB = copyBox->getObjectId();
+                               oldIdNewIdBoxes[idcB]=idBox;
+                               cont = _controllers[idBox];                                                             
+                               GBlackBoxModel* newbox  = (GBlackBoxModel*)cont->getModel();
+                               newbox->setInicPoint(xInic,yInic,zInic);                                                        
+                               int num = newbox->getNumInputPorts();
+                               for(int j=0;j<num;j++)
+                               {
+                                       newbox->setValueToInputPort(j,copyBox->getValueInputPort(j));
+                               }
+                               newbox->notifyObservers(_idManager);
+                       }
+                       else if(type==GCONNECTOR)
+                       {
+                               int idCon = cont->getId();
+                               connections.push_back(idCon);
+                       }
+                       
+               }
+               
+               for(int i = 0 ;i<connections.size();i++)
+               {
+                       int objId = connections[i];
+                       GObjectController *cont = objectsMap[objId];                    
+                       GConnectorModel* connectModel = (GConnectorModel*)cont->getModel();
+                       
+                       GPortModel* startPort = connectModel->getStartPort();
+                       int startPortIndex = startPort->getPosInBox();
+                       GPortModel* endPort = connectModel->getEndPort();
+                       int endPortIndex = endPort->getPosInBox();                      
+
+                       GBlackBoxModel* startPortParentBox = (GBlackBoxModel*)startPort->getParentBox();
+                       GBlackBoxModel* endPortParentBox = (GBlackBoxModel*)endPort->getParentBox();
+                       
+                       int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
+                       int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
+
+                       GBlackBoxModel* newStartBox = (GBlackBoxModel*)_controllers[idNewStartBox]->getModel();
+                       GBlackBoxModel* newEndBox = (GBlackBoxModel*)_controllers[idNewEndBox]->getModel();
+
+                       GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
+                       GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
+
+                       // Creates connection 
+                       int idCon = createGConnector(newStartPort);
+                       GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
+                       GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
+                       vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
+                       tempp->endContourCreation();
+                       conMod->setEndPort(newEndPort);                                 
+                       conView->updateStartEndPoints();
+               }
+
+               std::map<int, int>::iterator itIds;             
+               for(itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds)
+               {
+                       int idOld = itIds->first;
+                       int idNew = itIds->second;
+
+                       GBlackBoxModel* oldBox = (GBlackBoxModel*)objectsMap[idOld]->getModel();
+                       GBlackBoxModel* newBox = (GBlackBoxModel*)_controllers[idNew]->getModel();
+
+                       std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
+                       std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
+                       std::vector<int> newInputConnections = newBox->getConnectedInputs();
+                       std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
+
+                       for(int k = 0; k<oldInputConnections.size();k++)
+                       {
+                               bool exist=false;
+                               int toCreate=-1;
+                               for(int l = 0; l<newInputConnections.size() && !exist;l++)
+                               {
+                                       if(oldInputConnections[k]==newInputConnections[l])
+                                       {
+                                               exist=true;
+                                       }
+                               }
+
+                               if(exist==false)
+                               {
+                                       //Create complex input 
+                                       int posInBox = oldInputConnections[k];
+                                       GPortModel* inputPort = oldBox->getInputPort(posInBox);
+                                       std::string inputPortName = inputPort->getBBTKName();
+                                       int idInputPort = createGComplexBoxInputPort(inputPortName);
+                                       GObjectController *cont = _controllers[idInputPort];
+                                       GBoxModel *cbmod = (GBoxModel*)cont->getModel();
+                                       double xIn,yIn,zIn;
+                                       inputPort->getInicPoint(xIn,yIn,zIn);
+                                       yIn+=20;
+                                       cbmod->setInicPoint(xIn,yIn,zIn);
+                                       cbmod->notifyObservers(_idManager);
+                                                                                                               
+                                       GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
+
+                                       // Creates connection 
+                                       int idCon = createGConnector(cbmod->getOutputPort(0));
+                                       GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
+                                       GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
+                                       vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
+                                       tempp->endContourCreation();
+                                       conMod->setEndPort(inputPortEnd);                                       
+                                       conView->updateStartEndPoints();
+
+                               }
+                                       
+                       }
+                       
+
+                       for(int k = 0; k<oldOutputConnections.size();k++)
+                       {
+                               bool exist=false;
+                               int toCreate=-1;
+                               for(int l = 0; l<newOutputConnections.size() && !exist;l++)
+                               {
+                                       if(oldOutputConnections[k]==newOutputConnections[l])
+                                       {
+                                               exist=true;
+                                       }
+                               }
+
+                               if(exist==false)
+                               {
+                                       //Create complex output 
+                                       int posInBox = oldOutputConnections[k];
+                                       GPortModel* outputPort = oldBox->getOutputPort(posInBox);
+                                       std::string outputPortName = outputPort->getBBTKName();
+                                       int idOutputPort = createGComplexBoxOutputPort(outputPortName);
+                                       GObjectController *cont = _controllers[idOutputPort];
+                                       GBoxModel *cbmod = (GBoxModel*)cont->getModel();
+                                       double xIn,yIn,zIn;
+                                       outputPort->getInicPoint(xIn,yIn,zIn);
+                                       yIn-=20;
+                                       cbmod->setInicPoint(xIn,yIn,zIn);
+                                       cbmod->notifyObservers(_idManager);
+                                                                                                               
+                                       GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
+
+                                       // Creates connection 
+                                       int idCon = createGConnector(outputPortEnd);
+                                       GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
+                                       GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
+                                       vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
+                                       tempp->endContourCreation();
+                                       conMod->setEndPort(cbmod->getInputPort(0));                                     
+                                       conView->updateStartEndPoints();
+
+                               }
+                                       
+                       }
+
+               }
+
+
+       }
+
+       //=========================================================================     
+
 }  // EO namespace bbtk
 
 // EOF