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-2012 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  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   
37 -class Spin_id:
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 # Check the element type. 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 # Store the args. 81 self.name = name 82 self.default = default 83 self.can_be_none = can_be_none 84 85 # The combo choices, if not supplied. 86 if combo_choices == None or combo_choices == []: 87 combo_choices = ['@N', '@N*', '@C', '@C*', '@H', '@H*', '@O', '@O*', '@P', '@P*'] 88 89 # Init. 90 sub_sizer = wx.BoxSizer(wx.HORIZONTAL) 91 92 # Left padding. 93 sub_sizer.AddSpacer(padding) 94 95 # The description. 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 # The divider. 101 if not divider: 102 raise RelaxError("The divider position has not been supplied.") 103 104 # Spacing. 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 # The input field. 114 style = wx.CB_DROPDOWN 115 self._field = wx.ComboBox(parent, -1, '', style=style) 116 117 # Update the choices. 118 self.UpdateChoices(combo_choices=combo_choices, combo_data=combo_data, combo_default=default) 119 120 # Set up the input field. 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 # Right padding. 126 sub_sizer.AddSpacer(padding) 127 128 # Add to the main sizer. 129 sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0) 130 131 # Spacing below the widget. 132 if spacer == None: 133 sizer.AddStretchSpacer() 134 else: 135 sizer.AddSpacer(spacer) 136 137 # Initialise the tooltip string, if not supplied. 138 if tooltip == None: 139 tooltip = '' 140 141 # Add the ID string documentation to the tooltip. 142 for type, element in id_string_doc.element_loop(): 143 if type == 'paragraph': 144 # Initial spacing. 145 tooltip += '\n\n' 146 147 # The text. 148 tooltip += element 149 150 # Set the tooltip. 151 text.SetToolTip(wx.ToolTip(tooltip)) 152 self._field.SetToolTip(wx.ToolTip(tooltip))
153 154
155 - def Clear(self):
156 """Special method for clearing or resetting the GUI element.""" 157 158 # Clear the value. 159 self._field.Clear() 160 self._field.SetValue('')
161 162
163 - def GetValue(self):
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 # An element selected from the list. 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 # A non-list value. 178 if value == None: 179 value = gui_to_str(self._field.GetValue()) 180 181 # Return the value. 182 return value
183 184
185 - def SetValue(self, value):
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 # Loop until the proper client data is found. 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 # No value found. 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 # Store the current selection's client data to restore at the end. 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 # First clear all data. 225 self.Clear() 226 227 # Set the data if needed. 228 if combo_data == None: 229 combo_data = deepcopy(combo_choices) 230 231 # Loop over the choices and data, adding both to the end. 232 for i in range(len(combo_choices)): 233 self._field.Insert(str_to_gui(combo_choices[i]), i, combo_data[i]) 234 235 # Set the default selection. 236 if sel == None and combo_default != None: 237 # Translate if needed. 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 # Set the selection. 249 if set_sel: 250 self._field.SetStringSelection(string) 251 252 # Set the value. 253 else: 254 self._field.SetValue(string) 255 256 # Restore the selection. 257 else: 258 for j in range(self._field.GetCount()): 259 if self._field.GetClientData(j) == sel: 260 self._field.SetSelection(j)
261