Package gui :: Package input_elements :: Module spin_id
[hide private]
[frames] | no frames]

Source Code for Module gui.input_elements.spin_id

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2010-2013 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """GUI element for the user input of spin IDs.""" 
 24   
 25  # Python module imports. 
 26  from copy import deepcopy 
 27  import wx 
 28   
 29  # relax module imports. 
 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   
36 -class Spin_id:
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 # Check the element type. 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 # Store the args. 80 self.name = name 81 self.default = default 82 self.can_be_none = can_be_none 83 84 # The combo choices, if not supplied. 85 if combo_choices == None or combo_choices == []: 86 combo_choices = ['@N', '@N*', '@C', '@C*', '@H', '@H*', '@O', '@O*', '@P', '@P*'] 87 88 # Init. 89 sub_sizer = wx.BoxSizer(wx.HORIZONTAL) 90 91 # Left padding. 92 sub_sizer.AddSpacer(padding) 93 94 # The description. 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 # The divider. 100 if not divider: 101 raise RelaxError("The divider position has not been supplied.") 102 103 # Spacing. 104 x, y = text.GetSize() 105 sub_sizer.AddSpacer((divider - x, 0)) 106 107 # The input field. 108 style = wx.CB_DROPDOWN 109 self._field = wx.ComboBox(parent, -1, '', style=style) 110 111 # Update the choices. 112 self.UpdateChoices(combo_choices=combo_choices, combo_data=combo_data, combo_default=default) 113 114 # Set up the input field. 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 # Right padding. 120 sub_sizer.AddSpacer(padding) 121 122 # Add to the main sizer. 123 sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0) 124 125 # Spacing below the widget. 126 if spacer == None: 127 sizer.AddStretchSpacer() 128 else: 129 sizer.AddSpacer(spacer) 130 131 # Initialise the tooltip string, if not supplied. 132 if tooltip == None: 133 tooltip = '' 134 135 # Add the ID string documentation to the tooltip. 136 for type, element in id_string_doc.element_loop(): 137 if type == 'paragraph': 138 # Initial spacing. 139 tooltip += '\n\n' 140 141 # The text. 142 tooltip += element 143 144 # Set the tooltip. 145 text.SetToolTipString(tooltip) 146 self._field.SetToolTipString(tooltip)
147 148
149 - def Clear(self):
150 """Special method for clearing or resetting the GUI element.""" 151 152 # Clear the value. 153 self._field.Clear() 154 self._field.SetValue('')
155 156
157 - def GetValue(self):
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 # An element selected from the list. 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 # A non-list value. 172 if value == None: 173 value = gui_to_str(self._field.GetValue()) 174 175 # Return the value. 176 return value
177 178
179 - def SetValue(self, value):
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 # Loop until the proper client data is found. 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 # No value found. 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 # Store the current selection's client data to restore at the end. 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 # First clear all data. 219 self.Clear() 220 221 # Set the data if needed. 222 if combo_data == None: 223 combo_data = deepcopy(combo_choices) 224 225 # Loop over the choices and data, adding both to the end. 226 for i in range(len(combo_choices)): 227 self._field.Insert(str_to_gui(combo_choices[i]), i, combo_data[i]) 228 229 # Set the default selection. 230 if sel == None and combo_default != None: 231 # Translate if needed. 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 # Set the selection. 243 if set_sel: 244 self._field.SetStringSelection(string) 245 246 # Set the value. 247 else: 248 self._field.SetValue(string) 249 250 # Restore the selection. 251 else: 252 for j in range(self._field.GetCount()): 253 if self._field.GetClientData(j) == sel: 254 self._field.SetSelection(j)
255