Package gui :: Package analyses :: Package elements :: Module float_element
[hide private]
[frames] | no frames]

Source Code for Module gui.analyses.elements.float_element

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009-2010 Michael Bieri                                       # 
  4  # Copyright (C) 2009-2011,2013 Edward d'Auvergne                              # 
  5  #                                                                             # 
  6  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  7  #                                                                             # 
  8  # This program is free software: you can redistribute it and/or modify        # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation, either version 3 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # This program is distributed in the hope that it will be useful,             # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Module containing the base class for all frames.""" 
 25   
 26  # Python module imports. 
 27  import wx 
 28  import wx.lib.buttons 
 29   
 30  # relax module imports. 
 31  from gui.fonts import font 
 32  from gui.string_conv import str_to_gui 
 33   
 34   
35 -class Float_ctrl:
36 """The analysis specific floating point number control. 37 38 This consists of two elements: wx.StaticText and wx.TextCtrl. 39 """ 40
41 - def __init__(self, box, parent, text="", default="", tooltip=None, editable=True, width_text=200, width_button=80, spacer=0):
42 """Create a text selection element for the frame. 43 44 This consists of a horizontal layout with a static text element, a text control, and an optional button. 45 46 @param box: The box element to pack the structure file selection GUI element into. 47 @type box: wx.BoxSizer instance 48 @param parent: The parent GUI element. 49 @type parent: wx object 50 @keyword text: The static text. 51 @type text: str 52 @keyword default: The default text of the control. 53 @type default: str 54 @keyword tooltip: The tooltip which appears on hovering over the text or input field. 55 @type tooltip: str 56 @keyword editable: A flag specifying if the control is editable or not. 57 @type editable: bool 58 @keyword width_text: The width of the text element. 59 @type width_text: int 60 @keyword width_button: The width of the standard button used in the other elements. 61 @type width_button: int 62 @keyword spacer: The horizontal spacing between the elements. 63 @type spacer: int 64 """ 65 66 # Horizontal packing for this element. 67 sizer = wx.BoxSizer(wx.HORIZONTAL) 68 69 # The label. 70 self.label = wx.StaticText(parent, -1, text) 71 self.label.SetMinSize((width_text, -1)) 72 self.label.SetFont(font.normal) 73 sizer.Add(self.label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0) 74 75 # The size for all elements, based on this text. 76 size = self.label.GetSize() 77 size_horizontal = size[1] + 8 78 79 # Spacer. 80 sizer.AddSpacer((spacer, -1)) 81 82 # The floating point number input field. 83 self.field = wx.TextCtrl(parent, -1, str_to_gui(default)) 84 self.field.SetMinSize((-1, size_horizontal)) 85 self.field.SetFont(font.normal) 86 self.field.SetEditable(editable) 87 if not editable: 88 colour = parent.GetBackgroundColour() 89 self.field.SetOwnBackgroundColour(colour) 90 sizer.Add(self.field, 1, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0) 91 92 # Catch all input to ensure that it is a float. 93 self.field.Bind(wx.EVT_KEY_DOWN, self.pre_input) 94 self.field.Bind(wx.EVT_TEXT, self.post_input) 95 self.field.Bind(wx.EVT_KEY_UP, self.end_input) 96 97 # Spacer. 98 sizer.AddSpacer((spacer, -1)) 99 100 # Empty space where buttons normally are. 101 sizer.AddSpacer((width_button, -1)) 102 103 # Add the element to the box. 104 box.Add(sizer, 0, wx.ALL|wx.EXPAND, 0) 105 106 # Tooltip. 107 if tooltip: 108 self.label.SetToolTipString(tooltip) 109 self.field.SetToolTipString(tooltip)
110 111
112 - def Enable(self, enable=True):
113 """Enable or disable the element for user input. 114 115 @keyword enable: The flag specifying if the element should be enabled or disabled. 116 @type enable: bool 117 """ 118 119 # Call the text control method. 120 self.field.Enable(enable)
121 122
123 - def GetValue(self):
124 """Set the value of the control. 125 126 @return: The value of the text control. 127 @rtype: int 128 """ 129 130 # Get the value from the text control. 131 return self.field.GetValue()
132 133
134 - def SetValue(self, value):
135 """Set the value of the control. 136 137 @param value: The value to set the text control to. 138 @type value: text 139 """ 140 141 # Set the value of the text control. 142 return self.field.SetValue(value)
143 144
145 - def end_input(self, event):
146 """Restore the cursor position at the end if needed. 147 148 This does not work so well as multiple wx.EVT_KEY_DOWN events can occur for a single wx.EVT_TEXT. 149 150 @param event: The wx event. 151 @type event: wx event 152 """ 153 154 # Restore the input position. 155 if self._restore_pos_flag: 156 self.field.SetInsertionPoint(self._pos) 157 158 # Thaw the field to allow the text to be displayed. 159 self.field.Thaw()
160 161
162 - def pre_input(self, event):
163 """Catch all key presses to store the original value and position and freeze the element. 164 165 @param event: The wx event. 166 @type event: wx event 167 """ 168 169 # Initialise the restore position flag. 170 self._restore_pos_flag = False 171 172 # Store the value and position to restore if the input is bad. 173 self._value = self.field.GetValue() 174 self._pos = self.field.GetInsertionPoint() 175 176 # Freeze the field so that the changed text is only shown at the end. 177 self.field.Freeze() 178 179 # Continue the event to allow text to be entered. 180 event.Skip()
181 182
183 - def post_input(self, event):
184 """Check that the user input is a float when the text changes, restoring the value if not. 185 186 @param event: The wx event. 187 @type event: wx event 188 """ 189 190 # Get the value. 191 value = self.field.GetValue() 192 193 # Initialise the restore flag. 194 flag = False 195 196 # Check if it is a number and, if not, restore the original text. 197 try: 198 float(value) 199 except ValueError: 200 flag = True 201 202 # Do not allow spaces. 203 if ' ' in value: 204 flag = True 205 206 # Do not allow 'e' for the exponent. 207 if 'e' in value or 'E' in value: 208 flag = True 209 210 # Restore the original text and set the flag to restore the cursor position. 211 if flag: 212 self.field.SetValue(self._value) 213 self._restore_pos_flag = True 214 215 # Continue the event. 216 event.Skip()
217