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