]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
39f1237a99d7524167b9a6bff1e686a87ec91ffb
[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                 _isComplexBox=false;
52
53                 if( _baseView!=NULL )
54                 {
55                         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);                      
56                         configureBaseView();
57                         _worldState=NOTHING_HAPPENS;
58                         registerController(this);
59                         
60                 }
61
62         }
63
64         //=========================================================================
65
66         void wxVtkSceneManager::disconnectDrop()
67         {
68                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
69         }
70
71         //=========================================================================
72
73         wxVtkSceneManager::~wxVtkSceneManager()
74         {
75         }
76  
77         //=========================================================================
78
79         void wxVtkSceneManager::configureBaseView()
80         {
81                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
82
83                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
84
85                 // Important to activate the 2D interaction system
86                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
87                 interactorstylebaseview->SetInteractor ( iren );
88                 iren->SetInteractorStyle(interactorstylebaseview);
89                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
90                 
91                 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
92                 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
93                 
94                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
95                 _baseView->GetRenderer()->GradientBackgroundOff();
96                 _baseView->Refresh();
97         }
98
99         //=========================================================================
100
101         int wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
102         {
103
104                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
105                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
106
107                 int type = GBLACKBOX;
108
109                 //Create the MVC Objects
110                 
111                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
112                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
113                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
114                 
115                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
116                 
117                 //Prepares the initial model
118                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
119
120                 double xx = x;
121                 double yy = windowHeight-y;
122                 
123                 //z value is not important yet, because it is only used a parallel projection
124                 double zz = 900;
125
126                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
127                 model->setInicPoint(xx,yy,zz);
128                 
129                 _numBoxes++;
130                 std::stringstream stream;
131                 
132                 if(_numBoxes<10)
133                 {
134                         stream << "Box0" << _numBoxes;
135                 }
136                 else
137                 {
138                         stream << "Box" << _numBoxes;
139                 }
140                 std::string arraystring = stream.str();
141
142                 model->setBBTKName(arraystring);
143                 model->setBBTKType(boxType);
144                 model->setBBTKPackage(packageName);
145                                 
146                 model->addObserver(view);
147                 model->addObserver(this);
148
149                 //Iterate and create the input ports
150                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
151                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
152
153                 int i=0;
154                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
155                 {
156                         BlackBoxInputDescriptor *desc = itInput->second;
157                         createGInputPort(GINPUTPORT,i,model,desc);
158                         i++;
159                 }
160
161                 //Iterate and create the output ports
162                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
163                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
164
165                 i=0;
166                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
167                 {
168                         BlackBoxOutputDescriptor *desc = itOutput->second;
169                         createGOutputPort(GOUTPUTPORT,i,model,desc);
170                         i++;
171                 }
172
173
174                 //Associates the view with the correspondent renderer and the  model.
175                 //(NOTE: Refresh is only made by the view)
176                 view->setModel(model);
177                 view->setBaseView(_baseView);
178                 view->initVtkObjects();
179                 
180                 //Associates the controller with the correspondent model and view
181                 controller->setModelAndView(model,view);
182
183                 //Resgiter change to the observers of the actual model
184                 model->notifyObservers(_idManager);
185                 
186                 //Register the controller of the new object
187                 registerController((InteractorStyleMaracas*) controller);
188
189                 //Add the object to the objects list 
190                 int newId = _controllers.size();
191                 controller->setId(newId);
192                 _controllers[newId] = controller;
193                 return newId;
194
195         }
196
197         //=========================================================================
198
199         int wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
200         {
201                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
202                 blackBox->addInputPort((GPortModel*)portController->getModel());
203                 return portController->getId();
204         }
205
206         //=========================================================================
207
208         int wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
209         {
210                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
211                 blackBox->addOutputPort((GPortModel*)portController->getModel());
212                 return portController->getId();
213         }
214
215         //=========================================================================
216
217         GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBlackBoxModel *blackBox)
218         {
219                 int type = GPORT;
220
221                 //Create the MVC Objects
222                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
223                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
224                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
225
226                 model->registerInBox(blackBox,portType, posInBox);
227                 
228                 model->setBBTKType(bbtkType);
229                 model->setBBTKName(bbtkName);
230
231                 model->addObserver(view);
232                 model->addObserver(this);
233
234                 //Associates the view with the correspondent renderer and the  model.
235                 //(NOTE: Refresh is only made by the view)
236                 view->setModel(model);
237                 view->setBaseView(_baseView);
238                 view->initVtkObjects();
239                 
240                 //Associates the controller with the correspondent model and view
241                 controller->setModelAndView(model,view);
242
243                 model->notifyObservers(_idManager);
244
245                 //Register the controller of the new object
246                 registerController((InteractorStyleMaracas*) controller);
247                 
248                 int newId = _controllers.size();
249                 controller->setId(newId);
250                 _controllers[newId] = controller;
251
252                 return (GPortController*)controller;
253         }
254
255         //=========================================================================
256
257         int wxVtkSceneManager::createGConnector(GPortModel* startPort)
258         {
259                 int type = GCONNECTOR;
260
261                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
262                 manualConnectorContourView* manContourView      = new manualConnectorContourView();
263                 manualContourModel* manContourModel     = new manualContourModel();
264
265                 GConnectorController* connectorcontroller = new GConnectorController();                         
266                 GConnectorModel* connectorModel = new GConnectorModel();
267                 vtkGConnectorView* connectorView = new vtkGConnectorView();
268                 connectorModel->setGObjectType(type);
269
270                 manContourModel->SetCloseContour(false);
271                 connectorModel->setStartPort(startPort);
272
273                 manContourView->SetModel( manContourModel );
274                 manContourView->SetWxVtkBaseView( _baseView );
275                 manContourView->SetRange( 0.5 );
276                 manContourView->SetZ( 900 );
277
278                 manContourView->SetColorNormalContour(0, 0, 1);
279                 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
280                 manContourView->SetColorSelectContour(1, 0.8, 0);
281                 manContourView->SetWidthLine(1);
282                 manContourView->SetShowText(false);
283
284                 manContourControl->SetModelView( manContourModel , manContourView );
285                 
286                 manContourControl->CreateNewManualContour();
287
288                 manContourView->RefreshContour();
289
290
291                 double x,y,z;
292                 connectorModel->getInicPoint(x,y,z);
293
294                 manContourControl->SetState(1);
295                 manContourModel->SetCloseContour(false);
296                 
297                 manContourModel->AddPoint(x,y,z);
298                 manContourView->AddPoint();
299
300                 manContourModel->AddPoint(x,y,z);
301                 manContourView->AddPoint();
302
303                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
304                 manContourControl->_bakIdPoint=bak;
305                 manContourView->Refresh();
306
307                 manContourControl->SetMoving( false );
308
309                 registerController((InteractorStyleMaracas*) connectorcontroller);
310
311                 connectorcontroller->setManualContourController(manContourControl);             
312                 connectorModel->setManualContourModel(manContourModel);
313                 connectorView->setManualContourView(manContourView);
314                 connectorView->setModel(connectorModel);
315                 connectorView->setBaseView(_baseView);
316                 connectorcontroller->setModelAndView(connectorModel,connectorView);
317
318                 connectorModel->addObserver(connectorView);
319                 connectorModel->addObserver(this);
320
321                 int newId = _controllers.size();
322                 connectorcontroller->setId(newId);
323                 _controllers[newId] = connectorcontroller;
324
325                 return newId;
326         }
327
328         //=========================================================================
329
330         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
331         {
332                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
333                 baseViewControlManager->AddInteractorStyleMaracas( param );
334         }
335
336         //=========================================================================
337
338         void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
339         {
340                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
341                 baseViewControlManager->RemoveInteractorStyleMaracas( param );
342         }
343
344         //=========================================================================
345
346         vtkRenderer* wxVtkSceneManager::getRenderer()
347         {
348                 return _baseView->GetRenderer();
349         }
350
351         //=========================================================================
352
353         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
354         {
355                 return _baseView->GetRenWin();
356         }
357         //=========================================================================
358
359                 
360         void wxVtkSceneManager::update(int idController,int command)
361         {
362                 if(command != NO_COMMAND)
363                 {
364                         if(command == ADD_TO_SELECTED)
365                         {
366                                 GObjectController* cont = _controllers[idController];
367
368                                 bool foundID=false;
369                                 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
370                                 {
371                                         int id = _selectedObjects[i];
372                                         if(id==idController)
373                                         {
374                                                 foundID = true;
375                                         }
376                                 }
377                                 if(!foundID)
378                                 {
379                                         int id = idController;
380                                         _selectedObjects.push_back(id);
381                                 }
382
383                         }
384                         else if(command == INIT_CREATION_CONTOUR)
385                         {       
386                                 _worldState = INIT_CREATION_CONTOUR;
387                                 GObjectController* cont = _controllers[idController];
388                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
389                                 
390
391                                 // The last one is the controller of the connector
392                                 for(int i=0;i<_controllers.size();i++)
393                                 {
394                                         GObjectController* cont = _controllers[i];
395                                         if(cont->getGObjectType() == GPORT )
396                                         {
397                                                 GPortModel* port = (GPortModel*)cont->getModel();
398                                                 if(port->getPortType()==GINPUTPORT)
399                                                 {
400                                                         cont->SetActive(true);
401                                                 }
402                                                 else
403                                                 {
404                                                         cont->getView()->setState(NOTHING_HAPPENS);
405                                                         cont->getModel()->notifyObservers(_idManager);
406                                                         cont->SetActive(false);
407                                                 }
408                                         }
409                                         else
410                                         {
411                                                 cont->getView()->setState(NOTHING_HAPPENS);
412                                                 cont->getModel()->notifyObservers(_idManager);
413                                                 cont->SetActive(false);
414                                         }                               
415                                 }
416
417                                 _selectedObjects.clear();
418
419                                 createGConnector(startOutputPort);
420
421                         }
422                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
423                         {                               
424                                 _worldState = NOTHING_HAPPENS;
425                                 int id = _controllers.size()-1;
426                                 GObjectController* cont = _controllers[id];                     
427                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
428
429                                 GObjectController* finPort = _controllers[idController];
430                                 if(finPort->getGObjectType() == GPORT)
431                                 {
432                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
433                                         modelContour->setEndPort(modelPort);
434                                 }                       
435
436                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
437                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
438                                 connView->Refresh();
439
440                                 for(int i=0;i<_controllers.size();i++)
441                                 {
442                                         GObjectController* cont = _controllers[i];
443                                         if(cont->getView()!=NULL)
444                                         {
445                                                 cont->getView()->setState(NOTHING_HAPPENS);
446                                                 cont->getModel()->notifyObservers(_idManager);
447                                         }
448                                         cont->SetActive(true);                                                          
449                                 }
450                         }
451                         
452                 }
453         }
454
455         //=========================================================================
456
457         bool wxVtkSceneManager::OnMouseMove()
458         {
459                 int X,Y;
460                 wxVTKRenderWindowInteractor *wxVTKiren;
461                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
462                 wxVTKiren->GetEventPosition(X,Y);
463         
464                 
465                 if(_worldState == DRAG_OBJECTS)
466                 {                       
467                         for (int i=0; i<_selectedObjects.size(); i++)
468                         {
469                                 int id = _selectedObjects[i];
470                                 GObjectController* cont = _controllers[id];
471                                 if(_startDragging)
472                                 {
473                                         cont->getView()->setStartDragging(true);
474                                 }
475                                 cont->getView()->moveObject(X,Y);
476                                 cont->getView()->setState(DRAG);
477                                 cont->getModel()->notifyObservers(_idManager);
478                         }
479
480                         std::map<int, GObjectController*>::iterator it;
481
482                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
483                         {
484                                 GObjectController *desc = it->second;
485                                 if(desc->getGObjectType()==GCONNECTOR)
486                                 {
487                                         vtkGConnectorView* vconn = (vtkGConnectorView*)desc->getView();
488                                         vconn->updateStartEndPoints();
489                                 }
490                         }
491                         
492                         _startDragging=false;
493
494                 }
495                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
496                 {
497                         std::map<int, GObjectController*>::iterator it;
498
499                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
500                         {
501                                 GObjectController *desc = it->second;
502                                 int type = desc->getGObjectType();
503                                 int state = desc->getView()->getState();
504                                 
505                                 if(state == HIGHLIGHTED){
506
507                                         updateStatusBar(desc->getStatusText());
508                                         if(type==GBLACKBOX)
509                                         {
510                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
511                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
512                                         }
513                                 }
514                         }
515                 }
516                 
517                 
518
519
520                 return true;
521         }
522
523         //=========================================================================
524         
525         bool wxVtkSceneManager::OnLeftButtonDown()
526         {
527                 if(_worldState==INIT_CREATION_CONTOUR)
528                 {
529                         bool isOverPort=false;
530                         std::map<int, GObjectController*>::iterator it;
531                         for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
532                         {
533                                 GObjectController *desc = it->second;
534                                 if(desc->getGObjectType()==GPORT)
535                                 {
536                                         GPortModel* portmod=(GPortModel*)desc->getModel();
537                                         vtkGObjectView* portView=desc->getView();
538                                         if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
539                                         {
540                                                 isOverPort=true;
541                                         }
542                                 }
543                         }
544                         
545                         if(isOverPort==false)
546                         {
547                                 _worldState=NOTHING_HAPPENS;
548                                 int lastId = _controllers.size()-1;
549                                 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
550                                 connector->removeFromScene();
551                                 unregisterController(connector);
552                                 _controllers.erase(lastId);                     
553
554                                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
555                                 {
556                                         GObjectController *desc = it->second;
557                                         desc->SetActive(true);
558                                         desc->getView()->setState(NOTHING_HAPPENS);
559                                         desc->getModel()->notifyObservers(_idManager);
560                                 }
561                         }
562                 }
563                 
564                 if(_selectedObjects.size()!=0)
565                 {
566                         _worldState = DRAG_OBJECTS;
567                         _startDragging = true;
568
569                         for (int i = 0; i < _selectedObjects.size(); i++)
570                         {
571                                 int id = _selectedObjects[i];
572                                 GObjectController* cont = _controllers[id];
573                                 cont->getView()->setState(DRAG);
574                                 cont->getModel()->notifyObservers(_idManager);
575                         }
576                 }
577                 
578                 
579                 
580                 return true;
581         }
582
583         //=========================================================================
584         
585         bool wxVtkSceneManager::OnLeftButtonUp()
586         {
587                 if(_worldState == DRAG_OBJECTS)
588                 {
589                         _worldState = NOTHING_HAPPENS;
590
591                         for (int i = 0; i < _selectedObjects.size(); i++)
592                         {
593                                 int id = _selectedObjects[i];
594                                 GObjectController* cont = _controllers[id];
595                                 cont->getView()->setState(SELECTED);
596                                 cont->getModel()->notifyObservers(_idManager);
597                         }
598                 }
599                 return true;
600         }
601
602         //=========================================================================
603
604         bool wxVtkSceneManager::OnRightButtonUp()
605         {
606                 if(_worldState==INIT_CREATION_CONTOUR)
607                 {
608                         _worldState=NOTHING_HAPPENS;
609                         int lastId = _controllers.size()-1;
610                         GConnectorController *connector = (GConnectorController*)_controllers[lastId];
611                         connector->removeFromScene();
612                         unregisterController(connector);
613                         _controllers.erase(lastId);                     
614
615                         std::map<int, GObjectController*>::iterator it;
616                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
617                         {
618                                 GObjectController *desc = it->second;
619                                 desc->SetActive(true);
620                                 desc->getView()->setState(NOTHING_HAPPENS);
621                                 desc->getModel()->notifyObservers(_idManager);
622                         }
623                 }
624
625                         
626                 for (int i = 0; i < _selectedObjects.size(); i++)
627                 {
628                         int id = _selectedObjects[i];
629                         GObjectController* cont = _controllers[id];
630                         cont->SetActive(true);
631                         cont->getView()->setState(NOTHING_HAPPENS);
632                         cont->getModel()->notifyObservers(_idManager);
633                 }
634
635                 _selectedObjects.clear();
636
637                 return true;
638         }
639
640         //=========================================================================
641
642         bool wxVtkSceneManager::OnLeftDClick()
643         {
644                 int X,Y;
645                 wxVTKRenderWindowInteractor *wxVTKiren;
646                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
647                 wxVTKiren->GetEventPosition(X,Y);
648
649                 std::map<int, GObjectController*>::iterator it;
650
651                 bool clickOnObject = false;
652
653                 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
654                 {
655                         GObjectController *cont = it->second;
656                         int type = cont->getGObjectType();
657                         
658                         if(cont->getView()->isPointInside(X,Y))
659                         {
660                                 if(type==GBLACKBOX)
661                                 {
662                                         for (int i=0; i<_selectedObjects.size(); i++)
663                                         {
664                                                 int id = _selectedObjects[i];
665                                                 GObjectController* control = _controllers[id];
666                                                 control->getView()->setState(NOTHING_HAPPENS);
667                                         }
668                                         _selectedObjects.clear();
669
670                                         GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
671                                         _parent->editBlackBox(bbmodel);
672                                 }
673                                 clickOnObject = true;                   
674                         }
675                 }
676
677                 if(clickOnObject==false)
678                 {
679                         //_parent->editDiagramParameters(this);
680                 }
681
682                 return true;
683         }
684         
685         //=========================================================================
686
687         bool wxVtkSceneManager::OnChar()
688         {       
689                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
690                 
691                 // KeyCode 127 : Delete Key
692                 // KeyCode 8 : Backspace Key
693                 if(keyCode == 8 || keyCode == 127)
694                 {
695                         if(_selectedObjects.size()>0)
696                         {
697                                 for(int i=0;i<_selectedObjects.size();i++)
698                                 {
699                                         int id = _selectedObjects[i];
700                                         deleteObject(id);
701                                 }
702                                 _selectedObjects.clear();
703                         }
704                 }
705
706                 return true;
707         }
708
709         //=========================================================================
710
711         void wxVtkSceneManager::deleteObject(int id)
712         {
713                 GObjectController *control = _controllers[id];
714                 std::vector<int> controllersToRemove;
715
716                 if(control->getGObjectType()==GBLACKBOX)
717                 {
718                         GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
719                         std::vector<GPortModel*> inputs = bbmod->getInputPorts();
720                         
721                         bool boxConnected = false;
722
723                         // Add box input controllers to be removed
724                         for(int i = 0;i<inputs.size();i++)
725                         {
726                                 controllersToRemove.push_back(inputs[i]->getObjectId());
727                                 if(inputs[i]->isConnected())
728                                 {
729                                         boxConnected = true;
730                                 }
731                         }
732
733                         std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
734
735                         // Add box output controllers to be removed
736                         for(int i = 0;i<outputs.size();i++)
737                         {
738                                 controllersToRemove.push_back(outputs[i]->getObjectId());
739                                 if(outputs[i]->isConnected())
740                                 {
741                                         boxConnected = true;
742                                 }
743                         }
744
745                         // Add connection controllers to be removed
746                         std::map<int, GObjectController*>::iterator it;
747                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
748                         {
749                                 GObjectController *cont = it->second;
750                                 int type = cont->getGObjectType();
751                                 if(type==GCONNECTOR)
752                                 {
753                                         GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
754                                         if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
755                                         {
756                                                 controllersToRemove.push_back(conMod->getObjectId());
757                                         }
758                                         if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
759                                         {
760                                                 controllersToRemove.push_back(conMod->getObjectId());
761                                         }
762                                 }
763                         }
764
765                         // Add box controller to be removed
766                         controllersToRemove.push_back(bbmod->getObjectId());
767                 }
768                 else if(control->getGObjectType()==GCONNECTOR)
769                 {                       
770                         GConnectorModel *conMod = (GConnectorModel*)control->getModel();
771                         controllersToRemove.push_back(conMod->getObjectId());
772                 }
773
774                 for(int i = 0;i<controllersToRemove.size();i++)
775                 {
776                         int id = controllersToRemove[i];
777                         GObjectController *cont = _controllers[id];                     
778                         cont->removeFromScene();
779                         unregisterController((InteractorStyleMaracas*)cont);                    
780                         _controllers.erase(id);
781                 }
782
783
784         }
785
786         //=========================================================================
787
788         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
789         {
790                 _parent->displayBlackBoxInfo(packageName,boxName);
791         }
792
793         //=========================================================================
794
795         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
796         {
797                 _parent->updateStatusBar(textStatus);
798         }
799
800         //=========================================================================
801
802         std::string wxVtkSceneManager::getDiagramScript()
803         {
804                 bool existsExec=false;
805
806                 std::vector<std::string> packages;
807                 std::vector<int> boxes;
808                 std::vector<int> connections;
809                 std::vector<int> execBoxes;
810
811                 std::map<int, GObjectController*>::iterator it;
812
813                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
814                 {
815                         GObjectController *desc = it->second;
816                         int type = desc->getGObjectType();
817
818                         if(type==GBLACKBOX)
819                         {
820                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
821                                 
822                                 std::string pkg = mod->getBBTKPackage();
823                                 bool existsPkg = false;
824                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
825                                 {
826                                         if(packages[t]==pkg)
827                                         {
828                                                 existsPkg=true;
829                                         }
830                                 }
831                                 if(!existsPkg)
832                                 {
833                                         packages.push_back(pkg);
834                                 }
835
836
837                                 boxes.push_back(it->first);
838                                 if(mod->isExecutable())
839                                 {
840                                         execBoxes.push_back(it->first);
841                                         existsExec=true;
842                                 }
843                         }
844                         else if(type==GCONNECTOR)
845                         {
846                                 connections.push_back(it->first);
847                         }
848                 }
849
850                 std::string script = "";
851                 script+="# BBTK GEditor Script\n";
852                 script+="# ----------------------\n";
853                 if(existsExec)
854                 {
855                         int i;
856                         for(i = 0; i<packages.size();i++)
857                         {
858                                 script+="include ";
859                                 script+=packages[i];
860                                 script+="\n";
861                         }
862
863                         script+="include std\n";
864
865                         for(i = 0; i<boxes.size();i++)
866                         {
867                                 script+="new ";
868                                 int id = boxes[i];
869                                 GObjectController *control = _controllers[id];
870                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
871
872                                 script+=model->getBBTKType();
873                                 script+=" ";
874                                 script+=model->getBBTKName();
875                                 script+="\n";
876
877                                 std::vector<GPortModel*> inputs = model->getInputPorts();
878                                 for(int j = 0; j<inputs.size();j++)
879                                 {
880                                         GPortModel* inputPort = inputs[j];
881                                         if(inputPort->isValueSet())
882                                         {
883                                                 script+="set ";
884                                                 script+=model->getBBTKName();
885                                                 script+=".";
886                                                 script+=inputPort->getBBTKName();
887                                                 script+=" ";
888                                                 script+=inputPort->getValue();
889                                                 script+="\n";
890                                         }
891                                 }
892
893                         }
894
895                         for(i = 0; i<connections.size();i++)
896                         {
897                                 script+="connect ";
898                                 int id = connections[i];
899                                 GObjectController *control = _controllers[id];
900                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
901
902                                 //Start Connection info
903                                 GPortModel *start = model->getStartPort();
904                                 script+=start->getParentBox()->getBBTKName();
905                                 script+=".";
906                                 script+=start->getBBTKName();
907
908                                 script+=" ";
909
910                                 //End Connection info
911                                 GPortModel *end = model->getEndPort();
912                                 script+=end->getParentBox()->getBBTKName();
913                                 script+=".";
914                                 script+=end->getBBTKName();
915
916                                 script+="\n";
917                         }
918
919                         for(i = 0; i<execBoxes.size();i++)
920                         {
921                                 script+="exec ";
922                                 int id = execBoxes[i];
923                                 GObjectController *control = _controllers[id];
924                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
925
926                                 script+=model->getBBTKName();
927                                 script+="\n";
928                         }
929
930                 }
931
932                 return script;
933         }
934
935         //=========================================================================
936
937         void wxVtkSceneManager::deleteAllBoxes()
938         {               
939                 std::map<int, GObjectController*>::iterator it;
940                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
941                 {
942                         GObjectController *cont = it->second;           
943                         cont->removeFromScene();
944                         unregisterController((InteractorStyleMaracas*)cont);
945                 }
946                 _selectedObjects.clear();
947                 _controllers.clear();   
948                 refreshScene();
949         }
950
951         //=========================================================================
952
953         void wxVtkSceneManager::refreshScene()
954         {
955                 _baseView->RefreshView();
956         }
957
958         //=========================================================================
959         
960         void wxVtkSceneManager::centerView()
961         {
962                 double temp[3];
963                 _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
964                 _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0,0,temp[2]);
965                 _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
966                 _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0,0,temp[2]);
967                 _baseView->RefreshView();
968
969         }
970
971         //=========================================================================
972
973         void wxVtkSceneManager::saveDiagram(std::string &content)
974         {
975                 //Print boxes
976                 char buffer [50];
977                 std::vector<int> boxes = getBoxes();
978                 int bsize = boxes.size();
979                 content+="BOXES:";
980                 sprintf (buffer, "%d", bsize);
981                 content+=buffer;
982                 content+="\n";
983                 
984                 for(int i = 0;i<bsize;i++)
985                 {
986                         int id = boxes[i];
987                         GObjectController *cont = _controllers[id];
988                         cont->getModel()->save(content);
989                 }
990
991                 //Print connections
992                 std::vector<int> connections = getConnections();
993                 int csize = connections.size();
994                 content+="CONNECTIONS:";
995                 sprintf (buffer, "%d", csize);
996                 content+=buffer;
997                 content+="\n";
998                 
999                 for(int i = 0;i<csize;i++)
1000                 {
1001                         int id = connections[i];
1002                         GObjectController *cont = _controllers[id];
1003                         cont->getModel()->save(content);
1004                 }
1005
1006         }
1007
1008         //=========================================================================
1009
1010         void wxVtkSceneManager::loadDiagram(ifstream &inputStream)
1011         {
1012
1013                 std::string line="";    
1014                 char delims[] = ":";
1015                 char *result = NULL;
1016         getline(inputStream,line);
1017
1018                 bool start = false;
1019                 while ( !inputStream.eof() ) 
1020                 {
1021                         if(line=="" || line[0]=='#')
1022                         {
1023                                 getline(inputStream,line);
1024                         }
1025                         else if(line=="APP_START")
1026                         {
1027                                 start = true;
1028                                 break;
1029                         }
1030                 }
1031
1032                 if(start)
1033                 {
1034                         //----------
1035                         getline(inputStream,line);//BOXES:num
1036                         char boxes[9];
1037                         strcpy( boxes, line.c_str() );
1038                         result = strtok( boxes, delims );
1039                         result = strtok( NULL, delims );
1040                                         
1041                         int numBoxes;
1042                         std::istringstream is(result);
1043                         is >> numBoxes;
1044                         
1045                         for(int i = 0;i<numBoxes;i++)
1046                         {
1047                                 //----------
1048                                 getline(inputStream,line);//BOX
1049                                 getline(inputStream,line);//package:type:name
1050                                 char box[100];
1051                                 strcpy( box, line.c_str() );
1052                                 result = strtok( box, delims );//package
1053                                 std::string package(result);
1054                                 result = strtok( NULL, delims );//type
1055                                 std::string type(result);
1056                                 result = strtok( NULL, delims );//name
1057                                 std::string name(result);
1058
1059                                 getline(inputStream,line);//ISEXEC:TRUE|FALSE
1060                                 char exec[15];
1061                                 strcpy( exec, line.c_str() );
1062                                 result = strtok( exec, delims );//ISEXEC                                
1063                                 result = strtok( NULL, delims );//TRUE|FALSE
1064                                 std::string isExec(result);
1065
1066                                 //----------
1067                                 getline(inputStream,line);//xInic:yInic:zInic
1068                                 char coord[80];
1069                                 strcpy( coord, line.c_str() );
1070                                 result = strtok( coord, delims );//xInic
1071                                 std::string xInic(result);
1072                                 result = strtok( NULL, delims );//yInic
1073                                 std::string yInic(result);
1074                                 result = strtok( NULL, delims );//zInic
1075                                 std::string zInic(result);
1076
1077                                 double xIn, yIn, zIn;
1078                                 std::istringstream xSt(xInic);
1079                                 xSt >> xIn;
1080                                 std::istringstream ySt(yInic);
1081                                 ySt >> yIn;
1082                                 std::istringstream zSt(zInic);
1083                                 zSt >> zIn;
1084                                                 
1085                                 //----------
1086                                 getline(inputStream,line);//xEnd:yEnd:zEnd
1087                                 strcpy( coord, line.c_str() );
1088                                 result = strtok( coord, delims );//xEnd
1089                                 std::string xEnd(result);
1090                                 result = strtok( NULL, delims );//yEnd
1091                                 std::string yEnd(result);
1092                                 result = strtok( NULL, delims );//zEnd
1093                                 std::string zEnd(result);
1094
1095                                 double xEn, yEn, zEn;
1096                                 std::istringstream xEt(xEnd);
1097                                 xEt >> xEn;
1098                                 std::istringstream yEt(yEnd);
1099                                 yEt >> yEn;
1100                                 std::istringstream zEt(zEnd);
1101                                 zEt >> zEn;
1102
1103                                 int idBox = createGBlackBox(0,0,package,type);
1104                                 GObjectController *cont = _controllers[idBox];
1105                                 GBlackBoxModel *bbmod = (GBlackBoxModel*)cont->getModel();
1106                                 bbmod->setBBTKName(name);
1107                                 bbmod->setInicPoint(xIn,yIn,zIn);
1108                                 bbmod->setFinalPoint(xEn,yEn,zEn);
1109                                 if(isExec=="TRUE")
1110                                 {
1111                                         bbmod->setExecutable(true);
1112                                 }
1113                                 else if(isExec=="FALSE")
1114                                 {
1115                                         bbmod->setExecutable(false);
1116                                 }
1117                                                         
1118                                 //----------
1119                                 getline(inputStream,line);//PORT o FIN_BOX
1120                                 std::string port=line.substr(0,4);
1121                                 while(port=="PORT")
1122                                 {
1123                                         getline(inputStream,line);//name:value
1124                                         char poort[20];
1125                                         strcpy( poort, line.c_str() );
1126                                         result = strtok( poort, delims );//name
1127                                         std::string name(result);
1128                                         result = strtok( NULL, delims );//value
1129                                         std::string value(result);
1130
1131                                         bbmod->setValueToInput(name,value);                                     
1132
1133                                         getline(inputStream,line);//PORT o FIN_BOX
1134                                         port=line.substr(0,4);
1135                                 }               
1136
1137                                 bbmod->notifyObservers(_idManager);
1138                         }
1139
1140                         /// CONNECTIONS
1141                         //----------
1142                         getline(inputStream,line);//CONNECTIONS:num
1143                         char conns[15];
1144                         strcpy( conns, line.c_str() );
1145                         result = strtok( conns, delims );
1146                         result = strtok( NULL, delims );
1147                                         
1148                         int numConns;
1149                         std::istringstream isCons(result);
1150                         isCons >> numConns;
1151                                         
1152                         for(int i = 0;i<numConns;i++)
1153                         {
1154                                 //----------
1155                                 getline(inputStream,line);//CONNECTION
1156                                 getline(inputStream,line);//Startbox.PortName:EndBox.PortName
1157
1158                                 char connec[100];
1159                                 strcpy( connec, line.c_str() );
1160                                 result = strtok( connec, delims );
1161                                 std::string nameStartBox(result);
1162                                 result = strtok( NULL, delims );
1163                                 std::string nameStartPort(result);
1164                                 result = strtok( NULL, delims );
1165                                 std::string nameEndBox(result);
1166                                 result = strtok( NULL, delims );
1167                                 std::string nameEndPort(result);
1168
1169                                 std::vector<int> lstB = getBoxes();
1170                                 
1171                                 GPortModel *startP=NULL;
1172                                 GPortModel *endP=NULL;                          
1173                                 for(int j = 0;j<lstB.size();j++)
1174                                 {
1175                                         int idB = lstB[j];
1176                                         GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1177                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1178                                         {                                               
1179                                                 startP = bbMod->getOutputPort(nameStartPort);
1180                                         }
1181                                         else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1182                                         {
1183                                                 endP = bbMod->getInputPort(nameEndPort);
1184                                         }
1185                                 }
1186                                 
1187                                 int idCon = createGConnector(startP);
1188                                 _worldState = NOTHING_HAPPENS;
1189                                 GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
1190
1191                                 GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1192                                 vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1193                                 tempp->endContourCreation();
1194                                 conMod->setEndPort(endP);                                       
1195                                 conView->updateStartEndPoints();
1196
1197                         }
1198         }
1199
1200         }
1201
1202         //=========================================================================
1203
1204         std::vector<int> wxVtkSceneManager::getBoxes()
1205         {
1206                 std::vector<int> vect;
1207                 std::map<int, GObjectController*>::iterator it;
1208                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1209                 {
1210                         GObjectController *cont = it->second;           
1211                         if(cont->getGObjectType()==GBLACKBOX)
1212                         {
1213                                 vect.push_back(cont->getId());
1214                         }
1215                 }
1216                 return vect;
1217         }
1218
1219         //=========================================================================
1220
1221         std::vector<int> wxVtkSceneManager::getConnections()
1222         {
1223                 std::vector<int> vect;
1224                 std::map<int, GObjectController*>::iterator it;
1225                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1226                 {
1227                         GObjectController *cont = it->second;           
1228                         if(cont->getGObjectType()==GCONNECTOR)
1229                         {
1230                                 vect.push_back(cont->getId());
1231                         }
1232                 }
1233                 return vect;
1234         }
1235
1236         //=========================================================================
1237         
1238         bool wxVtkSceneManager::isComplexBox()
1239         {
1240                 return _isComplexBox;
1241         }
1242
1243         //=========================================================================
1244
1245         void wxVtkSceneManager::setComplexBox(bool val)
1246         {
1247                 _isComplexBox=val;
1248         }
1249
1250         //=========================================================================
1251
1252 }  // EO namespace bbtk
1253
1254 // EOF
1255