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

Source Code for Module gui.input_elements.sequence_2D

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2012-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  """Module containing a set of special GUI elements to be used in the relax wizards.""" 
 24   
 25  # Python module imports. 
 26  import sys 
 27  import wx 
 28  import wx.lib.mixins.listctrl 
 29   
 30  # relax module imports. 
 31  from gui.input_elements.sequence import Sequence, Sequence_list_ctrl, Sequence_window 
 32  from status import Status; status = Status() 
 33   
 34   
35 -class Sequence_2D(Sequence):
36 """Wizard GUI element for the input of all types of 2D Python sequence objects. 37 38 The supported Python types include: 39 - list of floats 40 - list of integers 41 - list of strings 42 - tuple of floats 43 - tuple of integers 44 - tuple of strings 45 """ 46
47 - def __init__(self, name=None, default=None, parent=None, sizer=None, element_type='default', seq_type=None, value_type=None, dim=None, min=0, max=1000, titles=None, desc=None, combo_choices=None, combo_data=None, combo_list_min=None, tooltip=None, divider=None, padding=0, spacer=None, height_element=27, read_only=False, can_be_none=False):
48 """Set up the element. 49 50 @keyword name: The name of the element to use in titles, etc. 51 @type name: str 52 @keyword default: The default value of the element. 53 @type default: 2D sequence object 54 @keyword parent: The wizard GUI element. 55 @type parent: wx.Panel instance 56 @keyword sizer: The sizer to put the input field widget into. 57 @type sizer: wx.Sizer instance 58 @keyword element_type: The type of GUI element to create. If set to 'default', the wx.TextCtrl element with a button to bring up a dialog with ListCtrl will be used. If set to 'combo_list', the special gui.components.combo_list.Combo_list element will be used. 59 @type element_type: str 60 @keyword seq_type: The type of Python sequence. This should be one of 'list' or 'tuple'. 61 @type seq_type: str 62 @keyword value_type: The type of Python object that the value should be. This can be one of 'float', 'int', or 'str'. 63 @type value_type: str 64 @keyword dim: The dimensions that a list or tuple must conform to. For a 1D sequence, this can be a single value or a tuple of possible sizes. For a 2D sequence (a numpy matrix or list of lists), this must be a tuple of the fixed dimension sizes, e.g. a 3x5 matrix should be specified as (3, 5). 65 @type dim: int, tuple of int or None 66 @keyword min: For a SpinCtrl, the minimum value allowed. 67 @type min: int 68 @keyword max: For a SpinCtrl, the maximum value allowed. 69 @type max: int 70 @keyword titles: The titles of each of the elements of the fixed width second dimension. 71 @type titles: list of str 72 @keyword desc: The text description. 73 @type desc: str 74 @keyword combo_choices: The list of choices to present to the user. This is only used if the element_type is set to 'combo'. 75 @type combo_choices: list of str 76 @keyword combo_data: The data returned by a call to GetValue(). This is only used if the element_type is set to 'combo'. 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. 77 @type combo_data: list 78 @keyword combo_list_min: The minimum length for the Combo_list object. 79 @type combo_list_min: int or None 80 @keyword tooltip: The tooltip which appears on hovering over the text or input field. 81 @type tooltip: str 82 @keyword divider: The optional position of the divider. If None, the class variable _div_left will be used. 83 @type divider: None or int 84 @keyword padding: Spacing to the left and right of the widgets. 85 @type padding: int 86 @keyword spacer: The amount of spacing to add below the field in pixels. If None, a stretchable spacer will be used. 87 @type spacer: None or int 88 @keyword height_element: The height in pixels of the GUI element. 89 @type height_element: int 90 @keyword read_only: A flag which if True means that the text of the element cannot be edited. 91 @type read_only: bool 92 @keyword can_be_none: A flag which specifies if the element is allowed to have the None value. 93 @type can_be_none: bool 94 """ 95 96 # Store some of the args. 97 self.titles = titles 98 99 # Initialise the base class. 100 Sequence.__init__(self, name=name, default=default, parent=parent, sizer=sizer, element_type=element_type, seq_type=seq_type, value_type=value_type, dim=dim, min=min, max=max, desc=desc, combo_choices=combo_choices, combo_data=combo_data, combo_list_min=combo_list_min, tooltip=tooltip, divider=divider, padding=padding, spacer=spacer, height_element=height_element, read_only=read_only, can_be_none=can_be_none)
101 102
103 - def open_dialog(self, event):
104 """Open a special dialog for inputting a list of text values. 105 106 @param event: The wx event. 107 @type event: wx event 108 """ 109 110 # Initialise the model selection window. 111 win = Sequence_window_2D(name=self.name, seq_type=self.seq_type, value_type=self.value_type, titles=self.titles, dim=self.dim) 112 113 # Set the model selector window selections. 114 win.SetValue(self.GetValue()) 115 116 # Show the model selector window. 117 if status.show_gui: 118 win.ShowModal() 119 win.Close() 120 121 # Get the value. 122 value = win.GetValue() 123 124 # No sequence data. 125 if not len(value): 126 self.Clear() 127 128 # Set the values. 129 else: 130 self.SetValue(value) 131 132 # Destroy the window. 133 del win
134 135 136
137 -class Sequence_window_2D(Sequence_window):
138 """The Python 2D sequence object editor window.""" 139
140 - def __init__(self, name='', seq_type='list', value_type='str', dim=None, titles=None):
141 """Set up the string list editor window. 142 143 @keyword name: The name of the window. 144 @type name: str 145 @keyword seq_type: The type of Python sequence. This should be one of 'list' or 'tuple'. 146 @type seq_type: str 147 @keyword value_type: The type of Python data expected in the sequence. This should be one of 'float', 'int', or 'str'. 148 @type value_type: str 149 @keyword dim: The fixed dimensions that the sequence must conform to. 150 @type dim: tuple of int or None 151 @keyword titles: The titles of each of the elements of the fixed width second dimension. If the dim argument is given, the length of this list must match the second number. 152 @type titles: list of str 153 """ 154 155 # Store the titles. 156 self.titles = titles 157 if titles == None: 158 self.titles = [wx.EmptyString] * dim[1] 159 160 # Determine the dimensions if not given. 161 if dim == None: 162 dim = (None, len(self.titles)) 163 164 # Initialise the base class. 165 Sequence_window.__init__(self, name=name, seq_type=seq_type, value_type=value_type, dim=dim)
166 167
168 - def GetValue(self):
169 """Return the values as a 2D sequence of values. 170 171 @return: The list of lists of values. 172 @rtype: list of lists of str 173 """ 174 175 # Init. 176 values = [] 177 178 # Loop over the entries. 179 for i in range(self.sequence.GetItemCount()): 180 # Append a new list. 181 values.append([]) 182 183 # Loop over the items. 184 for j in range(self.dim[1]): 185 # The item. 186 item = self.sequence.GetItem(i, j) 187 188 # Append the value. 189 values[-1].append(self.convert_from_gui(item.GetText())) 190 191 # Sequence conversion. 192 if self.seq_type == 'tuple': 193 values[-1] = tuple(values[-1]) 194 195 # Sequence conversion. 196 if self.seq_type == 'tuple': 197 values = tuple(values) 198 199 # Return the list. 200 return values
201 202
203 - def SetValue(self, values):
204 """Set up the list of lists values. 205 206 @param values: The list of lists of values to add to the list. 207 @type values: list of lists of str or None 208 """ 209 210 # No value. 211 if values == None: 212 return 213 214 # Loop over the entries. 215 for i in range(len(values)): 216 # Fixed dimension sequences - set the first value of the pre-created list. 217 if self.dim[0] != None: 218 self.sequence.SetStringItem(index=i, col=0, label=self.convert_to_gui(values[i][0])) 219 220 # Variable dimension sequences - append the first value to the end of the blank list. 221 else: 222 self.sequence.InsertStringItem(sys.maxint, self.convert_to_gui(values[i][0])) 223 224 # Loop over the values. 225 for j in range(1, self.dim[1]): 226 # Set the value. 227 self.sequence.SetStringItem(i, j, self.convert_to_gui(values[i][j])) 228 229 # Refresh. 230 self.Refresh()
231 232
233 - def add_list(self, sizer):
234 """Set up the list control. 235 236 @param sizer: A sizer object. 237 @type sizer: wx.Sizer instance 238 """ 239 240 # The control. 241 self.sequence = Sequence_list_ctrl(self) 242 243 # Set the column title. 244 title = "%s%s" % (self.name[0].upper(), self.name[1:]) 245 246 # Add the columns. 247 for i in range(self.dim[1]): 248 self.sequence.InsertColumn(i, self.titles[i]) 249 self.sequence.SetColumnWidth(i, self.width/self.dim[1]) 250 251 # Add the table to the sizer. 252 sizer.Add(self.sequence, 1, wx.ALL|wx.EXPAND, 0) 253 254 # The fixed dimension sequence - add all the rows needed. 255 if self.dim[0] != None: 256 for i in range(self.dim[0]): 257 self.append_row(None)
258