]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOWxGimmickView.cpp
Added command line interaction with the synchronization option.
[creaImageIO.git] / src2 / creaImageIOWxGimmickView.cpp
index f5d50c9d351b1450801f2d9437579e3a7c7c0b38..824386795051727db757a73268c3759da8414d97 100644 (file)
@@ -9,9 +9,12 @@ using namespace crea;
 #include "icons/page-down.xpm"
 #include "icons/remove.xpm"
 #include "icons/database-add.xpm"
+#include "icons/create-database.xpm"
 #include "icons/help.xpm"
+#include "icons/synchronize.xpm"
 
 #include <wx/imaglist.h>
+#include <wx/popupwin.h>
 #include<boost/filesystem/operations.hpp>
 
 namespace creaImageIO
@@ -21,17 +24,19 @@ namespace creaImageIO
   // The ids of the different tools
   enum
     {
-      TOOL_ADDFILES_ID = 1,
-      TOOL_ADDDIR_ID = 2,
-      TOOL_REMOVE_ID = 3,
-      TOOL_ADDDATABASE_ID = 4,
-      TOOL_HELP_ID = 5
+         TOOL_CREATEDB_ID = 1,
+      TOOL_ADDFILES_ID = 2,
+      TOOL_ADDDIR_ID = 3,
+         TOOL_ADDDATABASE_ID = 4,
+      TOOL_REMOVE_ID = 5,
+         TOOL_SYNCHRONIZE_ID = 6,
+      TOOL_HELP_ID = 7
     };
   //======================================================================
 
   //================================================================
   // 
-  const int icon_number = 7;
+  const int icon_number = 8;
   // Icon ids
   typedef enum
     {
@@ -41,7 +46,9 @@ namespace creaImageIO
       Icon_page_down,
       Icon_remove,
       Icon_database_add,
-      Icon_help
+      Icon_help,
+         Icon_synchronize,
+         Icon_create_database
     }
     icon_id;
   //================================================================
@@ -172,6 +179,11 @@ namespace creaImageIO
     mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize,
                             style);
 
+       mToolAddFile = mToolBar->AddTool( TOOL_CREATEDB_ID, 
+                                     _T("Create database"),
+                                     mIcon->GetBitmap(Icon_create_database),
+                                     _T("Create DB from an Attributes Descriptor file")
+                                     );
     mToolAddFile = mToolBar->AddTool( TOOL_ADDFILES_ID, 
                                      _T("Add file(s)"),
                                      mIcon->GetBitmap(Icon_page_down),
@@ -182,17 +194,22 @@ namespace creaImageIO
                                      mIcon->GetBitmap(Icon_folder_down),
                                      _T("Add the content of a folder to database")
                                      );
+       mToolAddDatabase = mToolBar->AddTool( TOOL_ADDDATABASE_ID, 
+                                     _T("Open database"),
+                                     mIcon->GetBitmap(Icon_database_add),
+                                     _T("Open a local or distant database")
+                                     );
     mToolRemove = mToolBar->AddTool( TOOL_REMOVE_ID, 
                                      _T("Remove"),
                                      mIcon->GetBitmap(Icon_remove),
                                      _T("Remove selected items")
                                      );
-    mToolAddDatabase = mToolBar->AddTool( TOOL_ADDDATABASE_ID, 
-                                     _T("Open database"),
-                                     mIcon->GetBitmap(Icon_database_add),
-                                     _T("Open a local or distant database")
+    mToolSynchronize = mToolBar->AddTool( TOOL_SYNCHRONIZE_ID, 
+                                     _T("Synchronize"),
+                                     mIcon->GetBitmap(Icon_synchronize),
+                                     _T("Synchronizes the database with disk")
                                      );
-    mToolHelp = mToolBar->AddTool( TOOL_HELP_ID, 
+       mToolHelp = mToolBar->AddTool( TOOL_HELP_ID, 
                                      _T("Help"),
                                      mIcon->GetBitmap(Icon_help),
                                      _T("Open help window")
@@ -220,14 +237,14 @@ namespace creaImageIO
 
     // Add Notebook page
     mNotebook->AddPage( view, crea::std2wx(name) );
-
+       
   }
 
   //======================================================================
   void WxGimmickView::GetSelectedImages(std::vector<vtkImageData*>& s, int dim)
   {
        std::vector<std::string> files;
-       GetTreeViewMap()["Local database"]->GetSelectedAsString(files);
+       GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(files);
        ReadImagesNotThreaded(s,files,dim);
   }
   //======================================================================
@@ -235,7 +252,7 @@ namespace creaImageIO
   //======================================================================
   void WxGimmickView::GetSelectedFiles(std::vector<std::string>& s)
   {
-       GetTreeViewMap()["Local database"]->GetSelectedAsString(s);
+       GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(s);
   }
   //======================================================================
 
@@ -253,7 +270,7 @@ namespace creaImageIO
   void WxGimmickView::CreateIconList()
   {
     // Size of the icons;
-    int size = 24;
+    int size = 16;
 
     wxIcon icons[20];
     // should correspond to Icon_xxx enum
@@ -264,6 +281,8 @@ namespace creaImageIO
     icons[Icon_remove] = wxIcon(remove_xpm);
     icons[Icon_database_add] = wxIcon(database_add_xpm);
     icons[Icon_help] = wxIcon(help_xpm);
+       icons[Icon_synchronize] = wxIcon(synchronize_xpm);
+       icons[Icon_create_database] = wxIcon(create_database_xpm);
 
     //   unsigned int NbIcons = 8;
     // Make an image list containing small icons
@@ -289,6 +308,7 @@ namespace creaImageIO
   //=================================================
   void WxGimmickView::OnAddFiles(wxCommandEvent& event)
   {
+    mViewer->StopPlayer();
    long style = wxOPEN | wxFILE_MUST_EXIST | wxFD_MULTIPLE;
     std::string wc("*.*");
     wxFileDialog* FD = new wxFileDialog( 0, 
@@ -324,21 +344,24 @@ namespace creaImageIO
                               wxPD_CAN_ABORT );
 
        // TO DO : select the current tree handler
-       mGimmick->AddFiles("Local database",filenames);
+       mGimmick->AddFiles(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),filenames);
 
        mProgressDialog->Pulse(_T("Updating view..."));
 
-       UpdateTreeViewLevel("Local database",1);
+       UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
        delete mProgressDialog;
        DisplayAddSummary();    
+
       }
-       
+       mViewer->StartPlayer(); 
   }
   //=================================================
 
   //=================================================
   void WxGimmickView::OnAddDir(wxCommandEvent& event)
   {
+    mViewer->StopPlayer();
+       std::string name = crea::wx2std(mNotebook->GetCurrentPage()->GetName());
     long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
     wxDirDialog* FD = 
       new wxDirDialog( 0, 
@@ -348,6 +371,7 @@ namespace creaImageIO
     
     if (FD->ShowModal()==wxID_OK)
       {
+
        
                  std::string dirname = wx2std (FD->GetPath());
                  bool recurse =  isNeedRecursive(dirname);
@@ -371,16 +395,15 @@ namespace creaImageIO
                                        wxPD_CAN_ABORT );
                
                mCurrentDirectory = FD->GetPath();  
-               
-               // TO DO : select the current tree handler
-               mGimmick->AddDir("Local database",dirname,recurse);
-               
+               mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),dirname,recurse);
                mProgressDialog->Pulse(_T("Updating view..."));
                
-               UpdateTreeViewLevel("Local database",1);
+               UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
                delete mProgressDialog;
                DisplayAddSummary();
+
          }
+    mViewer->StartPlayer();
   }
 
     //=================================================
@@ -411,7 +434,6 @@ namespace creaImageIO
        
        if(sel.size()==0)
        {
-               
                valid= ValidateSelected(NULL,
                                mSelectionMinDimension,
                                mSelectionMaxDimension );
@@ -495,7 +517,7 @@ namespace creaImageIO
        //Going up
        prio = maxprio + 20;
        std::vector<tree::Node*> up;
-       GetTreeViewMap()["Local database"]->GetNodes(up,true);
+       GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(up,true);
        std::vector<tree::Node*>::iterator iterUp;
        for(iterUp=up.begin();iterUp!=up.end();++iterUp)
        {
@@ -513,7 +535,7 @@ namespace creaImageIO
        //Going down
        prio = maxprio + 19;
        std::vector<tree::Node*> down;
-       GetTreeViewMap()["Local database"]->GetNodes(down,false);
+       GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(down,false);
        std::vector<tree::Node*>::iterator iterDown;
        for(iterDown=down.begin();iterDown!=down.end();++iterDown)
        {
@@ -575,15 +597,53 @@ namespace creaImageIO
   //=================================================
   void WxGimmickView::OnRemove(wxCommandEvent& event)
   {
-    modifyValidationSignal(false);
-       //TODO Select current tree handler       
+       //TODO Select current tree handler       
     wxBusyCursor busy;
-    GetTreeViewMap()["Local database"]->RemoveSelected();
-
+    GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->RemoveSelected();
        ClearSelection();
   }
   //=================================================
 
+  
+  //=================================================
+  void WxGimmickView::AddIgnoreFile(tree::Node* toRemove)
+  {
+         mGimmick->RemoveFile("Local database",toRemove);
+         GetTreeViewMap()["Local database"]->UpdateLevel(1);
+  }
+
+   //=================================================
+  void WxGimmickView::OnSynchronize(wxCommandEvent& event)
+  {       
+    wxBusyCursor busy;
+       const wxString choices[] = { _T("Check database for files deletion and addition and give a report."), 
+                                                               _T("Check database for files deletion, addition and attributes change. Then give a report."), 
+                                                               _T("Repair database (remove deleted files and add new files)."), 
+                                                               _T("Repair database (remove deleted files, add new files and reset changed attributes).") } ;
+
+    wxSingleChoiceDialog dialog(this,
+                                                               _T("Select one of the following synchronization actions:\n")
+                                _T("Please note that, due to the heavy amount of operations required, this action might take a while."),
+                                _T("Synchronization Settings"),
+                                WXSIZEOF(choices), choices);
+
+    //dialog.SetSelection(0);
+
+    if (dialog.ShowModal() == wxID_OK)
+    {
+        int sel=dialog.GetSelection();
+               bool repair=false;
+               bool checkAttributes=false;
+               if(sel==2 || sel==3){repair=true;}
+               if(sel==1 || sel==3){checkAttributes=true;}
+               std::string mess=mGimmick->Synchronize(repair, checkAttributes);
+               wxMessageBox(std2wx(mess),_T("Synchronization result"),wxOK,this);
+               GetTreeViewMap()["Local database"]->UpdateLevel(1);
+               
+    }
+  }
+  //=================================================
+
   //=================================================
   /// AddProgress Gimmick callback
   void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
@@ -634,13 +694,98 @@ namespace creaImageIO
     wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
   }
 
+  //////////////////////////////////////////////////
+  // Add a DB to application                                     //
+  // @param event : WxEvent                                              //
+  // @return : -                                                                 //
+  //////////////////////////////////////////////////
+  void WxGimmickView::OnAddDB(wxCommandEvent& event)
+  {
+    
+         //Select DB
+         long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
+         std::string wc("*.sqlite3*");
+         wxFileDialog* FD = new wxFileDialog( 0, 
+                                        _T("Select file"),
+                                        _T(""),
+                                        _T(""),
+                                        crea::std2wx(wc),
+                                        style,
+                                        wxDefaultPosition);
+    
+    if (FD->ShowModal()==wxID_OK)
+       {
+               wxBusyCursor busy;
+               wxArrayString files;
+               FD->GetPaths(files);
+               std::stringstream st;
+               for(int i = 0; i< files.size(); i++)
+               {
+                       //get name of DB (file name)
+                 size_t pos = files[i].find_last_of(_T("\\"));
+                 std::string name = crea::wx2std(files[i].substr(pos+1));
+                 pos = name.find_last_of(".");
+                 name = name.substr(0,pos);
+                 //create TreeHandler
+                 mGimmick->addDB(name, crea::wx2std(files[i]));
+                 //create TreeView
+                 CreateSingleTreeView(name);
+               }
+       }
+  }
+    ////////////////////////////////////////////////////
+  // Create a DB from an Attributes Descriptor files  //
+  // @param event : WxEvent                                              //
+  // @return : -                                                                 //
+  //////////////////////////////////////////////////
+  void WxGimmickView::OnCreateDB(wxCommandEvent& event)
+  {
+    
+         //Select DB
+         long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
+         std::string wc("*.txt");
+         wxFileDialog* FD = new wxFileDialog( 0, 
+                                        _T("Select file"),
+                                        _T(""),
+                                        _T(""),
+                                        crea::std2wx(wc),
+                                        style,
+                                        wxDefaultPosition);
+    
+    if (FD->ShowModal()==wxID_OK)
+       {
+               wxBusyCursor busy;
+               wxArrayString afile;
+               FD->GetPaths(afile);
+               //get name of DB (file name)
+               std::string file = crea::wx2std(afile[0]);
+               size_t pos = file.find_last_of("\\");
+               std::string name = file.substr(pos+1);
+               std::string directory = file.substr(0,pos);
+               pos = name.find_last_of(".");
+               name = name.substr(0,pos);
+               //get directory to store DB
+               directory +=  "\\" + name + ".sqlite3";
+               //create createDB
+               mGimmick->createDB(name, file,directory);
+               //create TreeHandler
+               mGimmick->addDB(name, directory);
+               //create TreeView
+               CreateSingleTreeView(name);
+       }
+  }
+
    //=================================================
 
    //=================================================
   BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
+    EVT_TOOL(TOOL_CREATEDB_ID, WxGimmickView::OnCreateDB)
+       EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnCreateDB)
     EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
     EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
+       EVT_TOOL(TOOL_ADDDATABASE_ID, WxGimmickView::OnAddDB)
        EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
+       EVT_TOOL(TOOL_SYNCHRONIZE_ID, WxGimmickView::OnSynchronize)
   END_EVENT_TABLE()
   //=================================================