From e11848f7351056daf6aa018a3adcfcb8519ad779 Mon Sep 17 00:00:00 2001 From: cbeytas Date: Wed, 21 Nov 2018 14:43:07 -0500 Subject: [PATCH] Fix hide items in HypreTreeList Hiding items was only partially implemented and very buggy. This feature does not exist in CustomTreeCtrl but I see no reason why it couldn't be added in the future. This fix requires the arrow key fixes in order for arrow keys to work reliably in a tree with hidden items. Hidden items always report they are disabled and not expanded. Also updates the demo of HyperTreeList to add hide options to context menu so the feature can be tested. --- demo/agw/HyperTreeList.py | 21 +++++++++++++++++ wx/lib/agw/customtreectrl.py | 4 ++-- wx/lib/agw/hypertreelist.py | 45 ++++++++++++++++++++++++++++++++---- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/demo/agw/HyperTreeList.py b/demo/agw/HyperTreeList.py index 4c21c6fa..979c9bf9 100644 --- a/demo/agw/HyperTreeList.py +++ b/demo/agw/HyperTreeList.py @@ -2061,6 +2061,9 @@ class HyperTreeList(HTL.HyperTreeList): item7 = menu.Append(wx.ID_ANY, "Disable Item") + menu.AppendSeparator() + item14 = menu.Append(wx.ID_ANY, "Hide Item") + item15 = menu.Append(wx.ID_ANY, "Unhide All Items") menu.AppendSeparator() item8 = menu.Append(wx.ID_ANY, "Change Item Icons") menu.AppendSeparator() @@ -2084,6 +2087,8 @@ class HyperTreeList(HTL.HyperTreeList): self.Bind(wx.EVT_MENU, self.OnItemPrepend, item11) self.Bind(wx.EVT_MENU, self.OnItemAppend, item12) self.Bind(wx.EVT_MENU, self.OnItemBackground, item13) + self.Bind(wx.EVT_MENU, self.OnHideItem, item14) + self.Bind(wx.EVT_MENU, self.OnUnhideItems, item15) self.PopupMenu(menu) menu.Destroy() @@ -2145,6 +2150,22 @@ class HyperTreeList(HTL.HyperTreeList): event.Skip() + def OnHideItem(self, event): + + self.HideItem(self.current) + event.Skip() + + + def OnUnhideItems(self, event): + + item = self.GetRootItem() + while item: + if item.IsHidden(): + self.HideItem(item, False) + item = self.GetNext(item) + event.Skip() + + def OnItemIcons(self, event): bitmaps = [self.itemdict["normal"], self.itemdict["selected"], diff --git a/wx/lib/agw/customtreectrl.py b/wx/lib/agw/customtreectrl.py index 21221a47..b508c8eb 100644 --- a/wx/lib/agw/customtreectrl.py +++ b/wx/lib/agw/customtreectrl.py @@ -1962,7 +1962,7 @@ class GenericTreeItem(object): self._windowsize = size # We don't show the window if the item is collapsed - if self._isCollapsed: + if not self.IsExpanded(): self._wnd.Show(False) # The window is enabled only if the item is enabled @@ -2617,7 +2617,7 @@ class GenericTreeItem(object): return self, flags # if children are expanded, fall through to evaluate them - if self._isCollapsed: + if not self.IsExpanded(): return None, 0 # evaluate children diff --git a/wx/lib/agw/hypertreelist.py b/wx/lib/agw/hypertreelist.py index 1febddd4..735ded43 100644 --- a/wx/lib/agw/hypertreelist.py +++ b/wx/lib/agw/hypertreelist.py @@ -1325,6 +1325,7 @@ class TreeListItem(GenericTreeItem): self._col_images = [] self._owner = mainWin + self._hidden = False # We don't know the height here yet. self._text_x = 0 @@ -1332,7 +1333,6 @@ class TreeListItem(GenericTreeItem): GenericTreeItem.__init__(self, parent, text, ct_type, wnd, image, selImage, data) self._wnd = [None] # are we holding a window? - self._hidden = False if wnd: self.SetWindow(wnd) @@ -1398,7 +1398,10 @@ class TreeListItem(GenericTreeItem): :see: :meth:`TreeListMainWindow.HitTest() ` method for the flags explanation. """ - + # Hidden items are never evaluated. + if self.IsHidden(): + return None, flags, wx.NOT_FOUND + # for a hidden root node, don't evaluate it, but do evaluate children if not theCtrl.HasAGWFlag(wx.TR_HIDE_ROOT) or level > 0: @@ -1667,7 +1670,7 @@ class TreeListItem(GenericTreeItem): wnd.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) # We don't show the window if the item is collapsed - if self._isCollapsed: + if not self.IsExpanded(): wnd.Show(False) # The window is enabled only if the item is enabled @@ -1777,6 +1780,27 @@ class TreeListItem(GenericTreeItem): return self._wnd[column].GetSize() + def IsExpanded(self): + """ + Returns whether the item is expanded or not. + + :return: ``True`` if the item is expanded, ``False`` if it is collapsed or hidden. + """ + if self.IsHidden(): + return False + return not self._isCollapsed + + + def IsEnabled(self): + """ + Returns whether the item is enabled or not. + + :return: ``True`` if the item is enabled, ``False`` if it is disabled or hidden. + """ + if self.IsHidden(): + return False + return self._enabled + #----------------------------------------------------------------------------- # EditTextCtrl (internal) #----------------------------------------------------------------------------- @@ -2279,7 +2303,8 @@ class TreeListMainWindow(CustomTreeCtrl): :param `item`: an instance of :class:`TreeListItem`; """ - + if item.IsHidden(): + return False # An item is only visible if it's not a descendant of a collapsed item parent = item.GetParent() @@ -3827,7 +3852,11 @@ class TreeListMainWindow(CustomTreeCtrl): :param `item`: an instance of :class:`TreeListItem`; :param `dc`: an instance of :class:`wx.DC`. """ - + if item.IsHidden(): + # Hidden items have a height of 0. + item.SetHeight(0) + return + attr = item.GetAttributes() if attr and attr.HasFont(): @@ -4126,7 +4155,13 @@ class TreeListMainWindow(CustomTreeCtrl): """ item.Hide(hide) + # Recalculate positions and hide child windows. + self.CalculatePositions() + if self._hasWindows: + self.HideWindows() + # Refresh the tree. self.Refresh() + self.AdjustMyScrollbars() #----------------------------------------------------------------------------