]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
Added the possibility to delete all the boxes in the scene
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsKernelEditorGraphic / wxVtkSceneManager.cxx
1 /*=========================================================================                                                                               
2 Program:   bbtk
3 Module:    $RCSfile$
4 Language:  C++
5 Date:      $Date$
6 Version:   $Revision$
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32 *  \file 
33 *  \brief Class bbtk::wxVtkSceneManager . 
34 */
35
36
37 #include "wxVtkSceneManager.h"
38
39 namespace bbtk
40 {
41
42
43         //=========================================================================
44         wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView,int idManager)
45         {
46                 _parent = parent;
47                 _numBoxes=0;
48                 _idManager=idManager;
49                 _baseView=baseView;
50                 _startDragging=false;
51
52                 if( _baseView!=NULL )
53                 {
54                         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);                      
55                         configureBaseView();
56                         _worldState=NOTHING_HAPPENS;
57                         registerController(this);
58                         
59                 }
60
61         }
62
63         //=========================================================================
64
65         void wxVtkSceneManager::disconnectDrop()
66         {
67                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
68         }
69
70         //=========================================================================
71
72         wxVtkSceneManager::~wxVtkSceneManager()
73         {
74         }
75
76         //=========================================================================
77
78         void wxVtkSceneManager::configureBaseView()
79         {
80                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
81
82                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
83
84                 // Important to activate the 2D interaction system
85                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
86                 interactorstylebaseview->SetInteractor ( iren );
87                 iren->SetInteractorStyle(interactorstylebaseview);
88                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
89                 
90                 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
91                 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
92                 
93                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
94                 _baseView->GetRenderer()->GradientBackgroundOff();
95                 _baseView->Refresh();
96         }
97
98         //=========================================================================
99
100         void wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
101         {
102
103                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
104                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
105
106                 int type = GBLACKBOX;
107
108                 //Create the MVC Objects
109                 
110                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
111                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
112                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
113                 
114                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
115                 
116                 //Prepares the initial model
117                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
118
119                 double xx = x;
120                 double yy = windowHeight-y;
121                 
122                 //z value is not important yet, because it is only used a parallel projection
123                 double zz = 900;
124
125                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
126                 model->setInicPoint(xx,yy,zz);
127                 
128                 _numBoxes++;
129                 std::stringstream stream;
130                 
131                 if(_numBoxes<10)
132                 {
133                         stream << "Box0" << _numBoxes;
134                 }
135                 else
136                 {
137                         stream << "Box" << _numBoxes;
138                 }
139                 std::string arraystring = stream.str();
140
141                 model->setBBTKName(arraystring);
142                 model->setBBTKType(boxType);
143                 model->setBBTKPackage(packageName);
144                                 
145                 model->addObserver(view);
146                 model->addObserver(this);
147
148                 //Iterate and create the input ports
149                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
150                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
151
152                 int i=0;
153                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
154                 {
155                         BlackBoxInputDescriptor *desc = itInput->second;
156                         createGInputPort(GINPUTPORT,i,model,desc);
157                         i++;
158                 }
159
160                 //Iterate and create the output ports
161                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
162                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
163
164                 i=0;
165                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
166                 {
167                         BlackBoxOutputDescriptor *desc = itOutput->second;
168                         createGOutputPort(GOUTPUTPORT,i,model,desc);
169                         i++;
170                 }
171
172
173                 //Associates the view with the correspondent renderer and the  model.
174                 //(NOTE: Refresh is only made by the view)
175                 view->setModel(model);
176                 view->setBaseView(_baseView);
177                 view->initVtkObjects();
178                 
179                 //Associates the controller with the correspondent model and view
180                 controller->setModelAndView(model,view);
181
182                 //Resgiter change to the observers of the actual model
183                 model->notifyObservers(_idManager);
184                 
185                 //Register the controller of the new object
186                 registerController((InteractorStyleMaracas*) controller);
187
188                 //Add the object to the objects list 
189                 int newId = _controllers.size();
190                 controller->setId(newId);
191                 _controllers[newId] = controller;
192
193         }
194
195         //=========================================================================
196
197         void wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
198         {
199                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
200                 blackBox->addInputPort((GPortModel*)portController->getModel());
201         }
202
203         //=========================================================================
204
205         void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
206         {
207                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
208                 blackBox->addOutputPort((GPortModel*)portController->getModel());
209         }
210
211         //=========================================================================
212
213         GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBlackBoxModel *blackBox)
214         {
215                 int type = GPORT;
216
217                 //Create the MVC Objects
218                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
219                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
220                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
221
222                 model->registerInBox(blackBox,portType, posInBox);
223                 
224                 model->setBBTKType(bbtkType);
225                 model->setBBTKName(bbtkName);
226
227                 model->addObserver(view);
228                 model->addObserver(this);
229
230                 //Associates the view with the correspondent renderer and the  model.
231                 //(NOTE: Refresh is only made by the view)
232                 view->setModel(model);
233                 view->setBaseView(_baseView);
234                 view->initVtkObjects();
235                 
236                 //Associates the controller with the correspondent model and view
237                 controller->setModelAndView(model,view);
238
239                 model->notifyObservers(_idManager);
240
241                 //Register the controller of the new object
242                 registerController((InteractorStyleMaracas*) controller);
243                 
244                 int newId = _controllers.size();
245                 controller->setId(newId);
246                 _controllers[newId] = controller;
247
248                 return (GPortController*)controller;
249         }
250
251         //=========================================================================
252
253         void wxVtkSceneManager::createGConnector(GPortModel* startPort)
254         {
255                 int type = GCONNECTOR;
256
257                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
258                 manualConnectorContourView* manContourView      = new manualConnectorContourView();
259                 manualContourModel* manContourModel     = new manualContourModel();
260
261                 GConnectorController* connectorcontroller = new GConnectorController();                         
262                 GConnectorModel* connectorModel = new GConnectorModel();
263                 GConnectorView* connectorView = new GConnectorView();
264                 connectorModel->setGObjectType(type);
265
266                 manContourModel->SetCloseContour(false);
267                 connectorModel->setStartPort(startPort);
268
269                 manContourView->SetModel( manContourModel );
270                 manContourView->SetWxVtkBaseView( _baseView );
271                 manContourView->SetRange( 0.5 );
272                 manContourView->SetZ( 900 );
273
274                 manContourView->SetColorNormalContour(0, 0, 1);
275                 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
276                 manContourView->SetColorSelectContour(1, 0.8, 0);
277                 manContourView->SetWidthLine(1);
278
279                 manContourControl->SetModelView( manContourModel , manContourView );
280                 
281                 manContourControl->CreateNewManualContour();
282
283                 manContourView->RefreshContour();
284
285
286                 double x,y,z;
287                 connectorModel->getInicPoint(x,y,z);
288
289                 manContourControl->SetState(1);
290                 manContourModel->SetCloseContour(false);
291                 
292                 manContourModel->AddPoint(x,y,z);
293                 manContourView->AddPoint();
294
295                 manContourModel->AddPoint(x,y,z);
296                 manContourView->AddPoint();
297
298                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
299                 manContourControl->_bakIdPoint=bak;
300                 manContourView->Refresh();
301
302                 manContourControl->SetMoving( false );
303
304                 registerController((InteractorStyleMaracas*) connectorcontroller);
305
306                 connectorcontroller->setManualContourController(manContourControl);             
307                 connectorModel->setManualContourModel(manContourModel);
308                 connectorView->setManualContourView(manContourView);
309                 connectorView->setModel(connectorModel);
310                 connectorView->setBaseView(_baseView);
311                 connectorcontroller->setModelAndView(connectorModel,connectorView);
312
313                 int newId = _controllers.size();
314                 connectorcontroller->setId(newId);
315                 _controllers[newId] = connectorcontroller;
316
317         }
318
319         //=========================================================================
320
321         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
322         {
323                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
324                 baseViewControlManager->AddInteractorStyleMaracas( param );
325         }
326
327         //=========================================================================
328
329         void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
330         {
331                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
332                 baseViewControlManager->RemoveInteractorStyleMaracas( param );
333         }
334
335         //=========================================================================
336
337         vtkRenderer* wxVtkSceneManager::getRenderer()
338         {
339                 return _baseView->GetRenderer();
340         }
341
342         //=========================================================================
343
344         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
345         {
346                 return _baseView->GetRenWin();
347         }
348         //=========================================================================
349
350                 
351         void wxVtkSceneManager::update(int idController,int command)
352         {
353                 if(command != NO_COMMAND)
354                 {
355                         if(command == ADD_TO_SELECTED)
356                         {
357                                 GObjectController* cont = _controllers[idController];
358
359                                 bool foundID=false;
360                                 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
361                                 {
362                                         int id = _selectedObjects[i];
363                                         if(id==idController)
364                                         {
365                                                 foundID = true;
366                                         }
367                                 }
368                                 if(!foundID)
369                                 {
370                                         int id = idController;
371                                         _selectedObjects.push_back(id);
372                                 }
373
374                         }
375                         else if(command == INIT_CREATION_CONTOUR)
376                         {       
377                                 _worldState = INIT_CREATION_CONTOUR;
378                                 GObjectController* cont = _controllers[idController];
379                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
380                                 
381
382                                 // The last one is the controller of the connector
383                                 for(int i=0;i<_controllers.size();i++)
384                                 {
385                                         GObjectController* cont = _controllers[i];
386                                         if(cont->getGObjectType() == GPORT )
387                                         {
388                                                 GPortModel* port = (GPortModel*)cont->getModel();
389                                                 if(port->getPortType()==GINPUTPORT)
390                                                 {
391                                                         cont->SetActive(true);
392                                                 }
393                                                 else
394                                                 {
395                                                         cont->getView()->setState(NOTHING_HAPPENS);
396                                                         cont->getModel()->notifyObservers(_idManager);
397                                                         cont->SetActive(false);
398                                                 }
399                                         }
400                                         else
401                                         {
402                                                 cont->getView()->setState(NOTHING_HAPPENS);
403                                                 cont->getModel()->notifyObservers(_idManager);
404                                                 cont->SetActive(false);
405                                         }                               
406                                 }
407
408                                 _selectedObjects.clear();
409
410                                 createGConnector(startOutputPort);
411
412                         }
413                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
414                         {                               
415                                 _worldState = NOTHING_HAPPENS;
416                                 int id = _controllers.size()-1;
417                                 GObjectController* cont = _controllers[id];                     
418                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
419
420                                 GObjectController* finPort = _controllers[idController];
421                                 if(finPort->getGObjectType() == GPORT)
422                                 {
423                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
424                                         modelContour->setEndPort(modelPort);
425                                 }                       
426
427                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
428                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
429                                 connView->Refresh();
430
431                                 for(int i=0;i<_controllers.size();i++)
432                                 {
433                                         GObjectController* cont = _controllers[i];
434                                         if(cont->getView()!=NULL)
435                                         {
436                                                 cont->getView()->setState(NOTHING_HAPPENS);
437                                                 cont->getModel()->notifyObservers(_idManager);
438                                         }
439                                         cont->SetActive(true);                                                          
440                                 }
441                         }
442                         
443                 }
444         }
445
446         //=========================================================================
447
448         bool wxVtkSceneManager::OnMouseMove()
449         {
450                 int X,Y;
451                 wxVTKRenderWindowInteractor *wxVTKiren;
452                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
453                 wxVTKiren->GetEventPosition(X,Y);
454         
455                 
456                 if(_worldState == DRAG_OBJECTS)
457                 {                       
458                         for (int i=0; i<_selectedObjects.size(); i++)
459                         {
460                                 int id = _selectedObjects[i];
461                                 GObjectController* cont = _controllers[id];
462                                 if(_startDragging)
463                                 {
464                                         cont->getView()->setStartDragging(true);
465                                 }
466                                 cont->getView()->moveObject(X,Y);
467                                 cont->getView()->setState(DRAG);
468                                 cont->getModel()->notifyObservers(_idManager);
469                         }
470
471                         std::map<int, GObjectController*>::iterator it;
472
473                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
474                         {
475                                 GObjectController *desc = it->second;
476                                 if(desc->getGObjectType()==GCONNECTOR)
477                                 {
478                                         GConnectorView* vconn = (GConnectorView*)desc->getView();
479                                         vconn->updateStartEndPoints();
480                                 }
481                         }
482                         
483                         _startDragging=false;
484
485                 }
486                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
487                 {
488                         std::map<int, GObjectController*>::iterator it;
489
490                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
491                         {
492                                 GObjectController *desc = it->second;
493                                 int type = desc->getGObjectType();
494                                 int state = desc->getView()->getState();
495                                 
496                                 if(state == HIGHLIGHTED){
497
498                                         updateStatusBar(desc->getStatusText());
499                                         if(type==GBLACKBOX)
500                                         {
501                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
502                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
503                                         }
504                                 }
505                         }
506                 }
507                 
508                 
509
510
511                 return true;
512         }
513
514         //=========================================================================
515         
516         bool wxVtkSceneManager::OnLeftButtonDown()
517         {
518                 if(_worldState==INIT_CREATION_CONTOUR)
519                 {
520                         bool isOverPort=false;
521                         std::map<int, GObjectController*>::iterator it;
522                         for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
523                         {
524                                 GObjectController *desc = it->second;
525                                 if(desc->getGObjectType()==GPORT)
526                                 {
527                                         GPortModel* portmod=(GPortModel*)desc->getModel();
528                                         vtkGObjectView* portView=desc->getView();
529                                         if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
530                                         {
531                                                 isOverPort=true;
532                                         }
533                                 }
534                         }
535                         
536                         if(isOverPort==false)
537                         {
538                                 _worldState=NOTHING_HAPPENS;
539                                 int lastId = _controllers.size()-1;
540                                 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
541                                 connector->removeFromScene();
542                                 unregisterController(connector);
543                                 _controllers.erase(lastId);                     
544
545                                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
546                                 {
547                                         GObjectController *desc = it->second;
548                                         desc->SetActive(true);
549                                         desc->getView()->setState(NOTHING_HAPPENS);
550                                         desc->getModel()->notifyObservers(_idManager);
551                                 }
552                         }
553                 }
554                 
555                 if(_selectedObjects.size()!=0)
556                 {
557                         _worldState = DRAG_OBJECTS;
558                         _startDragging = true;
559
560                         for (int i = 0; i < _selectedObjects.size(); i++)
561                         {
562                                 int id = _selectedObjects[i];
563                                 GObjectController* cont = _controllers[id];
564                                 cont->getView()->setState(DRAG);
565                                 cont->getModel()->notifyObservers(_idManager);
566                         }
567                 }
568                 
569                 
570                 
571                 return true;
572         }
573
574         //=========================================================================
575         
576         bool wxVtkSceneManager::OnLeftButtonUp()
577         {
578                 if(_worldState == DRAG_OBJECTS)
579                 {
580                         _worldState = NOTHING_HAPPENS;
581
582                         for (int i = 0; i < _selectedObjects.size(); i++)
583                         {
584                                 int id = _selectedObjects[i];
585                                 GObjectController* cont = _controllers[id];
586                                 cont->getView()->setState(SELECTED);
587                                 cont->getModel()->notifyObservers(_idManager);
588                         }
589                 }
590                 return true;
591         }
592
593         //=========================================================================
594
595         bool wxVtkSceneManager::OnRightButtonUp()
596         {
597                 if(_worldState==INIT_CREATION_CONTOUR)
598                 {
599                         _worldState=NOTHING_HAPPENS;
600                         int lastId = _controllers.size()-1;
601                         GConnectorController *connector = (GConnectorController*)_controllers[lastId];
602                         connector->removeFromScene();
603                         unregisterController(connector);
604                         _controllers.erase(lastId);                     
605
606                         std::map<int, GObjectController*>::iterator it;
607                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
608                         {
609                                 GObjectController *desc = it->second;
610                                 desc->SetActive(true);
611                                 desc->getView()->setState(NOTHING_HAPPENS);
612                                 desc->getModel()->notifyObservers(_idManager);
613                         }
614                 }
615
616                         
617                 for (int i = 0; i < _selectedObjects.size(); i++)
618                 {
619                         int id = _selectedObjects[i];
620                         GObjectController* cont = _controllers[id];
621                         cont->SetActive(true);
622                         cont->getView()->setState(NOTHING_HAPPENS);
623                         cont->getModel()->notifyObservers(_idManager);
624                 }
625
626                 _selectedObjects.clear();
627
628                 return true;
629         }
630
631         //=========================================================================
632
633         bool wxVtkSceneManager::OnLeftDClick()
634         {
635                 int X,Y;
636                 wxVTKRenderWindowInteractor *wxVTKiren;
637                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
638                 wxVTKiren->GetEventPosition(X,Y);
639
640                 std::map<int, GObjectController*>::iterator it;
641
642                 bool clickOnObject = false;
643
644                 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
645                 {
646                         GObjectController *cont = it->second;
647                         int type = cont->getGObjectType();
648                         
649                         if(cont->getView()->isPointInside(X,Y))
650                         {
651                                 if(type==GBLACKBOX)
652                                 {
653                                         for (int i=0; i<_selectedObjects.size(); i++)
654                                         {
655                                                 int id = _selectedObjects[i];
656                                                 GObjectController* control = _controllers[id];
657                                                 control->getView()->setState(NOTHING_HAPPENS);
658                                         }
659                                         _selectedObjects.clear();
660
661                                         GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
662                                         _parent->editBlackBox(bbmodel);
663                                 }
664                                 clickOnObject = true;                   
665                         }
666                 }
667
668                 if(clickOnObject==false)
669                 {
670                         //_parent->editDiagramParameters(this);
671                 }
672
673                 return true;
674         }
675         
676         //=========================================================================
677
678         bool wxVtkSceneManager::OnChar()
679         {       
680                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
681                 
682                 // KeyCode 127 : Delete Key
683                 // KeyCode 8 : Backspace Key
684                 if(keyCode == 8 || keyCode == 127)
685                 {
686                         if(_selectedObjects.size()>0)
687                         {
688                                 for(int i=0;i<_selectedObjects.size();i++)
689                                 {
690                                         int id = _selectedObjects[i];
691                                         deleteObject(id);
692                                 }
693                                 _selectedObjects.clear();
694                         }
695                 }
696
697                 return true;
698         }
699
700         //=========================================================================
701
702         void wxVtkSceneManager::deleteObject(int id)
703         {
704                 GObjectController *control = _controllers[id];
705                 std::vector<int> controllersToRemove;
706
707                 if(control->getGObjectType()==GBLACKBOX)
708                 {
709                         GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
710                         std::vector<GPortModel*> inputs = bbmod->getInputPorts();
711                         
712                         bool boxConnected = false;
713
714                         // Add box input controllers to be removed
715                         for(int i = 0;i<inputs.size();i++)
716                         {
717                                 controllersToRemove.push_back(inputs[i]->getObjectId());
718                                 if(inputs[i]->isConnected())
719                                 {
720                                         boxConnected = true;
721                                 }
722                         }
723
724                         std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
725
726                         // Add box output controllers to be removed
727                         for(int i = 0;i<outputs.size();i++)
728                         {
729                                 controllersToRemove.push_back(outputs[i]->getObjectId());
730                                 if(outputs[i]->isConnected())
731                                 {
732                                         boxConnected = true;
733                                 }
734                         }
735
736                         // Add connection controllers to be removed
737                         std::map<int, GObjectController*>::iterator it;
738                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
739                         {
740                                 GObjectController *cont = it->second;
741                                 int type = cont->getGObjectType();
742                                 if(type==GCONNECTOR)
743                                 {
744                                         GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
745                                         if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
746                                         {
747                                                 controllersToRemove.push_back(conMod->getObjectId());
748                                         }
749                                         if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
750                                         {
751                                                 controllersToRemove.push_back(conMod->getObjectId());
752                                         }
753                                 }
754                         }
755
756                         // Add box controller to be removed
757                         controllersToRemove.push_back(bbmod->getObjectId());
758                 }
759
760                 for(int i = 0;i<controllersToRemove.size();i++)
761                 {
762                         int id = controllersToRemove[i];
763                         GObjectController *cont = _controllers[id];                     
764                         cont->removeFromScene();
765                         unregisterController((InteractorStyleMaracas*)cont);                    
766                         _controllers.erase(id);
767                 }
768
769
770         }
771
772         //=========================================================================
773
774         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
775         {
776                 _parent->displayBlackBoxInfo(packageName,boxName);
777         }
778
779         //=========================================================================
780
781         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
782         {
783                 _parent->updateStatusBar(textStatus);
784         }
785
786         //=========================================================================
787
788         std::string wxVtkSceneManager::getDiagramScript()
789         {
790                 bool existsExec=false;
791
792                 std::vector<std::string> packages;
793                 std::vector<int> boxes;
794                 std::vector<int> connections;
795                 std::vector<int> execBoxes;
796
797                 std::map<int, GObjectController*>::iterator it;
798
799                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
800                 {
801                         GObjectController *desc = it->second;
802                         int type = desc->getGObjectType();
803
804                         if(type==GBLACKBOX)
805                         {
806                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
807                                 
808                                 std::string pkg = mod->getBBTKPackage();
809                                 bool existsPkg = false;
810                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
811                                 {
812                                         if(packages[t]==pkg)
813                                         {
814                                                 existsPkg=true;
815                                         }
816                                 }
817                                 if(!existsPkg)
818                                 {
819                                         packages.push_back(pkg);
820                                 }
821
822
823                                 boxes.push_back(it->first);
824                                 if(mod->isExecutable())
825                                 {
826                                         execBoxes.push_back(it->first);
827                                         existsExec=true;
828                                 }
829                         }
830                         else if(type==GCONNECTOR)
831                         {
832                                 connections.push_back(it->first);
833                         }
834                 }
835
836                 std::string script = "";
837                 script+="# BBTK GEditor Script\n";
838                 script+="# ----------------------\n";
839                 if(existsExec)
840                 {
841                         int i;
842                         for(i = 0; i<packages.size();i++)
843                         {
844                                 script+="include ";
845                                 script+=packages[i];
846                                 script+="\n";
847                         }
848
849                         script+="include std\n";
850
851                         for(i = 0; i<boxes.size();i++)
852                         {
853                                 script+="new ";
854                                 int id = boxes[i];
855                                 GObjectController *control = _controllers[id];
856                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
857
858                                 script+=model->getBBTKType();
859                                 script+=" ";
860                                 script+=model->getBBTKName();
861                                 script+="\n";
862
863                                 std::vector<GPortModel*> inputs = model->getInputPorts();
864                                 for(int j = 0; j<inputs.size();j++)
865                                 {
866                                         GPortModel* inputPort = inputs[j];
867                                         if(inputPort->isValueSet())
868                                         {
869                                                 script+="set ";
870                                                 script+=model->getBBTKName();
871                                                 script+=".";
872                                                 script+=inputPort->getBBTKName();
873                                                 script+=" ";
874                                                 script+=inputPort->getValue();
875                                                 script+="\n";
876                                         }
877                                 }
878
879                         }
880
881                         for(i = 0; i<connections.size();i++)
882                         {
883                                 script+="connect ";
884                                 int id = connections[i];
885                                 GObjectController *control = _controllers[id];
886                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
887
888                                 //Start Connection info
889                                 GPortModel *start = model->getStartPort();
890                                 script+=start->getParentBox()->getBBTKName();
891                                 script+=".";
892                                 script+=start->getBBTKName();
893
894                                 script+=" ";
895
896                                 //End Connection info
897                                 GPortModel *end = model->getEndPort();
898                                 script+=end->getParentBox()->getBBTKName();
899                                 script+=".";
900                                 script+=end->getBBTKName();
901
902                                 script+="\n";
903                         }
904
905                         for(i = 0; i<execBoxes.size();i++)
906                         {
907                                 script+="exec ";
908                                 int id = execBoxes[i];
909                                 GObjectController *control = _controllers[id];
910                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
911
912                                 script+=model->getBBTKName();
913                                 script+="\n";
914                         }
915
916                 }
917
918                 return script;
919         }
920
921         //=========================================================================
922
923         void wxVtkSceneManager::deleteAllBoxes()
924         {
925                 std::map<int, GObjectController*>::iterator it;
926                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
927                 {
928                         GObjectController *cont = it->second;           
929                         cont->removeFromScene();
930                         unregisterController((InteractorStyleMaracas*)cont);
931                 }
932                 _selectedObjects.clear();
933                 _controllers.clear();   
934                 refreshScene();
935         }
936
937         //=========================================================================
938
939         void wxVtkSceneManager::refreshScene()
940         {
941                 _baseView->RefreshView();
942         }
943
944         //=========================================================================
945
946 }  // EO namespace bbtk
947
948 // EOF
949