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"
16 #include <wx/imaglist.h>
17 #include <wx/popupwin.h>
18 #include<boost/filesystem/operations.hpp>
23 //======================================================================
24 // The ids of the different tools
30 TOOL_ADDDATABASE_ID = 4,
32 TOOL_SYNCHRONIZE_ID = 6,
35 //======================================================================
37 //================================================================
39 const int icon_number = 8;
54 //================================================================
56 //================================================================
58 const icon_id Icon[5] = { Icon_Database,
64 //================================================================
67 //======================================================================
69 WxGimmickView::WxGimmickView(Gimmick* gimmick,
72 const wxPoint& pos, const wxSize& size,
75 int number_of_threads)
76 : wxPanel(parent,id,pos,size),
77 GimmickView(gimmick, number_of_threads),
81 GimmickDebugMessage(1,"WxGimmickView::WxGimmickView"
83 // Sets the current directory to the home dir
84 mCurrentDirectory = std2wx(gimmick->GetHomeDirectory());
86 // Connect the AddProgress callback
87 gimmick->ConnectAddProgressObserver
88 ( boost::bind( &WxGimmickView::OnAddProgress , this, _1 ) );
90 // Create the list of icons (mIcon)
94 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
96 // Create the tool bar
98 sizer->Add( mToolBar ,0, wxGROW ,0);
100 // Split part below toolbar into notebook for views and panel
101 // for preview, messages...
102 mSplitter = new wxSplitterWindow( this , -1);
106 mNotebook = new wxNotebook(mSplitter,
107 -1,wxDefaultPosition, wxDefaultSize, 0);
113 mSelectionMaxDimension= max_dim;
114 mSelectionMinDimension= min_dim;
120 mBottomPanel = new wxPanel(mSplitter,-1);
122 wxBoxSizer *bottom_sizer = new wxBoxSizer(wxVERTICAL); //HORIZONTAL);
126 mViewer = new WxViewer(mBottomPanel, wxID_ANY, wxT("Gimmick! Viewer"),wxDefaultPosition, wxDefaultSize );
127 pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
128 mViewer->SetImageVector(pointers);
129 mViewer->StartPlayer();
132 bottom_sizer->Add(mViewer,1,wxGROW,1);
135 mText = new wxStaticText(mBottomPanel, wxID_ANY, wxT("Welcome to Gimmick!"));
136 bottom_sizer->Add(mText,0,wxGROW,0);
140 mBottomPanel->SetSizer(bottom_sizer);
143 int hsize = size.GetHeight();
145 int top_minsize = 450;
146 int bottom_minsize = 50;
148 mSplitter->SetMinimumPaneSize( bottom_minsize );
149 mSplitter->SplitHorizontally( mNotebook, mBottomPanel,
152 sizer->Add( mSplitter,1,wxGROW ,0);
161 //======================================================================
163 //======================================================================
165 WxGimmickView::~WxGimmickView()
167 // stop the viewer before application exit.
168 mViewer->StopPlayer();
169 GimmickDebugMessage(1,"WxGimmickView::~WxGimmickView"
172 //======================================================================
174 //======================================================================
175 /// Creates the tool bar
176 void WxGimmickView::CreateToolBar()
178 long style = wxTB_HORIZONTAL | wxNO_BORDER | wxTB_TEXT;
179 mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize,
182 mToolAddFile = mToolBar->AddTool( TOOL_CREATEDB_ID,
183 _T("Create database"),
184 mIcon->GetBitmap(Icon_create_database),
185 _T("Create DB from an Attributes Descriptor file")
187 mToolAddFile = mToolBar->AddTool( TOOL_ADDFILES_ID,
189 mIcon->GetBitmap(Icon_page_down),
190 _T("Add one or more file to database")
192 mToolAddDir = mToolBar->AddTool( TOOL_ADDDIR_ID,
194 mIcon->GetBitmap(Icon_folder_down),
195 _T("Add the content of a folder to database")
197 mToolAddDatabase = mToolBar->AddTool( TOOL_ADDDATABASE_ID,
199 mIcon->GetBitmap(Icon_database_add),
200 _T("Open a local or distant database")
202 mToolRemove = mToolBar->AddTool( TOOL_REMOVE_ID,
204 mIcon->GetBitmap(Icon_remove),
205 _T("Remove selected items")
207 mToolSynchronize = mToolBar->AddTool( TOOL_SYNCHRONIZE_ID,
209 mIcon->GetBitmap(Icon_synchronize),
210 _T("Synchronizes the database with disk")
212 mToolHelp = mToolBar->AddTool( TOOL_HELP_ID,
214 mIcon->GetBitmap(Icon_help),
215 _T("Open help window")
217 //const wxBitmap& bitmap1, const wxString& shortHelpString = "", wxItemKind kind = wxITEM_NORMAL)
221 //======================================================================
224 //======================================================================
225 /// Create the tree view for TreeHandler provided
226 void WxGimmickView::CreateTreeView( TreeHandler* h)
228 std::string name(h->GetTree().GetAttribute("Name"));
229 GimmickMessage(2,"Creating the tree view for '"<<
230 name<<"'"<<std::endl);
231 // Create the WxTreeView
232 WxTreeView* view = new WxTreeView(h,this,mNotebook,-1);
234 // TO DO : TEST THAT A VIEW WITH SAME NAME IS NOT
235 // ALREADY IN THE MAP
236 GetTreeViewMap()[name] = view;
239 mNotebook->AddPage( view, crea::std2wx(name) );
243 //======================================================================
244 void WxGimmickView::GetSelectedImages(std::vector<vtkImageData*>& s, int dim)
246 std::vector<std::string> files;
247 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(files);
248 ReadImagesNotThreaded(s,files,dim);
250 //======================================================================
252 //======================================================================
253 void WxGimmickView::GetSelectedFiles(std::vector<std::string>& s)
255 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetSelectedAsString(s);
257 //======================================================================
259 //======================================================================
260 void WxGimmickView::GetImages(int dim,
261 const std::vector<std::string>& files,
262 std::vector<vtkImageData*>& s)
264 ReadImagesNotThreaded(s,files,dim);
266 //======================================================================
269 //=================================================
270 void WxGimmickView::CreateIconList()
272 // Size of the icons;
276 // should correspond to Icon_xxx enum
277 icons[Icon_accept] = wxIcon(accept_xpm);
278 icons[Icon_add] = wxIcon(add_xpm);
279 icons[Icon_folder_down] = wxIcon(folder_down_xpm);
280 icons[Icon_page_down] = wxIcon(page_down_xpm);
281 icons[Icon_remove] = wxIcon(remove_xpm);
282 icons[Icon_database_add] = wxIcon(database_add_xpm);
283 icons[Icon_help] = wxIcon(help_xpm);
284 icons[Icon_synchronize] = wxIcon(synchronize_xpm);
285 icons[Icon_create_database] = wxIcon(create_database_xpm);
287 // unsigned int NbIcons = 8;
288 // Make an image list containing small icons
289 mIcon = new wxImageList(size,size,true);
291 // Make all icons the same size = size of the first one
292 int sizeOrig = icons[0].GetWidth();
293 for ( size_t i = 0; i < icon_number; i++ )
295 if ( size == sizeOrig )
297 mIcon->Add(icons[i]);
301 mIcon->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
305 //=================================================
308 //=================================================
309 void WxGimmickView::OnAddFiles(wxCommandEvent& event)
311 mViewer->StopPlayer();
312 long style = wxOPEN | wxFILE_MUST_EXIST | wxFD_MULTIPLE;
313 std::string wc("*.*");
314 wxFileDialog* FD = new wxFileDialog( 0,
322 if (FD->ShowModal()==wxID_OK)
329 std::vector<std::string> filenames;
330 for (i=0;i<files.GetCount();++i)
332 filenames.push_back(wx2std(files[i]));
333 GimmickMessage(2,"Adding File "<<files[i]<<"."<<std::endl);
337 new wxProgressDialog(_T("Adding file(s)"),
342 // wxPD_ESTIMATED_TIME |
343 // wxPD_REMAINING_TIME |
346 // TO DO : select the current tree handler
347 mGimmick->AddFiles(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),filenames);
349 mProgressDialog->Pulse(_T("Updating view..."));
351 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
352 delete mProgressDialog;
356 mViewer->StartPlayer();
358 //=================================================
360 //=================================================
361 void WxGimmickView::OnAddDir(wxCommandEvent& event)
363 mViewer->StopPlayer();
364 std::string name = crea::wx2std(mNotebook->GetCurrentPage()->GetName());
365 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
368 _T("Select directory"),
372 if (FD->ShowModal()==wxID_OK)
376 std::string dirname = wx2std (FD->GetPath());
377 bool recurse = isNeedRecursive(dirname);
380 recurse = wxMessageBox(_T("Recurse into sub-directories ?"), _T("Scan directory"), wxYES_NO,this ) == wxYES ? true : false;
384 wxString title(_T("Adding directory"));
386 title = _T("Adding directory (recursive)");
388 new wxProgressDialog(_T("Adding directory"),
393 // wxPD_ESTIMATED_TIME |
394 // wxPD_REMAINING_TIME |
397 mCurrentDirectory = FD->GetPath();
398 mGimmick->AddDir(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),dirname,recurse);
399 mProgressDialog->Pulse(_T("Updating view..."));
401 UpdateTreeViewLevel(crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())),1);
402 delete mProgressDialog;
406 mViewer->StartPlayer();
409 //=================================================
410 // Test a directory to know if contains sub-directory to analyze
411 bool WxGimmickView::isNeedRecursive(std::string i_name)
413 boost::filesystem::directory_iterator iter(i_name), end_iter;
414 bool bfindir = false;
415 for(; iter != end_iter; ++iter)
417 if(boost::filesystem::is_directory(*iter))
424 //=================================================
426 //=================================================
427 void WxGimmickView::OnSelectionChange(const std::vector<tree::Node*>& sel, bool isSelection, int selection, bool needProcess)
429 GimmickDebugMessage(5,
430 "WxGimmickView::OnSelectionChange"
437 valid= ValidateSelected(NULL,
438 mSelectionMinDimension,
439 mSelectionMaxDimension );
444 std::vector<tree::Node*>::const_iterator i;
445 for(i=sel.begin();i!=sel.end()&&valid;++i)
447 valid= ValidateSelected((*i),
448 mSelectionMinDimension,
449 mSelectionMaxDimension );
454 valid= ValidateSelected(sel.front(),
455 mSelectionMinDimension,
456 mSelectionMaxDimension );
461 std::vector<tree::Node*>::const_iterator i;
462 for(i=sel.begin();i!=sel.end()&&valid;++i)
464 valid= ValidateSelected((*i),
465 mSelectionMinDimension,
466 mSelectionMaxDimension );
470 mText->SetLabel(crea::std2wx(GetMessage()));
473 ReadImageThreaded(sel);
479 ReadImageThreaded(sel);
484 //==================================================
486 //==================================================
487 ///Reads Images (Threaded)
488 void WxGimmickView::ReadImageThreaded(const std::vector<tree::Node*>& sel)
490 GimmickDebugMessage(5,
493 int maxprio = GetMaximalPriority();
494 int prio = maxprio + 2000;
498 //First load the selected images
499 mCurImageItemToShow = sel.front();
502 std::vector<tree::Node*>::const_iterator selected;
503 for(selected=sel.begin();selected!=sel.end();++selected)
505 GimmickDebugMessage(5,
506 "Requesting image from selected "
507 <<(*selected)->GetAttribute("FullFileName")
509 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
510 pointers.push_back(ph);
511 RequestReading(*selected,prio,index,ph);
512 // AddEntryToMap(*selected);
516 mViewer->SetImageVector(pointers);
519 std::vector<tree::Node*> up;
520 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(up,true);
521 std::vector<tree::Node*>::iterator iterUp;
522 for(iterUp=up.begin();iterUp!=up.end();++iterUp)
524 GimmickDebugMessage(5,
525 "Requesting image from neighbors up "
526 <<(*iterUp)->GetAttribute("FullFileName")
528 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
529 RequestReading(*iterUp,prio,-1,ph);
530 // AddEntryToMap(*iterUp);
532 if (prio == maxprio) break;
537 std::vector<tree::Node*> down;
538 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->GetNodes(down,false);
539 std::vector<tree::Node*>::iterator iterDown;
540 for(iterDown=down.begin();iterDown!=down.end();++iterDown)
542 GimmickDebugMessage(5,
543 "Requesting image from neighbors down "
544 <<(*iterDown)->GetAttribute("FullFileName")
546 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
547 RequestReading(*iterDown,prio,-1,ph);
548 // AddEntryToMap(*iterDown);
550 if (prio == maxprio) break;
556 ImagePointerHolder* ph=new ImagePointerHolder(GetDefaultImage());
557 pointers.push_back(ph);
558 mViewer->SetImageVector(pointers);
562 //==================================================
564 //==================================================
566 //==================================================
567 void WxGimmickView::OnInternalIdle()
569 if (!mConstructed) return;
570 static bool first_time = true;
575 // GimmickMessage(1,"WxGimmickView : Refresh viewer"<<std::endl);
576 // mViewer->StartPlayer();
579 mViewer->RefreshIfNecessary();
583 //==================================================
585 //==================================================
586 void WxGimmickView::ClearSelection()
589 pointers.push_back(new ImagePointerHolder(GetDefaultImage()));
590 mViewer->SetImageVector(pointers);
591 mViewer->RefreshIfNecessary();
595 //=================================================
597 //=================================================
598 void WxGimmickView::OnRemove(wxCommandEvent& event)
600 //TODO Select current tree handler
602 GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->RemoveSelected();
605 //=================================================
608 //=================================================
609 void WxGimmickView::AddIgnoreFile(tree::Node* toRemove)
611 mGimmick->RemoveFile("Local database",toRemove);
612 GetTreeViewMap()["Local database"]->UpdateLevel(1);
615 //=================================================
616 void WxGimmickView::OnSynchronize(wxCommandEvent& event)
619 const wxString choices[] = { _T("Check database for files deletion and addition and give a report."),
620 _T("Check database for files deletion, addition and attributes change. Then give a report."),
621 _T("Repair database (remove deleted files and add new files)."),
622 _T("Repair database (remove deleted files, add new files and reset changed attributes).") } ;
624 wxSingleChoiceDialog dialog(this,
625 _T("Select one of the following synchronization actions:\n")
626 _T("Please note that, due to the heavy amount of operations required, this action might take a while."),
627 _T("Synchronization Settings"),
628 WXSIZEOF(choices), choices);
630 //dialog.SetSelection(0);
632 if (dialog.ShowModal() == wxID_OK)
634 int sel=dialog.GetSelection();
636 bool checkAttributes=false;
637 if(sel==2 || sel==3){repair=true;}
638 if(sel==1 || sel==3){checkAttributes=true;}
639 std::string mess=mGimmick->Synchronize(repair, checkAttributes);
640 wxMessageBox(std2wx(mess),_T("Synchronization result"),wxOK,this);
641 GetTreeViewMap()["Local database"]->UpdateLevel(1);
645 //=================================================
647 //=================================================
648 /// AddProgress Gimmick callback
649 void WxGimmickView::OnAddProgress( Gimmick::AddProgress& p)
654 sprintf(mess,"%i dirs : %i files :\n %i handled - %i added",
655 p.GetNumberScannedDirs(),
656 p.GetNumberScannedFiles(),
657 p.GetNumberHandledFiles(),
658 p.GetNumberAddedFiles());
659 // std::cout << "OnAddProgress "<<mess<<std::endl;
660 wxString s(wxString::From8BitData(mess));
661 // std::cout << "Pulse"<<std::endl;
662 if (!mProgressDialog->Pulse(s))
666 // std::cout << "OnAddProgress ok"<<std::endl;
668 //=================================================
670 //=================================================
671 void WxGimmickView::DisplayAddSummary()
673 const Gimmick::AddProgress& p = mGimmick->GetAddProgress();
674 std::stringstream mess;
675 mess << "Dirs \tscanned\t: " << p.GetNumberScannedDirs() << "\n";
676 mess << "Files\tscanned\t: " << p.GetNumberScannedFiles() << "\n";
677 mess << "Files\thandled\t: " << p.GetNumberHandledFiles() << "\n\n";
678 mess << "Files\tadded \t: " << p.GetNumberAddedFiles() << "\n\n";
681 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",
683 (int)( summary.parse_time*100./summary.total_time),
684 summary.file_scan_time,
685 (int)(summary.file_scan_time*100./summary.total_time),
686 summary.update_structs_time,
687 (int)(summary.update_structs_time*100./summary.total_time),
688 summary.update_database_time,
689 (int)(summary.update_database_time*100./summary.total_time),
690 summary.total_time );
694 wxMessageBox(std2wx(mess.str()),_T("Addition result"),wxOK,this);
697 //////////////////////////////////////////////////
698 // Add a DB to application //
699 // @param event : WxEvent //
701 //////////////////////////////////////////////////
702 void WxGimmickView::OnAddDB(wxCommandEvent& event)
706 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
707 std::string wc("*.sqlite3*");
708 wxFileDialog* FD = new wxFileDialog( 0,
716 if (FD->ShowModal()==wxID_OK)
721 std::stringstream st;
722 for(int i = 0; i< files.size(); i++)
724 //get name of DB (file name)
725 size_t pos = files[i].find_last_of(_T("\\"));
726 std::string name = crea::wx2std(files[i].substr(pos+1));
727 pos = name.find_last_of(".");
728 name = name.substr(0,pos);
730 mGimmick->addDB(name, crea::wx2std(files[i]));
732 CreateSingleTreeView(name);
736 ////////////////////////////////////////////////////
737 // Create a DB from an Attributes Descriptor files //
738 // @param event : WxEvent //
740 //////////////////////////////////////////////////
741 void WxGimmickView::OnCreateDB(wxCommandEvent& event)
745 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
746 std::string wc("*.txt");
747 wxFileDialog* FD = new wxFileDialog( 0,
755 if (FD->ShowModal()==wxID_OK)
760 //get name of DB (file name)
761 std::string file = crea::wx2std(afile[0]);
762 size_t pos = file.find_last_of("\\");
763 std::string name = file.substr(pos+1);
764 std::string directory = file.substr(0,pos);
765 pos = name.find_last_of(".");
766 name = name.substr(0,pos);
767 //get directory to store DB
768 directory += "\\" + name + ".sqlite3";
770 mGimmick->createDB(name, file,directory);
772 mGimmick->addDB(name, directory);
774 CreateSingleTreeView(name);
778 //=================================================
780 //=================================================
781 BEGIN_EVENT_TABLE(WxGimmickView, wxPanel)
782 EVT_TOOL(TOOL_CREATEDB_ID, WxGimmickView::OnCreateDB)
783 EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnCreateDB)
784 EVT_TOOL(TOOL_ADDFILES_ID, WxGimmickView::OnAddFiles)
785 EVT_TOOL(TOOL_ADDDIR_ID, WxGimmickView::OnAddDir)
786 EVT_TOOL(TOOL_ADDDATABASE_ID, WxGimmickView::OnAddDB)
787 EVT_TOOL(TOOL_REMOVE_ID, WxGimmickView::OnRemove)
788 EVT_TOOL(TOOL_SYNCHRONIZE_ID, WxGimmickView::OnSynchronize)
790 //=================================================
792 } // EO namespace creaImageIO