File: unit_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 (118 lines) | stat: -rw-r--r-- 4,086 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
# (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!

import wx

try:
    from scimath.units.unit_parser import unit_parser
except ImportError:
    unit_parser = None

from .default_renderer import DefaultRenderer


class UnitRenderer(DefaultRenderer):
    def DrawForeground(self, grid, attr, dc, rect, row, col, isSelected):
        dc.SetBackgroundMode(wx.TRANSPARENT)
        text = grid.model.GetValue(row, col)
        # print 'Rendering ', row, col, text, text.__class__
        dc.SetFont(self.font)
        dc.DrawText(text, rect.x + 1, rect.y + 1)

    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))

        text = grid.model.GetValue(row, col)

        if isSelected:
            dc.SetBrush(DefaultRenderer.selected_cells)
        elif unit_parser and unit_parser.parse_unit(text).is_valid():
            dc.SetBrush(DefaultRenderer.normal_cells)
        else:
            dc.SetBrush(DefaultRenderer.error_cells)

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


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


class MultiUnitRenderer(DefaultRenderer):
    def __init__(
        self, color="black", font="ARIAL", fontsize=8, suppress_warnings=False
    ):

        self.suppress_warnings = suppress_warnings
        DefaultRenderer.__init__(self, color, font, fontsize)

    def DrawForeground(self, grid, attr, dc, rect, row, col, isSelected):
        dc.SetBackgroundMode(wx.TRANSPARENT)
        text = grid.model.GetValue(row, col)
        dc.SetFont(self.font)
        dc.DrawText(text, rect.x + 1, rect.y + 1)

    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))

        text = grid.model.GetValue(row, col)
        if unit_parser:
            this_unit = unit_parser.parse_unit(text, self.suppress_warnings)
        else:
            this_unit = None

        # Todo - clean up this hardcoded logic/column position mess

        family = grid.model.GetValue(row, 6)

        # AI units of 'impedance ((kg/s)*(g/cc))' creates list of 3, not 2!
        try:
            family, other_text = family[:-1].split("(")
        except:
            family, other_text = family[:-1].split(" ")

        if unit_parser:
            other_unit = unit_parser.parse_unit(
                other_text, self.suppress_warnings
            )
            dimensionally_equivalent = this_unit.can_convert(other_unit)
        else:
            other_unit = None
            dimensionally_equivalent = False

        if isSelected:
            dc.SetBrush(DefaultRenderer.selected_cells)
        elif not this_unit or not this_unit.is_valid():
            dc.SetBrush(DefaultRenderer.error_cells)
        elif not dimensionally_equivalent:
            dc.SetBrush(DefaultRenderer.warn_cells)
        else:
            dc.SetBrush(DefaultRenderer.normal_cells)

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