1 #include <creaImageIOWxTreeView.h>
2 #include <creaImageIOGimmickView.h>
3 #include <creaImageIOSystem.h>
4 #include <wx/splitter.h>
6 ///Comparing function for ordering algorithm. Takes parameters as strings.
7 int wxCALLBACK CompareFunctionStrings(long item1, long item2, long sortData)
9 std::string s1((char*)((long*)item1)[1]);
10 std::string s2((char*)((long*)item2)[1]);
34 ///Comparing function for ordering algorithm. Takes parameters as ints.
35 int wxCALLBACK CompareFunctionInts(long item1, long item2, long sortData)
37 int val1=atoi((char*)((long*)item1)[1]);
38 int val2=atoi((char*)((long*)item2)[1]);
64 //=====================================================================
66 WxTreeView::WxTreeView(TreeHandler* handler,
71 TreeView(handler,gimmick)
73 GimmickDebugMessage(1,"WxTreeView::WxTreeView"
77 // Split part below toolbar into notebook for views and panel
78 // for preview, messages...
80 // mSplitter = new wxSplitterWindow( this , -1);
83 wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
85 int ctrl_style = wxLC_REPORT | wxLC_VRULES;
86 int col_style = wxLIST_FORMAT_LEFT;
88 // Creating the ListCtrl for the levels > 0 (not for Root level)
90 i < handler->GetTree().GetNumberOfLevels();
94 GimmickDebugMessage(5,"Creating view for level "<<i
98 // If the first level : parent = this
99 wxWindow* sparent = this;
100 // else parent = last splitter
101 if (i>1) sparent = mLevelList[i-2].wxSplitter;
103 level.wxSplitter = new wxSplitterWindow( sparent , -1);
106 level.wxSplitter->Show(false);
108 // level.wxSplitter->SetMinimumPaneSize(100);
110 wxListCtrl* ctrl = new wxListCtrl(level.wxSplitter,
116 level.wxSplitter->Initialize(ctrl);
119 // Create the columns : one for each attribute of the level
122 tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
123 for (a = handler->GetTree().GetAttributeDescriptorList(i).begin();
124 a != handler->GetTree().GetAttributeDescriptorList(i).end();
128 GimmickDebugMessage(5,"Creating column "<<col<<" : "
132 if(a->GetFlags()!=creaImageIO::tree::AttributeDescriptor::PRIVATE)
135 if(a->GetName()=="UNKNOWN")
138 title += handler->GetTree().GetLevelDescriptor(i+1).GetName();
139 if (title[title.size()-1]!='s')
147 ctrl->InsertColumn(col,
150 level.key.push_back(a->GetKey());
151 // ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE );
157 mLevelList.push_back(level);
163 wxMenuItem* m1=menu->Append(wxID_ANY, _T("&Sort ascending"));
164 wxMenuItem* m2=menu->Append(wxID_ANY, _T("&Sort descending"));
165 wxMenuItem* m3=menu->Append(wxID_ANY, _T("&Filter"));
166 mAscendingID=m1->GetId();
167 mDescendingID=m2->GetId();
168 mFilterID=m3->GetId();
169 Connect( mAscendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
170 Connect( mDescendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
171 Connect( mFilterID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupFilter) );
173 #endif // wxUSE_MENUS
176 /// Initialize the first level splitter
178 sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW ,0);
182 mSelectionMade=false;
184 //CreateColorPalette();
192 //=====================================================================
194 //=====================================================================
196 WxTreeView::~WxTreeView()
198 GimmickDebugMessage(1,"WxTreeView::~WxTreeView"
201 //=====================================================================
204 //=====================================================================
211 //=====================================================================
212 std::vector<tree::Node*> WxTreeView::GetSelected(int level)
215 // the selection of upper level
216 std::vector<tree::Node*> sel;
220 sel.push_back(GetTreeHandler()->GetTree().GetTree());
224 int n = GetCtrl(l-1)->GetItemCount();
225 for (int i = 0; i < n; i++)
227 if ( GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED))
229 long adr = GetCtrl(l-1)->GetItemData(i);
230 tree::Node* n = ((ItemData*)adr)->node;
233 std::vector<tree::Node*>::iterator it;
235 it = sel.insert ( it , n );
248 //=====================================================================
250 ///Removes selected nodes on given level
251 void WxTreeView::RemoveSelected( int level )
253 std::vector<tree::Node*> sel=GetSelected(level+1);
255 if (wxMessageBox(_T("Delete file(s) ?"),
257 wxYES_NO,this ) == wxYES)
263 std::vector<tree::Node*>::iterator i;
264 for (i=sel.begin(); i!=sel.end(); ++i)
266 GimmickDebugMessage(2,
271 GetTreeHandler()->Remove(*i);
280 //=====================================================================
283 //=====================================================================
284 /// Updates a level of the view (adds or removes children, proganizes, etc.)
285 void WxTreeView::UpdateLevel( int level )
287 GimmickDebugMessage(1,
288 GetTreeHandler()->GetTree().GetLabel()
289 <<" view : updating level "<<level
293 RecursiveUpdateLevel(level);
295 for (i=0; i<level-1; i++)
297 if (!GetSplitter(i)->IsSplit())
298 GetSplitter(i)->SplitVertically( GetCtrl(i), GetSplitter(i+1),
301 if (GetSplitter(i)->IsSplit()) GetSplitter(i)->Unsplit();
304 //=====================================================================
306 /// Recursive method called upon by UpdateLevel to refresh all windows
307 void WxTreeView::RecursiveUpdateLevel( int level )
309 GimmickDebugMessage(2,
310 GetTreeHandler()->GetTree().GetLabel()
311 <<" view : updating level (recursive)"<<level
315 std::vector<tree::Node*> sel=GetSelected(level);
319 // to speed up inserting we hide the control temporarily
321 GetCtrl(l)->DeleteAllItems();
323 std::vector<tree::Node*>::iterator i;
325 for (i=sel.begin(); i!=sel.end(); ++i)
327 GimmickDebugMessage(2,
328 "adding children of '"
334 //Adds items and sets their attributes
335 GetCtrl(l)->InsertItem(0, _T(""));
336 GetTreeHandler()->LoadChildren(*i,1);
337 tree::Node::ChildrenListType::reverse_iterator j;
338 for (j = (*i)->GetChildrenList().rbegin();
339 j!= (*i)->GetChildrenList().rend();
342 wxListItem* item= new wxListItem;
343 item->SetMask(wxLIST_MASK_STATE |
345 // wxLIST_MASK_IMAGE |
347 // wxLIST_MASK_WIDTH |
351 ItemData* data = new ItemData;
360 GetCtrl(l)->InsertItem(*item);
364 for (int k=0; k<GetCtrl(l)->GetColumnCount(); k++)
366 std::string val = (*j)->GetAttribute(mLevelList[l].key[k]);
367 if (val.size()==0) val = "?";
368 item->SetText( crea::std2wx(val));
370 GetCtrl(l)->SetItem(*item);
374 GetCtrl(l)->DeleteItem(0);
380 //=====================================================================
383 //================================================================
384 void WxTreeView::OnSelectedChanged(wxListEvent& event)
386 GimmickDebugMessage(1,
387 GetTreeHandler()->GetTree().GetLabel()
388 <<" view : item selected "
392 info.m_itemId = event.m_itemIndex;
393 mLastSelected = event.m_itemIndex;
394 // retrieve the level
395 wxObject* obj = event.GetEventObject();
396 unsigned int level = 0;
397 for (level = 0; level<mLevelList.size(); ++level)
399 if ( GetCtrl(level) == obj ) break;
401 GimmickDebugMessage(1,
405 // Update the children level (if selection not at last level)
406 if (level<mLevelList.size()-1)
408 UpdateLevel( level + 2 );
409 // Reset the viewer setting the default image
410 GetGimmickView()->ClearSelection();
412 // Select all images if the selection is at series level
413 if (level==mLevelList.size()-2) SelectLowerLevel();
414 // Validate selected images if the selection is at image level
415 if (level==(mLevelList.size()-1)&&mProcess)
417 if(event.GetEventType()==10145)
419 ValidateSelectedImages (true);
423 ValidateSelectedImages (false);
428 //================================================================
430 //================================================================
431 void WxTreeView::SelectLowerLevel()
434 int level=mLevelList.size()-1;
437 item = GetCtrl(level)->GetNextItem(item,
442 if(item==(GetCtrl(level)->GetItemCount()-1))
450 GetCtrl(level)->SetItemState(item,wxLIST_STATE_SELECTED, wxLIST_MASK_STATE
451 | wxLIST_MASK_TEXT |wxLIST_MASK_IMAGE | wxLIST_MASK_DATA | wxLIST_MASK_WIDTH | wxLIST_MASK_FORMAT);
455 //================================================================
456 //================================================================
458 void WxTreeView::OnColClick(wxListEvent& event)
460 mColumnSelected=event.m_col;
462 clientpt.x = wxGetMousePosition().x - this->GetScreenPosition().x;
463 clientpt.y = wxGetMousePosition().y - this->GetScreenPosition().y;
464 senderCtrl = event.GetEventObject();
465 unsigned int level = 0;
466 for (level = 0; level<mLevelList.size(); ++level)
468 if ( GetCtrl(level) == senderCtrl ) break;
470 PopupMenu(menu, clientpt);
474 //================================================================
475 //================================================================
477 void WxTreeView::OnPopupFilter(wxCommandEvent& event)
480 GimmickDebugMessage(7,
481 "WxTreeView::OnEndLabelEdit"
483 wxObject* ctrl = event.GetEventObject();
484 unsigned int level = 0;
485 for (level = 0; level<mLevelList.size(); ++level)
487 if ( GetCtrl(level) == senderCtrl ) break;
489 std::string filter = crea::wx2std(wxGetTextFromUser(_T("Enter the filter to apply"), _T("Filter On Column")));
494 UpdateLevel(level+1);
496 std::vector<long> items;
501 it = GetCtrl(level)->GetNextItem(it,
506 long adr = GetCtrl(level)->GetItemData(it);
507 tree::Node* nod = ((ItemData*)adr)->node;
508 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected]);
511 if(att.find(filter)>900)
523 items.push_back(it-del);
527 std::vector<long>::iterator iter;
528 for(iter=items.begin();iter!=items.end();++iter)
530 GetCtrl(level)->DeleteItem(*iter);
532 GetGimmickView()->ClearSelection();
534 //================================================================
536 //================================================================
537 void WxTreeView::OnPopupSort(wxCommandEvent& event)
540 unsigned int level = 0;
541 for (level = 0; level<mLevelList.size(); ++level)
543 if ( GetCtrl(level) == senderCtrl ) break;
545 if(event.GetId()==mAscendingID)
549 else if(event.GetId()==mDescendingID)
556 //================================================================
557 void WxTreeView::OnSort(int level)
559 //Obtain the column name and the level that needs to be organized
562 //Sets the data for the items to be sorted
565 int n = GetCtrl(level)->GetItemCount();
566 for (int i = 0; i < n; i++)
569 //Gets current item data
570 long adr = GetCtrl(level)->GetItemData(i);
572 //Extracts the node and the type of attribute
573 tree::Node* nod = ((ItemData*)adr)->node;
576 (*nod).GetAttributeDescriptor(mLevelList[level].key[mColumnSelected]).DecodeType(ty);
578 //Obtains the organizing attribute
579 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected]);
581 char* d= new char [att.size()+1];
582 strcpy (d, att.c_str());
585 long* lp= new long[2];
589 //Sets it as the data
590 GetCtrl(level)->SetItemData(i,(long)lp);
597 GetCtrl(level)->SortItems(CompareFunctionInts, 0);
601 GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
609 GetCtrl(level)->SortItems(CompareFunctionInts, 1);
613 GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
617 //Resets original data
619 std::vector<tree::Node*>::iterator selection;
620 std::vector<long> change;
624 it = GetCtrl(level)->GetNextItem(it,
629 //Gets current item data, extracts the node and resets it
630 long item = GetCtrl(level)->GetItemData(it);
631 GetCtrl(level)->SetItemData(it,((long*)item)[0]);
632 tree::Node* n= ((ItemData*)((long*)item)[0])->node;
638 //================================================================
639 void WxTreeView::ValidateSelectedImages(bool isSelection)
641 GimmickDebugMessage(7,
642 "WxTreeView::ValidateSelectedImages"
644 std::vector<tree::Node*> sel(GetSelected(mLevelList.size()+1));
645 GetGimmickView()->OnSelectionChange(sel,isSelection,(mLastSelected-1), mProcess);
648 //================================================================
651 //================================================================
652 void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
654 long item = mLastSelected;
655 int level=mLevelList.size()-1;
656 //Gets current item data
657 long adr = GetCtrl(level)->GetItemData(item);
659 tree::Node* nod = ((ItemData*)adr)->node;
664 item = GetCtrl(level)->GetNextItem(item,
669 item = GetCtrl(level)->GetNextItem(item,
676 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0 /*&& item!=0*/)
678 adr = GetCtrl(level)->GetItemData(item);
679 nod = ((ItemData*)adr)->node;
680 nodes.push_back(nod);
686 //================================================================
687 void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
689 int level=mLevelList.size();
690 std::vector<tree::Node*> sel=GetSelected(level+1);
691 std::vector<tree::Node*>::iterator i;
693 for (i=sel.begin(); i!=sel.end(); ++i)
695 std::string filename=(*i)->GetAttribute("FullFileName");
696 s.push_back(filename);
700 //================================================================
701 void WxTreeView::SetColor(int l, int item)
704 GetCtrl(l)->SetItemTextColour(item, wxColourDatabase().Find
705 (crea::std2wx(mColorPalette[colorId])));
706 GetCtrl(l)->SetItemState(item,wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED); /*
708 //Setting the color according to the parent
711 item.SetBackgroundColour
712 (wxColourDatabase().Find
713 (crea::std2wx(mColorPalette[colorId])));
716 (*j,wxColourDatabase().Find
717 (crea::std2wx(mColorPalette[colorId]))));
727 else if(l!=mLevelList.size()-1)
729 item.SetBackgroundColour(mColorMap[*i]);
730 mColorMap.insert(NodeColorPair(*j,mColorMap[*i]));
734 item.SetBackgroundColour(mColorMap[*i]);
737 //================================================================
738 void WxTreeView::CreateColorPalette()
740 GimmickDebugMessage(6,"WxTreeView::CreateColorPalette");
741 mColorPalette.push_back("WHITE");
742 mColorPalette.push_back("LIGHT GREY");
743 mColorPalette.push_back("AQUAMARINE");
744 mColorPalette.push_back("MEDIUM FOREST GREEN");
745 mColorPalette.push_back("INDIAN RED");
746 mColorPalette.push_back("KHAKI");
747 mColorPalette.push_back("ORANGE");
748 mColorPalette.push_back("LIGHT BLUE");
749 mColorPalette.push_back("LIGHT STEEL BLUE");
750 mColorPalette.push_back("PINK");
751 mColorPalette.push_back("PLUM");
752 mColorPalette.push_back("PURPLE");
753 mColorPalette.push_back("RED");
754 mColorPalette.push_back("SEA GREEN");
755 mColorPalette.push_back("SIENNA");
756 mColorPalette.push_back("SKY BLUE");
757 mColorPalette.push_back("SLATE BLUE");
758 mColorPalette.push_back("SPRING GREEN");
759 mColorPalette.push_back("TAN");
760 mColorPalette.push_back("THISTLE");
761 mColorPalette.push_back("TURQUOISE");
762 mColorPalette.push_back("VIOLET");
763 mColorPalette.push_back("VIOLET RED");
764 mColorPalette.push_back("WHEAT");
765 mColorPalette.push_back("YELLOW");
766 mColorPalette.push_back("YELLOW GREEN");
767 mColorPalette.push_back("BLUE");
768 mColorPalette.push_back("BLUE VIOLET");
769 mColorPalette.push_back("BROWN");
770 mColorPalette.push_back("CADET BLUE");
771 mColorPalette.push_back("CORAL");
772 mColorPalette.push_back("CORNFLOWER BLUE");
773 mColorPalette.push_back("CYAN");
774 mColorPalette.push_back("DARK GREY");
775 mColorPalette.push_back("DARK GREEN");
776 mColorPalette.push_back("DARK OLIVE GREEN");
777 mColorPalette.push_back("DARK ORCHID");
778 mColorPalette.push_back("DARK SLATE BLUE");
779 mColorPalette.push_back("DARK SLATE GREY");
780 mColorPalette.push_back("DARK TURQUOISE");
781 mColorPalette.push_back("FIREBRICK");
782 mColorPalette.push_back("FOREST GREEN");
783 mColorPalette.push_back("GOLD");
784 mColorPalette.push_back("GOLDENROD");
785 mColorPalette.push_back("GREY");
786 mColorPalette.push_back("GREEN");
787 mColorPalette.push_back("GREEN YELLOW");
788 mColorPalette.push_back("LIME GREEN");
789 mColorPalette.push_back("MAGENTA");
790 mColorPalette.push_back("MAROON");
791 mColorPalette.push_back("MEDIUM AQUAMARINE");
792 mColorPalette.push_back("MEDIUM BLUE");
793 mColorPalette.push_back("MEDIUM GOLDENROD");
794 mColorPalette.push_back("MEDIUM ORCHID");
795 mColorPalette.push_back("MEDIUM SEA GREEN");
796 mColorPalette.push_back("MEDIUM SLATE BLUE");
797 mColorPalette.push_back("MEDIUM SPRING GREEN");
798 mColorPalette.push_back("MEDIUM TURQUOISE");
799 mColorPalette.push_back("MEDIUM VIOLET RED");
800 mColorPalette.push_back("MIDNIGHT BLUE");
801 mColorPalette.push_back("NAVY");
802 mColorPalette.push_back("ORANGE RED");
803 mColorPalette.push_back("ORCHID, PALE GREEN");
804 mColorPalette.push_back("STEEL BLUE");
805 mColorPalette.push_back("BLACK");
809 //================================================================
810 BEGIN_EVENT_TABLE(WxTreeView, wxPanel)
812 EVT_SIZE(MyFrame::OnSize)
814 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
815 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
816 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
817 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
818 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
819 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
820 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
821 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
822 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
823 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
825 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
826 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
827 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
828 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
829 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
830 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
831 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
832 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
833 EVT_MENU(LIST_SORT, MyFrame::OnSort)
834 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
835 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
836 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
837 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
838 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
839 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
840 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
841 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
842 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
844 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
845 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
848 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
849 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
850 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
852 EVT_LIST_BEGIN_LABEL_EDIT(-1, WxTreeView::OnBeginLabelEdit)
853 EVT_LIST_END_LABEL_EDIT(-1, WxTreeView::OnEndLabelEdit)
855 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
856 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
857 #if WXWIN_COMPATIBILITY_2_4
858 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
859 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
862 EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnSelectedChanged)
864 EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnSelectedChanged)
866 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
867 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
868 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
870 EVT_LIST_COL_RIGHT_CLICK(-1, WxTreeView::OnColClick)
872 EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
874 //EVT_LEFT_DOWN(WxTreeView::OnMouseClick)
876 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
877 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
878 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
880 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
883 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
885 EVT_CHAR(MyListCtrl::OnChar)
887 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
891 } // EO namespace creaImageIO