]> Creatis software - bbtkGEditor.git/blobdiff - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
Reduce Refresh Calls to Boost Speed
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsWxGUIEditorGraphic / wxVtkSceneManager.cxx
index 645d959e12e38b7e2dcbe18bd699ecb206bb2772..670ed13ad7c0aeff4afb3ec1939c443dc3d6e0ce 100644 (file)
@@ -61,9 +61,9 @@ wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent,
                printf(
                                "RaC-EED 21-06-2010 %p wxVtkSceneManager::wxVtkSceneManager If you comment this line, the drag and drop functionnality is not initialized.\n",
                                this);
-#ifdef __APPLE__
+//#ifdef __APPLE__
                _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);
-#endif
+//#endif
 
                configureBaseView();
                _worldState = NOTHING_HAPPENS;
@@ -87,9 +87,10 @@ void wxVtkSceneManager::disconnectDrop() {
 
 wxVtkSceneManager::~wxVtkSceneManager() {
        //FCY memory leaks
-       printf("EED %p ~wxVtkSceneManager()\n", this);
+       printf("EED %p ~wxVtkSceneManager() START\n", this);
        disconnectDrop();
        delete _baseView;
+       printf("EED %p ~wxVtkSceneManager() END\n", this);
 }
 
 //=========================================================================
@@ -113,7 +114,6 @@ void wxVtkSceneManager::configureBaseView() {
 
        _baseView->GetRenderer()->SetBackground(0.9, 0.9, 0.9);
        _baseView->GetRenderer()->GradientBackgroundOn();
-       _baseView->Refresh();
 
        // Actos Port_Text
        _textActor = vtkTextActor3D::New();
@@ -530,14 +530,19 @@ int wxVtkSceneManager::createGConnector(GPortModel* startPort) {
        manContourView->SetColorNormalContour(0, 0, 1);
        manContourView->SetColorEditContour(0.5, 0.5, 0.5);
        manContourView->SetColorSelectContour(1, 0.8, 0);
-       manContourView->SetWidthLine(1);
+        
+        /**
+         * JGRR AND CM
+         * Width increased from 2 to 5, it simplifies line handling and spline control :P
+         * @param WidthLine  
+         */ 
+        manContourView->SetWidthLine( 3 ) ;
        manContourView->SetShowText(false);
 
        manContourControl->SetModelView(manContourModel, manContourView);
 
        manContourControl->CreateNewManualContour();
 
-       manContourView->RefreshContour();
 
        double x, y, z;
        connectorModel->getInicPoint(x, y, z);
@@ -553,8 +558,7 @@ int wxVtkSceneManager::createGConnector(GPortModel* startPort) {
 
        int bak = manContourControl->GetNumberOfPointsManualContour() - 1;
        manContourControl->_bakIdPoint = bak;
-       manContourView->Refresh();
-
+       
        manContourControl->SetMoving(false);
 
        connectorcontroller->setModelAndView(connectorModel, connectorView);
@@ -570,6 +574,7 @@ int wxVtkSceneManager::createGConnector(GPortModel* startPort) {
        connectorModel->addObserver(connectorView);
        connectorModel->addObserver(this);
 
+        manContourView->Refresh();
        return newId;
 }
 
@@ -648,7 +653,40 @@ void wxVtkSceneManager::update(int idController, int command) {
                                _selectedObjects.erase(_selectedObjects.begin() + index);
                        }
                } else if (command == INIT_CREATION_CONTOUR) {
-                       _worldState = INIT_CREATION_CONTOUR;
+
+                // JGRR & CM WH
+                typedef std::map<int , GObjectController*>::iterator TIterator ;
+                TIterator iter ;
+                TIterator iter_end ;
+                // Find current item in the controller
+                iter = _controllers.find( idController ) ;
+                iter_end = _controllers.end( ) ;
+                // If item exist 
+                if ( iter != iter_end ) {
+                    GObjectController* gobjc = iter->second ;
+
+                    std::string bbtkType = gobjc->getModel( )->getBBTKType( ) ;
+                    // And if the item is a port
+                    if ( gobjc->getModel( )->getGObjectType( ) == GPORT ) {
+                        TIterator c_it = _controllers.begin( ) ;
+                        // Go through all componets 
+                        for ( c_it ; c_it != iter_end ; c_it++ ) {
+                            GObjectController* obc = c_it->second ;
+                            int obc_type = obc->getModel( )->getGObjectType( ) ;
+                            std::string obc_bbtkType = obc->getModel( )->getBBTKType( ) ;
+                            // if the component is a port and the type is the same of that selected
+                            if ( ( obc_type == GPORT ) && ( obc_bbtkType == bbtkType ) ) {
+                                // Highliht element 
+                                obc->getView( )->setState( POSSIBLE_CONNECTION ) ;                             
+                                //obc->getView( )->setState( HIGHLIGHTED ) ;
+                            }
+                        }
+                    }
+                }
+
+                // EO JGRR & CM WH  
+                     
+                        _worldState = INIT_CREATION_CONTOUR;
                        GObjectController* cont = _controllers[idController];
                        GPortModel* startOutputPort = (GPortModel*) cont->getModel();
 
@@ -685,7 +723,31 @@ void wxVtkSceneManager::update(int idController, int command) {
                        if (finPort->getGObjectType() == GPORT) {
                                GPortModel* modelPort = (GPortModel*) finPort->getModel();
                                modelContour->setEndPort(modelPort);
-                               _parent->saveTempDiagram();
+
+                                // JGRR CM                     
+                                typedef std::map<int, GObjectController*>::iterator TIterator;
+                                TIterator c_it = _controllers.begin();
+                                TIterator iter_end = _controllers.end();
+                                // Go through all componets 
+                                for (c_it; c_it != iter_end; c_it++) {
+                                    GObjectController* obc = c_it->second;
+                                    int obc_type = obc->getModel()->getGObjectType();
+                                    int obc_state = obc->getView()->getState();
+                                    // if the component is a port and the type is the same of that selected
+                                    if ((obc_type == GPORT) && (obc_state == POSSIBLE_CONNECTION)) {
+                                        // Return element to neutral state if not selected
+                                        obc->getView()->setState(NOTHING_HAPPENS);
+                                    }
+                                    if ((obc_type == GPORT) && (obc_state == SELECTED_POSSIBLE_CONNECTION)) {
+                                        // Change element to connected state
+                                        obc->getView()->setState(HIGHLIGHTED);
+
+                                    }
+
+                                }
+                                // EO JGRR CM                                
+                    
+                                _parent->saveTempDiagram();
                        }
 
                        manualConnectorContourController
@@ -694,8 +756,7 @@ void wxVtkSceneManager::update(int idController, int command) {
                        manualConnectorContourView
                                        * connView =
                                                        (manualConnectorContourView*) manCont->GetManualViewBaseContour();
-                       connView->Refresh();
-
+                                     
                        std::map<int, GObjectController*>::iterator it2;
 
                        for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) {
@@ -706,10 +767,11 @@ void wxVtkSceneManager::update(int idController, int command) {
                                }
                                cont->SetActive(true);
                        }
+                        connView->Refresh();          
                }
 
        }
-
+        
 }
 
 //=========================================================================
@@ -757,7 +819,9 @@ bool wxVtkSceneManager::OnMouseMove() {
                        int state = desc->getView()->getState();
                        desc->getModel()->getCenter(px, py, pz);
 
-                       if (state == HIGHLIGHTED) {
+                        // JGRR & CM
+                       if ((state == HIGHLIGHTED) || (state == SELECTED_POSSIBLE_CONNECTION )) {
+                        // EO JGRR & CM    
                                okStatusMessage = true;
                                updateStatusBar(desc->getStatusText());
                                if (type == GBLACKBOX) {
@@ -812,8 +876,15 @@ bool wxVtkSceneManager::OnLeftButtonDown() {
                                if (portmod->getPortType() == GINPUTPORT
                                                && portView->getState() == HIGHLIGHTED) {
                                        isOverPort = true;
-                               } // if
-                       } // if
+                                 } // if
+                                // JGRR & CM                    
+                                if ( portmod->getPortType( ) == GINPUTPORT &&  portView->getState( ) == SELECTED_POSSIBLE_CONNECTION   ) {
+                                    isOverPort = true ;
+                                    portView->setState( HIGHLIGHTED);
+                                }                    
+                                // EO JGRR & CM 
+
+                        } // if
                } // for
 
                if (isOverPort == false) {
@@ -902,6 +973,26 @@ bool wxVtkSceneManager::OnRightButtonUp() {
        return true;
 }
 
+ ///JLGR 21-05-2012
+
+bool wxVtkSceneManager::OnMiddleButtonDown() {
+
+
+        this->_vtkInteractorStyleBaseView->StartPan();
+
+        return true;
+    }
+
+bool wxVtkSceneManager::OnMiddleButtonUp() {
+
+
+        this->_vtkInteractorStyleBaseView->EndPan();
+
+
+        return true;
+    }
+
+
 //=========================================================================
 
 GObjectController *wxVtkSceneManager::GetGBlackBoxControlerPointedByMouse() {
@@ -933,6 +1024,7 @@ void wxVtkSceneManager::UnSelectBlackBoxes() {
                int id = _selectedObjects[i];
                GObjectController* control = _controllers[id];
                control->getView()->setState(NOTHING_HAPPENS);
+               control->getModel()->notifyObservers(_idManager);
        }// for _selectedObjects
        _selectedObjects.clear();
 }
@@ -992,7 +1084,8 @@ bool wxVtkSceneManager::OnLeftDClick() {
 
 bool wxVtkSceneManager::OnChar() {
        char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
-
+        int ctrlkey = _vtkInteractorStyleBaseView->GetInteractor()->GetControlKey();
+        
        // KeyCode 127 : Delete Key
        // KeyCode 8 : Backspace Key
        if (keyCode == 8 || keyCode == 127) {
@@ -1008,6 +1101,19 @@ bool wxVtkSceneManager::OnChar() {
                        _selectedObjects.clear();
                }
        }
+        ctrlkey+=keyCode;
+        /// Duplicate 
+             if(ctrlkey==5){                
+                if(_selectedObjects.size()){
+                    std::cout<<"objects to copy :";
+                    for(int i=0;i<_selectedObjects.size();i++){
+                       std::cout<<_selectedObjects.at(i)<<" ";
+                    }
+                    DuplicateObjects(getSelectedObjects());   
+                }else{
+                    std::cout<<"No objects to copy\n";
+                }
+            }  
 
        return true;
 }
@@ -1106,7 +1212,7 @@ void wxVtkSceneManager::deleteObject(int id) {
                        _controllers.erase(it);
                }//if
        }// for
-       _parent->saveTempDiagram();
+       _parent->saveTempandUpdate();
 }
 
 //=========================================================================
@@ -1523,7 +1629,6 @@ void wxVtkSceneManager::deleteAllBoxes() {
        }
        _selectedObjects.clear();
        _controllers.clear();
-       refreshScene();
 }
 
 //=========================================================================
@@ -1546,8 +1651,6 @@ void wxVtkSceneManager::centerView() {
        _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0, 0, temp[2]);
        _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
        _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0, 0, temp[2]);
-       _baseView->RefreshView();
-
 }
 
 //=========================================================================
@@ -1994,7 +2097,6 @@ void wxVtkSceneManager::loadDiagram(stringstream &inputStream) {
 
        } // start
 
-       refresh();
 }
 //=========================================================================
 
@@ -2180,13 +2282,108 @@ int wxVtkSceneManager::addObjectController(GObjectController* objController) {
        int newId = _contLastId;//_controllers.size();
        objController->setId(newId);
        _controllers[newId] = objController;
-       std::cout
-                       << "DFCH: int wxVtkSceneManager::addObjectController(GObjectController* objController) ---- _contLastId = "
-                       << _contLastId << std::endl;
+       //std::cout << "DFCH: int wxVtkSceneManager::addObjectController(GObjectController* objController) ---- _contLastId = " << _contLastId << std::endl;
        _contLastId++;
        return newId;
 }
 
+//=========================================================================
+
+void wxVtkSceneManager::DuplicateObjects(std::map<int, GObjectController*> objectsMap) {
+
+        std::map<int, int> oldIdNewIdBoxes;
+        std::vector<int> connections;
+
+        std::vector<int> newBoxesID;
+
+        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->move(xInic + 20, yInic + 20, zInic);
+                newbox->notifyObservers(_idManager);
+                newBoxesID.push_back(newbox->getObjectId());
+
+            } else if (type == GCONNECTOR) {
+                int idCon = cont->getId();
+                connections.push_back(idCon);
+            }
+
+        }
+
+        for (int i = 0; i < (int) 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();
+        }
+
+        /// the new selected boxes are the duplicate ones
+
+        UnSelectBlackBoxes();
+        for (int i = 0; i < newBoxesID.size(); i++) {
+            _selectedObjects.push_back(newBoxesID.at(i));
+        }
+
+        for (int i = 0; i < (int) _selectedObjects.size(); i++) {
+            int id = _selectedObjects[i];
+            GObjectController* cont = _controllers[id];
+
+            cont->getView()->setState(SELECTED);
+            cont->getModel()->notifyObservers(_idManager);
+
+        }
+    }
+
+
 //=========================================================================
 
 int wxVtkSceneManager::getNumSelectedObjects() {
@@ -2471,6 +2668,59 @@ std::string wxVtkSceneManager::GetDescription() {
 //=========================================================================
 
 
+void wxVtkSceneManager::printAll(int com , int sta ){
+        
+         int tmpState = sta ;
+        if ( tmpState == NOTHING_HAPPENS ) {
+            std::cout << "CONTROLER STATE---->NOTHING_HAPPENS\n" ;
+        }
+        if ( tmpState == HIGHLIGHTED ) {
+            std::cout << "CONTROLER STATE---->HIGHLIGHTED\n" ;
+        }
+        if ( tmpState == POSSIBLE_CONNECTION ) {
+            std::cout << "CONTROLER STATE---->POSSIBLE_CONNECTION\n" ;
+        }
+        if ( tmpState == SELECTED_POSSIBLE_CONNECTION ) {
+            std::cout << "CONTROLER STATE---->SELECTED_POSSIBLE_CONNECTION\n" ;
+        }
+        if ( tmpState == CLICKED ) {
+            std::cout << "CONTROLER STATE---->CLICKED\n" ;
+        }
+        if ( tmpState == DRAG ) {
+            std::cout << "CONTROLER STATE---->DRAG\n" ;
+        }
+        if ( tmpState == SELECTED ) {
+            std::cout << "CONTROLER STATE---->SELECTED\n" ;
+        }
+        if ( tmpState == CREATING_CONTOUR ) {
+            std::cout << "CONTROLER STATE---->CREATING_CONTOUR\n" ;
+        }   
+         
+         // :P 
+
+        int command = com ;
+        if ( command == INIT_CREATION_CONTOUR ) {
+            std::cout << "COMMAND ----> INIT_CREATION_CONTOUR \n" ;
+        }
+        if ( command == FIN_CREATION_CONTOUR ) {
+            std::cout << "COMMAND ----> FIN_CREATION_CONTOUR \n" ;
+        }
+        if ( command == ADD_TO_SELECTED ) {
+            std::cout << "COMMAND ----> ADD_TO_SELECTED \n" ;
+        }
+        if ( command == DRAG_OBJECTS ) {
+            std::cout << "COMMAND ----> DRAG_OBJECTS \n" ;
+        }
+        if ( command == EDIT_BLACKBOX ) {
+            std::cout << "COMMAND ----> EDIT_BLACKBOX \n" ;
+        }
+        if ( command == REMOVE_FROM_SELECTED ) {
+            std::cout << "COMMAND ----> REMOVE_FROM_SELECTED \n" ;
+        }
+        
+    }
+
+
 } // EO namespace bbtk
 
 // EOF