1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module containing the base class for all frames."""
25
26
27 import wx
28 import wx.lib.buttons
29
30
31 import dep_check
32 from gui.fonts import font
33 from gui.string_conv import str_to_gui
34
35
37 """The analysis specific floating point number control.
38
39 This consists of two elements: wx.StaticText and wx.TextCtrl.
40 """
41
42 - def __init__(self, box, parent, text="", default="", tooltip=None, editable=True, width_text=200, width_button=80, spacer=0):
43 """Create a text selection element for the frame.
44
45 This consists of a horizontal layout with a static text element, a text control, and an optional button.
46
47 @param box: The box element to pack the structure file selection GUI element into.
48 @type box: wx.BoxSizer instance
49 @param parent: The parent GUI element.
50 @type parent: wx object
51 @keyword text: The static text.
52 @type text: str
53 @keyword default: The default text of the control.
54 @type default: str
55 @keyword tooltip: The tooltip which appears on hovering over the text or input field.
56 @type tooltip: str
57 @keyword editable: A flag specifying if the control is editable or not.
58 @type editable: bool
59 @keyword width_text: The width of the text element.
60 @type width_text: int
61 @keyword width_button: The width of the standard button used in the other elements.
62 @type width_button: int
63 @keyword spacer: The horizontal spacing between the elements.
64 @type spacer: int
65 """
66
67
68 sizer = wx.BoxSizer(wx.HORIZONTAL)
69
70
71 self.label = wx.StaticText(parent, -1, text)
72 self.label.SetMinSize((width_text, -1))
73 self.label.SetFont(font.normal)
74 sizer.Add(self.label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
75
76
77 dc = wx.ScreenDC()
78 dc.SetFont(font.normal)
79 x, y = dc.GetTextExtent(text)
80 size_horizontal = y + 8
81
82
83 if dep_check.wx_classic:
84 sizer.AddSpacer((spacer, -1))
85 else:
86 sizer.AddSpacer(spacer)
87
88
89 self.field = wx.TextCtrl(parent, -1, str_to_gui(default))
90 self.field.SetMinSize((-1, size_horizontal))
91 self.field.SetFont(font.normal)
92 self.field.SetEditable(editable)
93 if not editable:
94 colour = parent.GetBackgroundColour()
95 self.field.SetOwnBackgroundColour(colour)
96 sizer.Add(self.field, 1, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
97
98
99 self.field.Bind(wx.EVT_KEY_DOWN, self.pre_input)
100 self.field.Bind(wx.EVT_TEXT, self.post_input)
101 self.field.Bind(wx.EVT_KEY_UP, self.end_input)
102
103
104 if dep_check.wx_classic:
105 sizer.AddSpacer((spacer, -1))
106 else:
107 sizer.AddSpacer(spacer)
108
109
110 if dep_check.wx_classic:
111 sizer.AddSpacer((width_button, -1))
112 else:
113 sizer.AddSpacer(width_button)
114
115
116 box.Add(sizer, 0, wx.ALL|wx.EXPAND, 0)
117
118
119 if tooltip:
120 self.label.SetToolTip(wx.ToolTip(tooltip))
121 self.field.SetToolTip(wx.ToolTip(tooltip))
122
123
124 - def Enable(self, enable=True):
125 """Enable or disable the element for user input.
126
127 @keyword enable: The flag specifying if the element should be enabled or disabled.
128 @type enable: bool
129 """
130
131
132 self.field.Enable(enable)
133
134
136 """Set the value of the control.
137
138 @return: The value of the text control.
139 @rtype: int
140 """
141
142
143 return self.field.GetValue()
144
145
147 """Set the value of the control.
148
149 @param value: The value to set the text control to.
150 @type value: text
151 """
152
153
154 return self.field.SetValue(value)
155
156
172
173
193
194
196 """Check that the user input is a float when the text changes, restoring the value if not.
197
198 @param event: The wx event.
199 @type event: wx event
200 """
201
202
203 value = self.field.GetValue()
204
205
206 flag = False
207
208
209 try:
210 float(value)
211 except ValueError:
212 flag = True
213
214
215 if ' ' in value:
216 flag = True
217
218
219 if 'e' in value or 'E' in value:
220 flag = True
221
222
223 if flag:
224 self.field.SetValue(self._value)
225 self._restore_pos_flag = True
226
227
228 event.Skip()
229