]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/bbtkwxGUIEditorGraphicBBS.cxx
Buttons to define a complexbox
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsWxGUIEditorGraphic / bbtkwxGUIEditorGraphicBBS.cxx
1 /*=========================================================================                                                                               
2 Program:   bbtk
3 Module:    $RCSfile$
4 Language:  C++
5 Date:      $Date$
6 Version:   $Revision$
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32 *  \file 
33 *  \brief Class bbtk::WxInterfaceEditorGraphicBBS . 
34 */
35
36 #include "bbtkwxGUIEditorGraphicBBS.h"
37 #include "creaWx.h"
38
39 namespace bbtk
40 {
41
42
43         //=========================================================================
44         wxGUIEditorGraphicBBS::wxGUIEditorGraphicBBS(wxFrame *parent)
45                 : wxFrame(parent, -1, _T("bbtkGEditor"),wxDefaultPosition, wxSize(1200,800))
46         {
47                 _frameAUIMgr = new wxAuiManager(this);
48                 
49
50                 /*std::string datadir( crea::System::GetExecutablePath() );
51                 std::string datadir (".");
52                 cout<<"RaC //------------------"<<endl;
53                 cout<<"RaC //TODO wxGUIEditorGraphicBBS constructor.Initialize datadir with  crea::System::GetExecutablePath() ."<<endl;
54                 cout<<"RaC //------------------"<<endl;
55
56             #ifdef LINUX // assume this is OSX 
57                    datadir=datadir+"/../share/creaContours";
58             #endif // MACOSX    
59         
60                 #ifdef MACOSX // assume this is OSX 
61                         datadir=datadir+"/../../../../share/creaContours";
62                 #endif // MACOSX        
63
64                 _dataDir = datadir;*/
65
66                 initMenu();
67                 initToolbar();
68                 initTabPanelsManager();
69                 initPackageBrowser();
70                 initHelpHTMLBrowser();
71
72                 CreateStatusBar();
73                 _frameAUIMgr->Update();
74                 _actualPkgBrowserBoxName="";
75                 _actualPkgBrowserPkgName="";
76         }
77
78
79         //=========================================================================
80         wxGUIEditorGraphicBBS::~wxGUIEditorGraphicBBS()
81         {
82                 _frameAUIMgr->UnInit();
83         }
84
85         //=========================================================================
86         void wxGUIEditorGraphicBBS::initToolbar()
87         {        
88                 wxBitmap bmp_new(new_xpm);
89                 wxBitmap bmp_open(open_xpm);
90                 wxBitmap bmp_save(save_xpm);
91                 wxBitmap bmp_run(run_xpm);
92                 wxBitmap bmp_delete(delete_xpm);
93                 wxBitmap bmp_centerview(centerview_xpm);
94                 wxBitmap bmp_complexbox(complexbox_xpm);
95
96                 wxToolBar  *toolbar = new wxToolBar(this, wxID_ANY);
97
98                 //Adds a tool btn to the toolbar
99                 toolbar->AddTool(ID_NEW,_T("New"),bmp_new, wxNullBitmap, wxITEM_NORMAL,_T("New tab"), _T("Create a new panel tab"));
100                 toolbar->AddTool(ID_OPEN,_T("Open diagram"),bmp_open, wxNullBitmap, wxITEM_NORMAL,_T("Open a diagram"), _T("Open a diagram"));
101                 toolbar->AddTool(ID_SAVE,_T("Save diagram"),bmp_save, wxNullBitmap, wxITEM_NORMAL,_T("Saves actual diagram"), _T("Saves actual diagram"));
102                 toolbar->AddTool(ID_RUN,_T("Run"),bmp_run, wxNullBitmap, wxITEM_NORMAL,_T("Execute actual diagram"), _T("Execute actual diagram"));
103                 toolbar->AddTool(ID_DELETEALL,_T("Delete all"),bmp_delete, wxNullBitmap, wxITEM_NORMAL,_T("Delete all boxes"), _T("Delete all boxes"));
104                 toolbar->AddTool(ID_CENTERVIEW,_T("Center view"),bmp_centerview, wxNullBitmap, wxITEM_NORMAL,_T("Center view"), _T("Center view"));
105                 toolbar->AddSeparator();
106                 toolbar->AddTool(ID_BTNCOMPLEXBOX,_T("Complex box"),complexbox_xpm, wxNullBitmap, wxITEM_NORMAL,_T("Complex box"), _T("Complex box"));
107                 toolbar->SetToggle(ID_BTNCOMPLEXBOX,true);
108                 toolbar->AddTool(ID_ADDCOMPLEXBOXINPUT,_T("Add input to Complex box"),bmp_centerview, wxNullBitmap, wxITEM_NORMAL,_T("Add input to Complex box"), _T("Add input to Complex box"));
109                 toolbar->AddTool(ID_ADDCOMPLEXBOXOUTPUT,_T("Add output to Complex box"),bmp_centerview, wxNullBitmap, wxITEM_NORMAL,_T("Add output Complex box"), _T("Add output Complex box"));
110                 toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT,false);
111                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT,false);
112                 
113                 toolbar->SetMargins( 2, 2 );
114                 toolbar->Realize();
115                 SetToolBar(toolbar);
116
117                 // connect command event handlers
118                 Connect(ID_NEW,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateNewTab));
119                 Connect(ID_OPEN,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnOpenDiagram));
120                 Connect(ID_SAVE,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualDiagram));
121                 Connect(ID_RUN,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExecuteActualDiagram));
122                 Connect(ID_DELETEALL,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram));
123                 Connect(ID_CENTERVIEW,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCenterViewActualDiagram));
124                 Connect(ID_BTNCOMPLEXBOX,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnComplexBox));
125                 Connect(ID_ADDCOMPLEXBOXINPUT,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxInput));
126                 Connect(ID_ADDCOMPLEXBOXOUTPUT,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxOutput));
127
128         }
129         //=========================================================================
130         void wxGUIEditorGraphicBBS::initHelpHTMLBrowser()
131         {
132                 //TO FIX THE PATH OF BBTK BIN  IN ORDER TO USE REGENERATE EXECUTABLES
133                 //RegenerateAll();
134                 _helpHtmlBrowser = new WxGUIHtmlBrowser(this,wxSize(200,0));
135                 _frameAUIMgr->AddPane(_helpHtmlBrowser,wxAuiPaneInfo().Right().MinSize(200,200));       
136         }
137
138         //=========================================================================
139         void wxGUIEditorGraphicBBS::initMenu()
140         {
141                 // create a menu bar
142                 wxMenu *fileMenu = new wxMenu;
143
144                 // the "About" item should be in the help menu
145                 wxMenu *helpMenu = new wxMenu;
146                 helpMenu->Append(wxID_ABOUT, _T("&About..."), _T("About"));
147                 fileMenu->Append(wxID_EXIT, _T("E&xit"), _T("Exit"));
148
149                 Connect(wxID_EXIT,wxEVT_COMMAND_MENU_SELECTED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExit));
150
151                 // now append the freshly created menu to the menu bar...
152                 wxMenuBar *menuBar = new wxMenuBar();
153                 menuBar->Append(fileMenu, _T("&File"));
154                 menuBar->Append(helpMenu, _T("&Help"));
155
156                 // attach this menu bar to the frame
157                 SetMenuBar(menuBar);
158         }
159
160         //=========================================================================
161         void wxGUIEditorGraphicBBS::initTabPanelsManager()
162         {
163                 _notebook = new wxAuiNotebook(this,wxID_NOTEBOOK,wxDefaultPosition,wxDefaultSize,wxAUI_NB_TOP|wxAUI_NB_TAB_MOVE|wxAUI_NB_CLOSE_ON_ACTIVE_TAB|wxAUI_NB_TAB_FIXED_WIDTH|wxAUI_NB_WINDOWLIST_BUTTON);
164                 _tabsMgr = new wxTabPanelsManager(this);
165                 _frameAUIMgr->AddPane(_notebook,wxAuiPaneInfo().CenterPane());  
166
167         }
168         
169         //=========================================================================
170         void wxGUIEditorGraphicBBS::initPackageBrowser()
171         {
172                 _pkgBrowser = new WxGUIPackageBrowser2(this);
173                 _pkgBrowser->IncludeAll();
174                 _frameAUIMgr->AddPane(_pkgBrowser,wxAuiPaneInfo().Left().MinSize(200,200).CloseButton(false));          
175         }
176
177         //================================================================
178
179         void wxGUIEditorGraphicBBS::DoRegeneratePackageDoc( const std::string& pack )
180         {
181                 std::string mess("Regenerating doc for package '");
182                 if (pack!="-a"){
183                         mess += pack + "'";
184                 }
185                 else
186                 {
187                         mess = "Regenerating doc for all packages";
188                 }
189                 
190                 mess += " ... please wait";             
191
192                 SetStatusText( std2wx(mess) );
193
194                 //BBTK_BUSY_CURSOR;
195
196                 std::string command;
197                 #if defined(WIN32)
198                         command = "\"";
199                 #endif
200                         command += ConfigurationFile::GetInstance().Get_bin_path();
201                         command += ConfigurationFile::GetInstance().Get_file_separator();
202                         command += "bbRegeneratePackageDoc";
203                 #if defined(WIN32)
204                         command += "\"";
205                 #endif
206
207                 command += " " + pack + " -q";
208             bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
209
210                 if ( ! system ( command.c_str() ) )
211                 {
212                         SetStatusText( _T("Done !"));
213                         /*
214                         wxMessageBox(_T("Done !"),_T("Regenerate package '")
215                                          +std2wx(pack)+_T("' doc"),
216                                          wxOK | wxICON_INFORMATION);
217                         */
218                 }
219                 else 
220                 {
221                         SetStatusText( _T("Done !"));
222                         wxString err(_T("An error occured while running '"));
223                         err +=  bbtk::std2wx(command) + _T("'");
224                         wxMessageBox(err,_T("Regenerate package doc"),wxOK | wxICON_ERROR);      
225             }
226         }
227
228         //================================================================
229         void wxGUIEditorGraphicBBS::DoRegenerateBoxesLists()
230         {
231                 SetStatusText( _T("Regenerating boxes lists ... please wait") );
232                 //BBTK_BUSY_CURSOR ;
233
234                 std::string command;
235                 #if defined(WIN32)
236                         command = "\"";
237                 #endif
238                         command += ConfigurationFile::GetInstance().Get_bin_path();
239                         command += ConfigurationFile::GetInstance().Get_file_separator();
240                         command += "bbRegenerateBoxesLists";
241
242                 #if defined(WIN32)
243                         command += "\"";
244                 #endif
245                         command += " -q";
246
247                 bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
248
249                 if ( ! system ( command.c_str() ) )
250                 {
251                         SetStatusText( _T("Done !"));
252                         /*
253                         wxMessageBox(_T("Done !"),_T("Regenerate boxes lists"),
254                                          wxOK | wxICON_INFORMATION);
255                         */
256                 }
257                 else 
258                 {
259                         SetStatusText( _T("Done !"));
260                         wxString err(_T("An error occured while running '"));
261                         err +=  bbtk::std2wx(command) + _T("'");
262                         wxMessageBox(err,_T("Regenerate boxes lists"),wxOK | wxICON_ERROR);      
263                 }
264         }
265         
266   //================================================================
267         void wxGUIEditorGraphicBBS::RegenerateAll()
268         {
269                 DoRegeneratePackageDoc("-a");   
270                 DoRegenerateBoxesLists();
271         }
272
273         //================================================================
274
275         wxAuiNotebook* wxGUIEditorGraphicBBS::getAuiNotebook()
276         {
277                 return _notebook;
278         }
279
280         //================================================================
281
282         void wxGUIEditorGraphicBBS::displayBlackBoxInfo(std::string packageName, std::string boxName)
283         {
284                 if(_actualPkgBrowserPkgName != packageName || _actualPkgBrowserBoxName != boxName )
285                 {
286                         _actualPkgBrowserPkgName = packageName;
287                         _actualPkgBrowserBoxName = boxName;
288
289                         BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxName);
290                         _pkgBrowser->WxGUIBlackBoxListUserOnSelected(descriptor.get());
291                 }
292         }
293
294         //=========================================================================
295
296         void wxGUIEditorGraphicBBS::editBlackBox(GBlackBoxModel *bbmodel)
297         {
298                 wxBlackBoxEditionDialog* dialog = new wxBlackBoxEditionDialog(this,bbmodel);
299                 dialog->Show();
300         }
301
302         //=========================================================================
303
304         void wxGUIEditorGraphicBBS::editDiagramParameters(wxVtkSceneManager* scene)
305         {
306                 wxBlackBoxEditionDialog* dialog = new wxBlackBoxEditionDialog(this,scene);
307                 dialog->Show();
308         }
309
310         //=========================================================================
311
312         void wxGUIEditorGraphicBBS::updateStatusBar(std::string textStatus)
313         {
314                 SetStatusText(crea::std2wx(textStatus));
315         }
316
317         //=========================================================================
318
319         void wxGUIEditorGraphicBBS::executeScript(std::string script)
320         {
321                 std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
322                 std::string dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
323                 std::string filename = dir + separator + "tmp_bbtk.bbs";
324                 
325                 ofstream tempFile;
326                 tempFile.open(filename.c_str());
327                 tempFile << script;
328                 tempFile.close();
329                 
330                 std::string command = "\"";
331
332                 #ifdef WIN32
333                         command += "\"";
334                 #endif
335
336                 //command += ConfigurationFile::GetInstance().Get_bin_path();
337                 command +="C:/temp/bbtkBIN/RelWithDebInfo//";
338
339                 #ifdef MACOSX
340                         command += separator + "bbi.app/Contents/MacOS/bbi\" ";
341                 #else 
342                         command += separator + "bbi\" ";
343                 #endif
344                 command += "\""+filename + "\"";
345
346                 #ifdef WIN32
347                         command += "\"";
348                 #endif
349
350                 command += " & ";
351                   
352                 printf ("RaC wxGUIEditorGraphicBBS::executeScript %s \n" , command.c_str() );
353                 system( command.c_str() );
354         }
355
356         //=========================================================================
357         // EVENT HANDLERS 
358         //=========================================================================
359
360         void wxGUIEditorGraphicBBS::OnCreateNewTab(wxCommandEvent& event)
361         {
362                 _tabsMgr->addNewTab();  
363                 refreshToolbar();
364         }
365
366         //=========================================================================
367
368         void wxGUIEditorGraphicBBS::OnExecuteActualDiagram(wxCommandEvent& event)
369         {
370                 std::string script = _tabsMgr->getActualDiagramScript();
371                 cout<<"RaC wxGUIEditorGraphicBBS::executeActualDiagram SCRIPT"<<endl;
372                 cout<<script<<endl;
373
374                 executeScript(script);          
375         }
376
377         //=========================================================================
378
379         /*****************************************************/
380         /* HANDLERS 
381         /*****************************************************/
382         void wxGUIEditorGraphicBBS::OnSaveActualDiagram(wxCommandEvent& event)
383         {
384                 wxFileDialog * saveFileDialog = new wxFileDialog(this,wxT("Save  actual diagram"),wxEmptyString,"NewDiagram","*.bbd",wxSAVE|wxOVERWRITE_PROMPT);
385                 if (saveFileDialog->ShowModal() == wxID_OK)
386                 {
387                         wxString fileName = saveFileDialog->GetPath();
388                         
389                         ofstream file;
390                         file.open(fileName.c_str());
391
392                         std::string content="";
393                         
394                         // writing file header
395                         content += "# ----------------------------------\n";
396                         content += "# - BBTKGEditor v 1.0 BBD BlackBox Diagram file\n";
397                         content += "# - ";
398                         content += fileName;
399                         content += "\n";
400                         content += "# ----------------------------------\n";
401                         content += "\n";
402                         content += "APP_START\n";
403                         _tabsMgr->saveActualDiagram(content);           
404                         content += "APP_END\n";
405                         file << content;
406                         file.close();
407                 }
408
409         }
410
411         //=========================================================================
412
413         void wxGUIEditorGraphicBBS::refreshToolbar()
414         {
415                 wxToolBar* toolbar = GetToolBar();
416                 if(_tabsMgr->isActualDiagramComplexBox())
417                 {
418                         toolbar->ToggleTool(ID_BTNCOMPLEXBOX,true);
419                 }
420                 else
421                 {
422                         toolbar->ToggleTool(ID_BTNCOMPLEXBOX,false);
423                 }
424
425                 bool temp = toolbar->GetToolState(ID_BTNCOMPLEXBOX);
426                 if(temp)
427                 {
428                         toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT,true);
429                         toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT,true);
430                 }
431                 else
432                 {
433                         toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT,false);
434                         toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT,false);
435                 }
436         }
437
438         //=========================================================================
439
440         void wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram(wxCommandEvent& event)
441         {
442                 wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Are you sure to delete all boxes of the diagram?"), wxT("Delete all boxes"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
443                 if(dial->ShowModal() == wxID_YES)
444                 {
445                         _tabsMgr->deleteAllBoxesActualDiagram();
446                 }
447         }
448
449         //=========================================================================
450
451         void wxGUIEditorGraphicBBS::OnCenterViewActualDiagram(wxCommandEvent& event)
452         {
453                 _tabsMgr->centerViewActualDiagram();
454         }
455
456         //=========================================================================
457
458         
459         void wxGUIEditorGraphicBBS::OnOpenDiagram(wxCommandEvent& event)
460         {
461                 wxFileDialog * openFileDialog = new wxFileDialog(this,wxT("Open diagram"),wxEmptyString,"","*.bbd",wxOPEN|wxFILE_MUST_EXIST);
462                 if (openFileDialog->ShowModal() == wxID_OK)
463                 {
464                         wxString fileName = openFileDialog->GetPath();
465                         
466                         ifstream inputStream;
467                         inputStream.open(fileName.c_str());
468
469                         _tabsMgr->addNewTab();
470                         _tabsMgr->loadDiagram(inputStream);
471                         
472                         inputStream.close();
473                         
474
475                 }
476         }
477
478         //=========================================================================
479
480         void wxGUIEditorGraphicBBS::OnClickBtnComplexBox(wxCommandEvent& event)
481         {
482                 wxToolBar* toolbar = GetToolBar();
483                 bool temp = toolbar->GetToolState(ID_BTNCOMPLEXBOX);
484                 if(temp)
485                 {
486                         toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT,true);
487                         toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT,true);
488                         _tabsMgr->setActualDiagramComplexBox(true);
489                 }
490                 else
491                 {
492                         toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT,false);
493                         toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT,false);
494                         _tabsMgr->setActualDiagramComplexBox(false);
495                 }
496                 
497         }
498
499         //=========================================================================
500
501         void wxGUIEditorGraphicBBS::OnAddComplexBoxInput(wxCommandEvent& event)
502         {
503         }
504
505         //=========================================================================
506
507         void wxGUIEditorGraphicBBS::OnAddComplexBoxOutput(wxCommandEvent& event)
508         {
509         }
510
511         //=========================================================================
512
513         void wxGUIEditorGraphicBBS::OnExit(wxCommandEvent& event)
514         {
515                 Close(true);
516         }
517
518         //=========================================================================
519
520 }  // EO namespace bbtk
521
522 // EOF
523