]> Creatis software - bbtkGEditor.git/blobdiff - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
Actual Version : It is possible to define, to create and to save a complex box ....
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsKernelEditorGraphic / wxVtkSceneManager.cxx
index 9fd1e7cdda6f1239a074209b360f666b7acd476b..784c3f667f9fd4aa1d301e716449145e37ead717 100644 (file)
@@ -48,6 +48,7 @@ namespace bbtk
                _idManager=idManager;
                _baseView=baseView;
                _startDragging=false;
+               _isComplexBox=false;
 
                if( _baseView!=NULL )
                {
@@ -72,7 +73,7 @@ namespace bbtk
        wxVtkSceneManager::~wxVtkSceneManager()
        {
        }
-
        //=========================================================================
 
        void wxVtkSceneManager::configureBaseView()
@@ -99,7 +100,6 @@ namespace bbtk
 
        int wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
        {
-
                int windowWidth=_baseView->GetRenWin()->GetSize()[0];
                int windowHeight=_baseView->GetRenWin()->GetSize()[1];
 
@@ -195,7 +195,125 @@ namespace bbtk
 
        //=========================================================================
 
-       int wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
+       int wxVtkSceneManager::createGComplexBoxInputPort(std::string inputName)
+       {
+               int windowWidth=_baseView->GetRenWin()->GetSize()[0];
+               int windowHeight=_baseView->GetRenWin()->GetSize()[1];
+               
+               int type = GCOMPLEXINPUTPORT;
+
+               //Create the MVC Objects
+               
+               GComplexBoxPortModel *model = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
+               vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
+               GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
+                               
+               //Prepares the initial model 
+
+               double xx = 5;
+               double yy = windowHeight-5;
+               
+               //z value is not important yet, because it is only used a parallel projection
+               double zz = 900;
+
+               _baseView->TransCoordScreenToWorld(xx,yy,zz);
+               model->setInicPoint(xx,yy,zz);
+                               
+               model->setBBTKName(inputName);
+               model->setBBTKType("ComplexInputPort");
+               model->setComplexPortType(type);
+                               
+               model->addObserver(view);
+               model->addObserver(this);
+               
+               //create the output port                
+               GPortController* portController = createGPort(GOUTPUTPORT,inputName,"ComplexInputPort",0,model);
+               model->addOutputPort((GPortModel*)portController->getModel());
+               
+               //Associates the view with the correspondent renderer and the  model.
+               //(NOTE: Refresh is only made by the view)
+               view->setModel(model);
+               view->setBaseView(_baseView);
+               view->initVtkObjects();
+               
+               //Associates the controller with the correspondent model and view
+               controller->setModelAndView(model,view);
+
+               //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;
+               return newId;
+       }
+
+       //=========================================================================
+       
+       int wxVtkSceneManager::createGComplexBoxOutputPort(std::string outputName)
+       {
+               int windowWidth=_baseView->GetRenWin()->GetSize()[0];
+               int windowHeight=_baseView->GetRenWin()->GetSize()[1];
+
+               int type = GCOMPLEXOUTPUTPORT;
+
+               //Create the MVC Objects
+               
+               GComplexBoxPortModel *model = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
+               vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
+               GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
+                               
+               //Prepares the initial model 
+
+               double xx = 5;
+               double yy = windowHeight-5;
+               
+               //z value is not important yet, because it is only used a parallel projection
+               double zz = 900;
+
+               _baseView->TransCoordScreenToWorld(xx,yy,zz);
+               model->setInicPoint(xx,yy,zz);
+                               
+               model->setBBTKName(outputName);
+               model->setBBTKType("ComplexOutputPort");
+               model->setComplexPortType(type);
+                               
+               model->addObserver(view);
+               model->addObserver(this);
+               
+               //create the output port                
+               GPortController* portController = createGPort(GINPUTPORT,outputName,"ComplexInputPort",0,model);
+               model->addInputPort((GPortModel*)portController->getModel());
+               
+               //Associates the view with the correspondent renderer and the  model.
+               //(NOTE: Refresh is only made by the view)
+               view->setModel(model);
+               view->setBaseView(_baseView);
+               view->initVtkObjects();
+               
+               //Associates the controller with the correspondent model and view
+               controller->setModelAndView(model,view);
+
+               //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;
+               return newId;
+       }
+
+       //=========================================================================
+
+       int wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxInputDescriptor *desc)
        {
                GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
                blackBox->addInputPort((GPortModel*)portController->getModel());
@@ -204,7 +322,7 @@ namespace bbtk
 
        //=========================================================================
 
-       int wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
+       int wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
        {
                GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
                blackBox->addOutputPort((GPortModel*)portController->getModel());
@@ -213,7 +331,7 @@ namespace bbtk
 
        //=========================================================================
 
-       GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBlackBoxModel *blackBox)
+       GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBoxModel *blackBox)
        {
                int type = GPORT;
 
@@ -798,7 +916,7 @@ namespace bbtk
 
        //=========================================================================
 
-       std::string wxVtkSceneManager::getDiagramScript()
+       std::string wxVtkSceneManager::getDiagramBBS()
        {
                bool existsExec=false;
 
@@ -933,6 +1051,201 @@ namespace bbtk
 
        //=========================================================================
 
+       std::string wxVtkSceneManager::saveComplexBoxBBS(std::string cbName,std::string cbAuthor,std::string cbCategory,std::string cbDescription)
+       {
+
+               std::vector<std::string> packages;
+               std::vector<int> boxes;
+               std::vector<int> connections;
+               std::vector<int> execBoxes;
+
+               std::map<int, GObjectController*>::iterator it;
+
+               for(it = _controllers.begin(); it != _controllers.end(); ++it)
+               {
+                       GObjectController *desc = it->second;
+                       int type = desc->getGObjectType();
+
+                       if(type==GBLACKBOX)
+                       {
+                               GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
+                               
+                               std::string pkg = mod->getBBTKPackage();
+                               bool existsPkg = false;
+                               for(int t = 0;t<packages.size() && existsPkg == false;t++)
+                               {
+                                       if(packages[t]==pkg)
+                                       {
+                                               existsPkg=true;
+                                       }
+                               }
+                               if(!existsPkg)
+                               {
+                                       packages.push_back(pkg);
+                               }
+
+
+                               boxes.push_back(it->first);
+                               if(mod->isExecutable())
+                               {
+                                       execBoxes.push_back(it->first);
+                               }
+                       }
+                       else if(type==GCONNECTOR)
+                       {
+                               connections.push_back(it->first);
+                       }
+               }
+
+               std::string script = "";
+               int i;
+               for(i = 0; i<packages.size();i++)
+               {
+                       script+="include ";
+                       script+=packages[i];
+                       script+="\n";
+               }
+
+               script+="include std\n";
+
+               // Definition of a complex box
+               script+="define ";
+               script+=cbName;
+               script+="\n";
+
+               script+="author \" ";
+               script+=cbAuthor;
+               script+="\"\n";
+
+               script+="description \" ";
+               script+=cbDescription;
+               script+="\"\n";
+
+               // Create boxes
+               for(i = 0; i<boxes.size();i++)
+               {
+                       script+="new ";
+                       int id = boxes[i];
+                       GObjectController *control = _controllers[id];
+                       GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
+
+                       script+=model->getBBTKType();
+                       script+=" ";
+                       script+=model->getBBTKName();
+                       script+="\n";
+
+                               std::vector<GPortModel*> inputs = model->getInputPorts();
+                               for(int j = 0; j<inputs.size();j++)
+                               {
+                                       GPortModel* inputPort = inputs[j];
+                                       if(inputPort->isValueSet())
+                                       {
+                                               script+="set ";
+                                               script+=model->getBBTKName();
+                                               script+=".";
+                                               script+=inputPort->getBBTKName();
+                                               script+=" ";
+                                               script+=inputPort->getValue();
+                                               script+="\n";
+                                       }
+                               }
+
+                       }
+
+                       // Create connections in the script. If the connection is made with a complex port, it is created the input or output
+
+                       std::string complexInputs="";
+                       std::string complexOutputs="";
+
+                       for(i = 0; i<connections.size();i++)
+                       {
+                               int id = connections[i];
+                               GObjectController *control = _controllers[id];
+                               GConnectorModel *model = (GConnectorModel*)control->getModel();
+
+                               //Connection info
+                               GPortModel *start = model->getStartPort();
+                               GBoxModel *startBox =start->getParentBox();
+
+                               GPortModel *end = model->getEndPort();
+                               GBoxModel *endBox =end->getParentBox();
+
+                               if(startBox->getGObjectType()==GCOMPLEXINPUTPORT)
+                               {
+                                       complexInputs+="input ";
+                                       complexInputs+=startBox->getBBTKName();
+
+                                       complexInputs+=" ";
+                                       complexInputs+=endBox->getBBTKName();
+                                       complexInputs+=".";
+                                       complexInputs+=end->getBBTKName();
+
+                                       complexInputs+=" ";
+                                       complexInputs+="\" \"";
+
+                                       complexInputs+="\n";
+                               }
+                               else if(endBox->getGObjectType()==GCOMPLEXOUTPUTPORT)
+                               {
+                                       complexOutputs+="output ";
+                                       complexOutputs+=endBox->getBBTKName();
+
+                                       complexOutputs+=" ";
+                                       complexOutputs+=startBox->getBBTKName();
+                                       complexOutputs+=".";
+                                       complexOutputs+=start->getBBTKName();
+
+                                       complexOutputs+=" ";
+                                       complexOutputs+="\" \"";
+
+                                       complexOutputs+="\n";
+                               }
+                               else
+                               {
+                                       script+="connect ";
+                                       script+=startBox->getBBTKName();
+                                       script+=".";
+                                       script+=start->getBBTKName();
+
+                                       script+=" ";
+
+                                       //End Connection info
+                                       script+=endBox->getBBTKName();
+                                       script+=".";
+                                       script+=end->getBBTKName();
+
+                                       script+="\n";
+                               }
+                       }
+
+                       for(i = 0; i<execBoxes.size();i++)
+                       {
+                               script+="exec ";
+                               int id = execBoxes[i];
+                               GObjectController *control = _controllers[id];
+                               GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
+
+                               script+=model->getBBTKName();
+                               script+="\n";
+                       }
+
+                       script+="\n";
+                       script+="# Complex input ports\n";
+                       script+=complexInputs;
+
+                       script+="\n";
+                       script+="# Complex output ports\n";
+                       script+=complexOutputs;
+
+                       script+="\n";
+                       script+="endefine";
+                       script+="\n";           
+
+               return script;
+       }
+
+       //=========================================================================
+
        void wxVtkSceneManager::deleteAllBoxes()
        {               
                std::map<int, GObjectController*>::iterator it;
@@ -1233,6 +1546,20 @@ namespace bbtk
        }
 
        //=========================================================================
+       
+       bool wxVtkSceneManager::isComplexBox()
+       {
+               return _isComplexBox;
+       }
+
+       //=========================================================================
+
+       void wxVtkSceneManager::setComplexBox(bool val)
+       {
+               _isComplexBox=val;
+       }
+
+       //=========================================================================
 
 }  // EO namespace bbtk