1from fsui import Panel, Color
2
3
4class StatusElement(Panel):
5    def __init__(self, parent):
6        """
7
8        :type parent: fsui.Widget
9        """
10        Panel.__init__(self, parent, paintable=True)
11        # self.set_background_color(Color(0xd0, 0xd0, 0xd0))
12        self.set_background_color(parent.get_background_color())
13        self.icon = None
14        self.right_icon = None
15        self.right_icon_disabled = None
16        self.text = ""
17        self.active = True
18
19    def on_paint(self):
20        dc = self.create_dc()
21        from .StatusBar import StatusBar
22
23        StatusBar.draw_element_background(self, dc)
24        self.paint_element(dc)
25
26    def get_draw_operations(self):
27        operations = []
28        x = 0
29        h = self.get_min_height()
30        if self.icon:
31            x += 6
32            if self.active:
33                image = self.icon
34            else:
35                image = self.icon.grey_scale()
36            operations.append(("image", image, x, (h - 16) // 2))
37            x += 16
38        if self.text:
39            x += 6
40            tw, th = self.measure_text(self.text)
41            # tw = 200
42            # th = 20
43            operations.append(("text", self.text, x, (h - th) // 2))
44            x += tw
45        if self.right_icon:
46            x += 6
47            if self.active:
48                image = self.right_icon
49            elif self.right_icon_disabled:
50                image = self.right_icon_disabled
51            else:
52                image = self.right_icon.grey_scale()
53            operations.append(("image", image, x, (h - 16) // 2))
54            x += 16
55        x += 6
56        operations.append(("null", None, x, 0))
57        return operations
58
59    def get_min_height(self):
60        return 28
61
62    def get_min_width(self):
63        operations = self.get_draw_operations()
64        return operations[-1][2]
65
66    def paint_element(self, dc):
67        # orig_text_color = dc.get_text_color()
68        if not self.active:
69            dc.set_text_color(Color(0x80, 0x80, 0x80))
70        operations = self.get_draw_operations()
71        for draw_type, draw_object, x, y in operations:
72            if draw_type == "image":
73                dc.draw_image(draw_object, x, y)
74            elif draw_type == "text":
75                # if not self.active:
76                #     dc.set_text_color(Color(0x80, 0x80, 0x80))
77                dc.draw_text(draw_object, x, y)
78                # if not self.active:
79                #     dc.set_text_color(orig_text_color)
80