]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIPackageBrowser.cxx
515a707cdb1fd4f0156ab5d587b2ca597ee7ffab
[bbtk.git] / kernel / src / bbtkWxGUIPackageBrowser.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxGUIPackageBrowser.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/03/31 13:18:04 $
7   Version:   $Revision: 1.3 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*//**
18  * \brief Short description in one line
19  * 
20  * Long description which 
21  * can span multiple lines
22  */
23 /**
24  * \file 
25  * \brief 
26  */
27 /**
28  * \class bbtk::
29  * \brief 
30  */
31
32
33 #ifdef _USE_WXWIDGETS_
34
35 #define CHECKBOXVIEW 1
36
37 #include "bbtkWxGUIPackageBrowser.h"
38 #include "ThirdParty/wx/treemultictrl/wxTreeMultiCtrl.h"
39 #include "bbtkInterpreter.h"
40 #include "bbtkBlackBoxInputDescriptor.h"
41 #include "bbtkBlackBoxOutputDescriptor.h"
42 #include "bbtkWxBlackBox.h"
43 #include "wx/grid.h"
44 #include <wx/statline.h>
45
46
47 namespace bbtk
48 {
49   //================================================================
50   WxGUIPackageBrowserBlackBox::WxGUIPackageBrowserBlackBox(wxWindow* parent,
51                                                            WxGUIPackageBrowser* browser,
52                                                            BlackBoxDescriptor* descr) :
53     wxPanel(parent, -1),
54     mBrowser(browser),
55     mDescriptor(descr)
56   {
57     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
58     
59     wxStaticText* s = new wxStaticText(this,-1,
60                                        std2wx(descr->GetDescription()));
61     sizer->Add(s,0,wxGROW);
62     s = new wxStaticText(this,-1,
63                          std2wx(descr->GetAuthor()));
64     sizer->Add(s,0,wxGROW);
65     s = new wxStaticText(this,-1,
66                          std2wx(descr->GetCategory()));
67     sizer->Add(s,0,wxGROW);
68
69     std::vector<std::vector<std::string> > user_defined;
70     std::vector<std::vector<std::string> > ubb_defined;
71     std::vector<std::vector<std::string> > wxbb_defined;
72     
73     std::string titlecol("#BBBBFF");
74     std::string usercol("#FFFFFF");
75     std::string ubbcol("#DDFFFF");
76     std::string wxbbcol("#EEFFFF");
77
78    const BlackBoxDescriptor::InputDescriptorMapType& imap = 
79       descr->GetInputDescriptorMap();
80     BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
81     for ( in = imap.begin();  in != imap.end(); ++in ) 
82     {
83       // Skips system-defined inputs
84       std::string col(usercol);
85       int iotype = 0;
86       if (in->second->GetCreatorTypeInfo() == 
87           typeid(AtomicBlackBoxDescriptor))
88         {
89           col = ubbcol; 
90           iotype = 1;
91         }
92       else if (in->second->GetCreatorTypeInfo() == 
93                typeid(WxBlackBoxDescriptor))
94         {
95           col = wxbbcol; 
96           iotype = 2;
97         }
98
99       std::string name(in->second->GetName());
100       //Utilities::html_format(name);
101       
102       std::string type("<");
103       type += in->second->GetTypeName();    
104       type += ">";
105       //Utilities::html_format(type);
106       
107       std::string descr(in->second->GetDescription());
108       //Utilities::html_format(descr);
109       
110       std::vector<std::string> out;
111       out.push_back(name);
112       out.push_back(type);
113       out.push_back(descr);
114       /* = 
115         "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
116         +"\"><B><PRE> "+name+" </PRE></B></TD>"
117         + "<TD style='vertical-align: top;' bgcolor=\""+col
118         +"\"><I><PRE> "+type+" </PRE></I></TD>"
119         + "<TD style='vertical-align: top;' bgcolor=\""+col
120         +"\">"+descr+"</TD></TR>\n";
121       */
122
123       if (iotype==0) user_defined.push_back(out);
124       else if (iotype==1) ubb_defined.push_back(out);
125       else if (iotype==2) wxbb_defined.push_back(out);
126       
127     }
128
129  
130     wxBoxSizer *inputs = 
131       new wxStaticBoxSizer
132       ( new wxStaticBox(this, wxID_ANY, _T("Inputs")), wxVERTICAL );    
133     wxFlexGridSizer* grid = new wxFlexGridSizer(3);
134
135     wxTextAttr ionameattr(*wxRED,*wxBLACK);
136     wxTextAttr iotypeattr(*wxBLACK,*wxRED);
137
138     int n = 0;
139     std::vector<std::vector<std::string> >::iterator hi;
140     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
141       {
142         /*
143         wxTextCtrl* t = new wxTextCtrl(this,-1,_T("")
144                                        ,wxDefaultPosition
145                                        ,wxDefaultSize 
146                                        ,wxTE_READONLY
147                                        );
148         t->SetDefaultStyle(ionameattr);
149         t->AppendText( std2wx((*hi)[0]) );
150         grid->Add(t);
151         
152         t =  new wxTextCtrl(this,-1,_T("")
153                                        ,wxDefaultPosition
154                                        ,wxDefaultSize 
155                                        ,wxTE_READONLY
156                                        );
157         t->SetDefaultStyle(iotypeattr);
158         t->AppendText( std2wx((*hi)[1]) );
159         grid->Add(t);
160         */
161         
162         grid->Add( new wxStaticText(this,-1,std2wx((*hi)[0])));
163         grid->Add( new wxStaticText(this,-1,std2wx((*hi)[1])));
164         grid->Add( new wxStaticText(this,-1,std2wx((*hi)[2])));
165         //      grid->SetCellValue( n, 0, std2wx((*hi)[0]) );
166         //      grid->SetReadOnly( n, 0 );
167      }
168     // grid->Add( new wxStaticLine(this,-1));
169    for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
170       {
171         grid->Add( new wxStaticText(this,-1,
172                                     std2wx((*hi)[0])));
173         grid->Add( new wxStaticText(this,-1,
174                                     std2wx((*hi)[1])));
175         grid->Add( new wxStaticText(this,-1,
176                                     std2wx((*hi)[2])));
177       }
178     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
179       {
180         grid->Add( new wxStaticText(this,-1,
181                                     std2wx((*hi)[0])));
182         grid->Add( new wxStaticText(this,-1,
183                                     std2wx((*hi)[1])));
184         grid->Add( new wxStaticText(this,-1,
185                                     std2wx((*hi)[2])));
186       }
187
188     inputs->Add(grid,0,wxGROW);
189     sizer->Add(inputs,0,wxGROW);
190
191     // Outputs
192     user_defined.clear();
193     ubb_defined.clear();
194     wxbb_defined.clear();
195     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
196       descr->GetOutputDescriptorMap();
197     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator out;
198     for ( out = omap.begin();  out != omap.end(); ++out ) 
199     {
200       // Skips system-defined inputs
201       std::string col(usercol);
202       int iotype = 0;
203       if (out->second->GetCreatorTypeInfo() == 
204           typeid(AtomicBlackBoxDescriptor))
205         {
206           col = ubbcol; 
207           iotype = 1;
208         }
209       else if (out->second->GetCreatorTypeInfo() == 
210                typeid(WxBlackBoxDescriptor))
211         {
212           col = wxbbcol; 
213           iotype = 2;
214         }
215
216       std::string name(out->second->GetName());
217       //Utilities::html_format(name);
218       
219       std::string type("<");
220       type += out->second->GetTypeName();    
221       type += ">";
222       //Utilities::html_format(type);
223       
224       std::string descr(out->second->GetDescription());
225       //Utilities::html_format(descr);
226       
227       std::vector<std::string> out;
228       out.push_back(name);
229       out.push_back(type);
230       out.push_back(descr);
231       /* = 
232         "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
233         +"\"><B><PRE> "+name+" </PRE></B></TD>"
234         + "<TD style='vertical-align: top;' bgcolor=\""+col
235         +"\"><I><PRE> "+type+" </PRE></I></TD>"
236         + "<TD style='vertical-align: top;' bgcolor=\""+col
237         +"\">"+descr+"</TD></TR>\n";
238       */
239
240       if (iotype==0) user_defined.push_back(out);
241       else if (iotype==1) ubb_defined.push_back(out);
242       else if (iotype==2) wxbb_defined.push_back(out);
243       
244     }
245
246     wxBoxSizer *outputs = 
247       new wxStaticBoxSizer
248       ( new wxStaticBox(this, wxID_ANY, _T("Outputs")), wxVERTICAL );    
249     wxFlexGridSizer* ogrid = new wxFlexGridSizer(3);
250
251
252     n = 0;
253     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
254       {
255         ogrid->Add( new wxStaticText(this,-1,
256                                     std2wx((*hi)[0])));
257         ogrid->Add( new wxStaticText(this,-1,
258                                     std2wx((*hi)[1])));
259         ogrid->Add( new wxStaticText(this,-1,
260                                     std2wx((*hi)[2])));
261         //      grid->SetCellValue( n, 0, std2wx((*hi)[0]) );
262         //      grid->SetReadOnly( n, 0 );
263      }
264     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
265       {
266         ogrid->Add( new wxStaticText(this,-1,
267                                     std2wx((*hi)[0])));
268         ogrid->Add( new wxStaticText(this,-1,
269                                     std2wx((*hi)[1])));
270         ogrid->Add( new wxStaticText(this,-1,
271                                     std2wx((*hi)[2])));
272       }
273     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
274       {
275         ogrid->Add( new wxStaticText(this,-1,
276                                     std2wx((*hi)[0])));
277         ogrid->Add( new wxStaticText(this,-1,
278                                     std2wx((*hi)[1])));
279         ogrid->Add( new wxStaticText(this,-1,
280                                     std2wx((*hi)[2])));
281       }
282
283     outputs->Add(ogrid,0,wxGROW);
284     sizer->Add(outputs,0,wxGROW);
285
286   
287     SetSizer(sizer);
288     SetAutoLayout(true);
289     Layout();
290   }
291   //================================================================
292
293
294   //================================================================
295   WxGUIPackageBrowserBlackBox::~WxGUIPackageBrowserBlackBox()
296   {
297   }
298   //================================================================
299
300
301   //================================================================
302   WxGUIPackageBrowser::WxGUIPackageBrowser( wxWindow *parent,
303                                     WxGUIPackageBrowserUser* user )
304     : wxPanel(parent, -1),
305       mUser(user),
306       mInterpreter(0)
307   {
308
309     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
310     mTree = new wxTreeMultiCtrl(this, -1);
311     
312     mTree->SetBackgroundColour(*wxWHITE);
313     sizer->Add(mTree,1,wxGROW);
314
315 #ifndef LINUX
316     wxTreeMultiWindowInfo wndinfo(wxTMC_BG_ADJUST_CNT, 8, 0);
317 #else
318     wxTreeMultiWindowInfo wndinfo(wxTMC_BG_ADJUST_ALL, 8, 0);
319 #endif
320     
321     // make it a bit bigger
322     wxFont somefont;
323     somefont = mTree->GetCaptionFont();
324     somefont.SetFamily(wxMODERN);
325     somefont.SetWeight(wxBOLD);
326     somefont.SetPointSize(somefont.GetPointSize()+1);
327     mTree->SetCaptionFont(somefont);
328
329     mTree->SetSpacingY(1);
330     // mTree->SetCheckboxView(true);
331
332     SetSizer(sizer);
333     SetAutoLayout(true);
334     Layout();
335     
336   }
337   //================================================================
338
339   //================================================================
340   WxGUIPackageBrowser::~WxGUIPackageBrowser()
341   {
342     std::cout << "del interpreter" << std::endl;
343     if (mInterpreter) delete mInterpreter;
344     std::cout << "ok" << std::endl;
345   }
346   //================================================================
347
348   //================================================================
349   void WxGUIPackageBrowser::IncludeAll()
350   {
351     if (!mInterpreter) mInterpreter = new bbtk::Interpreter();
352     mInterpreter->SetCommandLine(true);
353     mInterpreter->InterpretLine("include *");
354
355     Factory* F = mInterpreter->GetExecuter()->GetFactory();
356     BuildFromFactory(F);
357   }
358   //================================================================
359   //================================================================
360   
361   void WxGUIPackageBrowser::BuildFromFactory(Factory* F)
362   {
363 #ifndef LINUX
364     wxTreeMultiWindowInfo wndinfo(wxTMC_BG_ADJUST_CNT, 8, 0);
365 #else
366     wxTreeMultiWindowInfo wndinfo(wxTMC_BG_ADJUST_ALL, 8, 0);
367 #endif
368     mTree->DeleteAllItems();    
369     const Factory::PackageMapType& M = F->GetPackageMap();
370     Factory::PackageMapType::const_iterator i;
371     for (i=M.begin();i!=M.end();++i)
372       {
373         Package* P = i->second.mPackage;
374         wxString packname = std2wx(P->GetName());
375         if (packname==_T("user")) continue;
376         wxTreeMultiItem pack = mTree->AddRoot(packname,packname);
377
378         wxString packinfo = std2wx(P->GetDescription());
379         packinfo += _T(" - by ")+ std2wx(P->GetAuthor());
380         packinfo += _T(" - v. ")+ std2wx(P->GetVersion());
381         mTree->AppendWindow(pack, 
382                             new wxStaticText(mTree,
383                                              -1,packinfo));
384         Package::BlackBoxMapType::iterator j;
385         for (j=P->GetBlackBoxMap().begin();
386              j!=P->GetBlackBoxMap().end();
387              ++j)
388           {
389             wxString boxname = std2wx(j->second->GetTypeName());
390             wxTreeMultiItem box = mTree->AppendNode(pack,boxname,boxname);
391             WxGUIPackageBrowserBlackBox* boxdescr 
392               = new WxGUIPackageBrowserBlackBox(mTree,this,j->second);
393             mTree->AppendWindow(box, boxdescr, _T(""), 
394                                 wndinfo, wxTMC_SPAN_WIDTH);
395
396           }
397       }
398     mTree->CollapseNodes(true);
399
400     // Test exclude 
401     wxTreeMultiItem item = mTree->FindItem(_T("Add"));
402     if(item.IsOk()) mTree->Exclude(item);
403   }
404   //================================================================
405
406   //================================================================
407   WxGUIPackageBrowserWindow::WxGUIPackageBrowserWindow( wxWindow *parent, 
408                                                     wxString title, 
409                                                     wxSize size)
410     : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
411   {     
412     
413     
414     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
415
416     mBrowser = new WxGUIPackageBrowser(this);
417     mBrowser->IncludeAll();
418     sizer->Add(mBrowser,1,wxGROW);
419     
420     //    WxGUICommand* com = new WxGUICommand(this,this);
421     //    sizer->Add(com);
422     
423     SetSizer(sizer);
424
425     // Creates and sets the parent window of all bbtk windows
426     wxWindow* top = new wxPanel(this,-1);
427     top->Hide();
428     
429     Wx::SetTopWindow(top);
430    
431     SetAutoLayout(true);
432     Layout();
433   }
434   //================================================================
435
436   //================================================================
437   WxGUIPackageBrowserWindow::~WxGUIPackageBrowserWindow()
438   {
439   }
440   //================================================================
441
442 } // namespace bbtk
443
444
445 #endif //_USE_WXWIDGETS_