]> Creatis software - creaImageIO.git/blob - src2/creaImageIOWxTreeView.cpp
Debugged the thread and added a deletion on exit
[creaImageIO.git] / src2 / creaImageIOWxTreeView.cpp
1 #include <creaImageIOWxTreeView.h>
2 #include <creaImageIOGimmickView.h>
3 #include <creaImageIOSystem.h>
4 #include <wx/splitter.h>
5 ///Comparing function for ordering algorithm. Takes parameters as strings.
6 int wxCALLBACK CompareFunctionStrings(long item1, long item2, long sortData)
7 {       
8         std::string s1((char*)((long*)item1)[1]);
9         std::string s2((char*)((long*)item2)[1]);
10         if(sortData==1)
11         {
12                 // inverse the order
13                 if (s1 < s2)
14                         return 1;
15                 if (s1 > s2)
16                         return -1;
17
18                 return 0;
19         }
20         else
21         {
22                 if (s1 < s2)
23                         return -1;
24                 if (s1 > s2)
25                         return 1;
26
27                 return 0;
28
29         }
30         
31 }
32
33 ///Comparing function for ordering algorithm. Takes parameters as ints.
34 int wxCALLBACK CompareFunctionInts(long item1, long item2, long sortData)
35 {       
36         int val1=atoi((char*)((long*)item1)[1]);
37         int val2=atoi((char*)((long*)item2)[1]);
38         if(sortData==1)
39         {
40                 // inverse the order
41                 if (val1 < val2)
42                         return 1;
43                 if (val1 > val2)
44                         return -1;
45
46                 return 0;
47         }
48         else
49         {
50                 if (val1 < val2)
51                         return -1;
52                 if (val1 > val2)
53                         return 1;
54
55                 return 0;
56
57         }
58         
59 }
60
61 namespace creaImageIO
62 {
63   //=====================================================================
64   // CTor
65   WxTreeView::WxTreeView(TreeHandler* handler,
66                          GimmickView* gimmick,
67                          wxWindow* parent,
68                          const wxWindowID id)
69     : wxPanel(parent,id),
70       TreeView(handler,gimmick)
71   {
72     GimmickDebugMessage(1,"WxTreeView::WxTreeView"
73                         <<std::endl);
74
75     
76     // Split part below toolbar into notebook for views and panel
77     // for preview, messages...
78     // TO DO : Splitter
79     //    mSplitter = new wxSplitterWindow( this , -1);
80
81     // Global sizer
82     wxBoxSizer  *sizer = new wxBoxSizer(wxHORIZONTAL);
83     
84     int ctrl_style = wxLC_REPORT | wxLC_VRULES;
85     int col_style = wxLIST_FORMAT_LEFT;
86
87     // Creating the ListCtrl for the levels > 0 (not for Root level)
88     for (int i = 1;
89          i < handler->GetTree().GetNumberOfLevels();
90          ++i)
91       {
92
93         GimmickDebugMessage(5,"Creating view for level "<<i
94                             <<std::endl);
95         LevelType level;
96
97         // If the first level : parent = this
98         wxWindow* sparent = this;
99         // else parent = last splitter
100         if (i>1) sparent = mLevelList[i-2].wxSplitter;
101
102         level.wxSplitter = new wxSplitterWindow( sparent , -1);
103         //          level.wxSplitter->SetMinimumPaneSize(100);
104         
105         wxListCtrl* ctrl = new wxListCtrl(level.wxSplitter,
106                                           i,
107                                           wxDefaultPosition, 
108                                           wxDefaultSize,
109                                           ctrl_style);
110         level.wxCtrl = ctrl;
111         level.wxSplitter->Initialize(ctrl);
112
113         // Create the columns : one for each attribute of the level
114         int col = 0;
115         
116         tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
117         for (a  = handler->GetTree().GetAttributeDescriptorList(i).begin();
118              a != handler->GetTree().GetAttributeDescriptorList(i).end();
119              ++a)
120           {
121                   if(col==0)
122                   {
123                     wxListItem it;
124                     it.SetTextColour(*wxRED);
125                     it.SetText(_T("#C"));
126                 
127                     ctrl->InsertColumn(col,it);
128                     col++;
129                   }
130                   
131                 GimmickDebugMessage(5,"Creating column "<<col<<" : "
132                                 <<a->GetName()
133                                 <<std::endl);
134                 ctrl->InsertColumn(col, 
135                                 crea::std2wx(a->GetName()),
136                                 col_style);
137                 level.key.push_back(a->GetKey());
138                 //          ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE );
139                 col++;
140                 
141                 
142           }
143           
144         mLevelList.push_back(level);
145       }
146     
147
148     /// Initialize the first level splitter
149  
150       sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW  ,0);
151         mColumnSelected=1;
152         mLastSelected=0;
153         mDirection=true;
154     UpdateLevel(1);
155
156     SetSizer( sizer );     
157     SetAutoLayout(true);
158     Layout();
159
160   }
161   //=====================================================================
162
163   //=====================================================================
164   /// Destructor
165   WxTreeView::~WxTreeView()
166   {
167     GimmickDebugMessage(1,"WxTreeView::~WxTreeView"
168                         <<std::endl);
169   }
170   //=====================================================================
171   
172   
173   //=====================================================================
174   struct ItemData
175   {
176     tree::Node* node;
177         int id;
178   };
179  
180   //=====================================================================
181    std::vector<tree::Node*> WxTreeView::GetSelected(int level)
182   {
183     int l = level - 1;
184     // the selection of upper level
185     std::vector<tree::Node*> sel;
186         
187     if (level == 1) 
188       {
189         sel.push_back(GetTreeHandler()->GetTree().GetTree());
190       }
191     else 
192       {
193         int n = GetCtrl(l-1)->GetItemCount();
194         for (int i = 0; i < n; i++)
195           {
196             if ( GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED))
197               {
198                 long adr = GetCtrl(l-1)->GetItemData(i);
199                 tree::Node* n = ((ItemData*)adr)->node;
200                         if(mLastSelected==i)
201                         {
202                                 std::vector<tree::Node*>::iterator it;
203                                 it = sel.begin();
204                                 it = sel.insert ( it , n );
205                         }
206                         else
207                         {
208                                 sel.push_back(n);
209                         }
210               }
211           }     
212       }
213          
214           return sel;
215   }
216
217   //=====================================================================
218   
219   ///Removes selected nodes on given level
220   void WxTreeView::RemoveSelected( int level )
221   {
222           std::vector<tree::Node*> sel=GetSelected(level+1);
223           bool erase=false;
224           if (wxMessageBox(_T("Delete file(s) ?"),
225                          _T("Remove Files"),
226                          wxYES_NO,this ) == wxYES)
227           {
228             erase = true;
229           }
230           if(erase)
231           {
232                 std::vector<tree::Node*>::iterator i;
233                 for (i=sel.begin(); i!=sel.end(); ++i)
234                 {
235                         GimmickDebugMessage(2,
236                                         "deleting '"
237                                         <<(*i)->GetLabel()
238                                         <<"'"<<level
239                                         <<std::endl);
240                                 GetTreeHandler()->Remove(*i);
241                 }
242
243                 UpdateLevel(level);
244           }
245           
246   }
247     
248
249   //=====================================================================
250
251  
252   //=====================================================================
253   /// Updates a level of the view (adds or removes children, proganizes, etc.)
254   void WxTreeView::UpdateLevel( int level )
255   {
256     GimmickDebugMessage(1,
257                         GetTreeHandler()->GetTree().GetLabel()
258                         <<" view : updating level "<<level
259                         <<std::endl);
260     
261     wxBusyCursor busy;
262     RecursiveUpdateLevel(level);
263     int i;
264     for (i=0; i<level-1; i++)
265       {
266         if (!GetSplitter(i)->IsSplit()) 
267           GetSplitter(i)->SplitVertically(  GetCtrl(i), GetSplitter(i+1),
268                                             100 );
269       }
270     if (GetSplitter(i)->IsSplit()) GetSplitter(i)->Unsplit();    
271     
272   }
273   //=====================================================================
274   
275   /// Recursive method called upon by UpdateLevel to refresh all windows
276   void WxTreeView::RecursiveUpdateLevel( int level )
277   {
278           GimmickDebugMessage(2,
279                         GetTreeHandler()->GetTree().GetLabel()
280                         <<" view : updating level (recursive)"<<level
281                         <<std::endl);
282
283
284     std::vector<tree::Node*> sel=GetSelected(level);
285
286           int l = level - 1;
287  
288     // to speed up inserting we hide the control temporarily
289     GetCtrl(l)->Hide();
290     GetCtrl(l)->DeleteAllItems();
291     
292     std::vector<tree::Node*>::iterator i;
293     for (i=sel.begin(); i!=sel.end(); ++i)
294       {
295         GimmickDebugMessage(2,
296                             "adding children of '"
297                             <<(*i)->GetLabel()
298                             <<"'"<<level
299                             <<std::endl);
300         int _id=0;
301         //Adds columns
302         GetTreeHandler()->LoadChildren(*i,1);
303         tree::Node::ChildrenListType::reverse_iterator j;
304         for (j = (*i)->GetChildrenList().rbegin(); 
305              j!= (*i)->GetChildrenList().rend(); 
306              ++j)
307           {
308             wxListItem item;
309             item.SetMask(wxLIST_MASK_STATE | 
310                          wxLIST_MASK_TEXT |
311                          //                      wxLIST_MASK_IMAGE |
312                          wxLIST_MASK_DATA |
313                          //                      wxLIST_MASK_WIDTH |
314                          wxLIST_MASK_FORMAT
315                          );
316
317             ItemData* data = new ItemData;
318             
319                 data->node = *j;
320                 item.SetId(_id);
321
322                 data->id = _id;
323             item.SetData(data);
324             
325                 _id++;
326             long id=GetCtrl(l)->InsertItem(item);
327                 
328             std::ostringstream oss;
329             int n= GetTreeHandler()->GetNumberOfChildren(*j);
330             
331             oss << n;
332             std::string s(oss.str());
333             
334                 
335             item.SetText( crea::std2wx(s));
336             //      item.SetTextColour(*wxRED);
337             //      item.SetBackgroundColour(*wxBLACK); 
338             item.SetColumn(0);
339
340                 //Sets the last level as selecte....How to make it select only the first time?
341                 //if (level==mLevelList.size()) item.SetState(wxLIST_STATE_SELECTED);
342
343             GetCtrl(l)->SetItem(item);
344                 
345             //      GetCtrl(l)->SetItem(id,0, crea::std2wx(s));
346             //      GetCtrl(l)->SetColumnWidth(0, wxLIST_AUTOSIZE );
347
348           for (int k=1; k<GetCtrl(l)->GetColumnCount(); k++)
349               {
350   
351                 std::string val = (*j)->GetAttribute(mLevelList[l].key[k-1]);
352                 if (val.size()==0) val = "?";
353                 item.SetText( crea::std2wx(val));
354                 //              item.SetTextColour(*wxBLACK);
355                 //              item.SetBackgroundColour(*wxWHITE); 
356                 item.SetColumn(k);
357                 GetCtrl(l)->SetItem(item);
358
359                 //              GetCtrl(l)->SetItem(id,k, crea::std2wx(val));
360                 // GetCtrl(l)->SetColumnWidth(k, wxLIST_AUTOSIZE );
361                 
362               }
363             
364           }
365       }
366     
367     GetCtrl(l)->Show();
368
369     
370     if (level<mLevelList.size()) UpdateLevel(level+1);
371  }
372   //=====================================================================
373
374
375   //================================================================
376   void WxTreeView::OnSelectedChanged(wxListEvent& event)
377   { 
378     GimmickDebugMessage(1,
379                         GetTreeHandler()->GetTree().GetLabel()
380                         <<" view : item selected "
381                         <<std::endl);
382
383     wxListItem info;
384     info.m_itemId = event.m_itemIndex;
385     mLastSelected=event.m_itemIndex;
386         
387     // retrieve the level
388     wxObject* obj = event.GetEventObject();   
389     unsigned int level = 0;
390     for (level = 0; level<mLevelList.size(); ++level)
391       {
392         if ( GetCtrl(level) == obj ) break;
393       }
394     GimmickDebugMessage(1,
395                         " Level "<<level+1
396                         <<std::endl);
397
398         
399         mSelected=GetSelected(level+2);
400         
401     if (level<mLevelList.size()-1) UpdateLevel( level + 2 ); 
402
403     if (level==mLevelList.size()-1) ValidateSelectedImages ();
404
405   }
406   //================================================================
407
408 //================================================================
409   void WxTreeView::OnColClick(wxListEvent& event)
410   {       
411         //Obtain the column name and the level that needs to be organized
412         mColumnSelected=event.m_col;
413     GimmickDebugMessage(1,
414                 " Column selected: " <<event.m_col
415                         <<std::endl);
416
417         if(mColumnSelected!=0)
418         {
419                 wxObject* ctrl = event.GetEventObject(); 
420                 unsigned int level = 0;
421                 for (level = 0; level<mLevelList.size(); ++level)
422                 {
423                 if ( GetCtrl(level) == ctrl ) break;
424                 }
425                 
426                 UpdateLevel(level+1);
427                 
428                 wxBusyCursor busy;
429          
430                 int l = level - 1;
431                 
432                 //Sets the data for the items to be sorted
433                 std::string att;
434                 unsigned int ty=0;
435                 int n = GetCtrl(level)->GetItemCount();
436                 for (int i = 0; i < n; i++)
437                 {
438                           
439                         //Gets current item data
440                         long adr = GetCtrl(level)->GetItemData(i);
441                         //Extracts the node and the type of attribute
442                         tree::Node* nod = ((ItemData*)adr)->node;
443                         if(i==0)
444                         {
445                                 (*nod).GetAttributeDescriptor(mLevelList[level].key[mColumnSelected-1]).DecodeType(ty);
446                         }
447                         //Obtains the organizing attribute
448                         att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected-1]);
449                         
450                         char* d= new char [att.size()+1];
451                         strcpy (d, att.c_str());
452
453                         //Creates array
454                         long* lp= new long[2];
455                         lp[0]=adr;
456                         lp[1]=(long)d;
457                          
458                         //Sets it as the data
459                         GetCtrl(level)->SetItemData(i,(long)lp);
460                 }       
461                   
462                 if(mDirection)
463                 {
464                         if(ty==1)
465                         {
466                                 GetCtrl(level)->SortItems(CompareFunctionInts, 0);
467                         }
468                         else
469                         {
470                                 GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
471                         }
472                         
473                         mDirection=false;
474                 }
475                 else
476                 {
477                         if(ty==1)
478                         {
479                                 GetCtrl(level)->SortItems(CompareFunctionInts, 1);
480                         }
481                         else
482                         {
483                                 GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
484                         }
485                         mDirection=true;
486                 }
487
488                 //Resets original data
489                 std::vector<tree::Node*>::iterator selection;
490                 std::vector<long> change;
491                 long it = -1;
492                 for ( ;; )
493                 {
494                         it = GetCtrl(level)->GetNextItem(it,
495                                                                                 wxLIST_NEXT_ALL);
496                         if ( it == -1 )
497                                 break;
498                         //Gets current item data, extracts the node and resets it
499                         long item = GetCtrl(level)->GetItemData(it);
500                         GetCtrl(level)->SetItemData(it,((long*)item)[0]);
501                         tree::Node* n= ((ItemData*)((long*)item)[0])->node;
502                         for(selection=mSelected.begin();selection!=mSelected.end();++selection)
503                         {
504                                 if((*selection)->GetAttribute("ID").compare(n->GetAttribute("ID"))==0)
505                                 {
506                                         change.push_back(it);   
507                                 }
508                         }
509                         
510                 }
511                 std::vector<long>::iterator selectedIts;
512                 for(selectedIts=change.begin();selectedIts!=change.end();++selectedIts)
513                 {
514                         GetCtrl(level)->SetItemState(*selectedIts,wxLIST_STATE_SELECTED, wxLIST_MASK_STATE 
515                 | wxLIST_MASK_TEXT |wxLIST_MASK_IMAGE | wxLIST_MASK_DATA | wxLIST_MASK_WIDTH | wxLIST_MASK_FORMAT);
516                         
517                 }
518         }
519         
520    }
521   //================================================================
522
523
524   //================================================================
525   void WxTreeView::ValidateSelectedImages()
526   {
527     GimmickDebugMessage(7,
528                         "WxTreeView::ValidateSelectedImages" 
529                         <<std::endl);
530     std::vector<tree::Node*> sel(GetSelected(mLevelList.size()+1));
531     GetGimmickView()->OnSelectionChange(sel);
532     /*
533     //Send an event telling wether the selection is valid or not
534     wxCommandEvent event( 0, GetId() );
535     event.SetEventObject( this );
536     std::vector<tree::Node*> sel=GetSelected((mLevelList.size()+1));
537     event.SetClientData(&sel);
538     GetEventHandler()->ProcessEvent( event );
539     */
540     // 
541   }
542   //================================================================
543
544
545   //================================================================
546   void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
547   {
548         long item = mLastSelected;
549         int level=mLevelList.size()-1;
550         //Gets current item data
551         long adr = GetCtrl(level)->GetItemData(item);
552         //Extracts the node
553         tree::Node* nod = ((ItemData*)adr)->node;
554     for ( ;; )
555     {
556                 if(direction)
557                 {
558                         item = GetCtrl(level)->GetNextItem(item,
559                                      wxLIST_NEXT_ABOVE);
560                 }
561                 else
562                 {
563                         item = GetCtrl(level)->GetNextItem(item,
564                                      wxLIST_NEXT_BELOW);
565                 }
566         if ( item == -1 )
567                 {
568             break;
569                 }
570                 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0)
571                 {
572                         adr = GetCtrl(level)->GetItemData(item);
573                         nod = ((ItemData*)adr)->node;
574                         nodes.push_back(nod);
575                 }
576     }
577
578   }
579
580   //================================================================
581   void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
582   {
583           int level=mLevelList.size();
584         std::vector<tree::Node*> sel=GetSelected(level+1);
585         std::vector<tree::Node*>::iterator i;
586         
587     for (i=sel.begin(); i!=sel.end(); ++i)
588       {
589                   std::string filename=(*i)->GetAttribute("FullFileName");
590                   s.push_back(filename);
591           }
592   }
593   //================================================================
594   BEGIN_EVENT_TABLE(WxTreeView, wxPanel)
595   /*
596     EVT_SIZE(MyFrame::OnSize)
597
598     EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
599     EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
600     EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
601     EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
602     EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
603     EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
604     EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
605     EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
606     EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
607     EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
608
609     EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
610     EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
611     EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
612     EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
613     EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
614     EVT_MENU(LIST_ADD, MyFrame::OnAdd)
615     EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
616     EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
617     EVT_MENU(LIST_SORT, MyFrame::OnSort)
618     EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
619     EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
620     EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
621     EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
622     EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
623     EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
624     EVT_MENU(LIST_THAW, MyFrame::OnThaw)
625     EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
626     EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
627
628     EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
629     EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
630 END_EVENT_TABLE()
631
632 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
633     EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
634     EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
635     EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
636     EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
637     EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
638     EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
639 #if WXWIN_COMPATIBILITY_2_4
640     EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
641     EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
642 #endif
643   */
644     EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnSelectedChanged)
645   
646     EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnSelectedChanged)
647         /*
648     EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
649     EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
650     EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
651
652     EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
653         */
654     EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
655         /*
656     EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
657     EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
658     EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
659
660     EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
661
662 #if USE_CONTEXT_MENU
663     EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
664 #endif
665     EVT_CHAR(MyListCtrl::OnChar)
666
667     EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
668   */
669 END_EVENT_TABLE()
670   
671 } // EO namespace creaImageIO
672