1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """
19 Contains the LegendDrawer class.
20 Tests are defined :doc:`Here <unit_canvas_drawing_drawers_legenddrawer>`.
21 """
22
23 import wx
24 import timelinelib.wxgui.components.font as font
25 from timelinelib.canvas.drawing.graphobject import GraphObject
26 from timelinelib.canvas.drawing.utils import darken_color
27
28
29
30
31 IP = 3
32 OP = 5
33
34 BOTTOM_LEFT = 0
35 """Default value."""
36 TOP_LEFT = 1
37 """."""
38 TOP_RIGHT = 2
39 """."""
40 BOTTOM_RIGHT = 3
41 """."""
42
43
45 """
46 The legend is a box containing one item row for each category
47 displayed in a timeline. An item contains a text and colored
48 box. The text is the category text and the color is the color
49 of the category. The legend box can be placed at different
50 locations on the Teimeline panel. When measures are calculated
51 it's assumed that it will be placed in the upper left corner::
52 OP
53 IP | |
54 | | >| |< text_height
55 +--------------------------+---
56 | | IP
57 | +---+ |---
58 | Xxxxxxx yyyy | | |
59 | +---+ |---
60 | | IP
61 | +---+ |---
62 | Xxxxxxx yyyy | | | text_height
63 | +---+ |---
64 | | | : |
65 | text_width : |
66 | : |
67 +--------------------------+
68 | | | |
69 OP IP
70 """
71
72 - def __init__(self, dc, scene, categories):
76
78 """Draw the legend on the Timeline panel."""
79 go = self._create_graph_object()
80 self._draw_rectangle(go)
81 self._draw_legend_items(go)
82
86
90
92 self._draw_rectangle(go)
93
98
100 tw, th = self._get_text_metrics(self._categories)
101 box_width = tw + th + OP + 2 * IP
102 box_height = len(self._categories) * (IP + th) + IP
103 return self._create_legend(box_width, box_height, tw, th)
104
105 - def _get_text_metrics(self, categories):
106 """
107 Return the text width of the longest category text and the
108 height of the first category text.
109 """
110 font.set_legend_text_font(self._scene._appearance.get_legend_font(), self._dc)
111 twth = [self._dc.GetTextExtent(cat.name) for cat in categories]
112 maxw = max(twth, key=lambda x: x[0])[0]
113 return maxw, twth[0][1]
114
116 go = GraphObject(w=box_width, h=box_height)
117 go.brush_color = wx.Brush(wx.Colour(255, 255, 255), wx.BRUSHSTYLE_SOLID)
118 go.pen_color = wx.Pen(wx.Colour(0, 0, 0), 1, wx.PENSTYLE_SOLID)
119 go.childs = self._legend_items(tw, th)
120 go.translate(OP, OP)
121 self._set_legend_pos(go)
122 return go
123
125 collector = []
126 for i in range(len(self._categories)):
127 y = i * (th + IP)
128 go = GraphObject(y=y, text=self._categories[i].name)
129 go.add_child(self._color_box(tw, th, y, self._categories[i]))
130 go.translate(IP, IP)
131 collector.append(go)
132 return collector
133
139
148