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