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