File: grid_cell_image_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 (165 lines) | stat: -rw-r--r-- 5,475 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# (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!

""" A renderer which will display a cell-specific image in addition to some
    text displayed in the same way the standard string renderer normally
    would. """


import wx

from wx.grid import GridCellRenderer
from wx.grid import GridCellStringRenderer
from wx import SOLID, Brush, Rect, TRANSPARENT_PEN


class GridCellImageRenderer(GridCellRenderer):
    """ A renderer which will display a cell-specific image in addition to some
        text displayed in the same way the standard string renderer normally
        would. """

    def __init__(self, provider=None):
        """ Build a new PyGridCellImageRenderer.

            'provider', if provided, should implement
            get_image_for_cell(row, col) to specify an image to appear
            in the cell at row, col. """

        GridCellRenderer.__init__(self)

        # save the string renderer to use for text.
        self._string_renderer = GridCellStringRenderer()

        self._provider = provider

        return

    # ------------------------------------------------------------------------
    # GridCellRenderer interface.
    # ------------------------------------------------------------------------
    def Draw(self, grid, attr, dc, rect, row, col, isSelected):
        """ Draw the appropriate icon into the specified grid cell. """

        # clear the cell first
        if isSelected:
            bgcolor = grid.GetSelectionBackground()
        else:
            bgcolor = grid.GetCellBackgroundColour(row, col)

        dc.SetBackgroundMode(SOLID)
        dc.SetBrush(Brush(bgcolor, SOLID))
        dc.SetPen(TRANSPARENT_PEN)
        dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)

        # find the correct image for this cell
        bmp = self._get_image(grid, row, col)
        # find the correct text for this cell
        text = self._get_text(grid, row, col)

        # figure out placement -- we try to center things!
        # fixme: we should be responding to the horizontal/vertical
        #        alignment info in the attr object!
        size = self.GetBestSize(grid, attr, dc, row, col)

        halign, valign = attr.GetAlignment()

        # width first
        wdelta = rect.width - size.GetWidth()
        x = rect.x
        if halign == wx.ALIGN_CENTRE and wdelta > 0:
            x += wdelta / 2

        # now height
        hdelta = rect.height - size.GetHeight()

        y = rect.y
        if valign == wx.ALIGN_CENTRE and hdelta > 0:
            y += hdelta / 2

        dc.SetClippingRegion(*rect)

        if bmp is not None:
            # now draw our image into it
            dc.DrawBitmap(bmp, x, y, 1)
            x += bmp.GetWidth()

        if text is not None and text != "":
            width = rect.x + rect.width - x
            height = rect.y + rect.height - y
            # draw any text that should be included
            new_rect = Rect(x, y, width, height)
            self._string_renderer.Draw(
                grid, attr, dc, new_rect, row, col, isSelected
            )

        dc.DestroyClippingRegion()

    def GetBestSize(self, grid, attr, dc, row, col):
        """ Determine best size for the cell. """

        # find the correct image
        bmp = self._get_image(grid, row, col)

        if bmp is not None:
            bmp_size = wx.Size(bmp.GetWidth(), bmp.GetHeight())
        else:
            bmp_size = wx.Size(0, 0)

        # find the correct text for this cell
        text = self._get_text(grid, row, col)

        if text is not None:
            text_size = self._string_renderer.GetBestSize(
                grid, attr, dc, row, col
            )
        else:
            text_size = wx.Size(0, 0)

        result = wx.Size(
            bmp_size.width + text_size.width,
            max(bmp_size.height, text_size.height),
        )

        return result

    def Clone(self):
        return GridCellImageRenderer(self._provider)

    # ------------------------------------------------------------------------
    # protected 'GridCellIconRenderer' interface.
    # ------------------------------------------------------------------------
    def _get_image(self, grid, row, col):
        """ Returns the correct bmp for the data at row, col. """
        bmp = None
        if self._provider is not None and hasattr(
            self._provider, "get_image_for_cell"
        ):
            # get the image from the specified provider
            img = self._provider.get_image_for_cell(grid, row, col)
            if img is not None:
                bmp = img.create_bitmap()
            else:
                bmp = None

        return bmp

    def _get_text(self, grid, row, col):
        """ Returns the correct text for the data at row, col. """

        text = None
        if self._provider is not None and hasattr(
            self._provider, "get_text_for_cell"
        ):
            # get the image from the specified provider
            text = self._provider.get_text_for_cell(grid, row, col)
        else:
            text = grid.GetCellValue(row, col)

        return text