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