]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/bbtkwxGUIEditorGraphicBBS.cxx
Now it works when a tab is changed ...
[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
95                 wxToolBar  *_toolbar = new wxToolBar(this, wxID_ANY);
96
97                 //Adds a tool btn to the toolbar
98                 _toolbar->AddTool(ID_NEW,_T("New"),bmp_new, wxNullBitmap, wxITEM_NORMAL,_T("New tab"), _T("Create a new panel tab"));
99                 _toolbar->AddTool(ID_OPEN,_T("Open diagram"),bmp_open, wxNullBitmap, wxITEM_NORMAL,_T("Open a diagram"), _T("Open a diagram"));
100                 _toolbar->AddTool(ID_SAVE,_T("Save diagram"),bmp_save, wxNullBitmap, wxITEM_NORMAL,_T("Saves actual diagram"), _T("Saves actual diagram"));
101                 _toolbar->AddTool(ID_RUN,_T("Run"),bmp_run, wxNullBitmap, wxITEM_NORMAL,_T("Execute actual diagram"), _T("Execute actual diagram"));
102                 _toolbar->AddTool(ID_DELETEALL,_T("Delete all"),bmp_delete, wxNullBitmap, wxITEM_NORMAL,_T("Delete all boxes"), _T("Delete all boxes"));
103                 _toolbar->AddTool(ID_CENTERVIEW,_T("Center view"),bmp_centerview, wxNullBitmap, wxITEM_NORMAL,_T("Center view"), _T("Center view"));
104                 
105
106                 _toolbar->SetMargins( 2, 2 );
107                 _toolbar->Realize();
108                 SetToolBar(_toolbar);
109
110                 // connect command event handlers
111                 Connect(ID_NEW,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateNewTab));
112                 Connect(ID_OPEN,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnOpenDiagram));
113                 Connect(ID_SAVE,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualDiagram));
114                 Connect(ID_RUN,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExecuteActualDiagram));
115                 Connect(ID_DELETEALL,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram));
116                 Connect(ID_CENTERVIEW,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCenterViewActualDiagram));
117
118         }
119         //=========================================================================
120         void wxGUIEditorGraphicBBS::initHelpHTMLBrowser()
121         {
122                 //TO FIX THE PATH OF BBTK BIN  IN ORDER TO USE REGENERATE EXECUTABLES
123                 //RegenerateAll();
124                 _helpHtmlBrowser = new WxGUIHtmlBrowser(this,wxSize(200,0));
125                 _frameAUIMgr->AddPane(_helpHtmlBrowser,wxAuiPaneInfo().Right().MinSize(200,200));       
126         }
127
128         //=========================================================================
129         void wxGUIEditorGraphicBBS::initMenu()
130         {
131                 // create a menu bar
132                 wxMenu *fileMenu = new wxMenu;
133
134                 // the "About" item should be in the help menu
135                 wxMenu *helpMenu = new wxMenu;
136                 helpMenu->Append(wxID_ABOUT, _T("&About..."), _T("About"));
137                 fileMenu->Append(wxID_EXIT, _T("E&xit"), _T("Exit"));
138
139                 Connect(wxID_EXIT,wxEVT_COMMAND_MENU_SELECTED,wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExit));
140
141                 // now append the freshly created menu to the menu bar...
142                 wxMenuBar *menuBar = new wxMenuBar();
143                 menuBar->Append(fileMenu, _T("&File"));
144                 menuBar->Append(helpMenu, _T("&Help"));
145
146                 // attach this menu bar to the frame
147                 SetMenuBar(menuBar);
148         }
149
150         //=========================================================================
151         void wxGUIEditorGraphicBBS::initTabPanelsManager()
152         {
153                 _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);
154                 _tabsMgr = new wxTabPanelsManager(this);
155                 _frameAUIMgr->AddPane(_notebook,wxAuiPaneInfo().CenterPane());  
156
157         }
158         
159         //=========================================================================
160         void wxGUIEditorGraphicBBS::initPackageBrowser()
161         {
162                 _pkgBrowser = new WxGUIPackageBrowser2(this);
163                 _pkgBrowser->IncludeAll();
164                 _frameAUIMgr->AddPane(_pkgBrowser,wxAuiPaneInfo().Left().MinSize(200,200).CloseButton(false));          
165         }
166
167         //================================================================
168
169         void wxGUIEditorGraphicBBS::DoRegeneratePackageDoc( const std::string& pack )
170         {
171                 std::string mess("Regenerating doc for package '");
172                 if (pack!="-a"){
173                         mess += pack + "'";
174                 }
175                 else
176                 {
177                         mess = "Regenerating doc for all packages";
178                 }
179                 
180                 mess += " ... please wait";             
181
182                 SetStatusText( std2wx(mess) );
183
184                 //BBTK_BUSY_CURSOR;
185
186                 std::string command;
187                 #if defined(WIN32)
188                         command = "\"";
189                 #endif
190                         command += ConfigurationFile::GetInstance().Get_bin_path();
191                         command += ConfigurationFile::GetInstance().Get_file_separator();
192                         command += "bbRegeneratePackageDoc";
193                 #if defined(WIN32)
194                         command += "\"";
195                 #endif
196
197                 command += " " + pack + " -q";
198             bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
199
200                 if ( ! system ( command.c_str() ) )
201                 {
202                         SetStatusText( _T("Done !"));
203                         /*
204                         wxMessageBox(_T("Done !"),_T("Regenerate package '")
205                                          +std2wx(pack)+_T("' doc"),
206                                          wxOK | wxICON_INFORMATION);
207                         */
208                 }
209                 else 
210                 {
211                         SetStatusText( _T("Done !"));
212                         wxString err(_T("An error occured while running '"));
213                         err +=  bbtk::std2wx(command) + _T("'");
214                         wxMessageBox(err,_T("Regenerate package doc"),wxOK | wxICON_ERROR);      
215             }
216         }
217
218         //================================================================
219         void wxGUIEditorGraphicBBS::DoRegenerateBoxesLists()
220         {
221                 SetStatusText( _T("Regenerating boxes lists ... please wait") );
222                 //BBTK_BUSY_CURSOR ;
223
224                 std::string command;
225                 #if defined(WIN32)
226                         command = "\"";
227                 #endif
228                         command += ConfigurationFile::GetInstance().Get_bin_path();
229                         command += ConfigurationFile::GetInstance().Get_file_separator();
230                         command += "bbRegenerateBoxesLists";
231
232                 #if defined(WIN32)
233                         command += "\"";
234                 #endif
235                         command += " -q";
236
237                 bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
238
239                 if ( ! system ( command.c_str() ) )
240                 {
241                         SetStatusText( _T("Done !"));
242                         /*
243                         wxMessageBox(_T("Done !"),_T("Regenerate boxes lists"),
244                                          wxOK | wxICON_INFORMATION);
245                         */
246                 }
247                 else 
248                 {
249                         SetStatusText( _T("Done !"));
250                         wxString err(_T("An error occured while running '"));
251                         err +=  bbtk::std2wx(command) + _T("'");
252                         wxMessageBox(err,_T("Regenerate boxes lists"),wxOK | wxICON_ERROR);      
253                 }
254         }
255         
256   //================================================================
257         void wxGUIEditorGraphicBBS::RegenerateAll()
258         {
259                 DoRegeneratePackageDoc("-a");   
260                 DoRegenerateBoxesLists();
261         }
262
263         //================================================================
264
265         wxAuiNotebook* wxGUIEditorGraphicBBS::getAuiNotebook()
266         {
267                 return _notebook;
268         }
269
270         //================================================================
271
272         void wxGUIEditorGraphicBBS::displayBlackBoxInfo(std::string packageName, std::string boxName)
273         {
274                 if(_actualPkgBrowserPkgName != packageName || _actualPkgBrowserBoxName != boxName )
275                 {
276                         _actualPkgBrowserPkgName = packageName;
277                         _actualPkgBrowserBoxName = boxName;
278
279                         BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxName);
280                         _pkgBrowser->WxGUIBlackBoxListUserOnSelected(descriptor.get());
281                 }
282         }
283
284         //=========================================================================
285
286         void wxGUIEditorGraphicBBS::editBlackBox(GBlackBoxModel *bbmodel)
287         {
288                 wxBlackBoxEditionDialog* dialog = new wxBlackBoxEditionDialog(this,bbmodel);
289                 dialog->Show();
290         }
291
292         //=========================================================================
293
294         void wxGUIEditorGraphicBBS::editDiagramParameters(wxVtkSceneManager* scene)
295         {
296                 wxBlackBoxEditionDialog* dialog = new wxBlackBoxEditionDialog(this,scene);
297                 dialog->Show();
298         }
299
300         //=========================================================================
301
302         void wxGUIEditorGraphicBBS::updateStatusBar(std::string textStatus)
303         {
304                 SetStatusText(crea::std2wx(textStatus));
305         }
306
307         //=========================================================================
308
309         void wxGUIEditorGraphicBBS::executeScript(std::string script)
310         {
311                 std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
312                 std::string dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
313                 std::string filename = dir + separator + "tmp_bbtk.bbs";
314                 
315                 ofstream tempFile;
316                 tempFile.open(filename.c_str());
317                 tempFile << script;
318                 tempFile.close();
319                 
320                 std::string command = "\"";
321
322                 #ifdef WIN32
323                         command += "\"";
324                 #endif
325
326                 //command += ConfigurationFile::GetInstance().Get_bin_path();
327                 command +="C:/temp/bbtkBIN/RelWithDebInfo//";
328
329                 #ifdef MACOSX
330                         command += separator + "bbi.app/Contents/MacOS/bbi\" ";
331                 #else 
332                         command += separator + "bbi\" ";
333                 #endif
334                 command += "\""+filename + "\"";
335
336                 #ifdef WIN32
337                         command += "\"";
338                 #endif
339
340                 command += " & ";
341                   
342                 printf ("RaC wxGUIEditorGraphicBBS::executeScript %s \n" , command.c_str() );
343                 system( command.c_str() );
344         }
345
346         //=========================================================================
347         // EVENT HANDLERS 
348         //=========================================================================
349
350         void wxGUIEditorGraphicBBS::OnCreateNewTab(wxCommandEvent& event)
351         {
352                 _tabsMgr->addNewTab();          
353         }
354
355         //=========================================================================
356
357         void wxGUIEditorGraphicBBS::OnExecuteActualDiagram(wxCommandEvent& event)
358         {
359                 std::string script = _tabsMgr->getActualDiagramScript();
360                 cout<<"RaC wxGUIEditorGraphicBBS::executeActualDiagram SCRIPT"<<endl;
361                 cout<<script<<endl;
362
363                 executeScript(script);          
364         }
365
366         //=========================================================================
367
368         /*****************************************************/
369         /* HANDLERS 
370         /*****************************************************/
371         void wxGUIEditorGraphicBBS::OnSaveActualDiagram(wxCommandEvent& event)
372         {
373                 wxFileDialog * saveFileDialog = new wxFileDialog(this,wxT("Save  actual diagram"),wxEmptyString,"NewDiagram","*.bbd",wxSAVE|wxOVERWRITE_PROMPT);
374                 if (saveFileDialog->ShowModal() == wxID_OK)
375                 {
376                         wxString fileName = saveFileDialog->GetPath();
377                         
378                         ofstream file;
379                         file.open(fileName.c_str());
380
381                         std::string content="";
382                         
383                         // writing file header
384                         content += "# ----------------------------------\n";
385                         content += "# - BBTKGEditor v 1.0 BBD BlackBox Diagram file\n";
386                         content += "# - ";
387                         content += fileName;
388                         content += "\n";
389                         content += "# ----------------------------------\n";
390                         content += "\n";
391                         content += "APP_START\n";
392                         _tabsMgr->saveActualDiagram(content);           
393                         content += "APP_END\n";
394                         file << content;
395                         file.close();
396                 }
397
398         }
399
400         //=========================================================================
401
402         void wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram(wxCommandEvent& event)
403         {
404                 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);
405                 if(dial->ShowModal() == wxID_YES)
406                 {
407                         _tabsMgr->deleteAllBoxesActualDiagram();
408                 }
409         }
410
411         //=========================================================================
412
413         void wxGUIEditorGraphicBBS::OnCenterViewActualDiagram(wxCommandEvent& event)
414         {
415                 _tabsMgr->centerViewActualDiagram();
416         }
417
418         //=========================================================================
419
420         
421         void wxGUIEditorGraphicBBS::OnOpenDiagram(wxCommandEvent& event)
422         {
423                 wxFileDialog * openFileDialog = new wxFileDialog(this,wxT("Open diagram"),wxEmptyString,"","*.bbd",wxOPEN|wxFILE_MUST_EXIST);
424                 if (openFileDialog->ShowModal() == wxID_OK)
425                 {
426                         wxString fileName = openFileDialog->GetPath();
427                         
428                         ifstream inputStream;
429                         inputStream.open(fileName.c_str());
430
431                         _tabsMgr->addNewTab();
432                         _tabsMgr->loadDiagram(inputStream);
433                         
434                         inputStream.close();
435                         
436
437                 }
438         }
439
440         //=========================================================================
441
442         void wxGUIEditorGraphicBBS::OnExit(wxCommandEvent& event)
443         {
444                 Close(true);
445         }
446
447         //=========================================================================
448
449 }  // EO namespace bbtk
450
451 // EOF
452