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