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