]> Creatis software - creaImageIO.git/blob - src2/creaImageIOWxGimmickView.cpp
*** empty log message ***
[creaImageIO.git] / src2 / creaImageIOWxGimmickView.cpp
1 #include <creaImageIOWxGimmickView.h>
2 #include <creaImageIOWxTreeView.h>
3 #include <creaImageIOSystem.h>
4 using namespace crea;
5 // Icons
6 #include "icons/accept.xpm"
7 #include "icons/add.xpm"
8 #include "icons/folder-down.xpm"
9 #include "icons/page-down.xpm"
10 #include "icons/remove.xpm"
11 #include "icons/database-add.xpm"
12 #include "icons/create-database.xpm"
13 #include "icons/help.xpm"
14 #include "icons/synchronize.xpm"
15 #include "icons/settings.xpm"
16 #include "icons/tools.xpm"
17
18 #include <wx/imaglist.h>
19 #include <wx/popupwin.h>
20 #include<boost/filesystem/operations.hpp>
21
22 namespace creaImageIO
23 {
24    
25   //======================================================================
26   // The ids of the different tools
27   enum
28     {
29           TOOL_CREATEDB_ID = 1,
30       TOOL_ADDFILES_ID = 2,
31       TOOL_ADDDIR_ID = 3,
32           TOOL_ADDDATABASE_ID = 4,
33       TOOL_REMOVE_ID = 5,
34           TOOL_SYNCHRONIZE_ID = 6,
35       TOOL_HELP_ID = 7,
36           TOOL_SETTINGS_ID = 8,
37           TOOL_TOOLS_ID = 9
38     };
39   //======================================================================
40
41   //================================================================
42   // 
43   const int icon_number = 11;
44   // Icon ids
45   typedef enum
46     {
47           Icon_create_database,
48       Icon_accept,
49       Icon_add,
50       Icon_folder_down,
51       Icon_page_down,
52           Icon_database_add,
53       Icon_remove,
54           Icon_synchronize,
55       Icon_help,
56           Icon_settings,
57       Icon_tools
58     }
59     icon_id;
60   //================================================================
61
62   //================================================================
63   /*
64   const icon_id Icon[5] = { Icon_Database,  
65                             Icon_Patient,
66                             Icon_Study,
67                             Icon_Series,
68                             Icon_Image };
69   */
70   //================================================================
71
72
73   //======================================================================
74   // CTor
75   WxGimmickView::WxGimmickView(Gimmick* gimmick,
76                                wxWindow *parent, 
77                                const wxWindowID id,
78                                const wxPoint& pos, const wxSize& size,
79                                int min_dim,
80                                    int max_dim,
81                                int number_of_threads)
82     : wxPanel(parent,id,pos,size),
83       GimmickView(gimmick, number_of_threads),
84       mProgressDialog(0),
85       mConstructed(false)
86   {
87     GimmickDebugMessage(1,"WxGimmickView::WxGimmickView"
88                         <<std::endl);
89     // Sets the current directory to the home dir
90     mCurrentDirectory =  std2wx(gimmick->GetHomeDirectory());
91
92      // Connect the AddProgress callback
93     gimmick->ConnectAddProgressObserver
94       ( boost::bind( &WxGimmickView::OnAddProgress , this, _1 ) );
95
96     // Create the list of icons (mIcon)
97     CreateIconList();
98
99     // Global sizer
100     wxBoxSizer  *sizer = new wxBoxSizer(wxVERTICAL);
101
102     // Create the tool bar
103     CreateToolBar(); 
104     sizer->Add( mToolBar ,0, wxGROW  ,0);
105
106     // Split part below toolbar into notebook for views and panel
107     // for preview, messages...
108     mSplitter = new wxSplitterWindow( this , -1);
109  
110    
111     // Notebook
112     mNotebook = new wxNotebook(mSplitter,
113                                -1,wxDefaultPosition, wxDefaultSize, 0);
114
115     //Gimmick
116     mGimmick=gimmick;
117
118       
119     mSelectionMaxDimension= max_dim;
120     mSelectionMinDimension= min_dim;
121     
122     // Create the views
123     CreateTreeViews();
124
125     // Bottom panel 
126     mBottomPanel = new wxPanel(mSplitter,-1);
127     
128           wxBoxSizer    *bottom_sizer = new wxBoxSizer(wxVERTICAL); //HORIZONTAL);
129     
130     
131     // Previewer
132     mViewer = new WxViewer(mBottomPanel, wxID_ANY, wxT("Gimmick! Viewer"),wxDefaultPosition, wxDefaultSize );
133         pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
134         mViewer->SetImageVector(pointers);
135         mViewer->StartPlayer();
136
137
138     bottom_sizer->Add(mViewer,1,wxGROW,1);
139     //    mViewer->Show();
140
141           mText = new wxStaticText(mBottomPanel, wxID_ANY, wxT("Welcome to Gimmick!"));
142           bottom_sizer->Add(mText,0,wxGROW,0);
143
144           
145           
146     mBottomPanel->SetSizer(bottom_sizer);
147
148     // Splitting
149     int hsize = size.GetHeight();
150
151     int top_minsize = 450;
152     int bottom_minsize = 50;
153
154     mSplitter->SetMinimumPaneSize( bottom_minsize );
155     mSplitter->SplitHorizontally( mNotebook, mBottomPanel, 
156                                   top_minsize);
157
158     sizer->Add( mSplitter,1,wxGROW  ,0);
159
160
161     SetSizer( sizer );     
162     SetAutoLayout(true);
163     Layout();
164     
165     mConstructed = true;
166   }
167   //======================================================================
168
169   //======================================================================
170   /// Destructor
171   WxGimmickView::~WxGimmickView()
172   {
173         // stop the viewer before application exit.
174         mViewer->StopPlayer();
175     GimmickDebugMessage(1,"WxGimmickView::~WxGimmickView"
176                         <<std::endl);
177   }
178   //======================================================================
179   
180   //======================================================================
181   /// Creates the tool bar
182   void WxGimmickView::CreateToolBar()
183   {
184     long style = wxTB_HORIZONTAL | wxNO_BORDER | wxTB_TEXT;
185     mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize,
186                              style);
187
188         mToolAddFile = mToolBar->AddTool( TOOL_CREATEDB_ID, 
189                                       _T("Create database"),
190                                       mIcon->GetBitmap(Icon_create_database),
191                                       _T("Create DB from an Attributes Descriptor file")
192                                       );
193     mToolAddFile = mToolBar->AddTool( TOOL_ADDFILES_ID, 
194                                       _T("Add file(s)"),
195                                       mIcon->GetBitmap(Icon_page_down),
196                                       _T("Add one or more file to database")
197                                       );
198     mToolAddDir = mToolBar->AddTool( TOOL_ADDDIR_ID, 
199                                       _T("Add folder"),
200                                       mIcon->GetBitmap(Icon_folder_down),
201                                       _T("Add the content of a folder to database")
202                                       );
203         mToolAddDatabase = mToolBar->AddTool( TOOL_ADDDATABASE_ID, 
204                                       _T("Open database"),
205                                       mIcon->GetBitmap(Icon_database_add),
206                                       _T("Open a local or distant database")
207                                       );
208     mToolRemove = mToolBar->AddTool( TOOL_REMOVE_ID, 
209                                       _T("Remove"),
210                                       mIcon->GetBitmap(Icon_remove),
211                                       _T("Remove selected items")
212                                       );
213     mToolSynchronize = mToolBar->AddTool( TOOL_SYNCHRONIZE_ID, 
214                                       _T("Synchronize"),
215                                       mIcon->GetBitmap(Icon_synchronize),
216                                       _T("Synchronizes the database with disk")
217                                       );
218         mToolHelp = mToolBar->AddTool( TOOL_HELP_ID, 
219                                       _T("Help"),
220                                       mIcon->GetBitmap(Icon_help),
221                                       _T("Open help window")
222                                       );
223         mToolSettings = mToolBar->AddTool( TOOL_SETTINGS_ID, 
224                                       _T("System settings"),
225                                       mIcon->GetBitmap(Icon_settings),
226                                       _T("Allows the modification of various system settings")
227                                       );
228         mToolTools = mToolBar->AddTool( TOOL_TOOLS_ID, 
229                                       _T("Tools"),
230                                       mIcon->GetBitmap(Icon_tools),
231                                       _T("Applies tools to images")
232                                       );
233     //const wxBitmap& bitmap1, const wxString& shortHelpString = "", wxItemKind kind = wxITEM_NORMAL)
234
235     mToolBar->Realize();
236   }
237   //======================================================================
238
239  
240   //======================================================================
241   /// Create the tree view for TreeHandler provided
242   void WxGimmickView::CreateTreeView( TreeHandler* h)
243   {
244     std::string name(h->GetTree().GetAttribute("Name"));
245     GimmickMessage(2,"Creating the tree view for '"<<
246                    name<<"'"<<std::endl);
247     // Create the WxTreeView
248     WxTreeView* view = new WxTreeView(h,this,mNotebook,-1);
249
250     // TO DO : TEST THAT A VIEW WITH SAME NAME IS NOT
251     // ALREADY IN THE MAP
252     GetTreeViewMap()[name] = view;
253
254     // Add Notebook page
255     mNotebook->AddPage( view, crea::std2wx(name) );
256         
257   }
258
259   //======================================================================
260   void WxGimmickView::GetSelectedImages(std::vector<vtkImageData*>& s, int dim)
261   {
262         std::vector<std::string> files;
263         GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(files);
264         ReadImagesNotThreaded(s,files,dim);
265   }
266   //======================================================================
267
268   //======================================================================
269   void WxGimmickView::GetSelectedFiles(std::vector<std::string>& s)
270   {
271         GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(s);
272   }
273   //======================================================================
274
275   //======================================================================
276   void WxGimmickView::GetImages(int dim, 
277                                 const std::vector<std::string>& files, 
278                                 std::vector<vtkImageData*>& s)
279   {
280         ReadImagesNotThreaded(s,files,dim);
281   }
282   //======================================================================
283
284
285   //=================================================
286   void WxGimmickView::CreateIconList()
287   {
288     // Size of the icons;
289     int size = 16;
290
291     wxIcon icons[20];
292     // should correspond to Icon_xxx enum
293     icons[Icon_accept] = wxIcon(accept_xpm);
294     icons[Icon_add] = wxIcon(add_xpm);
295     icons[Icon_folder_down] = wxIcon(folder_down_xpm);
296     icons[Icon_page_down] = wxIcon(page_down_xpm);
297     icons[Icon_remove] = wxIcon(remove_xpm);
298     icons[Icon_database_add] = wxIcon(database_add_xpm);
299     icons[Icon_help] = wxIcon(help_xpm);
300         icons[Icon_synchronize] = wxIcon(synchronize_xpm);
301         icons[Icon_create_database] = wxIcon(create_database_xpm);
302         icons[Icon_settings] = wxIcon(settings_xpm);
303         icons[Icon_tools] = wxIcon(tools_xpm);
304
305     //   unsigned int NbIcons = 8;
306     // Make an image list containing small icons
307     mIcon = new wxImageList(size,size,true);
308     
309     // Make all icons the same size = size of the first one
310     int sizeOrig = icons[0].GetWidth();
311     for ( size_t i = 0; i < icon_number; i++ )
312       {
313         if ( size == sizeOrig )
314           {
315             mIcon->Add(icons[i]);
316           }
317         else
318           {
319             mIcon->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
320           }
321       }
322   }
323   //=================================================
324
325
326   //=================================================
327   void WxGimmickView::OnAddFiles(wxCommandEvent& event)
328   {
329     mViewer->StopPlayer();
330    long style = wxOPEN | wxFILE_MUST_EXIST | wxFD_MULTIPLE;
331     std::string wc("*.*");
332     wxFileDialog* FD = new wxFileDialog( 0, 
333                                          _T("Select file"),
334                                          _T(""),
335                                          _T(""),
336                                          crea::std2wx(wc),
337                                          style,
338                                          wxDefaultPosition);
339     
340     if (FD->ShowModal()==wxID_OK)
341       {
342         wxBusyCursor busy;
343
344         wxArrayString files;
345         FD->GetPaths(files);
346         unsigned int i;
347         std::vector<std::string> filenames;
348         for (i=0;i<files.GetCount();++i)
349         {
350           filenames.push_back(wx2std(files[i]));
351           GimmickMessage(2,"Adding File "<<files[i]<<"."<<std::endl);
352         }
353
354         mProgressDialog = 
355           new wxProgressDialog(_T("Adding file(s)"),
356                                _T(""),
357                                1000,
358                                this,
359                                wxPD_ELAPSED_TIME |
360                                //                              wxPD_ESTIMATED_TIME | 
361                                //                              wxPD_REMAINING_TIME |
362                                wxPD_CAN_ABORT );
363
364         // TO DO : select the current tree handler
365         mGimmick->AddFiles(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),filenames);
366
367         mProgressDialog->Pulse(_T("Updating view..."));
368
369         UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
370         delete mProgressDialog;
371         DisplayAddSummary();    
372
373       }
374         mViewer->StartPlayer(); 
375   }
376   //=================================================
377
378   //=================================================
379   void WxGimmickView::OnAddDir(wxCommandEvent& event)
380   {
381     mViewer->StopPlayer();
382         std::string name = crea::wx2std(mNotebook->GetCurrentPage()->GetName());
383     long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
384     wxDirDialog* FD = 
385       new wxDirDialog( 0, 
386                        _T("Select directory"),
387                        mCurrentDirectory,
388                        style);
389     
390     if (FD->ShowModal()==wxID_OK)
391       {
392
393         
394                   std::string dirname = wx2std (FD->GetPath());
395                   bool recurse =  isNeedRecursive(dirname);
396                   if (recurse)
397                  {
398              recurse = wxMessageBox(_T("Recurse into sub-directories ?"),  _T("Scan directory"),         wxYES_NO,this ) == wxYES ? true : false;
399                  }
400                 
401                 wxBusyCursor busy;
402                 wxString title(_T("Adding directory"));
403                 if (recurse) 
404                 title = _T("Adding directory (recursive)");
405                 mProgressDialog = 
406                 new wxProgressDialog(_T("Adding directory"),
407                                         _T(""),
408                                         1000,
409                                         this,
410                                         wxPD_ELAPSED_TIME |
411                                         //                             wxPD_ESTIMATED_TIME | 
412                                         //                             wxPD_REMAINING_TIME |
413                                         wxPD_CAN_ABORT );
414                 
415                 mCurrentDirectory = FD->GetPath();  
416                 mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),dirname,recurse);
417                 mProgressDialog->Pulse(_T("Updating view..."));
418                 
419                 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
420                 delete mProgressDialog;
421                 DisplayAddSummary();
422
423           }
424     mViewer->StartPlayer();
425   }
426
427     //=================================================
428    // Test a directory to know if contains sub-directory to analyze
429   bool WxGimmickView::isNeedRecursive(std::string i_name)
430   {
431       boost::filesystem::directory_iterator iter(i_name), end_iter;
432           bool bfindir = false;
433                   for(; iter != end_iter; ++iter)
434                   {
435                           if(boost::filesystem::is_directory(*iter))
436                           {
437                                   return true;
438                           }
439                   }
440                   return false;
441   }
442   //=================================================
443
444   //=================================================
445   void WxGimmickView::OnSelectionChange(const std::vector<tree::Node*>& sel, bool isSelection, int selection, bool needProcess)
446   {      
447     GimmickDebugMessage(5,
448                         "WxGimmickView::OnSelectionChange"
449                         <<std::endl);
450     wxBusyCursor busy;
451         bool valid=true;
452         
453         if(sel.size()==0)
454         {
455                 valid= ValidateSelected(NULL,
456                                 mSelectionMinDimension,
457                                 mSelectionMaxDimension );
458         }
459         else if(needProcess)
460         {
461                 ResetExtent();
462                 std::vector<tree::Node*>::const_iterator i;
463                 for(i=sel.begin();i!=sel.end()&&valid;++i)
464                 {
465                         valid= ValidateSelected((*i),
466                                 mSelectionMinDimension,
467                                 mSelectionMaxDimension );
468                 }
469         }
470         else if(isSelection)
471         {
472                 valid= ValidateSelected(sel.front(),
473                                 mSelectionMinDimension,
474                                 mSelectionMaxDimension );
475         }
476         else
477         {
478                 ResetExtent();
479                 std::vector<tree::Node*>::const_iterator i;
480                 for(i=sel.begin();i!=sel.end()&&valid;++i)
481                 {
482                         valid= ValidateSelected((*i),
483                                 mSelectionMinDimension,
484                                 mSelectionMaxDimension );
485                 }
486         }
487         mText->SetLabel(crea::std2wx(GetMessage()));
488     /*if(valid)
489       {
490         ReadImageThreaded(sel);
491       }
492     else
493       {
494                   ClearSelection();
495       }*/
496         ReadImageThreaded(sel);
497
498     
499    }
500
501   //==================================================
502
503   //==================================================
504   ///Reads Images (Threaded)
505   void WxGimmickView::ReadImageThreaded(const std::vector<tree::Node*>& sel)
506   {     
507    GimmickDebugMessage(5,
508                        "ReadImageThreaded"
509                        <<std::endl);
510    int maxprio = GetMaximalPriority();
511    int prio = maxprio + 2000;
512
513    if(sel.size()>0)
514    {
515    //First load the selected images
516    mCurImageItemToShow = sel.front();
517    pointers.clear();
518    int index = 0;
519    std::vector<tree::Node*>::const_iterator selected;
520    for(selected=sel.begin();selected!=sel.end();++selected)
521      {
522        GimmickDebugMessage(5,
523                            "Requesting image from selected "
524                            <<(*selected)->GetAttribute("FullFileName")
525                            <<std::endl);
526            ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
527            pointers.push_back(ph);
528        RequestReading(*selected,prio,index,ph);
529        //       AddEntryToMap(*selected);
530        prio--;
531        index++;
532      }
533         mViewer->SetImageVector(pointers);
534         //Going up
535         prio = maxprio + 20;
536         std::vector<tree::Node*> up;
537         GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(up,true);
538         std::vector<tree::Node*>::iterator iterUp;
539         for(iterUp=up.begin();iterUp!=up.end();++iterUp)
540         {
541                 GimmickDebugMessage(5,
542                                 "Requesting image from neighbors up "
543                                 <<(*iterUp)->GetAttribute("FullFileName")
544                                 <<std::endl);
545                 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
546                 RequestReading(*iterUp,prio,-1,ph);
547                 //              AddEntryToMap(*iterUp);
548                 prio--;
549                 if (prio == maxprio) break;
550         }
551
552         //Going down
553         prio = maxprio + 19;
554         std::vector<tree::Node*> down;
555         GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(down,false);
556         std::vector<tree::Node*>::iterator iterDown;
557         for(iterDown=down.begin();iterDown!=down.end();++iterDown)
558         {
559                 GimmickDebugMessage(5,
560                                 "Requesting image from neighbors down "
561                                 <<(*iterDown)->GetAttribute("FullFileName")
562                                 <<std::endl);
563                 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
564                 RequestReading(*iterDown,prio,-1,ph);
565                 //              AddEntryToMap(*iterDown);
566                 prio--;
567                 if (prio == maxprio) break;
568         }
569    }
570    else
571    {
572            pointers.clear();
573            ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
574            pointers.push_back(ph);
575            mViewer->SetImageVector(pointers);
576    }
577   }
578
579   //==================================================
580
581   //==================================================
582
583   //==================================================
584    void  WxGimmickView::OnInternalIdle()
585   {
586    if (!mConstructed) return;
587    static bool first_time = true;
588    if (false)
589    {
590         first_time = false;
591      }
592    //   GimmickMessage(1,"WxGimmickView : Refresh viewer"<<std::endl);
593         //  mViewer->StartPlayer();
594    if(mViewer)
595    {
596            mViewer->RefreshIfNecessary();
597    }
598   }
599
600    //==================================================
601
602   //==================================================
603    void  WxGimmickView::ClearSelection()
604   {
605         pointers.clear();
606         pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
607         mViewer->SetImageVector(pointers);
608         mViewer->RefreshIfNecessary();
609         ResetExtent();
610   }
611   
612   //=================================================
613  
614   //=================================================
615   void WxGimmickView::OnRemove(wxCommandEvent& event)
616   {
617         //TODO Select current tree handler       
618     wxBusyCursor busy;
619     GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->RemoveSelected();
620         ClearSelection();
621   }
622   //=================================================
623
624   
625   //=================================================
626   void WxGimmickView::AddIgnoreFile(tree::Node* toRemove)
627   {
628           mGimmick->RemoveFile(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),toRemove);
629           GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
630   }
631
632   //=================================================
633   void WxGimmickView::CopyFiles(const std::vector<std::string>& filenames)
634   {
635           mGimmick->CopyFiles(filenames, crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())));
636           wxMessageBox(std2wx("The selected files have been copied"),_T("Copy files"),wxOK,this);
637   }
638
639    //=================================================
640   void WxGimmickView::OnSynchronize(wxCommandEvent& event)
641   {       
642     wxBusyCursor busy;
643         const wxString choices[] = { _T("Check database for files deletion and addition and give a report."), 
644                                                                 _T("Check database for files deletion, addition and attributes change. Then give a report."), 
645                                                                 _T("Repair database (remove deleted files and add new files)."), 
646                                                                 _T("Repair database (remove deleted files, add new files and reset changed attributes).") } ;
647
648     wxSingleChoiceDialog dialog(this,
649                                                                 _T("Select one of the following synchronization actions:\n")
650                                 _T("Please note that, due to the heavy amount of operations required, this action might take a while."),
651                                 _T("Synchronization Settings"),
652                                 WXSIZEOF(choices), choices);
653
654     //dialog.SetSelection(0);
655
656     if (dialog.ShowModal() == wxID_OK)
657     {
658         int sel=dialog.GetSelection();
659                 bool repair=false;
660                 bool checkAttributes=false;
661                 if(sel==2 || sel==3){repair=true;}
662                 if(sel==1 || sel==3){checkAttributes=true;}
663                 std::string mess=mGimmick->Synchronize(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),repair, checkAttributes);
664                 wxMessageBox(std2wx(mess),_T("Synchronization result"),wxOK,this);
665                 if(sel==2 || sel==3){
666                 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
667                 }
668                 
669     }
670   }
671   //=================================================
672
673   //=================================================
674   void WxGimmickView::OnSettings(wxCommandEvent& event)
675   {
676     wxDialog* dial= new wxDialog (this,-1,_T("System Settings"),wxDefaultPosition, wxSize(450,600));
677     wxBoxSizer  *siz = new wxBoxSizer(wxVERTICAL);
678     // Notebook
679     wxNotebook* nb= new wxNotebook(dial, -1, wxDefaultPosition, wxDefaultSize, 0);
680     
681     siz->Add( nb,1,wxGROW  ,0);  
682     CreateSettingsDialog(nb);
683     dial->SetSizer(siz);
684     dial->ShowModal();
685   }
686   //=================================================
687
688   //=================================================
689   //AndresDonadio
690   void WxGimmickView::OnTools(wxCommandEvent& event)
691   {
692                 mViewer->StopPlayer();
693                 
694                 wxDialog* dial = new wxDialog (this,-1,"Tools",wxDefaultPosition, wxSize(300,250));
695
696                 wxSizer* buttonsSizer = dial->CreateSeparatedButtonSizer(wxOK|wxCANCEL);
697                 wxNotebook* nb= new wxNotebook(dial, -1, wxDefaultPosition, wxDefaultSize, 0);
698
699                 wxBoxSizer *dialSizer = new wxBoxSizer(wxVERTICAL);     
700                 dialSizer->Add(nb,1,wxGROW,0);
701                 dialSizer->Add(buttonsSizer,0,wxGROW);
702
703                 //First page: Bruker Image Reader
704                 WxGimmickTools * gimmickTools = new WxGimmickTools(nb, mCurrentDirectory);
705                 nb->AddPage( gimmickTools, crea::std2wx("Bruker Image Reader") );
706
707                 dial->SetSizer(dialSizer, true);
708                 dial->Layout();
709                 dial->ShowModal();
710
711                 if (dial->GetReturnCode() == wxID_OK)
712                         {
713                                 if (nb->GetSelection()==0)//Selection: Bruker Image Reader
714                                 {
715                                         std::string inputDir = crea::wx2std(gimmickTools->getInputDir());
716                                         std::string outputDir = crea::wx2std(gimmickTools->getOutputDir());
717                                         bool addToDB = gimmickTools->getCheckBoxValue();
718
719                                         if (inputDir.compare("")!=0 && outputDir.compare("")!=0)
720                                         {
721                                                 /*
722                                                 Bruker2Dicom b2d;    
723                                                 b2d.SetInputDirectory(crea::wx2std(inputDir));
724                                                 b2d.SetOutputDirectory(crea::wx2std(outputDir));
725                                                 b2d.SetConvertModeToDicom();
726                                                 b2d.Execute();
727                                                 */
728
729                                                 if (addToDB)
730                                                 {
731                                                         mProgressDialog = new wxProgressDialog(_T("Adding directory"),_T(""),1000,this,wxPD_ELAPSED_TIME |wxPD_CAN_ABORT );
732                                                         mCurrentDirectory = outputDir;  
733                                                         mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),crea::wx2std(outputDir),true);
734                                                         mProgressDialog->Pulse(_T("Updating view..."));
735                                                         
736                                                         UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
737                                                         delete mProgressDialog;
738                                                         DisplayAddSummary();
739                                                 }       
740                                         }
741
742                                         else
743                                         {
744                                                 wxMessageBox(_T("One or both of the directory fields are empty"),_T("Empty Fields"),wxOK,this);
745                                         }
746                                 }
747                         }
748
749                 mViewer->StartPlayer();
750   }
751   //=================================================
752
753   void WxGimmickView::CreateSettingsDialog(wxNotebook* nb)
754   {
755           //First page: Customization of configurations
756           wxPanel* customConfig=new wxPanel(nb);
757           nb->AddPage( customConfig, crea::std2wx("Customize Configuration") );
758           //Second page: Creation of Databases
759           wxPanel* databaseCreation=new wxPanel(nb);
760           nb->AddPage( databaseCreation, crea::std2wx("Create Database") );
761           //Third page: CD/DVD Watch
762           wxPanel* cdWatch=new wxPanel(nb);
763           nb->AddPage( cdWatch, crea::std2wx("CD/DVD") );
764           //Fourth page: Selection of attributes to show (edit)
765           wxPanel* attSelection=new wxPanel(nb);
766           nb->AddPage( attSelection, crea::std2wx("Selection of Attributes") );
767   }
768
769   //=================================================
770   /// AddProgress Gimmick callback
771   void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
772   {
773
774     char mess[200];
775    
776         sprintf(mess,"%i dirs : %i files :\n            %i handled - %i added",
777            p.GetNumberScannedDirs(),
778            p.GetNumberScannedFiles(),
779            p.GetNumberHandledFiles(),
780            p.GetNumberAddedFiles());
781     //    std::cout << "OnAddProgress "<<mess<<std::endl;
782     wxString s(wxString::From8BitData(mess));
783     //  std::cout << "Pulse"<<std::endl;
784     if (!mProgressDialog->Pulse(s)) 
785       {
786         p.SetStop();
787       }
788     //  std::cout << "OnAddProgress ok"<<std::endl;
789   }
790   //=================================================
791
792   //=================================================
793   void WxGimmickView::DisplayAddSummary()
794   {
795     const Gimmick::AddProgress& p = mGimmick->GetAddProgress();
796     std::stringstream mess;
797     mess << "Dirs \tscanned\t: " << p.GetNumberScannedDirs()  << "\n";
798     mess << "Files\tscanned\t: " << p.GetNumberScannedFiles() << "\n";
799     mess << "Files\thandled\t: " << p.GetNumberHandledFiles() << "\n\n";
800     mess << "Files\tadded  \t: " << p.GetNumberAddedFiles()   << "\n\n";
801
802     /*    char times[500];
803     sprintf(times,"Time to parse dir \t\t: %ld ms \t%d°/o\nTime to read files info \t: %ld ms \t%d°/o\nTime to update structs \t: %ld ms \t%d°/o\nTime to update database \t: %ld ms \t%d°/o\nTotal time \t\t\t: %ld ms",
804             summary.parse_time,
805             (int)( summary.parse_time*100./summary.total_time),
806             summary.file_scan_time,
807             (int)(summary.file_scan_time*100./summary.total_time),
808             summary.update_structs_time,
809             (int)(summary.update_structs_time*100./summary.total_time),
810             summary.update_database_time,
811             (int)(summary.update_database_time*100./summary.total_time),
812             summary.total_time );
813     
814     mess << times;
815     */
816     wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
817   }
818
819   //////////////////////////////////////////////////
820   // Add a DB to application                                      //
821   // @param event : WxEvent                                               //
822   // @return : -                                                                  //
823   //////////////////////////////////////////////////
824   void WxGimmickView::OnAddDB(wxCommandEvent& event)
825   {
826     
827           //Select DB
828           long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
829           std::string wc("*.sqlite3*");
830           wxFileDialog* FD = new wxFileDialog( 0, 
831                                          _T("Select file"),
832                                          _T(""),
833                                          _T(""),
834                                          crea::std2wx(wc),
835                                          style,
836                                          wxDefaultPosition);
837     
838     if (FD->ShowModal()==wxID_OK)
839         {
840                 wxBusyCursor busy;
841                 wxArrayString files;
842                 FD->GetPaths(files);
843                 std::stringstream st;
844                 for(int i = 0; i< files.size(); i++)
845                 {
846                         //get name of DB (file name)
847                   size_t pos = files[i].find_last_of(_T("\\"));
848                   std::string name = crea::wx2std(files[i].substr(pos+1));
849                   pos = name.find_last_of(".");
850                   name = name.substr(0,pos);
851                   //create TreeHandler
852                   mGimmick->addDB(name, crea::wx2std(files[i]));
853                   //create TreeView
854                   CreateSingleTreeView(name);
855                 }
856         }
857   }
858     ////////////////////////////////////////////////////
859   // Create a DB from an Attributes Descriptor files  //
860   // @param event : WxEvent                                               //
861   // @return : -                                                                  //
862   //////////////////////////////////////////////////
863   void WxGimmickView::OnCreateDB(wxCommandEvent& event)
864   {
865     
866           //Select DB
867           long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
868           std::string wc("*.txt");
869           wxFileDialog* FD = new wxFileDialog( 0, 
870                                          _T("Select file"),
871                                          _T(""),
872                                          _T(""),
873                                          crea::std2wx(wc),
874                                          style,
875                                          wxDefaultPosition);
876     
877     if (FD->ShowModal()==wxID_OK)
878         {
879                 wxBusyCursor busy;
880                 wxArrayString afile;
881                 FD->GetPaths(afile);
882                 //get name of DB (file name)
883                 std::string file = crea::wx2std(afile[0]);
884                 size_t pos = file.find_last_of("\\");
885                 std::string name = file.substr(pos+1);
886                 std::string directory = file.substr(0,pos);
887                 pos = name.find_last_of(".");
888                 name = name.substr(0,pos);
889                 //get directory to store DB
890                 directory +=  "\\" + name + ".sqlite3";
891                 //create createDB
892                 mGimmick->createDB(name, file,directory);
893                 //create TreeHandler
894                 mGimmick->addDB(name, directory);
895                 //create TreeView
896                 CreateSingleTreeView(name);
897         }
898   }
899
900    //=================================================
901
902    //=================================================
903   BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
904     EVT_TOOL(TOOL_CREATEDB_ID, WxGimmickView::OnCreateDB)
905     EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
906     EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
907         EVT_TOOL(TOOL_ADDDATABASE_ID, WxGimmickView::OnAddDB)
908         EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
909         EVT_TOOL(TOOL_SYNCHRONIZE_ID, WxGimmickView::OnSynchronize)
910         EVT_TOOL(TOOL_SETTINGS_ID, WxGimmickView::OnSettings)
911         EVT_TOOL(TOOL_TOOLS_ID, WxGimmickView::OnTools)
912   END_EVENT_TABLE()
913   //=================================================
914
915 } // EO namespace creaImageIO
916
917