]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxGEditorTabPanel.cxx
#3210 bbGEditor Feature New Normal branch vtk7itk4wx3-mingw
[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,NULL);
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
134           //wxMessageBox(data);
135
136           std::string stdData = crea::wx2std(data);
137
138           //std::cout << "Text drop: '" << stdData << "'" << std::endl;
139
140           if(stdData.substr(0,4) == "box:")
141             {
142               int posT = stdData.find_first_of(':', 4);
143               std::string packageName = stdData.substr(4, posT-4);
144               posT = stdData.find_first_not_of(':', posT);
145               std::string boxType = stdData.substr(posT);
146
147               //std::cout << "box: Package='" << packageName << "' BoxType='" << boxType << "'" <<std::endl;
148               /*
149                     wxString foo( (data) );
150                     char str[150];
151                     strcpy( str, (const char*)foo.mb_str(wxConvUTF8) );
152
153                     char delims[] = ":";
154                      char *result = NULL;
155                      result = strtok( str, delims );
156                      packageName += result;
157
158                      result = strtok( NULL, delims );
159                      boxType += result;
160                */
161               if ( _panelsManager->TryToOpenScriptApplication(packageName,boxType) == false )
162                 {
163                   _sceneManager->createGBlackBox(x,y,packageName, boxType);
164                   saveTempandUpdate("drop box");
165                 }
166             }
167           // DFGO: file for linux
168           else if(stdData.substr(0,5) == "file:")
169             {
170               stdData=stdData.substr(0,stdData.size()-1);
171               std::cout << "Dropped file: " << stdData << std::endl;
172               std::stringstream ss(stdData);
173               std::getline(ss, stdData, (char)13);
174               while(!ss.eof())
175                 {
176                   std::string filePath = stdData.substr(7);
177                   std::cout << filePath << ": ";
178
179                           FILE *ff=fopen(filePath.c_str(),"r");
180                   if(
181 //                                wxFile::Exists( crea::std2wx(filePath)) 
182                                   ff!=NULL
183                                   )
184                     {
185                                   fclose(ff);
186                       std::cout << "File Exists." << std::endl;
187                       wxFileName fileToOpen(crea::std2wx(filePath));
188
189                       std::string filetype = crea::wx2std(fileToOpen.GetExt());
190                       std::string filename = crea::wx2std(fileToOpen.GetFullName());
191                       std::string filepath = crea::wx2std(fileToOpen.GetFullPath());
192
193
194                       if(filetype == "bbs")
195                         {
196                           std::cout << "Importing bbs file." << std::endl;
197                           wxMessageBox(crea::std2wx("You are opening a bbs file, "
198                               "make sure not to overwrite an existing bbg file "
199                               "when saving the pipeline."),
200                               crea::std2wx("Opening File Warning"), wxICON_INFORMATION);
201                           this->_panelsManager->GetParentGUIEditorGraphicBBS()->OpenBBS(filepath, filename);
202                         }
203                       else if(filetype == "bbg")
204                         {
205                           std::cout << "Opening bbg file." << std::endl;
206                           this->_panelsManager->GetParentGUIEditorGraphicBBS()->OpenDiagram(filepath, filename);
207                         }
208                       else
209                         {
210                           std::cout << "Unknown file type: Can't open this kind of file: " << filetype << std::endl;
211                         }
212                     }
213                   else
214                     {
215                       std::cout << "File Doesn't Exists. The given path is invalid: " << filePath << std::endl;
216                     }
217                   std::getline(ss, stdData, (char)13);
218                 }
219             }
220
221           return true;
222         }
223
224         //=========================================================================
225         void wxGEditorTabPanel::saveTempandUpdate(const std::string &action)
226         {
227                 _panelsManager->saveTempandUpdate(action);
228         }
229         //=========================================================================
230
231         void wxGEditorTabPanel::setPanelsManager(wxTabPanelsManager* panelsManager)
232         {
233                 _panelsManager = panelsManager;
234         }
235
236         //=========================================================================
237
238         void wxGEditorTabPanel::displayBlackBoxInfo(std::string packageName, std::string boxName)
239         {
240                 _panelsManager->displayBlackBoxInfo(packageName,boxName);
241         }
242
243         //=========================================================================
244
245         void wxGEditorTabPanel::updateStatusBar(std::string textStatus)
246         {
247                 _panelsManager->updateStatusBar(textStatus);
248         }
249
250         //=========================================================================
251
252         std::string wxGEditorTabPanel::getDiagramBBS(bool wln)
253         {
254                 return _sceneManager->getDiagramBBS(wln);
255         }
256
257         //=========================================================================
258
259         std::string wxGEditorTabPanel::saveComplexBoxBBS()
260         {
261                 return _sceneManager->saveComplexBoxBBS();
262         }
263
264         //=========================================================================
265
266         void wxGEditorTabPanel::editBlackBox(GBlackBoxModel *bbmodel)
267         {
268                 _panelsManager->editBlackBox(bbmodel);
269         }
270
271         //=========================================================================
272
273         void wxGEditorTabPanel::deleteAllBoxes()
274         {
275                 _sceneManager->deleteAllBoxes();
276         }
277
278         //=========================================================================
279
280         void wxGEditorTabPanel::centerView()
281         {
282                 if(_sceneManager)
283                 {
284                         _sceneManager->centerView();
285                 }
286         }
287
288         //=========================================================================
289
290         void wxGEditorTabPanel::saveDiagram(std::string &content, const std::string &path) //DFCH
291         {
292                 this->SetFullPath(path);
293                 _sceneManager->saveDiagram(content);
294         }
295
296         //=========================================================================
297
298         void wxGEditorTabPanel::saveTempDiagram(const std::string &action) //FCY
299         {
300                 std::string content = "";
301                 // writing file header
302                 content += "# ----------------------------------\n";
303                 content += "# - BBTKGEditor v 1.3 BBG BlackBox Diagram file\n";
304                 content += "# - ";
305                 content += "temp";
306                 content += "\n";
307                 content += "# ----------------------------------\n";
308                 content += "\n";
309                 content += "APP_START\n";
310                 _sceneManager->saveDiagram(content);
311                 content += "APP_END\n"; //put here to avoid mistakes
312                 if(_actualdo == states.end() ) // same size, write to the end
313                 {
314                         states.push_back (content);
315                         saveStringAction(action);
316                 }
317                 else
318                 {
319                         std::vector<std::string>::iterator it = _actualdo+1;
320                         states.erase(it, states.end());
321                         states.push_back(content);
322
323                         std::vector<std::string>::iterator sit = _sactualdo+1;
324                         sstates.erase(sit, sstates.end());
325                         saveStringAction(action);
326                 }
327                 _actualdo = states.end();
328                 _sactualdo = sstates.end();
329                         
330         }
331
332         //=========================================================================
333
334         void wxGEditorTabPanel::loadDiagram(ifstream &inputStream, const std::string &path) //DFCH
335         {
336                 this->SetFullPath(path);
337                 stringstream ss;
338                 ss << inputStream.rdbuf() ;
339                 _sceneManager->loadDiagram(ss);
340         }
341
342         //=========================================================================
343         unsigned short wxGEditorTabPanel::getUndoState( std::string &stun)
344         {
345                 stun = "Undo";
346                 if(sstates.size()>0)
347                 {
348                         if(_actualdo == states.begin() )
349 //                      if(sstates.size()==1 )
350                         {
351 //                              _sactualdo=sstates.begin();
352                                 return 0;
353                         } else {
354                                 stun +=" - ";
355 printf("EED wxGEditorTabPanel::getUndoState This mecanisme is not working  ??????    _sactualdo sstates      _actualdo state \n");              
356 //                              stun += _sactualdo->substr(0,(--_sactualdo)->find_last_of("."));                                
357                                 _sactualdo++;
358                                 return 1;
359                         }
360                 }
361         return 0;
362         }
363
364         unsigned short wxGEditorTabPanel::getRedoState( std::string &stre)
365         {
366                 stre = "Redo";
367                 if(states.size()>0)
368                 {
369                         if(_actualdo == --states.end() || states.size() == 1 || _actualdo == states.end())
370                                 return 0;
371                         else
372                         {
373                                 stre +=" - ";
374                                 stre += _sactualdo->substr(0,(++_sactualdo)->find_last_of("."));
375                                 --_sactualdo;
376                                 return 1;
377                         }
378                 }
379         }
380
381         //=========================================================================
382         void wxGEditorTabPanel::loadTempDiagram(unsigned short un) //FCY
383         {
384                 unsigned short res = 1;
385                 deleteAllBoxes();
386                 std::stringstream ss;
387                 if (un == 0) //undo
388                 {
389                         if(_actualdo == states.end() ) // same size, write to the end
390                         {
391                                 (_actualdo--);
392                                 (_sactualdo--);
393                         }
394                         if(_actualdo != states.begin() )
395                         {
396                                 ss << *(--_actualdo);
397                                 _sactualdo--;
398                         }
399                         else
400                         {
401                                 ss << *_actualdo;
402                                 res = 0;
403                         }
404                 }
405                 else //redo
406                 {
407                         _actualdo++;
408                         _sactualdo++;
409                         if(_actualdo != states.end())
410                         {
411                                 ss << *_actualdo;
412                         }
413                         else
414                         {
415                                 res = 0;
416                         }
417                 }
418                 _sceneManager->loadDiagram(ss);
419         
420         }
421
422         void wxGEditorTabPanel::saveStringAction(const std::string &action)
423         {
424                 char temp[50];
425                 sprintf(temp,".%d", sstates.size());
426                 sstates.push_back(action + temp);
427         }
428
429         //=========================================================================
430
431         int wxGEditorTabPanel::getPanelId()
432         {
433                 return _id;
434         }
435         
436
437
438         //=========================================================================
439
440         bool wxGEditorTabPanel::isComplexBox()
441         {
442                 return _sceneManager->isComplexBox();
443         }
444
445         //=========================================================================
446
447         void wxGEditorTabPanel::setComplexBox(bool val)
448         {
449                 _sceneManager->setComplexBox(val);
450         }
451
452         //=========================================================================
453
454         void wxGEditorTabPanel::addComplexInputPort(std::string portName)
455         {
456                 _sceneManager->createGComplexBoxInputPort(portName);
457         }
458
459         //=========================================================================
460
461         void wxGEditorTabPanel::addComplexOutputPort(std::string portName)
462         {
463                 _sceneManager->createGComplexBoxOutputPort(portName);
464         }
465
466         //=========================================================================
467
468         std::map<int,GObjectController*> wxGEditorTabPanel::getSelectedObjects()
469         {
470                 return _sceneManager->getSelectedObjects();
471         }
472
473         //=========================================================================
474
475         void wxGEditorTabPanel::addObjects(std::map<int,GObjectController*> objectsMap)
476         {
477                 _sceneManager->addObjects(objectsMap);
478         }
479
480         //=========================================================================
481
482         int wxGEditorTabPanel::getNumSelectedObjects()
483         {
484                 return _sceneManager->getNumSelectedObjects();
485         }
486
487         //=========================================================================
488
489
490         wxVtkSceneManager* wxGEditorTabPanel::getSceneManager()
491         {
492                 return _sceneManager;
493         }
494
495         //=========================================================================
496     std::string wxGEditorTabPanel::GetCbName()
497         {
498             return _sceneManager->GetCbName();
499         }
500
501
502         //=========================================================================
503     std::string wxGEditorTabPanel::GetCbPackageName()
504         {
505             return _sceneManager->GetCbPackageName();
506         }
507
508         //=========================================================================
509     std::string wxGEditorTabPanel::GetAuthor()
510         {
511             return _sceneManager->GetAuthor();
512         }
513
514         //=========================================================================
515     std::string wxGEditorTabPanel::GetDescription()
516         {
517             return _sceneManager->GetDescription();
518         }
519
520         //=========================================================================
521     std::string wxGEditorTabPanel::GetCategory()
522         {
523             return _sceneManager->GetCategory();
524         }
525         //=========================================================================
526                 std::string wxGEditorTabPanel::GetMessageKind()
527                 {
528             return _sceneManager->GetMessageKind();
529                 }
530         //=========================================================================
531      std::string wxGEditorTabPanel::GetMessageLevel()
532                 {
533             return _sceneManager->GetMessageLevel();
534                 }
535
536         //=========================================================================
537     void wxGEditorTabPanel::SetCbName(std::string cbName)
538     {
539         _sceneManager->SetCbName( cbName );
540     }
541
542         //=========================================================================
543     void wxGEditorTabPanel::SetCbPackageName(std::string packagename)
544     {
545         _sceneManager->SetCbPackageName( packagename );
546     }
547
548
549         //=========================================================================
550     void wxGEditorTabPanel::SetAuthor(std::string author)
551     {
552         _sceneManager->SetAuthor( author );
553     }
554
555
556         //=========================================================================
557     void wxGEditorTabPanel::SetCategory(std::string category)
558     {
559         _sceneManager->SetCategory( category );
560     }
561
562
563         //=========================================================================
564     void wxGEditorTabPanel::SetDescription(std::string description)
565     {
566         _sceneManager->SetDescription( description );
567     }
568
569         //=========================================================================
570                 void wxGEditorTabPanel::SetMessageKind(std::string kind)
571                 {
572         _sceneManager->SetMessageKind( kind );
573     }
574
575         //=========================================================================    
576     void wxGEditorTabPanel::SetMessageLevel(std::string level)
577                 {
578         _sceneManager->SetMessageLevel( level );
579     }
580     //=========================================================================
581         //DFCH
582         void wxGEditorTabPanel::SetFullPath( const std::string& fullpath )
583         {
584                 this->_fullPath = fullpath;
585         }
586         //=========================================================================
587         //DFCH
588         void wxGEditorTabPanel::SetFileName( const std::string& filename )
589         {
590                 this->_fileName = filename;
591         }
592         //=========================================================================
593         //DFCH
594         std::string wxGEditorTabPanel::GetFullPath( )
595         {
596                 return( this->_fullPath );
597         }
598         //=========================================================================
599         //DFCH
600     std::string wxGEditorTabPanel::GetFileName( )
601     {
602                 return( this->_fileName );
603         }
604
605
606
607
608 }  // EO namespace bbtk
609
610 // EOF