1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """GUI element for the user input of spin IDs."""
24
25
26 from copy import deepcopy
27 import wx
28
29
30 import dep_check
31 from gui.fonts import font
32 from gui.string_conv import gui_to_str, str_to_gui
33 from lib.errors import RelaxError
34 from pipe_control.mol_res_spin import id_string_doc
35
36
38 """GUI element for the input of spin ID strings."""
39
40 - def __init__(self, name=None, default=None, parent=None, element_type='default', sizer=None, desc="spin ID string", combo_choices=None, combo_data=None, tooltip=None, divider=None, padding=0, spacer=None, height_element=27, can_be_none=True):
41 """Set up the base spin ID element.
42
43 @keyword name: The name of the element to use in titles, etc.
44 @type name: str
45 @keyword default: The default value.
46 @type default: str or None
47 @keyword parent: The parent GUI element.
48 @type parent: wx.Panel instance
49 @keyword element_type: The type of GUI element to create. This currently only supports the 'default' type.
50 @type element_type: str
51 @keyword sizer: The sizer to put the input field widget into.
52 @type sizer: wx.Sizer instance
53 @keyword desc: The text description.
54 @type desc: str
55 @keyword combo_choices: The list of choices to present to the user.
56 @type combo_choices: list of str
57 @keyword combo_data: The data returned by a call to GetValue(). If supplied, it should be the same length at the combo_choices list. If not supplied, the combo_choices list will be used for the returned data.
58 @type combo_data: list
59 @keyword tooltip: The tooltip which appears on hovering over the text or input field.
60 @type tooltip: str
61 @keyword choices: The list of choices to present to the user.
62 @type choices: list of str
63 @keyword divider: The optional position of the divider. If None, the class variable _div_left will be used.
64 @type divider: None or int
65 @keyword padding: Spacing to the left and right of the widgets.
66 @type padding: int
67 @keyword spacer: The amount of spacing to add below the field in pixels. If None, a stretchable spacer will be used.
68 @type spacer: None or int
69 @keyword height_element: The height in pixels of the GUI element.
70 @type height_element: int
71 @keyword can_be_none: A flag which specifies if the element is allowed to have the None value.
72 @type can_be_none: bool
73 """
74
75
76 types = ['default']
77 if element_type not in types:
78 raise RelaxError("The %s element type '%s' must be one of %s." % (name, element_type, types))
79
80
81 self.name = name
82 self.default = default
83 self.can_be_none = can_be_none
84
85
86 if combo_choices == None or combo_choices == []:
87 combo_choices = ['@N', '@N*', '@C', '@C*', '@H', '@H*', '@O', '@O*', '@P', '@P*']
88
89
90 sub_sizer = wx.BoxSizer(wx.HORIZONTAL)
91
92
93 sub_sizer.AddSpacer(padding)
94
95
96 text = wx.StaticText(parent, -1, desc, style=wx.ALIGN_LEFT)
97 text.SetFont(font.normal)
98 sub_sizer.Add(text, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 0)
99
100
101 if not divider:
102 raise RelaxError("The divider position has not been supplied.")
103
104
105 dc = wx.ScreenDC()
106 dc.SetFont(font.normal)
107 x, y = dc.GetTextExtent(desc)
108 if dep_check.wx_classic:
109 sub_sizer.AddSpacer((divider - x, 0))
110 else:
111 sub_sizer.AddSpacer(int(divider - x))
112
113
114 style = wx.CB_DROPDOWN
115 self._field = wx.ComboBox(parent, -1, '', style=style)
116
117
118 self.UpdateChoices(combo_choices=combo_choices, combo_data=combo_data, combo_default=default)
119
120
121 self._field.SetMinSize((50, height_element))
122 self._field.SetFont(font.normal)
123 sub_sizer.Add(self._field, 1, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0)
124
125
126 sub_sizer.AddSpacer(padding)
127
128
129 sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0)
130
131
132 if spacer == None:
133 sizer.AddStretchSpacer()
134 else:
135 sizer.AddSpacer(spacer)
136
137
138 if tooltip == None:
139 tooltip = ''
140
141
142 for type, element in id_string_doc.element_loop():
143 if type == 'paragraph':
144
145 tooltip += '\n\n'
146
147
148 tooltip += element
149
150
151 text.SetToolTip(wx.ToolTip(tooltip))
152 self._field.SetToolTip(wx.ToolTip(tooltip))
153
154
156 """Special method for clearing or resetting the GUI element."""
157
158
159 self._field.Clear()
160 self._field.SetValue('')
161
162
164 """Special method for returning the value of the GUI element.
165
166 @return: The spin ID value.
167 @rtype: str or None
168 """
169
170
171 sel_index = self._field.GetSelection()
172 if sel_index == wx.NOT_FOUND:
173 value = None
174 else:
175 value = gui_to_str(self._field.GetClientData(sel_index))
176
177
178 if value == None:
179 value = gui_to_str(self._field.GetValue())
180
181
182 return value
183
184
186 """Special method for setting the value of the GUI element.
187
188 @param value: The spin ID to set.
189 @type value: str or None
190 """
191
192
193 found = False
194 for i in range(self._field.GetCount()):
195 if self._field.GetClientData(i) == value:
196 self._field.SetSelection(i)
197 found = True
198 break
199
200
201 if not found:
202 self._field.SetSelection(wx.NOT_FOUND)
203 self._field.SetValue(str_to_gui(value))
204
205
206 - def UpdateChoices(self, combo_choices=None, combo_data=None, combo_default=None):
207 """Special wizard method for updating the list of choices in a ComboBox type element.
208
209 @keyword combo_choices: The list of choices to present to the user.
210 @type combo_choices: list of str
211 @keyword combo_data: The data returned by a call to GetValue(). If supplied, it should be the same length at the combo_choices list. If not supplied, the combo_choices list will be used for the returned data.
212 @type combo_data: list
213 @keyword combo_default: The default value of the ComboBox. This is only used if the element_type is set to 'combo'.
214 @type combo_default: str or None
215 """
216
217
218 sel_index = self._field.GetSelection()
219 if sel_index == wx.NOT_FOUND:
220 sel = None
221 else:
222 sel = self._field.GetClientData(sel_index)
223
224
225 self.Clear()
226
227
228 if combo_data == None:
229 combo_data = deepcopy(combo_choices)
230
231
232 for i in range(len(combo_choices)):
233 self._field.Insert(str_to_gui(combo_choices[i]), i, combo_data[i])
234
235
236 if sel == None and combo_default != None:
237
238 if combo_default in combo_choices:
239 string = combo_default
240 set_sel = True
241 elif combo_default not in combo_data:
242 string = combo_default
243 set_sel = False
244 else:
245 string = combo_choices[combo_data.index(combo_default)]
246 set_sel = True
247
248
249 if set_sel:
250 self._field.SetStringSelection(string)
251
252
253 else:
254 self._field.SetValue(string)
255
256
257 else:
258 for j in range(self._field.GetCount()):
259 if self._field.GetClientData(j) == sel:
260 self._field.SetSelection(j)
261