]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxGEditorTabPanel.cxx
#2492 bbGEditor Bug New Normal - Changement of creaMaracasVisu deffinition
[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(states.size()>0)
347                 {
348                         if(_actualdo == states.begin() )
349                         {
350                                 
351                                 return 0;
352                         }
353                         else
354                         {
355                                 stun +=" - ";
356                                 stun += _sactualdo->substr(0,(--_sactualdo)->find_last_of("."));
357                                 _sactualdo++;
358                                 return 1;
359                         }
360                 }
361         }
362
363         unsigned short wxGEditorTabPanel::getRedoState( std::string &stre)
364         {
365                 stre = "Redo";
366                 if(states.size()>0)
367                 {
368                         if(_actualdo == --states.end() || states.size() == 1 || _actualdo == states.end())
369                                 return 0;
370                         else
371                         {
372                                 stre +=" - ";
373                                 stre += _sactualdo->substr(0,(++_sactualdo)->find_last_of("."));
374                                 --_sactualdo;
375                                 return 1;
376                         }
377                 }
378         }
379
380         //=========================================================================
381         void wxGEditorTabPanel::loadTempDiagram(unsigned short un) //FCY
382         {
383                 unsigned short res = 1;
384                 deleteAllBoxes();
385                 std::stringstream ss;
386                 if (un == 0) //undo
387                 {
388                         if(_actualdo == states.end() ) // same size, write to the end
389                         {
390                                 (_actualdo--);
391                                 (_sactualdo--);
392                         }
393                         if(_actualdo != states.begin() )
394                         {
395                                 ss << *(--_actualdo);
396                                 _sactualdo--;
397                         }
398                         else
399                         {
400                                 ss << *_actualdo;
401                                 res = 0;
402                         }
403                 }
404                 else //redo
405                 {
406                         _actualdo++;
407                         _sactualdo++;
408                         if(_actualdo != states.end())
409                         {
410                                 ss << *_actualdo;
411                         }
412                         else
413                         {
414                                 res = 0;
415                         }
416                 }
417                 _sceneManager->loadDiagram(ss);
418         
419         }
420
421         void wxGEditorTabPanel::saveStringAction(const std::string &action)
422         {
423                 char temp[50];
424                 sprintf(temp,".%d", sstates.size());
425                 sstates.push_back(action + temp);
426         }
427
428         //=========================================================================
429
430         int wxGEditorTabPanel::getPanelId()
431         {
432                 return _id;
433         }
434         
435
436
437         //=========================================================================
438
439         bool wxGEditorTabPanel::isComplexBox()
440         {
441                 return _sceneManager->isComplexBox();
442         }
443
444         //=========================================================================
445
446         void wxGEditorTabPanel::setComplexBox(bool val)
447         {
448                 _sceneManager->setComplexBox(val);
449         }
450
451         //=========================================================================
452
453         void wxGEditorTabPanel::addComplexInputPort(std::string portName)
454         {
455                 _sceneManager->createGComplexBoxInputPort(portName);
456         }
457
458         //=========================================================================
459
460         void wxGEditorTabPanel::addComplexOutputPort(std::string portName)
461         {
462                 _sceneManager->createGComplexBoxOutputPort(portName);
463         }
464
465         //=========================================================================
466
467         std::map<int,GObjectController*> wxGEditorTabPanel::getSelectedObjects()
468         {
469                 return _sceneManager->getSelectedObjects();
470         }
471
472         //=========================================================================
473
474         void wxGEditorTabPanel::addObjects(std::map<int,GObjectController*> objectsMap)
475         {
476                 _sceneManager->addObjects(objectsMap);
477         }
478
479         //=========================================================================
480
481         int wxGEditorTabPanel::getNumSelectedObjects()
482         {
483                 return _sceneManager->getNumSelectedObjects();
484         }
485
486         //=========================================================================
487
488
489         wxVtkSceneManager* wxGEditorTabPanel::getSceneManager()
490         {
491                 return _sceneManager;
492         }
493
494         //=========================================================================
495     std::string wxGEditorTabPanel::GetCbName()
496         {
497             return _sceneManager->GetCbName();
498         }
499
500
501         //=========================================================================
502     std::string wxGEditorTabPanel::GetCbPackageName()
503         {
504             return _sceneManager->GetCbPackageName();
505         }
506
507         //=========================================================================
508     std::string wxGEditorTabPanel::GetAuthor()
509         {
510             return _sceneManager->GetAuthor();
511         }
512
513         //=========================================================================
514     std::string wxGEditorTabPanel::GetDescription()
515         {
516             return _sceneManager->GetDescription();
517         }
518
519         //=========================================================================
520     std::string wxGEditorTabPanel::GetCategory()
521         {
522             return _sceneManager->GetCategory();
523         }
524         //=========================================================================
525                 std::string wxGEditorTabPanel::GetMessageKind()
526                 {
527             return _sceneManager->GetMessageKind();
528                 }
529         //=========================================================================
530      std::string wxGEditorTabPanel::GetMessageLevel()
531                 {
532             return _sceneManager->GetMessageLevel();
533                 }
534
535         //=========================================================================
536     void wxGEditorTabPanel::SetCbName(std::string cbName)
537     {
538         _sceneManager->SetCbName( cbName );
539     }
540
541         //=========================================================================
542     void wxGEditorTabPanel::SetCbPackageName(std::string packagename)
543     {
544         _sceneManager->SetCbPackageName( packagename );
545     }
546
547
548         //=========================================================================
549     void wxGEditorTabPanel::SetAuthor(std::string author)
550     {
551         _sceneManager->SetAuthor( author );
552     }
553
554
555         //=========================================================================
556     void wxGEditorTabPanel::SetCategory(std::string category)
557     {
558         _sceneManager->SetCategory( category );
559     }
560
561
562         //=========================================================================
563     void wxGEditorTabPanel::SetDescription(std::string description)
564     {
565         _sceneManager->SetDescription( description );
566     }
567
568         //=========================================================================
569                 void wxGEditorTabPanel::SetMessageKind(std::string kind)
570                 {
571         _sceneManager->SetMessageKind( kind );
572     }
573
574         //=========================================================================    
575     void wxGEditorTabPanel::SetMessageLevel(std::string level)
576                 {
577         _sceneManager->SetMessageLevel( level );
578     }
579     //=========================================================================
580         //DFCH
581         void wxGEditorTabPanel::SetFullPath( const std::string& fullpath )
582         {
583                 this->_fullPath = fullpath;
584         }
585         //=========================================================================
586         //DFCH
587         void wxGEditorTabPanel::SetFileName( const std::string& filename )
588         {
589                 this->_fileName = filename;
590         }
591         //=========================================================================
592         //DFCH
593         std::string wxGEditorTabPanel::GetFullPath( )
594         {
595                 return( this->_fullPath );
596         }
597         //=========================================================================
598         //DFCH
599     std::string wxGEditorTabPanel::GetFileName( )
600     {
601                 return( this->_fileName );
602         }
603
604
605
606
607 }  // EO namespace bbtk
608
609 // EOF