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