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