From fbe09519505bd9361183a82c12b3af5df5f443af Mon Sep 17 00:00:00 2001 From: "Pappenreiter.Thomas" Date: Wed, 28 Aug 2019 14:26:32 +0200 Subject: [PATCH] Feature: Colouring columns in hypertreelist --- unittests/test_lib_agw_hypertreelist.py | 41 +++++++++++ wx/lib/agw/hypertreelist.py | 92 ++++++++++++++++++++++++- 2 files changed, 132 insertions(+), 1 deletion(-) diff --git a/unittests/test_lib_agw_hypertreelist.py b/unittests/test_lib_agw_hypertreelist.py index c9b4014f..802e83cf 100644 --- a/unittests/test_lib_agw_hypertreelist.py +++ b/unittests/test_lib_agw_hypertreelist.py @@ -71,12 +71,53 @@ class lib_agw_hypertreelist_Tests(wtc.WidgetTestCase): HTL.TR_TWIST_BUTTONS HTL.TR_VIRTUAL HTL.TREE_HITTEST_ONITEMCHECKICON + HTL.TR_FILL_WHOLE_COLUMN_BACKGROUND def test_lib_agw_hypertreelistEvents(self): HTL.EVT_TREE_ITEM_CHECKED HTL.EVT_TREE_ITEM_CHECKING HTL.EVT_TREE_ITEM_HYPERLINK + def test_lib_agw_hypertreelistSetItemColour(self): + tree = HTL.HyperTreeList(self.frame) + tree.AddColumn("First column") + root = tree.AddRoot('root item') + child = tree.AppendItem(root, 'child item') + + self.assertEqual(None, tree.GetItemBackgroundColour(root)) + + colour = wx.RED + tree.SetItemBackgroundColour(child, colour) + self.assertEqual(colour, tree.GetItemBackgroundColour(child)) + + def test_lib_agw_hypertreelistSetItemColourOfColumns(self): + tree = HTL.HyperTreeList(self.frame) + tree.AddColumn("First column") + tree.AddColumn("Second column") + tree.AddColumn("Third column") + root = tree.AddRoot('root item') + child = tree.AppendItem(root, 'child item') + + colColour0 = wx.GREEN + colColour2 = wx.RED + tree.SetItemBackgroundColour(child, colColour0) + tree.SetItemBackgroundColour(child, colColour2, column=2) + + self.assertEqual(colColour2, tree.GetItemBackgroundColour(child, column=2)) + self.assertNotEqual(tree.GetItemBackgroundColour(child), + tree.GetItemBackgroundColour(child,column=2)) + self.assertEqual(None, tree.GetItemBackgroundColour(child, column=1)) + + def test_lib_agw_hypertreelistColourWholeItemColumns(self): + + tree = HTL.HyperTreeList(self.frame, agwStyle=HTL.TR_DEFAULT_STYLE| + HTL.TR_FILL_WHOLE_COLUMN_BACKGROUND) + tree.AddColumn("First column") + tree.AddColumn("Second column") + root = tree.AddRoot('root item') + + tree.SetItemBackgroundColour(root, wx.RED) + tree.SetItemBackgroundColour(root, wx.GREEN, column=1) #--------------------------------------------------------------------------- diff --git a/wx/lib/agw/hypertreelist.py b/wx/lib/agw/hypertreelist.py index 3143de20..7a3fee50 100644 --- a/wx/lib/agw/hypertreelist.py +++ b/wx/lib/agw/hypertreelist.py @@ -371,6 +371,12 @@ TR_NO_HEADER = 0x40000 """ Use this style to hide the columns header. """ # -------------------------------------------------------------------------- +# -------------------------------------------------------------------------- +# Additional HyperTreeList style for filling the whole background of the +# item columns +TR_FILL_WHOLE_COLUMN_BACKGROUND = 0x200000 +""" Use this style to fill the whole background of the item columns. """ +# -------------------------------------------------------------------------- # -------------------------------------------------------------------------- # Additional HyperTreeList style autosize the columns based on the widest @@ -1333,6 +1339,7 @@ class TreeListItem(GenericTreeItem): GenericTreeItem.__init__(self, parent, text, ct_type, wnd, image, selImage, data) self._wnd = [None] # are we holding a window? + self._bgColour = [None] if wnd: self.SetWindow(wnd) @@ -1804,6 +1811,36 @@ class TreeListItem(GenericTreeItem): return False return self._enabled + def GetBackgroundColour(self, column=0): + """ + Returns the associated background colour + + :param `column` an integer specifying the column index. + """ + + if column >= len(self._bgColour): + return None + + return self._bgColour[column] + + def SetBackgroundColour(self, colour, column=0): + """ + Sets the associated background colour + + :param `colour`: a valid :class:`wx.Colour` instance. + :param integer `column` + """ + + if type(self._bgColour) != type([]): + self._bgColour = [self._bgColour] + + if column < len(self._bgColour): + self._bgColour[column] = colour + elif column < self._owner.GetColumnCount(): + self._bgColour.extend([None] * (column - len(self._bgColour) + 1)) + self._bgColour[column] = colour + + #----------------------------------------------------------------------------- # EditTextCtrl (internal) #----------------------------------------------------------------------------- @@ -2298,6 +2335,32 @@ class TreeListMainWindow(CustomTreeCtrl): """ item.DeleteWindow(column=column) + def GetItemBackgroundColour(self, item, column=0): + """ + Returns the column background colour of the item + + :param `item`: an instance of :class:`TreeListItem` + :param integer `column` + """ + + return item.GetBackgroundColour(column) + + def SetItemBackgroundColour(self, item, colour, column=0): + """ + Sets the column background colour of the item + + :param `item`: an instance of :class:`TreeListItem` + :param `colour`: a valid :class:`wx.Colour` instance. + :param integer `column` + """ + + item.SetBackgroundColour(colour, column) + + if column == 0: + item.Attr().SetBackgroundColour(colour) + + self.RefreshLine(item) + # ---------------------------------------------------------------------------- # navigation # ---------------------------------------------------------------------------- @@ -3021,7 +3084,10 @@ class TreeListMainWindow(CustomTreeCtrl): # except for custom item backgrounds, works for both kinds of theme. elif drawItemBackground: - itemrect = wx.Rect(text_x-2, item.GetY() + off_h, text_w+2*_MARGIN, total_h - off_h) + if self.HasAGWFlag(TR_FILL_WHOLE_COLUMN_BACKGROUND): + itemrect = wx.Rect(text_x-2, item.GetY() + off_h, col_w-2*_MARGIN, total_h - off_h) + else: + itemrect = wx.Rect(text_x-2, item.GetY() + off_h, text_w+2*_MARGIN, total_h - off_h) dc.SetBrush(wx.Brush(colBg)) dc.DrawRectangle(itemrect) @@ -3029,6 +3095,17 @@ class TreeListMainWindow(CustomTreeCtrl): dc.SetTextForeground(colText) else: + + if self.HasAGWFlag(TR_FILL_WHOLE_COLUMN_BACKGROUND): + itemrect = wx.Rect(text_x-2, item.GetY() + off_h, col_w-2*_MARGIN, total_h - off_h) + else: + itemrect = wx.Rect(text_x-2, item.GetY() + off_h, text_w+2*_MARGIN, total_h - off_h) + colBgX = item.GetBackgroundColour(i) + + if colBgX != None and i != 0: + dc.SetBrush(wx.Brush(colBgX, wx.SOLID)) + dc.DrawRectangle(itemrect) + dc.SetTextForeground(colText) if self.HasAGWFlag(TR_COLUMN_LINES): # vertical lines between columns @@ -3177,6 +3254,19 @@ class TreeListMainWindow(CustomTreeCtrl): dc.DrawLine(0, y_top, total_width, y_top) dc.DrawLine(0, y_top+h, total_width, y_top+h) + + # fill background below twist buttons + if self.HasAGWFlag(TR_FILL_WHOLE_COLUMN_BACKGROUND): + attr = item.GetAttributes() + + if attr and attr.HasBackgroundColour(): + width = self._owner.GetEventHandler().GetColumn(self._main_column).GetWidth() + colBg = attr.GetBackgroundColour() + itemrect = wx.Rect(x_maincol, y-h-1, width, h+1) + + dc.SetBrush(wx.Brush(colBg, wx.SOLID)) + dc.DrawRectangle(itemrect) + # draw item self.PaintItem(item, dc)