]> Creatis software - bbtkGEditor.git/blobdiff - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
Added the possibility to delete all the boxes in the scene
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsKernelEditorGraphic / wxVtkSceneManager.cxx
index f1f1fed1e047a0ab667d5f178a6c9842c147b749..1e325329ed126f41586323d971cd538c3ca15307 100644 (file)
@@ -47,7 +47,6 @@ namespace bbtk
                _numBoxes=0;
                _idManager=idManager;
                _baseView=baseView;
-               _idLastController=0;
                _startDragging=false;
 
                if( _baseView!=NULL )
@@ -66,7 +65,7 @@ namespace bbtk
        void wxVtkSceneManager::disconnectDrop()
        {
                _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
-       }
+       }
 
        //=========================================================================
 
@@ -118,7 +117,7 @@ namespace bbtk
                //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
 
                double xx = x;
-               double yy =  windowHeight-y;
+               double yy = windowHeight-y;
                
                //z value is not important yet, because it is only used a parallel projection
                double zz = 900;
@@ -197,19 +196,21 @@ namespace bbtk
 
        void wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
        {
-               createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
+               GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
+               blackBox->addInputPort((GPortModel*)portController->getModel());
        }
 
        //=========================================================================
 
        void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
        {
-               createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
+               GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
+               blackBox->addOutputPort((GPortModel*)portController->getModel());
        }
 
        //=========================================================================
 
-       void 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,GBlackBoxModel *blackBox)
        {
                int type = GPORT;
 
@@ -219,7 +220,6 @@ namespace bbtk
                GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
 
                model->registerInBox(blackBox,portType, posInBox);
-               blackBox->addOutputPort(model);
                
                model->setBBTKType(bbtkType);
                model->setBBTKName(bbtkName);
@@ -244,6 +244,8 @@ namespace bbtk
                int newId = _controllers.size();
                controller->setId(newId);
                _controllers[newId] = controller;
+
+               return (GPortController*)controller;
        }
 
        //=========================================================================
@@ -305,6 +307,7 @@ namespace bbtk
                connectorModel->setManualContourModel(manContourModel);
                connectorView->setManualContourView(manContourView);
                connectorView->setModel(connectorModel);
+               connectorView->setBaseView(_baseView);
                connectorcontroller->setModelAndView(connectorModel,connectorView);
 
                int newId = _controllers.size();
@@ -323,6 +326,14 @@ namespace bbtk
 
        //=========================================================================
 
+       void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
+       {
+               vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
+               baseViewControlManager->RemoveInteractorStyleMaracas( param );
+       }
+
+       //=========================================================================
+
        vtkRenderer* wxVtkSceneManager::getRenderer()
        {
                return _baseView->GetRenderer();
@@ -428,6 +439,7 @@ namespace bbtk
                                        cont->SetActive(true);                                                          
                                }
                        }
+                       
                }
        }
 
@@ -503,6 +515,42 @@ namespace bbtk
        
        bool wxVtkSceneManager::OnLeftButtonDown()
        {
+               if(_worldState==INIT_CREATION_CONTOUR)
+               {
+                       bool isOverPort=false;
+                       std::map<int, GObjectController*>::iterator it;
+                       for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
+                       {
+                               GObjectController *desc = it->second;
+                               if(desc->getGObjectType()==GPORT)
+                               {
+                                       GPortModel* portmod=(GPortModel*)desc->getModel();
+                                       vtkGObjectView* portView=desc->getView();
+                                       if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
+                                       {
+                                               isOverPort=true;
+                                       }
+                               }
+                       }
+                       
+                       if(isOverPort==false)
+                       {
+                               _worldState=NOTHING_HAPPENS;
+                               int lastId = _controllers.size()-1;
+                               GConnectorController *connector = (GConnectorController*)_controllers[lastId];
+                               connector->removeFromScene();
+                               unregisterController(connector);
+                               _controllers.erase(lastId);                     
+
+                               for(it = _controllers.begin(); it != _controllers.end(); ++it)
+                               {
+                                       GObjectController *desc = it->second;
+                                       desc->SetActive(true);
+                                       desc->getView()->setState(NOTHING_HAPPENS);
+                                       desc->getModel()->notifyObservers(_idManager);
+                               }
+                       }
+               }
                
                if(_selectedObjects.size()!=0)
                {
@@ -546,6 +594,26 @@ namespace bbtk
 
        bool wxVtkSceneManager::OnRightButtonUp()
        {
+               if(_worldState==INIT_CREATION_CONTOUR)
+               {
+                       _worldState=NOTHING_HAPPENS;
+                       int lastId = _controllers.size()-1;
+                       GConnectorController *connector = (GConnectorController*)_controllers[lastId];
+                       connector->removeFromScene();
+                       unregisterController(connector);
+                       _controllers.erase(lastId);                     
+
+                       std::map<int, GObjectController*>::iterator it;
+                       for(it = _controllers.begin(); it != _controllers.end(); ++it)
+                       {
+                               GObjectController *desc = it->second;
+                               desc->SetActive(true);
+                               desc->getView()->setState(NOTHING_HAPPENS);
+                               desc->getModel()->notifyObservers(_idManager);
+                       }
+               }
+
+                       
                for (int i = 0; i < _selectedObjects.size(); i++)
                {
                        int id = _selectedObjects[i];
@@ -562,6 +630,147 @@ namespace bbtk
 
        //=========================================================================
 
+       bool wxVtkSceneManager::OnLeftDClick()
+       {
+               int X,Y;
+               wxVTKRenderWindowInteractor *wxVTKiren;
+               wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
+               wxVTKiren->GetEventPosition(X,Y);
+
+               std::map<int, GObjectController*>::iterator it;
+
+               bool clickOnObject = false;
+
+               for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
+               {
+                       GObjectController *cont = it->second;
+                       int type = cont->getGObjectType();
+                       
+                       if(cont->getView()->isPointInside(X,Y))
+                       {
+                               if(type==GBLACKBOX)
+                               {
+                                       for (int i=0; i<_selectedObjects.size(); i++)
+                                       {
+                                               int id = _selectedObjects[i];
+                                               GObjectController* control = _controllers[id];
+                                               control->getView()->setState(NOTHING_HAPPENS);
+                                       }
+                                       _selectedObjects.clear();
+
+                                       GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
+                                       _parent->editBlackBox(bbmodel);
+                               }
+                               clickOnObject = true;                   
+                       }
+               }
+
+               if(clickOnObject==false)
+               {
+                       //_parent->editDiagramParameters(this);
+               }
+
+               return true;
+       }
+       
+       //=========================================================================
+
+       bool wxVtkSceneManager::OnChar()
+       {       
+               char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
+               
+               // KeyCode 127 : Delete Key
+               // KeyCode 8 : Backspace Key
+               if(keyCode == 8 || keyCode == 127)
+               {
+                       if(_selectedObjects.size()>0)
+                       {
+                               for(int i=0;i<_selectedObjects.size();i++)
+                               {
+                                       int id = _selectedObjects[i];
+                                       deleteObject(id);
+                               }
+                               _selectedObjects.clear();
+                       }
+               }
+
+               return true;
+       }
+
+       //=========================================================================
+
+       void wxVtkSceneManager::deleteObject(int id)
+       {
+               GObjectController *control = _controllers[id];
+               std::vector<int> controllersToRemove;
+
+               if(control->getGObjectType()==GBLACKBOX)
+               {
+                       GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
+                       std::vector<GPortModel*> inputs = bbmod->getInputPorts();
+                       
+                       bool boxConnected = false;
+
+                       // Add box input controllers to be removed
+                       for(int i = 0;i<inputs.size();i++)
+                       {
+                               controllersToRemove.push_back(inputs[i]->getObjectId());
+                               if(inputs[i]->isConnected())
+                               {
+                                       boxConnected = true;
+                               }
+                       }
+
+                       std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
+
+                       // Add box output controllers to be removed
+                       for(int i = 0;i<outputs.size();i++)
+                       {
+                               controllersToRemove.push_back(outputs[i]->getObjectId());
+                               if(outputs[i]->isConnected())
+                               {
+                                       boxConnected = true;
+                               }
+                       }
+
+                       // Add connection controllers to be removed
+                       std::map<int, GObjectController*>::iterator it;
+                       for(it = _controllers.begin(); it != _controllers.end(); ++it)
+                       {
+                               GObjectController *cont = it->second;
+                               int type = cont->getGObjectType();
+                               if(type==GCONNECTOR)
+                               {
+                                       GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
+                                       if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
+                                       {
+                                               controllersToRemove.push_back(conMod->getObjectId());
+                                       }
+                                       if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
+                                       {
+                                               controllersToRemove.push_back(conMod->getObjectId());
+                                       }
+                               }
+                       }
+
+                       // Add box controller to be removed
+                       controllersToRemove.push_back(bbmod->getObjectId());
+               }
+
+               for(int i = 0;i<controllersToRemove.size();i++)
+               {
+                       int id = controllersToRemove[i];
+                       GObjectController *cont = _controllers[id];                     
+                       cont->removeFromScene();
+                       unregisterController((InteractorStyleMaracas*)cont);                    
+                       _controllers.erase(id);
+               }
+
+
+       }
+
+       //=========================================================================
+
        void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
        {
                _parent->displayBlackBoxInfo(packageName,boxName);
@@ -576,6 +785,164 @@ namespace bbtk
 
        //=========================================================================
 
+       std::string wxVtkSceneManager::getDiagramScript()
+       {
+               bool existsExec=false;
+
+               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);
+                                       existsExec=true;
+                               }
+                       }
+                       else if(type==GCONNECTOR)
+                       {
+                               connections.push_back(it->first);
+                       }
+               }
+
+               std::string script = "";
+               script+="# BBTK GEditor Script\n";
+               script+="# ----------------------\n";
+               if(existsExec)
+               {
+                       int i;
+                       for(i = 0; i<packages.size();i++)
+                       {
+                               script+="include ";
+                               script+=packages[i];
+                               script+="\n";
+                       }
+
+                       script+="include std\n";
+
+                       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";
+                                       }
+                               }
+
+                       }
+
+                       for(i = 0; i<connections.size();i++)
+                       {
+                               script+="connect ";
+                               int id = connections[i];
+                               GObjectController *control = _controllers[id];
+                               GConnectorModel *model = (GConnectorModel*)control->getModel();
+
+                               //Start Connection info
+                               GPortModel *start = model->getStartPort();
+                               script+=start->getParentBox()->getBBTKName();
+                               script+=".";
+                               script+=start->getBBTKName();
+
+                               script+=" ";
+
+                               //End Connection info
+                               GPortModel *end = model->getEndPort();
+                               script+=end->getParentBox()->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";
+                       }
+
+               }
+
+               return script;
+       }
+
+       //=========================================================================
+
+       void wxVtkSceneManager::deleteAllBoxes()
+       {
+               std::map<int, GObjectController*>::iterator it;
+               for(it = _controllers.begin(); it != _controllers.end(); ++it)
+               {
+                       GObjectController *cont = it->second;           
+                       cont->removeFromScene();
+                       unregisterController((InteractorStyleMaracas*)cont);
+               }
+               _selectedObjects.clear();
+               _controllers.clear();   
+               refreshScene();
+       }
+
+       //=========================================================================
+
+       void wxVtkSceneManager::refreshScene()
+       {
+               _baseView->RefreshView();
+       }
+
+       //=========================================================================
+
 }  // EO namespace bbtk
 
 // EOF