1 #include <creaImageIOWxGimmickView.h>
2 #include <creaImageIOWxTreeView.h>
3 #include <creaImageIOSystem.h>
6 #include "icons/accept.xpm"
7 #include "icons/add.xpm"
8 #include "icons/folder-down.xpm"
9 #include "icons/page-down.xpm"
10 #include "icons/remove.xpm"
11 #include "icons/database-add.xpm"
12 #include "icons/create-database.xpm"
13 #include "icons/help.xpm"
14 #include "icons/synchronize.xpm"
15 #include "icons/settings.xpm"
16 #include "icons/tools.xpm"
18 #include <wx/imaglist.h>
19 #include <wx/popupwin.h>
20 #include<boost/filesystem/operations.hpp>
25 //======================================================================
26 // The ids of the different tools
32 TOOL_ADDDATABASE_ID = 4,
34 TOOL_SYNCHRONIZE_ID = 6,
39 //======================================================================
41 //================================================================
43 const int icon_number = 11;
60 //================================================================
62 //================================================================
64 const icon_id Icon[5] = { Icon_Database,
70 //================================================================
73 //======================================================================
75 WxGimmickView::WxGimmickView(Gimmick* gimmick,
78 const wxPoint& pos, const wxSize& size,
81 int number_of_threads)
82 : wxPanel(parent,id,pos,size),
83 GimmickView(gimmick, number_of_threads),
87 GimmickDebugMessage(1,"WxGimmickView::WxGimmickView"
89 // Sets the current directory to the home dir
90 mCurrentDirectory = std2wx(gimmick->GetHomeDirectory());
92 // Connect the AddProgress callback
93 gimmick->ConnectAddProgressObserver
94 ( boost::bind( &WxGimmickView::OnAddProgress , this, _1 ) );
96 // Create the list of icons (mIcon)
100 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
102 // Create the tool bar
104 sizer->Add( mToolBar ,0, wxGROW ,0);
106 // Split part below toolbar into notebook for views and panel
107 // for preview, messages...
108 mSplitter = new wxSplitterWindow( this , -1);
112 mNotebook = new wxNotebook(mSplitter,
113 -1,wxDefaultPosition, wxDefaultSize, 0);
119 mSelectionMaxDimension= max_dim;
120 mSelectionMinDimension= min_dim;
126 mBottomPanel = new wxPanel(mSplitter,-1);
128 wxBoxSizer *bottom_sizer = new wxBoxSizer(wxVERTICAL); //HORIZONTAL);
132 mViewer = new WxViewer(mBottomPanel, wxID_ANY, wxT("Gimmick! Viewer"),wxDefaultPosition, wxDefaultSize );
133 pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
134 mViewer->SetImageVector(pointers);
135 mViewer->StartPlayer();
138 bottom_sizer->Add(mViewer,1,wxGROW,1);
141 mText = new wxStaticText(mBottomPanel, wxID_ANY, wxT("Welcome to Gimmick!"));
142 bottom_sizer->Add(mText,0,wxGROW,0);
146 mBottomPanel->SetSizer(bottom_sizer);
149 int hsize = size.GetHeight();
151 int top_minsize = 450;
152 int bottom_minsize = 50;
154 mSplitter->SetMinimumPaneSize( bottom_minsize );
155 mSplitter->SplitHorizontally( mNotebook, mBottomPanel,
158 sizer->Add( mSplitter,1,wxGROW ,0);
167 //======================================================================
169 //======================================================================
171 WxGimmickView::~WxGimmickView()
173 // stop the viewer before application exit.
174 mViewer->StopPlayer();
175 GimmickDebugMessage(1,"WxGimmickView::~WxGimmickView"
178 //======================================================================
180 //======================================================================
181 /// Creates the tool bar
182 void WxGimmickView::CreateToolBar()
184 long style = wxTB_HORIZONTAL | wxNO_BORDER | wxTB_TEXT;
185 mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize,
188 mToolAddFile = mToolBar->AddTool( TOOL_CREATEDB_ID,
189 _T("Create database"),
190 mIcon->GetBitmap(Icon_create_database),
191 _T("Create DB from an Attributes Descriptor file")
193 mToolAddFile = mToolBar->AddTool( TOOL_ADDFILES_ID,
195 mIcon->GetBitmap(Icon_page_down),
196 _T("Add one or more file to database")
198 mToolAddDir = mToolBar->AddTool( TOOL_ADDDIR_ID,
200 mIcon->GetBitmap(Icon_folder_down),
201 _T("Add the content of a folder to database")
203 mToolAddDatabase = mToolBar->AddTool( TOOL_ADDDATABASE_ID,
205 mIcon->GetBitmap(Icon_database_add),
206 _T("Open a local or distant database")
208 mToolRemove = mToolBar->AddTool( TOOL_REMOVE_ID,
210 mIcon->GetBitmap(Icon_remove),
211 _T("Remove selected items")
213 mToolSynchronize = mToolBar->AddTool( TOOL_SYNCHRONIZE_ID,
215 mIcon->GetBitmap(Icon_synchronize),
216 _T("Synchronizes the database with disk")
218 mToolHelp = mToolBar->AddTool( TOOL_HELP_ID,
220 mIcon->GetBitmap(Icon_help),
221 _T("Open help window")
223 mToolSettings = mToolBar->AddTool( TOOL_SETTINGS_ID,
224 _T("System settings"),
225 mIcon->GetBitmap(Icon_settings),
226 _T("Allows the modification of various system settings")
228 mToolTools = mToolBar->AddTool( TOOL_TOOLS_ID,
230 mIcon->GetBitmap(Icon_tools),
231 _T("Applies tools to images")
233 //const wxBitmap& bitmap1, const wxString& shortHelpString = "", wxItemKind kind = wxITEM_NORMAL)
237 //======================================================================
240 //======================================================================
241 /// Create the tree view for TreeHandler provided
242 void WxGimmickView::CreateTreeView( TreeHandler* h)
244 std::string name(h->GetTree().GetAttribute("Name"));
245 GimmickMessage(2,"Creating the tree view for '"<<
246 name<<"'"<<std::endl);
247 // Create the WxTreeView
248 WxTreeView* view = new WxTreeView(h,this,mNotebook,-1);
250 // TO DO : TEST THAT A VIEW WITH SAME NAME IS NOT
251 // ALREADY IN THE MAP
252 GetTreeViewMap()[name] = view;
255 mNotebook->AddPage( view, crea::std2wx(name) );
259 //======================================================================
260 void WxGimmickView::GetSelectedImages(std::vector<vtkImageData*>& s, int dim)
262 std::vector<std::string> files;
263 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(files);
264 ReadImagesNotThreaded(s,files,dim);
266 //======================================================================
268 //======================================================================
269 void WxGimmickView::GetSelectedFiles(std::vector<std::string>& s)
271 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(s);
273 //======================================================================
275 //======================================================================
276 void WxGimmickView::GetImages(int dim,
277 const std::vector<std::string>& files,
278 std::vector<vtkImageData*>& s)
280 ReadImagesNotThreaded(s,files,dim);
282 //======================================================================
285 //=================================================
286 void WxGimmickView::CreateIconList()
288 // Size of the icons;
292 // should correspond to Icon_xxx enum
293 icons[Icon_accept] = wxIcon(accept_xpm);
294 icons[Icon_add] = wxIcon(add_xpm);
295 icons[Icon_folder_down] = wxIcon(folder_down_xpm);
296 icons[Icon_page_down] = wxIcon(page_down_xpm);
297 icons[Icon_remove] = wxIcon(remove_xpm);
298 icons[Icon_database_add] = wxIcon(database_add_xpm);
299 icons[Icon_help] = wxIcon(help_xpm);
300 icons[Icon_synchronize] = wxIcon(synchronize_xpm);
301 icons[Icon_create_database] = wxIcon(create_database_xpm);
302 icons[Icon_settings] = wxIcon(settings_xpm);
303 icons[Icon_tools] = wxIcon(tools_xpm);
305 // unsigned int NbIcons = 8;
306 // Make an image list containing small icons
307 mIcon = new wxImageList(size,size,true);
309 // Make all icons the same size = size of the first one
310 int sizeOrig = icons[0].GetWidth();
311 for ( size_t i = 0; i < icon_number; i++ )
313 if ( size == sizeOrig )
315 mIcon->Add(icons[i]);
319 mIcon->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
323 //=================================================
326 //=================================================
327 void WxGimmickView::OnAddFiles(wxCommandEvent& event)
329 mViewer->StopPlayer();
330 long style = wxOPEN | wxFILE_MUST_EXIST | wxFD_MULTIPLE;
331 std::string wc("*.*");
332 wxFileDialog* FD = new wxFileDialog( 0,
340 if (FD->ShowModal()==wxID_OK)
347 std::vector<std::string> filenames;
348 for (i=0;i<files.GetCount();++i)
350 filenames.push_back(wx2std(files[i]));
351 GimmickMessage(2,"Adding File "<<files[i]<<"."<<std::endl);
355 new wxProgressDialog(_T("Adding file(s)"),
360 // wxPD_ESTIMATED_TIME |
361 // wxPD_REMAINING_TIME |
364 // TO DO : select the current tree handler
365 mGimmick->AddFiles(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),filenames);
367 mProgressDialog->Pulse(_T("Updating view..."));
369 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
370 delete mProgressDialog;
374 mViewer->StartPlayer();
376 //=================================================
378 //=================================================
379 void WxGimmickView::OnAddDir(wxCommandEvent& event)
381 mViewer->StopPlayer();
382 std::string name = crea::wx2std(mNotebook->GetCurrentPage()->GetName());
383 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
386 _T("Select directory"),
390 if (FD->ShowModal()==wxID_OK)
394 std::string dirname = wx2std (FD->GetPath());
395 bool recurse = isNeedRecursive(dirname);
398 recurse = wxMessageBox(_T("Recurse into sub-directories ?"), _T("Scan directory"), wxYES_NO,this ) == wxYES ? true : false;
402 wxString title(_T("Adding directory"));
404 title = _T("Adding directory (recursive)");
406 new wxProgressDialog(_T("Adding directory"),
411 // wxPD_ESTIMATED_TIME |
412 // wxPD_REMAINING_TIME |
415 mCurrentDirectory = FD->GetPath();
416 mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),dirname,recurse);
417 mProgressDialog->Pulse(_T("Updating view..."));
419 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
420 delete mProgressDialog;
424 mViewer->StartPlayer();
427 //=================================================
428 // Test a directory to know if contains sub-directory to analyze
429 bool WxGimmickView::isNeedRecursive(std::string i_name)
431 boost::filesystem::directory_iterator iter(i_name), end_iter;
432 bool bfindir = false;
433 for(; iter != end_iter; ++iter)
435 if(boost::filesystem::is_directory(*iter))
442 //=================================================
444 //=================================================
445 void WxGimmickView::OnSelectionChange(const std::vector<tree::Node*>& sel, bool isSelection, int selection, bool needProcess)
447 GimmickDebugMessage(5,
448 "WxGimmickView::OnSelectionChange"
455 valid= ValidateSelected(NULL,
456 mSelectionMinDimension,
457 mSelectionMaxDimension );
462 std::vector<tree::Node*>::const_iterator i;
463 for(i=sel.begin();i!=sel.end()&&valid;++i)
465 valid= ValidateSelected((*i),
466 mSelectionMinDimension,
467 mSelectionMaxDimension );
472 valid= ValidateSelected(sel.front(),
473 mSelectionMinDimension,
474 mSelectionMaxDimension );
479 std::vector<tree::Node*>::const_iterator i;
480 for(i=sel.begin();i!=sel.end()&&valid;++i)
482 valid= ValidateSelected((*i),
483 mSelectionMinDimension,
484 mSelectionMaxDimension );
487 mText->SetLabel(crea::std2wx(GetMessage()));
490 ReadImageThreaded(sel);
496 ReadImageThreaded(sel);
501 //==================================================
503 //==================================================
504 ///Reads Images (Threaded)
505 void WxGimmickView::ReadImageThreaded(const std::vector<tree::Node*>& sel)
507 GimmickDebugMessage(5,
510 int maxprio = GetMaximalPriority();
511 int prio = maxprio + 2000;
515 //First load the selected images
516 mCurImageItemToShow = sel.front();
519 std::vector<tree::Node*>::const_iterator selected;
520 for(selected=sel.begin();selected!=sel.end();++selected)
522 GimmickDebugMessage(5,
523 "Requesting image from selected "
524 <<(*selected)->GetAttribute("FullFileName")
526 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
527 pointers.push_back(ph);
528 RequestReading(*selected,prio,index,ph);
529 // AddEntryToMap(*selected);
533 mViewer->SetImageVector(pointers);
536 std::vector<tree::Node*> up;
537 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(up,true);
538 std::vector<tree::Node*>::iterator iterUp;
539 for(iterUp=up.begin();iterUp!=up.end();++iterUp)
541 GimmickDebugMessage(5,
542 "Requesting image from neighbors up "
543 <<(*iterUp)->GetAttribute("FullFileName")
545 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
546 RequestReading(*iterUp,prio,-1,ph);
547 // AddEntryToMap(*iterUp);
549 if (prio == maxprio) break;
554 std::vector<tree::Node*> down;
555 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(down,false);
556 std::vector<tree::Node*>::iterator iterDown;
557 for(iterDown=down.begin();iterDown!=down.end();++iterDown)
559 GimmickDebugMessage(5,
560 "Requesting image from neighbors down "
561 <<(*iterDown)->GetAttribute("FullFileName")
563 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
564 RequestReading(*iterDown,prio,-1,ph);
565 // AddEntryToMap(*iterDown);
567 if (prio == maxprio) break;
573 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
574 pointers.push_back(ph);
575 mViewer->SetImageVector(pointers);
579 //==================================================
581 //==================================================
583 //==================================================
584 void WxGimmickView::OnInternalIdle()
586 if (!mConstructed) return;
587 static bool first_time = true;
592 // GimmickMessage(1,"WxGimmickView : Refresh viewer"<<std::endl);
593 // mViewer->StartPlayer();
596 mViewer->RefreshIfNecessary();
600 //==================================================
602 //==================================================
603 void WxGimmickView::ClearSelection()
606 pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
607 mViewer->SetImageVector(pointers);
608 mViewer->RefreshIfNecessary();
612 //=================================================
614 //=================================================
615 void WxGimmickView::OnRemove(wxCommandEvent& event)
617 //TODO Select current tree handler
619 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->RemoveSelected();
622 //=================================================
625 //=================================================
626 void WxGimmickView::AddIgnoreFile(tree::Node* toRemove)
628 mGimmick->RemoveFile(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),toRemove);
629 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
632 //=================================================
633 void WxGimmickView::CopyFiles(const std::vector<std::string>& filenames)
635 mGimmick->CopyFiles(filenames, crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())));
636 wxMessageBox(std2wx("The selected files have been copied"),_T("Copy files"),wxOK,this);
639 //=================================================
640 void WxGimmickView::OnSynchronize(wxCommandEvent& event)
643 const wxString choices[] = { _T("Check database for files deletion and addition and give a report."),
644 _T("Check database for files deletion, addition and attributes change. Then give a report."),
645 _T("Repair database (remove deleted files and add new files)."),
646 _T("Repair database (remove deleted files, add new files and reset changed attributes).") } ;
648 wxSingleChoiceDialog dialog(this,
649 _T("Select one of the following synchronization actions:\n")
650 _T("Please note that, due to the heavy amount of operations required, this action might take a while."),
651 _T("Synchronization Settings"),
652 WXSIZEOF(choices), choices);
654 //dialog.SetSelection(0);
656 if (dialog.ShowModal() == wxID_OK)
658 int sel=dialog.GetSelection();
660 bool checkAttributes=false;
661 if(sel==2 || sel==3){repair=true;}
662 if(sel==1 || sel==3){checkAttributes=true;}
663 std::string mess=mGimmick->Synchronize(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),repair, checkAttributes);
664 wxMessageBox(std2wx(mess),_T("Synchronization result"),wxOK,this);
665 if(sel==2 || sel==3){
666 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
671 //=================================================
673 //=================================================
674 void WxGimmickView::OnSettings(wxCommandEvent& event)
676 wxDialog* dial= new wxDialog (this,-1,"System Settings",wxDefaultPosition, wxSize(450,600));
677 wxBoxSizer *siz = new wxBoxSizer(wxVERTICAL);
679 wxNotebook* nb= new wxNotebook(dial,
680 -1,wxDefaultPosition, wxDefaultSize, 0);
682 siz->Add( nb,1,wxGROW ,0);
683 CreateSettingsDialog(nb);
687 //=================================================
689 //=================================================
691 void WxGimmickView::OnTools(wxCommandEvent& event)
693 wxDialog* dial= new wxDialog (this,-1,"Tools",wxDefaultPosition, wxDefaultSize);
694 wxBoxSizer *siz = new wxBoxSizer(wxVERTICAL);
698 //=================================================
700 void WxGimmickView::CreateSettingsDialog(wxNotebook* nb)
702 //First page: Customization of configurations
703 wxPanel* customConfig=new wxPanel(nb);
704 nb->AddPage( customConfig, crea::std2wx("Customize Configuration") );
705 //Second page: Creation of Databases
706 wxPanel* databaseCreation=new wxPanel(nb);
707 nb->AddPage( databaseCreation, crea::std2wx("Create Database") );
708 //Third page: CD/DVD Watch
709 wxPanel* cdWatch=new wxPanel(nb);
710 nb->AddPage( cdWatch, crea::std2wx("CD/DVD") );
711 //Fourth page: Selection of attributes to show (edit)
712 wxPanel* attSelection=new wxPanel(nb);
713 nb->AddPage( attSelection, crea::std2wx("Selection of Attributes") );
718 //=================================================
719 /// AddProgress Gimmick callback
720 void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
725 sprintf(mess,"%i dirs : %i files :\n %i handled - %i added",
726 p.GetNumberScannedDirs(),
727 p.GetNumberScannedFiles(),
728 p.GetNumberHandledFiles(),
729 p.GetNumberAddedFiles());
730 // std::cout << "OnAddProgress "<<mess<<std::endl;
731 wxString s(wxString::From8BitData(mess));
732 // std::cout << "Pulse"<<std::endl;
733 if (!mProgressDialog->Pulse(s))
737 // std::cout << "OnAddProgress ok"<<std::endl;
739 //=================================================
741 //=================================================
742 void WxGimmickView::DisplayAddSummary()
744 const Gimmick::AddProgress& p = mGimmick->GetAddProgress();
745 std::stringstream mess;
746 mess << "Dirs \tscanned\t: " << p.GetNumberScannedDirs() << "\n";
747 mess << "Files\tscanned\t: " << p.GetNumberScannedFiles() << "\n";
748 mess << "Files\thandled\t: " << p.GetNumberHandledFiles() << "\n\n";
749 mess << "Files\tadded \t: " << p.GetNumberAddedFiles() << "\n\n";
752 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",
754 (int)( summary.parse_time*100./summary.total_time),
755 summary.file_scan_time,
756 (int)(summary.file_scan_time*100./summary.total_time),
757 summary.update_structs_time,
758 (int)(summary.update_structs_time*100./summary.total_time),
759 summary.update_database_time,
760 (int)(summary.update_database_time*100./summary.total_time),
761 summary.total_time );
765 wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
768 //////////////////////////////////////////////////
769 // Add a DB to application //
770 // @param event : WxEvent //
772 //////////////////////////////////////////////////
773 void WxGimmickView::OnAddDB(wxCommandEvent& event)
777 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
778 std::string wc("*.sqlite3*");
779 wxFileDialog* FD = new wxFileDialog( 0,
787 if (FD->ShowModal()==wxID_OK)
792 std::stringstream st;
793 for(int i = 0; i< files.size(); i++)
795 //get name of DB (file name)
796 size_t pos = files[i].find_last_of(_T("\\"));
797 std::string name = crea::wx2std(files[i].substr(pos+1));
798 pos = name.find_last_of(".");
799 name = name.substr(0,pos);
801 mGimmick->addDB(name, crea::wx2std(files[i]));
803 CreateSingleTreeView(name);
807 ////////////////////////////////////////////////////
808 // Create a DB from an Attributes Descriptor files //
809 // @param event : WxEvent //
811 //////////////////////////////////////////////////
812 void WxGimmickView::OnCreateDB(wxCommandEvent& event)
816 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
817 std::string wc("*.txt");
818 wxFileDialog* FD = new wxFileDialog( 0,
826 if (FD->ShowModal()==wxID_OK)
831 //get name of DB (file name)
832 std::string file = crea::wx2std(afile[0]);
833 size_t pos = file.find_last_of("\\");
834 std::string name = file.substr(pos+1);
835 std::string directory = file.substr(0,pos);
836 pos = name.find_last_of(".");
837 name = name.substr(0,pos);
838 //get directory to store DB
839 directory += "\\" + name + ".sqlite3";
841 mGimmick->createDB(name, file,directory);
843 mGimmick->addDB(name, directory);
845 CreateSingleTreeView(name);
849 //=================================================
851 //=================================================
852 BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
853 EVT_TOOL(TOOL_CREATEDB_ID, WxGimmickView::OnCreateDB)
854 EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
855 EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
856 EVT_TOOL(TOOL_ADDDATABASE_ID, WxGimmickView::OnAddDB)
857 EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
858 EVT_TOOL(TOOL_SYNCHRONIZE_ID, WxGimmickView::OnSynchronize)
859 EVT_TOOL(TOOL_SETTINGS_ID, WxGimmickView::OnSettings)
860 EVT_TOOL(TOOL_TOOLS_ID, WxGimmickView::OnTools)
862 //=================================================
864 } // EO namespace creaImageIO