"Fossies" - the Fresh Open Source Software Archive

Member "relax-5.0.0/gui/analyses/elements/float_element.py" (2 Dec 2019, 8278 Bytes) of package /linux/privat/relax-5.0.0.src.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "float_element.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.1.3_vs_5.0.0.

    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         size = self.label.GetSize()
   78         size_horizontal = size[1] + 8
   79 
   80         # Spacer.
   81         if dep_check.wx_classic:
   82             sizer.AddSpacer((spacer, -1))
   83         else:
   84             sizer.AddSpacer(spacer)
   85 
   86         # The floating point number input field.
   87         self.field = wx.TextCtrl(parent, -1, str_to_gui(default))
   88         self.field.SetMinSize((-1, size_horizontal))
   89         self.field.SetFont(font.normal)
   90         self.field.SetEditable(editable)
   91         if not editable:
   92             colour = parent.GetBackgroundColour()
   93             self.field.SetOwnBackgroundColour(colour)
   94         sizer.Add(self.field, 1, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
   95 
   96         # Catch all input to ensure that it is a float.
   97         self.field.Bind(wx.EVT_KEY_DOWN, self.pre_input)
   98         self.field.Bind(wx.EVT_TEXT, self.post_input)
   99         self.field.Bind(wx.EVT_KEY_UP, self.end_input)
  100 
  101         # Spacer.
  102         if dep_check.wx_classic:
  103             sizer.AddSpacer((spacer, -1))
  104         else:
  105             sizer.AddSpacer(spacer)
  106 
  107         # Empty space where buttons normally are.
  108         if dep_check.wx_classic:
  109             sizer.AddSpacer((width_button, -1))
  110         else:
  111             sizer.AddSpacer(width_button)
  112 
  113         # Add the element to the box.
  114         box.Add(sizer, 0, wx.ALL|wx.EXPAND, 0)
  115 
  116         # Tooltip.
  117         if tooltip:
  118             self.label.SetToolTip(wx.ToolTip(tooltip))
  119             self.field.SetToolTip(wx.ToolTip(tooltip))
  120 
  121 
  122     def Enable(self, enable=True):
  123         """Enable or disable the element for user input.
  124 
  125         @keyword enable:    The flag specifying if the element should be enabled or disabled.
  126         @type enable:       bool
  127         """
  128 
  129         # Call the text control method.
  130         self.field.Enable(enable)
  131 
  132 
  133     def GetValue(self):
  134         """Set the value of the control.
  135 
  136         @return:    The value of the text control.
  137         @rtype:     int
  138         """
  139 
  140         # Get the value from the text control.
  141         return self.field.GetValue()
  142 
  143 
  144     def SetValue(self, value):
  145         """Set the value of the control.
  146 
  147         @param value:   The value to set the text control to.
  148         @type value:    text
  149         """
  150 
  151         # Set the value of the text control.
  152         return self.field.SetValue(value)
  153 
  154 
  155     def end_input(self, event):
  156         """Restore the cursor position at the end if needed.
  157 
  158         This does not work so well as multiple wx.EVT_KEY_DOWN events can occur for a single wx.EVT_TEXT.
  159 
  160         @param event:   The wx event.
  161         @type event:    wx event
  162         """
  163 
  164         # Restore the input position.
  165         if self._restore_pos_flag:
  166             self.field.SetInsertionPoint(self._pos)
  167 
  168         # Thaw the field to allow the text to be displayed.
  169         self.field.Thaw()
  170 
  171 
  172     def pre_input(self, event):
  173         """Catch all key presses to store the original value and position and freeze the element.
  174 
  175         @param event:   The wx event.
  176         @type event:    wx event
  177         """
  178 
  179         # Initialise the restore position flag.
  180         self._restore_pos_flag = False
  181 
  182         # Store the value and position to restore if the input is bad.
  183         self._value = self.field.GetValue()
  184         self._pos = self.field.GetInsertionPoint()
  185 
  186         # Freeze the field so that the changed text is only shown at the end.
  187         self.field.Freeze()
  188 
  189         # Continue the event to allow text to be entered.
  190         event.Skip()
  191 
  192 
  193     def post_input(self, event):
  194         """Check that the user input is a float when the text changes, restoring the value if not.
  195 
  196         @param event:   The wx event.
  197         @type event:    wx event
  198         """
  199 
  200         # Get the value.
  201         value = self.field.GetValue()
  202 
  203         # Initialise the restore flag.
  204         flag = False
  205 
  206         # Check if it is a number and, if not, restore the original text.
  207         try:
  208             float(value)
  209         except ValueError:
  210             flag = True
  211 
  212         # Do not allow spaces.
  213         if ' ' in value:
  214             flag = True
  215 
  216         # Do not allow 'e' for the exponent.
  217         if 'e' in value or 'E' in value:
  218             flag = True
  219 
  220         # Restore the original text and set the flag to restore the cursor position.
  221         if flag:
  222             self.field.SetValue(self._value)
  223             self._restore_pos_flag = True
  224 
  225         # Continue the event.
  226         event.Skip()