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