File: default_renderer.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (109 lines) | stat: -rw-r--r-- 3,988 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!


from string import atof
import wx
from wx.grid import PyGridCellRenderer


class DefaultRenderer(PyGridCellRenderer):
    """ This renderer provides the default representation of an
    Enthought spreadsheet cell.
    """

    selected_cells = wx.Brush(wx.Colour(255, 255, 200), wx.SOLID)
    normal_cells = wx.Brush("white", wx.SOLID)
    odd_cells = wx.Brush(wx.Colour(240, 240, 240), wx.SOLID)
    error_cells = wx.Brush(wx.Colour(255, 122, 122), wx.SOLID)
    warn_cells = wx.Brush(wx.Colour(255, 242, 0), wx.SOLID)

    def __init__(self, color="black", font="ARIAL", fontsize=8):
        PyGridCellRenderer.__init__(self)
        self.color = color
        self.foundary = font
        self.fontsize = fontsize
        self.font = wx.Font(
            fontsize, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, font
        )

    def Clone(self):
        return DefaultRenderer(self.color, self.foundary, self.fontsize)

    def Draw(self, grid, attr, dc, rect, row, col, isSelected):
        self.DrawBackground(grid, attr, dc, rect, row, col, isSelected)
        self.DrawForeground(grid, attr, dc, rect, row, col, isSelected)
        dc.DestroyClippingRegion()

    def DrawBackground(self, grid, attr, dc, rect, row, col, isSelected):
        """ Erases whatever is already in the cell by drawing over it.
        """
        # We have to set the clipping region on the grid's DC,
        # otherwise the text will spill over to the next cell
        dc.SetClippingRegion(rect)

        # overwrite anything currently in the cell ...
        dc.SetBackgroundMode(wx.SOLID)

        dc.SetPen(wx.Pen(wx.WHITE, 1, wx.SOLID))

        if isSelected:
            dc.SetBrush(DefaultRenderer.selected_cells)
        elif row % 2:
            dc.SetBrush(DefaultRenderer.normal_cells)
        else:
            dc.SetBrush(DefaultRenderer.odd_cells)

        dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)

    def DrawForeground(self, grid, attr, dc, rect, row, col, isSelected):
        """ Draws the cell (text) on top of the existing background color.
        """
        dc.SetBackgroundMode(wx.TRANSPARENT)
        text = grid.model.GetValue(row, col)
        dc.SetTextForeground(self.color)
        dc.SetFont(self.font)
        dc.DrawText(self.FormatText(text), rect.x + 1, rect.y + 1)

        self.DrawEllipses(grid, attr, dc, rect, row, col, isSelected)

    def FormatText(self, text):
        """ Formats numbers to 3 decimal places.
        """
        try:
            text = "%0.3f" % atof(text)
        except:
            pass

        return text

    def DrawEllipses(self, grid, attr, dc, rect, row, col, isSelected):
        """ Adds three dots "..." to indicate the cell is truncated.
        """
        text = grid.model.GetValue(row, col)
        if not isinstance(text, str):
            msg = 'Problem appending "..." to cell: %d %d' % (row, col)
            raise TypeError(msg)

        width, height = dc.GetTextExtent(text)
        if width > rect.width - 2:
            width, height = dc.GetTextExtent("...")
            x = rect.x + 1 + rect.width - 2 - width
            dc.DrawRectangle(x, rect.y + 1, width + 1, height)
            dc.DrawText("...", x, rect.y + 1)

    def GetBestSize88(self, grid, attr, dc, row, col):
        """ This crashes the app - hmmmm. """
        size = PyGridCellRenderer.GetBestSize(self, grid, attr, dc, row, col)
        print("-----------------------------", size)
        return size


# -------------------------------------------------------------------------------