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