]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxGEditorTabPanel.cxx
Fix to file drop crashing:
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsWxGUIEditorGraphic / wxGEditorTabPanel.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 #
8 #  This software is governed by the CeCILL-B license under French law and 
9 #  abiding by the rules of distribution of free software. You can  use, 
10 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
11 #  license as circulated by CEA, CNRS and INRIA at the following URL 
12 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
13 #  or in the file LICENSE.txt.
14 #
15 #  As a counterpart to the access to the source code and  rights to copy,
16 #  modify and redistribute granted by the license, users are provided only
17 #  with a limited warranty  and the software's author,  the holder of the
18 #  economic rights,  and the successive licensors  have only  limited
19 #  liability. 
20 #
21 #  The fact that you are presently reading this means that you have had
22 #  knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------  
24 */
25
26 /*=========================================================================
27 Program:   bbtk
28 Module:    $RCSfile$
29 Language:  C++
30 Date:      $Date$
31 Version:   $Revision$
32 =========================================================================*/
33
34 /* ---------------------------------------------------------------------
35
36 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
37 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
38 *
39 *  This software is governed by the CeCILL-B license under French law and
40 *  abiding by the rules of distribution of free software. You can  use,
41 *  modify and/ or redistribute the software under the terms of the CeCILL-B
42 *  license as circulated by CEA, CNRS and INRIA at the following URL
43 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
44 *  or in the file LICENSE.txt.
45 *
46 *  As a counterpart to the access to the source code and  rights to copy,
47 *  modify and redistribute granted by the license, users are provided only
48 *  with a limited warranty  and the software's author,  the holder of the
49 *  economic rights,  and the successive licensors  have only  limited
50 *  liability.
51 *
52 *  The fact that you are presently reading this means that you have had
53 *  knowledge of the CeCILL-B license and that you accept its terms.
54 * ------------------------------------------------------------------------ */
55
56 /**
57 *  \file
58 *  \brief Class bbtk::wxGEditorTabPanel .
59 */
60
61
62 #include "wxGEditorTabPanel.h"
63 #include "creaWx.h"
64
65 namespace bbtk
66 {
67
68         //=========================================================================
69         wxGEditorTabPanel::wxGEditorTabPanel()
70         {
71                 printf ("EED %p wxGEditorTabPanel 1 ()\n" , this );
72         }
73
74         //=========================================================================
75         wxGEditorTabPanel::wxGEditorTabPanel(wxWindow *parent,int id, Factory::Pointer bbtkfactory)
76          : wxPanel(parent),wxTextDropTarget()
77         {
78                 _id=id;
79                 _panelAUIMgr    = new wxAuiManager(this);
80                 _sceneManager   = NULL;
81                 _actualdo               = states.begin();
82                 _sactualdo              = sstates.begin();
83                 _bbtkfactory    = bbtkfactory;
84                 initWxVtkCanvas();
85         }
86
87         //=========================================================================
88         wxGEditorTabPanel::~wxGEditorTabPanel()
89         {
90 //ED02JUIN2010          _sceneManager->disconnectDrop();
91
92         _panelsManager->VerifyLastTabPanel();
93                 //FCY memory leaks
94                 delete _panelAUIMgr;
95                 delete _sceneManager;
96         }
97
98         //=========================================================================
99         void wxGEditorTabPanel::initWxVtkCanvas()
100         {
101                 wxVtk3DBaseView *baseview = new wxVtk3DBaseView(this);
102                 baseview->Configure();
103 //EED 07 juin 2013
104                 baseview->GetCamera()->SetViewUp (0, 1, 0);
105                 baseview->GetCamera()->SetPosition (0, 0, 1);
106                 baseview->GetCamera()->SetFocalPoint (0, 0, 0);
107
108 //EED 15 oct 2012               _sceneManager=new wxVtkSceneManager(this,baseview,_id);
109                 _sceneManager=new wxVtkSceneManager(this,baseview,_id, _bbtkfactory);
110
111
112 //EED02JUIN2010
113                 printf("RaC-EED 21-06-2010 wxGEditorTabPanel::initWxVtkCanvas Remove the panelAUIMgr, and change to a normal sizer inside the wxGEditorTabPanel\n");
114                 _panelAUIMgr->AddPane((wxWindow*)baseview->GetWxVTKRenderWindowInteractor(),wxAuiPaneInfo().Fixed().CenterPane());
115
116                 _panelAUIMgr->Update();
117         }
118
119         //=========================================================================
120
121         bool wxGEditorTabPanel::OnDropText(wxCoord x, wxCoord y, const wxString& data)
122         {
123
124           /*
125            * DFGO
126            * To handle bbs and bbg:
127            * handle data parameter to detect if incoming string is file or bbs or
128            * bbg or blackbox.
129            *
130            * For a drag and drop tutorial read:
131            * http://zetcode.com/gui/wxwidgets/dragdrop/
132            */
133           std::string stdData = crea::wx2std(data);
134           //std::cout << "Text drop: " << stdData << std::endl;
135
136           if(stdData.substr(0,4) == "box:")
137             {
138               int posT = stdData.find_first_of(':', 4);
139               std::string packageName = stdData.substr(4, posT-4);
140               posT = stdData.find_first_not_of(':', posT);
141               std::string boxType = stdData.substr(posT);
142
143               std::cout << "box: Package='" << packageName << "' BoxType='" << boxType << "'" <<std::endl;
144               /*
145                     wxString foo( (data) );
146                     char str[150];
147                     strcpy( str, (const char*)foo.mb_str(wxConvUTF8) );
148
149                     char delims[] = ":";
150                      char *result = NULL;
151                      result = strtok( str, delims );
152                      packageName += result;
153
154                      result = strtok( NULL, delims );
155                      boxType += result;
156                */
157               if ( _panelsManager->TryToOpenScriptApplication(packageName,boxType) == false )
158                 {
159                   _sceneManager->createGBlackBox(x,y,packageName, boxType);
160                   saveTempandUpdate("drop box");
161                 }
162             }
163           // DFGO: file for linux
164           else if(stdData.substr(0,5) == "file:")
165             {
166               std::cout << "file to open: " << stdData << std::endl;
167             }
168
169           return true;
170         }
171
172         //=========================================================================
173         void wxGEditorTabPanel::saveTempandUpdate(const std::string &action)
174         {
175                 _panelsManager->saveTempandUpdate(action);
176         }
177         //=========================================================================
178
179         void wxGEditorTabPanel::setPanelsManager(wxTabPanelsManager* panelsManager)
180         {
181                 _panelsManager = panelsManager;
182         }
183
184         //=========================================================================
185
186         void wxGEditorTabPanel::displayBlackBoxInfo(std::string packageName, std::string boxName)
187         {
188                 _panelsManager->displayBlackBoxInfo(packageName,boxName);
189         }
190
191         //=========================================================================
192
193         void wxGEditorTabPanel::updateStatusBar(std::string textStatus)
194         {
195                 _panelsManager->updateStatusBar(textStatus);
196         }
197
198         //=========================================================================
199
200         std::string wxGEditorTabPanel::getDiagramBBS(bool wln)
201         {
202                 return _sceneManager->getDiagramBBS(wln);
203         }
204
205         //=========================================================================
206
207         std::string wxGEditorTabPanel::saveComplexBoxBBS()
208         {
209                 return _sceneManager->saveComplexBoxBBS();
210         }
211
212         //=========================================================================
213
214         void wxGEditorTabPanel::editBlackBox(GBlackBoxModel *bbmodel)
215         {
216                 _panelsManager->editBlackBox(bbmodel);
217         }
218
219         //=========================================================================
220
221         void wxGEditorTabPanel::deleteAllBoxes()
222         {
223                 _sceneManager->deleteAllBoxes();
224         }
225
226         //=========================================================================
227
228         void wxGEditorTabPanel::centerView()
229         {
230                 if(_sceneManager)
231                 {
232                         _sceneManager->centerView();
233                 }
234         }
235
236         //=========================================================================
237
238         void wxGEditorTabPanel::saveDiagram(std::string &content, const std::string &path) //DFCH
239         {
240                 this->SetFullPath(path);
241                 _sceneManager->saveDiagram(content);
242         }
243
244         //=========================================================================
245
246         void wxGEditorTabPanel::saveTempDiagram(const std::string &action) //FCY
247         {
248                 std::string content = "";
249                 // writing file header
250                 content += "# ----------------------------------\n";
251                 content += "# - BBTKGEditor v 1.3 BBG BlackBox Diagram file\n";
252                 content += "# - ";
253                 content += "temp";
254                 content += "\n";
255                 content += "# ----------------------------------\n";
256                 content += "\n";
257                 content += "APP_START\n";
258                 _sceneManager->saveDiagram(content);
259                 content += "APP_END\n"; //put here to avoid mistakes
260                 if(_actualdo == states.end() ) // same size, write to the end
261                 {
262                         states.push_back (content);
263                         saveStringAction(action);
264                 }
265                 else
266                 {
267                         std::vector<std::string>::iterator it = _actualdo+1;
268                         states.erase(it, states.end());
269                         states.push_back(content);
270
271                         std::vector<std::string>::iterator sit = _sactualdo+1;
272                         sstates.erase(sit, sstates.end());
273                         saveStringAction(action);
274                 }
275                 _actualdo = states.end();
276                 _sactualdo = sstates.end();
277                         
278         }
279
280         //=========================================================================
281
282         void wxGEditorTabPanel::loadDiagram(ifstream &inputStream, const std::string &path) //DFCH
283         {
284                 this->SetFullPath(path);
285                 stringstream ss;
286                 ss << inputStream.rdbuf() ;
287                 _sceneManager->loadDiagram(ss);
288         }
289
290         //=========================================================================
291         unsigned short wxGEditorTabPanel::getUndoState( std::string &stun)
292         {
293                 stun = "Undo";
294                 if(states.size()>0)
295                 {
296                         if(_actualdo == states.begin() )
297                         {
298                                 
299                                 return 0;
300                         }
301                         else
302                         {
303                                 stun +=" - ";
304                                 stun += _sactualdo->substr(0,(--_sactualdo)->find_last_of("."));
305                                 _sactualdo++;
306                                 return 1;
307                         }
308                 }
309         }
310
311         unsigned short wxGEditorTabPanel::getRedoState( std::string &stre)
312         {
313                 stre = "Redo";
314                 if(states.size()>0)
315                 {
316                         if(_actualdo == --states.end() || states.size() == 1 || _actualdo == states.end())
317                                 return 0;
318                         else
319                         {
320                                 stre +=" - ";
321                                 stre += _sactualdo->substr(0,(++_sactualdo)->find_last_of("."));
322                                 --_sactualdo;
323                                 return 1;
324                         }
325                 }
326         }
327
328         //=========================================================================
329         void wxGEditorTabPanel::loadTempDiagram(unsigned short un) //FCY
330         {
331                 unsigned short res = 1;
332                 deleteAllBoxes();
333                 std::stringstream ss;
334                 if (un == 0) //undo
335                 {
336                         if(_actualdo == states.end() ) // same size, write to the end
337                         {
338                                 (_actualdo--);
339                                 (_sactualdo--);
340                         }
341                         if(_actualdo != states.begin() )
342                         {
343                                 ss << *(--_actualdo);
344                                 _sactualdo--;
345                         }
346                         else
347                         {
348                                 ss << *_actualdo;
349                                 res = 0;
350                         }
351                 }
352                 else //redo
353                 {
354                         _actualdo++;
355                         _sactualdo++;
356                         if(_actualdo != states.end())
357                         {
358                                 ss << *_actualdo;
359                         }
360                         else
361                         {
362                                 res = 0;
363                         }
364                 }
365                 _sceneManager->loadDiagram(ss);
366         
367         }
368
369         void wxGEditorTabPanel::saveStringAction(const std::string &action)
370         {
371                 char temp[50];
372                 sprintf(temp,".%d", sstates.size());
373                 sstates.push_back(action + temp);
374         }
375
376         //=========================================================================
377
378         int wxGEditorTabPanel::getPanelId()
379         {
380                 return _id;
381         }
382         
383
384
385         //=========================================================================
386
387         bool wxGEditorTabPanel::isComplexBox()
388         {
389                 return _sceneManager->isComplexBox();
390         }
391
392         //=========================================================================
393
394         void wxGEditorTabPanel::setComplexBox(bool val)
395         {
396                 _sceneManager->setComplexBox(val);
397         }
398
399         //=========================================================================
400
401         void wxGEditorTabPanel::addComplexInputPort(std::string portName)
402         {
403                 _sceneManager->createGComplexBoxInputPort(portName);
404         }
405
406         //=========================================================================
407
408         void wxGEditorTabPanel::addComplexOutputPort(std::string portName)
409         {
410                 _sceneManager->createGComplexBoxOutputPort(portName);
411         }
412
413         //=========================================================================
414
415         std::map<int,GObjectController*> wxGEditorTabPanel::getSelectedObjects()
416         {
417                 return _sceneManager->getSelectedObjects();
418         }
419
420         //=========================================================================
421
422         void wxGEditorTabPanel::addObjects(std::map<int,GObjectController*> objectsMap)
423         {
424                 _sceneManager->addObjects(objectsMap);
425         }
426
427         //=========================================================================
428
429         int wxGEditorTabPanel::getNumSelectedObjects()
430         {
431                 return _sceneManager->getNumSelectedObjects();
432         }
433
434         //=========================================================================
435
436
437         wxVtkSceneManager* wxGEditorTabPanel::getSceneManager()
438         {
439                 return _sceneManager;
440         }
441
442         //=========================================================================
443     std::string wxGEditorTabPanel::GetCbName()
444         {
445             return _sceneManager->GetCbName();
446         }
447
448
449         //=========================================================================
450     std::string wxGEditorTabPanel::GetCbPackageName()
451         {
452             return _sceneManager->GetCbPackageName();
453         }
454
455         //=========================================================================
456     std::string wxGEditorTabPanel::GetAuthor()
457         {
458             return _sceneManager->GetAuthor();
459         }
460
461         //=========================================================================
462     std::string wxGEditorTabPanel::GetDescription()
463         {
464             return _sceneManager->GetDescription();
465         }
466
467         //=========================================================================
468     std::string wxGEditorTabPanel::GetCategory()
469         {
470             return _sceneManager->GetCategory();
471         }
472
473         //=========================================================================
474     void wxGEditorTabPanel::SetCbName(std::string cbName)
475     {
476         _sceneManager->SetCbName( cbName );
477     }
478
479         //=========================================================================
480     void wxGEditorTabPanel::SetCbPackageName(std::string packagename)
481     {
482         _sceneManager->SetCbPackageName( packagename );
483     }
484
485
486         //=========================================================================
487     void wxGEditorTabPanel::SetAuthor(std::string author)
488     {
489         _sceneManager->SetAuthor( author );
490     }
491
492
493         //=========================================================================
494     void wxGEditorTabPanel::SetCategory(std::string category)
495     {
496         _sceneManager->SetCategory( category );
497     }
498
499
500         //=========================================================================
501     void wxGEditorTabPanel::SetDescription(std::string description)
502     {
503         _sceneManager->SetDescription( description );
504     }
505     //=========================================================================
506         //DFCH
507         void wxGEditorTabPanel::SetFullPath( const std::string& fullpath )
508         {
509                 this->_fullPath = fullpath;
510         }
511         //=========================================================================
512         //DFCH
513         void wxGEditorTabPanel::SetFileName( const std::string& filename )
514         {
515                 this->_fileName = filename;
516         }
517         //=========================================================================
518         //DFCH
519         std::string wxGEditorTabPanel::GetFullPath( )
520         {
521                 return( this->_fullPath );
522         }
523         //=========================================================================
524         //DFCH
525     std::string wxGEditorTabPanel::GetFileName( )
526     {
527                 return( this->_fileName );
528         }
529
530
531
532
533 }  // EO namespace bbtk
534
535 // EOF