1 | import wx |
---|
2 | import sys |
---|
3 | |
---|
4 | if sys.platform.count("win32") > 0: |
---|
5 | FONT_VARIANT = 0 |
---|
6 | PNL_WIDTH = 460 |
---|
7 | else: |
---|
8 | FONT_VARIANT = 1 |
---|
9 | PNL_WIDTH = 500 |
---|
10 | FAMILY = ['serif', 'sans-serif', 'fantasy', 'monospace'] |
---|
11 | SIZE = [8, 9, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72] |
---|
12 | STYLE = ['normal', 'italic'] |
---|
13 | WEIGHT = ['light', 'normal', 'bold'] |
---|
14 | COLOR = ['black', 'blue', 'green', 'red', 'cyan', 'magenta', 'yellow'] |
---|
15 | |
---|
16 | |
---|
17 | class TextDialog(wx.Dialog): |
---|
18 | def __init__(self, parent, id, title, label='', unit=None): |
---|
19 | """ |
---|
20 | Dialog window pops- up when selecting 'Add Text' on the toolbar |
---|
21 | """ |
---|
22 | wx.Dialog.__init__(self, parent, id, title, size=(PNL_WIDTH, 300)) |
---|
23 | self.parent = parent |
---|
24 | # Font |
---|
25 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
26 | # default |
---|
27 | self.family = FAMILY[1] |
---|
28 | self.size = SIZE[3] |
---|
29 | self.style = STYLE[0] |
---|
30 | self.weight = WEIGHT[1] |
---|
31 | self.color = COLOR[0] |
---|
32 | self.tick_label = False |
---|
33 | # Dialog interface |
---|
34 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
35 | text_box = wx.BoxSizer(wx.HORIZONTAL) |
---|
36 | sizer = wx.GridBagSizer(1, 3) |
---|
37 | _BOX_WIDTH = 70 |
---|
38 | font_description = wx.StaticBox(self, -1, 'Font') |
---|
39 | font_box = wx.StaticBoxSizer(font_description, wx.VERTICAL) |
---|
40 | family_box = wx.BoxSizer(wx.HORIZONTAL) |
---|
41 | style_box = wx.BoxSizer(wx.HORIZONTAL) |
---|
42 | # tcA |
---|
43 | if unit != None: |
---|
44 | styles = wx.TAB_TRAVERSAL |
---|
45 | height = -1 |
---|
46 | unit_text = wx.StaticText(self, -1, 'Unit :') |
---|
47 | unit_text.SetToolTipString("Unit of the axis.") |
---|
48 | self.unit_ctrl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
49 | self.unit_ctrl.SetValue(str(unit)) |
---|
50 | unit_box = wx.BoxSizer(wx.HORIZONTAL) |
---|
51 | tick_label_text = wx.StaticText(self, -1, 'Tick label') |
---|
52 | tick_label_text.SetToolTipString("Apply to tick label too.") |
---|
53 | self.tick_label_check = wx.CheckBox(self, -1, '', (10, 10)) |
---|
54 | self.tick_label_check.SetValue(False) |
---|
55 | self.tick_label_check.SetToolTipString("Apply to tick label too.") |
---|
56 | wx.EVT_CHECKBOX(self, self.tick_label_check.GetId(), |
---|
57 | self.on_tick_label) |
---|
58 | enter_text = 'Enter text:' |
---|
59 | else: |
---|
60 | styles = wx.TAB_TRAVERSAL | wx.TE_MULTILINE | wx.TE_LINEWRAP | \ |
---|
61 | wx.TE_PROCESS_ENTER | wx.SUNKEN_BORDER | wx.HSCROLL |
---|
62 | height = 60 |
---|
63 | unit_text = None |
---|
64 | self.unit_ctrl = None |
---|
65 | unit_box = None |
---|
66 | tick_label_text = None |
---|
67 | self.tick_label_check = None |
---|
68 | enter_text = 'Enter text' |
---|
69 | if len(label) > 0: |
---|
70 | enter_text += " (this text won't be auto-updated if modified.):" |
---|
71 | else: |
---|
72 | enter_text += ":" |
---|
73 | self.text_string = wx.TextCtrl(self, -1, size=(PNL_WIDTH - 30, height), style=styles) |
---|
74 | self.text_string.SetValue(str(label)) |
---|
75 | self.text_string.SetToolTipString("The text that will be displayed.") |
---|
76 | # font family |
---|
77 | self.font_family = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
78 | wx.EVT_COMBOBOX(self.font_family, -1, self.on_family) |
---|
79 | self.font_family.SetMinSize((_BOX_WIDTH, -1)) |
---|
80 | self._set_family_list() |
---|
81 | self.font_family.SetSelection(1) |
---|
82 | self.font_family.SetToolTipString("Font family of the text.") |
---|
83 | # font weight |
---|
84 | self.font_weight = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
85 | wx.EVT_COMBOBOX(self.font_weight, -1, self.on_weight) |
---|
86 | self.font_weight.SetMinSize((_BOX_WIDTH, -1)) |
---|
87 | self._set_weight_list() |
---|
88 | self.font_weight.SetSelection(1) |
---|
89 | self.font_weight.SetToolTipString("Font weight of the text.") |
---|
90 | # font family |
---|
91 | self.font_size = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
92 | wx.EVT_COMBOBOX(self.font_size, -1, self.on_size) |
---|
93 | self.font_size.SetMinSize((_BOX_WIDTH, -1)) |
---|
94 | self._set_size_list() |
---|
95 | self.font_size.SetSelection(5) |
---|
96 | self.font_size.SetToolTipString("Font size of the text.") |
---|
97 | # font style |
---|
98 | self.font_style = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
99 | wx.EVT_COMBOBOX(self.font_style, -1, self.on_style) |
---|
100 | self.font_style.SetMinSize((_BOX_WIDTH, -1)) |
---|
101 | self._set_style_list() |
---|
102 | self.font_style.SetSelection(0) |
---|
103 | self.font_style.SetToolTipString("Font style of the text.") |
---|
104 | # font color |
---|
105 | self.font_color = wx.ComboBox(self, -1, style = wx.CB_READONLY) |
---|
106 | wx.EVT_COMBOBOX(self.font_color, -1, self.on_color) |
---|
107 | self.font_color.SetMinSize((_BOX_WIDTH, -1)) |
---|
108 | self._set_color_list() |
---|
109 | self.font_color.SetSelection(0) |
---|
110 | self.font_color.SetToolTipString("Font color of the text.") |
---|
111 | # Buttons on the bottom |
---|
112 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
113 | self.ok_button = wx.Button(self, wx.ID_OK, 'OK', |
---|
114 | size = (_BOX_WIDTH, 25)) |
---|
115 | self.close_button = wx.Button(self, wx.ID_CANCEL, 'Cancel', |
---|
116 | size = (_BOX_WIDTH, 25)) |
---|
117 | |
---|
118 | # Intro |
---|
119 | explanation = "Select font properties :" |
---|
120 | vbox.Add(sizer) |
---|
121 | ix = 0 |
---|
122 | iy = 1 |
---|
123 | sizer.Add(wx.StaticText(self, -1, explanation), (iy, ix), |
---|
124 | (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
125 | family_box.Add(wx.StaticText(self, -1, 'Family :', size = (50, -1)), |
---|
126 | 0, wx.TOP, 5) |
---|
127 | family_box.Add(self.font_family, 0, 0) |
---|
128 | family_box.Add((_BOX_WIDTH / 2, -1)) |
---|
129 | family_box.Add(wx.StaticText(self, -1, 'Size :', size = (50, -1)), |
---|
130 | 0, wx.TOP, 5) |
---|
131 | family_box.Add(self.font_size, 0, 0) |
---|
132 | if unit_box != None: |
---|
133 | family_box.Add((_BOX_WIDTH / 2, -1)) |
---|
134 | family_box.Add(tick_label_text, 0, 0) |
---|
135 | family_box.Add(self.tick_label_check, 0, 0) |
---|
136 | style_box.Add(wx.StaticText(self, -1, 'Style :', size = (50, -1)), |
---|
137 | 0, wx.TOP, 5) |
---|
138 | style_box.Add(self.font_style, 0, 0) |
---|
139 | style_box.Add((_BOX_WIDTH / 2, -1)) |
---|
140 | style_box.Add(wx.StaticText(self, -1, 'Weight :', size = (50, -1)), |
---|
141 | 0, wx.TOP, 5) |
---|
142 | style_box.Add(self.font_weight, 0, 0) |
---|
143 | style_box.Add((_BOX_WIDTH / 2, -1)) |
---|
144 | style_box.Add(wx.StaticText(self, -1, 'Color :', size = (45, -1)), |
---|
145 | 0, wx.TOP, 5) |
---|
146 | style_box.Add(self.font_color, 0, 0) |
---|
147 | font_box.Add(family_box, 0, 10) |
---|
148 | font_box.Add((0,5)) |
---|
149 | font_box.Add(style_box, 0, 10) |
---|
150 | iy += 1 |
---|
151 | ix = 0 |
---|
152 | sizer.Add(font_box, (iy, ix), |
---|
153 | (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
154 | iy += 2 |
---|
155 | ix = 0 |
---|
156 | sizer.Add(wx.StaticText(self, -1, enter_text), (iy, ix), |
---|
157 | (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
158 | text_box.Add((15, 10)) |
---|
159 | text_box.Add(self.text_string) |
---|
160 | vbox.Add(text_box, 0, wx.EXPAND, 15) |
---|
161 | if unit_box != None: |
---|
162 | unit_box.Add(unit_text, 0, 0) |
---|
163 | unit_box.Add(self.unit_ctrl, 0, 0) |
---|
164 | vbox.Add((5, 5)) |
---|
165 | vbox.Add(unit_box, 0, wx.LEFT, 15) |
---|
166 | |
---|
167 | vbox.Add((10, 10)) |
---|
168 | vbox.Add(self.static_line_1, 0, wx.EXPAND, 10) |
---|
169 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
170 | sizer_button.Add((20, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
171 | sizer_button.Add(self.ok_button, 0, |
---|
172 | wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10) |
---|
173 | sizer_button.Add(self.close_button, 0, |
---|
174 | wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10) |
---|
175 | vbox.Add(sizer_button, 0, wx.EXPAND | wx.BOTTOM | wx.TOP, 10) |
---|
176 | self.SetSizer(vbox) |
---|
177 | self.Centre() |
---|
178 | |
---|
179 | def _set_family_list(self): |
---|
180 | """ |
---|
181 | Set the list of font family |
---|
182 | """ |
---|
183 | # list of family choices |
---|
184 | for idx in range(len(FAMILY)): |
---|
185 | self.font_family.Append(FAMILY[idx], idx) |
---|
186 | |
---|
187 | def _set_size_list(self): |
---|
188 | """ |
---|
189 | Set the list of font size |
---|
190 | """ |
---|
191 | # list of size choices |
---|
192 | for idx in range(len(SIZE)): |
---|
193 | self.font_size.Append(str(SIZE[idx]), idx) |
---|
194 | |
---|
195 | def _set_weight_list(self): |
---|
196 | """ |
---|
197 | Set the list of font weight |
---|
198 | """ |
---|
199 | # list of weight choices |
---|
200 | for idx in range(len(WEIGHT)): |
---|
201 | self.font_weight.Append(WEIGHT[idx], idx) |
---|
202 | |
---|
203 | def _set_style_list(self): |
---|
204 | """ |
---|
205 | Set the list of font style |
---|
206 | """ |
---|
207 | # list of style choices |
---|
208 | for idx in range(len(STYLE)): |
---|
209 | self.font_style.Append(STYLE[idx], idx) |
---|
210 | |
---|
211 | def _set_color_list(self): |
---|
212 | """ |
---|
213 | Set the list of font color |
---|
214 | """ |
---|
215 | # list of tyle choices |
---|
216 | for idx in range(len(COLOR)): |
---|
217 | self.font_color.Append(COLOR[idx], idx) |
---|
218 | |
---|
219 | def on_tick_label(self, event): |
---|
220 | """ |
---|
221 | Set the font for tick label |
---|
222 | """ |
---|
223 | event.Skip() |
---|
224 | self.tick_label = self.tick_label_check.GetValue() |
---|
225 | |
---|
226 | def on_family(self, event): |
---|
227 | """ |
---|
228 | Set the family |
---|
229 | """ |
---|
230 | event.Skip() |
---|
231 | self.family = self.font_family.GetValue() |
---|
232 | |
---|
233 | def on_style(self, event): |
---|
234 | """ |
---|
235 | Set the style |
---|
236 | """ |
---|
237 | event.Skip() |
---|
238 | self.style = self.font_style.GetValue() |
---|
239 | |
---|
240 | def on_weight(self, event): |
---|
241 | """ |
---|
242 | Set the weight |
---|
243 | """ |
---|
244 | event.Skip() |
---|
245 | self.weight = self.font_weight.GetValue() |
---|
246 | |
---|
247 | def on_size(self, event): |
---|
248 | """ |
---|
249 | Set the size |
---|
250 | """ |
---|
251 | event.Skip() |
---|
252 | self.size = self.font_size.GetValue() |
---|
253 | |
---|
254 | def on_color(self, event): |
---|
255 | """ |
---|
256 | Set the color |
---|
257 | """ |
---|
258 | event.Skip() |
---|
259 | self.color = self.font_color.GetValue() |
---|
260 | |
---|
261 | def getText(self): |
---|
262 | """ |
---|
263 | Returns text string as input by user. |
---|
264 | """ |
---|
265 | return self.text_string.GetValue() |
---|
266 | |
---|
267 | def getUnit(self): |
---|
268 | """ |
---|
269 | Returns unit string as input by user. |
---|
270 | """ |
---|
271 | return self.unit_ctrl.GetValue() |
---|
272 | |
---|
273 | def getFamily(self): |
---|
274 | """ |
---|
275 | Returns font family for the text box |
---|
276 | """ |
---|
277 | return str(self.family) |
---|
278 | |
---|
279 | def getStyle(self): |
---|
280 | """ |
---|
281 | Returns font tyle for the text box |
---|
282 | """ |
---|
283 | return str(self.style) |
---|
284 | |
---|
285 | def getWeight(self): |
---|
286 | """ |
---|
287 | Returns font weight for the text box |
---|
288 | """ |
---|
289 | return str(self.weight) |
---|
290 | |
---|
291 | def getSize(self): |
---|
292 | """ |
---|
293 | Returns font size for the text box |
---|
294 | """ |
---|
295 | return int(self.size) |
---|
296 | |
---|
297 | def getColor(self): |
---|
298 | """ |
---|
299 | Returns font size for the text box |
---|
300 | """ |
---|
301 | return str(self.color) |
---|
302 | |
---|
303 | def getTickLabel(self): |
---|
304 | """ |
---|
305 | Bool for use on tick label |
---|
306 | """ |
---|
307 | return self.tick_label |
---|