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