[wxPython-users] Sorting grid background label color

m.defel at tiscali.it m.defel at tiscali.it
Fri Mar 2 09:12:04 PST 2007


Starting from the wiki example here's the following code to draw the 
custom column labels with an arrow showing the sorted column on a wx.
grid, but using a GraphicsContext so to have a nice looking triangle. 
On windows it shows ok but on linux the label background color is 
white. Any ideas?

class SortingGrid(wx.grid.Grid):

    def __init__(self, parent, isTotali=False):

        wx.grid.Grid.__init__(self, parent, -1)
        
        # A click in the column header will set these:
        self.sortedColumn = -1
        self.sortedColumnDescending = False
        
        # trap the column label's paint event:
        columnLabelWindow = self.GetGridColLabelWindow()
        wx.EVT_PAINT(columnLabelWindow, self.OnColumnHeaderPaint)
       
    def OnColumnHeaderPaint(self, evt):
        w = self.GetGridColLabelWindow()
        dc = wx.PaintDC(w)

        gc = wx.GraphicsContext.Create(dc)
        gc.PushState() 
        
        brushNorm = dc.GetBrush()
        penNorm = dc.GetPen()
        
        clientRect = w.GetClientRect()
        font = dc.GetFont()
        
        # For each column, draw it's rectangle, it's column name,
        # and it's sort indicator, if appropriate:

        totColSize = -self.GetViewStart()[0]*self.
GetScrollPixelsPerUnit()[0] # Thanks Roger Binns
        for col in range(self.GetNumberCols()):

            colSize = self.GetColSize(col)
            labelHeight = self.GetColLabelSize()

            gc.SetBrush(gc.CreateBrush(brushNorm)) #?
            gc.SetPen(wx.Pen(wx.BLACK, 0.5))

            rect = (totColSize+2,2,colSize-4,labelHeight-4)
            
            gc.DrawRoundedRectangle(rect[0] - (col<>0 and 1 or 0), rect
[1],
                             rect[2] + (col<>0 and 1 or 0), rect[3], 
5)        
                             
            totColSize += colSize

            if col == self.sortedColumn:
                font.SetWeight(wx.BOLD)
                # draw a triangle, pointed up or down, at the
                # top left of the column.
                left = rect[0] + 3
                top = rect[1] + 3

                gc.PushState()
                gc.SetBrush(gc.CreateBrush(wx.Brush(wx.BLACK, wx.
SOLID))) #createbrush?
                gc.SetPen(wx.Pen(wx.BLACK, 0.5))
                if self.sortedColumnDescending:
                    path = gc.CreatePath()
                    path.MoveToPoint(left,top)
                    path.AddLineToPoint(left+6,top)
                    path.AddLineToPoint(left+3,top+4)
                    path.AddLineToPoint(left,top)                    
                    gc.DrawPath(path)
                else:
                    path = gc.CreatePath()
                    path.MoveToPoint(left+3,top)
                    path.AddLineToPoint(left+6,top+4)
                    path.AddLineToPoint(left,top+4)
                    path.AddLineToPoint(left+3,top)
                    gc.DrawPath(path)
                gc.PopState()
                
            else:
                font.SetWeight(wx.NORMAL)

            dc.SetFont(font)
            dc.DrawLabel("%s" % self.GetTable().colLabels[col],
                     rect, wx.ALIGN_CENTER | wx.ALIGN_TOP)
        
        gc.PopState() 

    def processSort(self, gridCol, reversed):
        self.sortedColumn = gridCol
        self.sortedColumnDescending = not reversed
        self.Refresh()
        
    def clearSort(self):
        self.sortedColumn = None
        self.Refresh()



Naviga e telefona senza limiti con Tiscali     
Scopri le promozioni Tiscali adsl: navighi e telefoni senza canone Telecom

http://abbonati.tiscali.it/adsl/





More information about the wxpython-users mailing list