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