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