]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
cab3936f7976ff5f98d0e03977c28e1a31aa6af1
[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                 connectorcontroller->setModelAndView(connectorModel,connectorView);
311
312                 int newId = _controllers.size();
313                 connectorcontroller->setId(newId);
314                 _controllers[newId] = connectorcontroller;
315
316         }
317
318         //=========================================================================
319
320         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
321         {
322                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
323                 baseViewControlManager->AddInteractorStyleMaracas( param );
324         }
325
326         //=========================================================================
327
328         void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
329         {
330                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
331                 baseViewControlManager->RemoveInteractorStyleMaracas( param );
332         }
333
334         //=========================================================================
335
336         vtkRenderer* wxVtkSceneManager::getRenderer()
337         {
338                 return _baseView->GetRenderer();
339         }
340
341         //=========================================================================
342
343         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
344         {
345                 return _baseView->GetRenWin();
346         }
347         //=========================================================================
348
349                 
350         void wxVtkSceneManager::update(int idController,int command)
351         {
352                 if(command != NO_COMMAND)
353                 {
354                         if(command == ADD_TO_SELECTED)
355                         {
356                                 GObjectController* cont = _controllers[idController];
357
358                                 bool foundID=false;
359                                 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
360                                 {
361                                         int id = _selectedObjects[i];
362                                         if(id==idController)
363                                         {
364                                                 foundID = true;
365                                         }
366                                 }
367                                 if(!foundID)
368                                 {
369                                         int id = idController;
370                                         _selectedObjects.push_back(id);
371                                 }
372
373                         }
374                         else if(command == INIT_CREATION_CONTOUR)
375                         {       
376                                 _worldState = INIT_CREATION_CONTOUR;
377                                 GObjectController* cont = _controllers[idController];
378                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
379                                 
380
381                                 // The last one is the controller of the connector
382                                 for(int i=0;i<_controllers.size();i++)
383                                 {
384                                         GObjectController* cont = _controllers[i];
385                                         if(cont->getGObjectType() == GPORT )
386                                         {
387                                                 GPortModel* port = (GPortModel*)cont->getModel();
388                                                 if(port->getPortType()==GINPUTPORT)
389                                                 {
390                                                         cont->SetActive(true);
391                                                 }
392                                                 else
393                                                 {
394                                                         cont->getView()->setState(NOTHING_HAPPENS);
395                                                         cont->getModel()->notifyObservers(_idManager);
396                                                         cont->SetActive(false);
397                                                 }
398                                         }
399                                         else
400                                         {
401                                                 cont->getView()->setState(NOTHING_HAPPENS);
402                                                 cont->getModel()->notifyObservers(_idManager);
403                                                 cont->SetActive(false);
404                                         }                               
405                                 }
406
407                                 _selectedObjects.clear();
408
409                                 createGConnector(startOutputPort);
410
411                         }
412                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
413                         {                               
414                                 _worldState = NOTHING_HAPPENS;
415                                 int id = _controllers.size()-1;
416                                 GObjectController* cont = _controllers[id];                     
417                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
418
419                                 GObjectController* finPort = _controllers[idController];
420                                 if(finPort->getGObjectType() == GPORT)
421                                 {
422                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
423                                         modelContour->setEndPort(modelPort);
424                                 }                       
425
426                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
427                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
428                                 connView->Refresh();
429
430                                 for(int i=0;i<_controllers.size();i++)
431                                 {
432                                         GObjectController* cont = _controllers[i];
433                                         if(cont->getView()!=NULL)
434                                         {
435                                                 cont->getView()->setState(NOTHING_HAPPENS);
436                                                 cont->getModel()->notifyObservers(_idManager);
437                                         }
438                                         cont->SetActive(true);                                                          
439                                 }
440                         }
441                         
442                 }
443         }
444
445         //=========================================================================
446
447         bool wxVtkSceneManager::OnMouseMove()
448         {
449                 int X,Y;
450                 wxVTKRenderWindowInteractor *wxVTKiren;
451                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
452                 wxVTKiren->GetEventPosition(X,Y);
453         
454                 
455                 if(_worldState == DRAG_OBJECTS)
456                 {                       
457                         for (int i=0; i<_selectedObjects.size(); i++)
458                         {
459                                 int id = _selectedObjects[i];
460                                 GObjectController* cont = _controllers[id];
461                                 if(_startDragging)
462                                 {
463                                         cont->getView()->setStartDragging(true);
464                                 }
465                                 cont->getView()->moveObject(X,Y);
466                                 cont->getView()->setState(DRAG);
467                                 cont->getModel()->notifyObservers(_idManager);
468                         }
469
470                         std::map<int, GObjectController*>::iterator it;
471
472                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
473                         {
474                                 GObjectController *desc = it->second;
475                                 if(desc->getGObjectType()==GCONNECTOR)
476                                 {
477                                         GConnectorView* vconn = (GConnectorView*)desc->getView();
478                                         vconn->updateStartEndPoints();
479                                 }
480                         }
481                         
482                         _startDragging=false;
483
484                 }
485                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
486                 {
487                         std::map<int, GObjectController*>::iterator it;
488
489                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
490                         {
491                                 GObjectController *desc = it->second;
492                                 int type = desc->getGObjectType();
493                                 int state = desc->getView()->getState();
494                                 
495                                 if(state == HIGHLIGHTED){
496
497                                         updateStatusBar(desc->getStatusText());
498                                         if(type==GBLACKBOX)
499                                         {
500                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
501                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
502                                         }
503                                 }
504                         }
505                 }
506                 
507                 
508
509
510                 return true;
511         }
512
513         //=========================================================================
514         
515         bool wxVtkSceneManager::OnLeftButtonDown()
516         {
517                 if(_worldState==INIT_CREATION_CONTOUR)
518                 {
519                         bool isOverPort=false;
520                         std::map<int, GObjectController*>::iterator it;
521                         for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
522                         {
523                                 GObjectController *desc = it->second;
524                                 if(desc->getGObjectType()==GPORT)
525                                 {
526                                         GPortModel* portmod=(GPortModel*)desc->getModel();
527                                         vtkGObjectView* portView=desc->getView();
528                                         if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
529                                         {
530                                                 isOverPort=true;
531                                         }
532                                 }
533                         }
534                         
535                         if(isOverPort==false)
536                         {
537                                 _worldState=NOTHING_HAPPENS;
538                                 int lastId = _controllers.size()-1;
539                                 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
540                                 connector->removeFromScene();
541                                 unregisterController(connector);
542                                 _controllers.erase(lastId);                     
543
544                                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
545                                 {
546                                         GObjectController *desc = it->second;
547                                         desc->SetActive(true);
548                                         desc->getView()->setState(NOTHING_HAPPENS);
549                                         desc->getModel()->notifyObservers(_idManager);
550                                 }
551                         }
552                 }
553                 
554                 if(_selectedObjects.size()!=0)
555                 {
556                         _worldState = DRAG_OBJECTS;
557                         _startDragging = true;
558
559                         for (int i = 0; i < _selectedObjects.size(); i++)
560                         {
561                                 int id = _selectedObjects[i];
562                                 GObjectController* cont = _controllers[id];
563                                 cont->getView()->setState(DRAG);
564                                 cont->getModel()->notifyObservers(_idManager);
565                         }
566                 }
567                 
568                 
569                 
570                 return true;
571         }
572
573         //=========================================================================
574         
575         bool wxVtkSceneManager::OnLeftButtonUp()
576         {
577                 if(_worldState == DRAG_OBJECTS)
578                 {
579                         _worldState = NOTHING_HAPPENS;
580
581                         for (int i = 0; i < _selectedObjects.size(); i++)
582                         {
583                                 int id = _selectedObjects[i];
584                                 GObjectController* cont = _controllers[id];
585                                 cont->getView()->setState(SELECTED);
586                                 cont->getModel()->notifyObservers(_idManager);
587                         }
588                 }
589                 return true;
590         }
591
592         //=========================================================================
593
594         bool wxVtkSceneManager::OnRightButtonUp()
595         {
596                 if(_worldState==INIT_CREATION_CONTOUR)
597                 {
598                         _worldState=NOTHING_HAPPENS;
599                         int lastId = _controllers.size()-1;
600                         GConnectorController *connector = (GConnectorController*)_controllers[lastId];
601                         connector->removeFromScene();
602                         unregisterController(connector);
603                         _controllers.erase(lastId);                     
604
605                         std::map<int, GObjectController*>::iterator it;
606                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
607                         {
608                                 GObjectController *desc = it->second;
609                                 desc->SetActive(true);
610                                 desc->getView()->setState(NOTHING_HAPPENS);
611                                 desc->getModel()->notifyObservers(_idManager);
612                         }
613                 }
614
615                         
616                 for (int i = 0; i < _selectedObjects.size(); i++)
617                 {
618                         int id = _selectedObjects[i];
619                         GObjectController* cont = _controllers[id];
620                         cont->SetActive(true);
621                         cont->getView()->setState(NOTHING_HAPPENS);
622                         cont->getModel()->notifyObservers(_idManager);
623                 }
624
625                 _selectedObjects.clear();
626
627                 return true;
628         }
629
630         //=========================================================================
631
632         bool wxVtkSceneManager::OnLeftDClick()
633         {
634                 int X,Y;
635                 wxVTKRenderWindowInteractor *wxVTKiren;
636                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
637                 wxVTKiren->GetEventPosition(X,Y);
638
639                 std::map<int, GObjectController*>::iterator it;
640
641                 bool clickOnObject = false;
642
643                 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
644                 {
645                         GObjectController *cont = it->second;
646                         int type = cont->getGObjectType();
647                         
648                         if(type==GBLACKBOX)
649                         {
650                                 if(cont->getView()->isPointInside(X,Y))
651                                 {
652                                         for (int i=0; i<_selectedObjects.size(); i++)
653                                         {
654                                                 int id = _selectedObjects[i];
655                                                 GObjectController* control = _controllers[id];
656                                                 control->getView()->setState(NOTHING_HAPPENS);
657                                         }
658                                         _selectedObjects.clear();
659
660                                         GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
661                                         _parent->editBlackBox(bbmodel);
662                                 }
663                                 clickOnObject = true;                   
664                         }
665                 }
666
667                 if(clickOnObject==false)
668                 {
669                         _parent->editDiagramParameters(this);
670                 }
671
672                 return true;
673         }
674         
675         //=========================================================================
676
677         bool wxVtkSceneManager::OnChar()
678         {       
679                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
680                 
681                 // KeyCode 127 : Delete Key
682                 // KeyCode 8 : Backspace Key
683                 if(keyCode == 8 || keyCode == 127)
684                 {
685                         if(_selectedObjects.size()>0)
686                         {
687                                 for(int i=0;i<_selectedObjects.size();i++)
688                                 {
689                                         int id = _selectedObjects[i];
690                                         removeObject(id);
691                                 }
692                                 _selectedObjects.clear();
693                         }
694                 }
695
696                 return true;
697         }
698
699         //=========================================================================
700
701         void wxVtkSceneManager::removeObject(int id)
702         {
703                 GObjectController *control = _controllers[id];
704                 std::vector<int> controllersToRemove;
705
706                 if(control->getGObjectType()==GBLACKBOX)
707                 {
708                         GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
709                         std::vector<GPortModel*> inputs = bbmod->getInputPorts();
710                         
711                         bool boxConnected = false;
712
713                         // Add box input controllers to be removed
714                         for(int i = 0;i<inputs.size();i++)
715                         {
716                                 controllersToRemove.push_back(inputs[i]->getObjectId());
717                                 if(inputs[i]->isConnected())
718                                 {
719                                         boxConnected = true;
720                                 }
721                         }
722
723                         std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
724
725                         // Add box output controllers to be removed
726                         for(int i = 0;i<outputs.size();i++)
727                         {
728                                 controllersToRemove.push_back(outputs[i]->getObjectId());
729                                 if(outputs[i]->isConnected())
730                                 {
731                                         boxConnected = true;
732                                 }
733                         }
734
735                         // Add connection controllers to be removed
736                         std::map<int, GObjectController*>::iterator it;
737                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
738                         {
739                                 GObjectController *cont = it->second;
740                                 int type = cont->getGObjectType();
741                                 if(type==GCONNECTOR)
742                                 {
743                                         GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
744                                         if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
745                                         {
746                                                 controllersToRemove.push_back(conMod->getObjectId());
747                                         }
748                                         if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
749                                         {
750                                                 controllersToRemove.push_back(conMod->getObjectId());
751                                         }
752                                 }
753                         }
754
755                         // Add box controller to be removed
756                         controllersToRemove.push_back(bbmod->getObjectId());
757                 }
758
759                 for(int i = 0;i<controllersToRemove.size();i++)
760                 {
761                         int id = controllersToRemove[i];
762                         GObjectController *cont = _controllers[id];                     
763                         cont->removeFromScene();
764                         unregisterController((InteractorStyleMaracas*)cont);                    
765                         _controllers.erase(id);
766                 }
767
768
769         }
770
771         //=========================================================================
772
773         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
774         {
775                 _parent->displayBlackBoxInfo(packageName,boxName);
776         }
777
778         //=========================================================================
779
780         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
781         {
782                 _parent->updateStatusBar(textStatus);
783         }
784
785         //=========================================================================
786
787         std::string wxVtkSceneManager::getDiagramScript()
788         {
789                 bool existsExec=false;
790
791                 std::vector<std::string> packages;
792                 std::vector<int> boxes;
793                 std::vector<int> connections;
794                 std::vector<int> execBoxes;
795
796                 std::map<int, GObjectController*>::iterator it;
797
798                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
799                 {
800                         GObjectController *desc = it->second;
801                         int type = desc->getGObjectType();
802
803                         if(type==GBLACKBOX)
804                         {
805                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
806                                 
807                                 std::string pkg = mod->getBBTKPackage();
808                                 bool existsPkg = false;
809                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
810                                 {
811                                         if(packages[t]==pkg)
812                                         {
813                                                 existsPkg=true;
814                                         }
815                                 }
816                                 if(!existsPkg)
817                                 {
818                                         packages.push_back(pkg);
819                                 }
820
821
822                                 boxes.push_back(it->first);
823                                 if(mod->isExecutable())
824                                 {
825                                         execBoxes.push_back(it->first);
826                                         existsExec=true;
827                                 }
828                         }
829                         else if(type==GCONNECTOR)
830                         {
831                                 connections.push_back(it->first);
832                         }
833                 }
834
835                 std::string script = "";
836                 script+="# BBTK GEditor Script\n";
837                 script+="# ----------------------\n";
838                 if(existsExec)
839                 {
840                         int i;
841                         for(i = 0; i<packages.size();i++)
842                         {
843                                 script+="include ";
844                                 script+=packages[i];
845                                 script+="\n";
846                         }
847
848                         script+="include std\n";
849
850                         for(i = 0; i<boxes.size();i++)
851                         {
852                                 script+="new ";
853                                 int id = boxes[i];
854                                 GObjectController *control = _controllers[id];
855                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
856
857                                 script+=model->getBBTKType();
858                                 script+=" ";
859                                 script+=model->getBBTKName();
860                                 script+="\n";
861
862                                 std::vector<GPortModel*> inputs = model->getInputPorts();
863                                 for(int j = 0; j<inputs.size();j++)
864                                 {
865                                         GPortModel* inputPort = inputs[j];
866                                         if(inputPort->isValueSet())
867                                         {
868                                                 script+="set ";
869                                                 script+=model->getBBTKName();
870                                                 script+=".";
871                                                 script+=inputPort->getBBTKName();
872                                                 script+=" ";
873                                                 script+=inputPort->getValue();
874                                                 script+="\n";
875                                         }
876                                 }
877
878                         }
879
880                         for(i = 0; i<connections.size();i++)
881                         {
882                                 script+="connect ";
883                                 int id = connections[i];
884                                 GObjectController *control = _controllers[id];
885                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
886
887                                 //Start Connection info
888                                 GPortModel *start = model->getStartPort();
889                                 script+=start->getParentBox()->getBBTKName();
890                                 script+=".";
891                                 script+=start->getBBTKName();
892
893                                 script+=" ";
894
895                                 //End Connection info
896                                 GPortModel *end = model->getEndPort();
897                                 script+=end->getParentBox()->getBBTKName();
898                                 script+=".";
899                                 script+=end->getBBTKName();
900
901                                 script+="\n";
902                         }
903
904                         for(i = 0; i<execBoxes.size();i++)
905                         {
906                                 script+="exec ";
907                                 int id = execBoxes[i];
908                                 GObjectController *control = _controllers[id];
909                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
910
911                                 script+=model->getBBTKName();
912                                 script+="\n";
913                         }
914
915                 }
916
917                 return script;
918         }
919
920         //=========================================================================
921
922 }  // EO namespace bbtk
923
924 // EOF
925