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,2016 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  import dep_check 
 32  from gui.fonts import font 
 33  from gui.string_conv import str_to_gui 
 34   
 35   
36 -class Float_ctrl:
37 """The analysis specific floating point number control. 38 39 This consists of two elements: wx.StaticText and wx.TextCtrl. 40 """ 41
42 - def __init__(self, box, parent, text="", default="", tooltip=None, editable=True, width_text=200, width_button=80, spacer=0):
43 """Create a text selection element for the frame. 44 45 This consists of a horizontal layout with a static text element, a text control, and an optional button. 46 47 @param box: The box element to pack the structure file selection GUI element into. 48 @type box: wx.BoxSizer instance 49 @param parent: The parent GUI element. 50 @type parent: wx object 51 @keyword text: The static text. 52 @type text: str 53 @keyword default: The default text of the control. 54 @type default: str 55 @keyword tooltip: The tooltip which appears on hovering over the text or input field. 56 @type tooltip: str 57 @keyword editable: A flag specifying if the control is editable or not. 58 @type editable: bool 59 @keyword width_text: The width of the text element. 60 @type width_text: int 61 @keyword width_button: The width of the standard button used in the other elements. 62 @type width_button: int 63 @keyword spacer: The horizontal spacing between the elements. 64 @type spacer: int 65 """ 66 67 # Horizontal packing for this element. 68 sizer = wx.BoxSizer(wx.HORIZONTAL) 69 70 # The label. 71 self.label = wx.StaticText(parent, -1, text) 72 self.label.SetMinSize((width_text, -1)) 73 self.label.SetFont(font.normal) 74 sizer.Add(self.label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0) 75 76 # The size for all elements, based on this text. 77 dc = wx.ScreenDC() 78 dc.SetFont(font.normal) 79 x, y = dc.GetTextExtent(text) 80 size_horizontal = y + 8 81 82 # Spacer. 83 if dep_check.wx_classic: 84 sizer.AddSpacer((spacer, -1)) 85 else: 86 sizer.AddSpacer(spacer) 87 88 # The floating point number input field. 89 self.field = wx.TextCtrl(parent, -1, str_to_gui(default)) 90 self.field.SetMinSize((-1, size_horizontal)) 91 self.field.SetFont(font.normal) 92 self.field.SetEditable(editable) 93 if not editable: 94 colour = parent.GetBackgroundColour() 95 self.field.SetOwnBackgroundColour(colour) 96 sizer.Add(self.field, 1, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0) 97 98 # Catch all input to ensure that it is a float. 99 self.field.Bind(wx.EVT_KEY_DOWN, self.pre_input) 100 self.field.Bind(wx.EVT_TEXT, self.post_input) 101 self.field.Bind(wx.EVT_KEY_UP, self.end_input) 102 103 # Spacer. 104 if dep_check.wx_classic: 105 sizer.AddSpacer((spacer, -1)) 106 else: 107 sizer.AddSpacer(spacer) 108 109 # Empty space where buttons normally are. 110 if dep_check.wx_classic: 111 sizer.AddSpacer((width_button, -1)) 112 else: 113 sizer.AddSpacer(width_button) 114 115 # Add the element to the box. 116 box.Add(sizer, 0, wx.ALL|wx.EXPAND, 0) 117 118 # Tooltip. 119 if tooltip: 120 self.label.SetToolTip(wx.ToolTip(tooltip)) 121 self.field.SetToolTip(wx.ToolTip(tooltip))
122 123
124 - def Enable(self, enable=True):
125 """Enable or disable the element for user input. 126 127 @keyword enable: The flag specifying if the element should be enabled or disabled. 128 @type enable: bool 129 """ 130 131 # Call the text control method. 132 self.field.Enable(enable)
133 134
135 - def GetValue(self):
136 """Set the value of the control. 137 138 @return: The value of the text control. 139 @rtype: int 140 """ 141 142 # Get the value from the text control. 143 return self.field.GetValue()
144 145
146 - def SetValue(self, value):
147 """Set the value of the control. 148 149 @param value: The value to set the text control to. 150 @type value: text 151 """ 152 153 # Set the value of the text control. 154 return self.field.SetValue(value)
155 156
157 - def end_input(self, event):
158 """Restore the cursor position at the end if needed. 159 160 This does not work so well as multiple wx.EVT_KEY_DOWN events can occur for a single wx.EVT_TEXT. 161 162 @param event: The wx event. 163 @type event: wx event 164 """ 165 166 # Restore the input position. 167 if self._restore_pos_flag: 168 self.field.SetInsertionPoint(self._pos) 169 170 # Thaw the field to allow the text to be displayed. 171 self.field.Thaw()
172 173
174 - def pre_input(self, event):
175 """Catch all key presses to store the original value and position and freeze the element. 176 177 @param event: The wx event. 178 @type event: wx event 179 """ 180 181 # Initialise the restore position flag. 182 self._restore_pos_flag = False 183 184 # Store the value and position to restore if the input is bad. 185 self._value = self.field.GetValue() 186 self._pos = self.field.GetInsertionPoint() 187 188 # Freeze the field so that the changed text is only shown at the end. 189 self.field.Freeze() 190 191 # Continue the event to allow text to be entered. 192 event.Skip()
193 194
195 - def post_input(self, event):
196 """Check that the user input is a float when the text changes, restoring the value if not. 197 198 @param event: The wx event. 199 @type event: wx event 200 """ 201 202 # Get the value. 203 value = self.field.GetValue() 204 205 # Initialise the restore flag. 206 flag = False 207 208 # Check if it is a number and, if not, restore the original text. 209 try: 210 float(value) 211 except ValueError: 212 flag = True 213 214 # Do not allow spaces. 215 if ' ' in value: 216 flag = True 217 218 # Do not allow 'e' for the exponent. 219 if 'e' in value or 'E' in value: 220 flag = True 221 222 # Restore the original text and set the flag to restore the cursor position. 223 if flag: 224 self.field.SetValue(self._value) 225 self._restore_pos_flag = True 226 227 # Continue the event. 228 event.Skip()
229