"Fossies" - the Fresh Open Source Software Archive

Member "relax-5.0.0/gui/analyses/elements/text_element.py" (2 Dec 2019, 7782 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 "text_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 graphics import fetch_icon
   33 from gui.fonts import font
   34 from gui.string_conv import str_to_gui
   35 
   36 
   37 class Text_ctrl:
   38     """The analysis specific text control.
   39 
   40     This consists of three elements:  wx.StaticText, wx.TextCtrl, and wx.Button.
   41     """
   42 
   43     def __init__(self, box, parent, text="", default="", tooltip=None, tooltip_button=None, button_text=" Change", control=wx.TextCtrl, icon=fetch_icon('oxygen.actions.document-open', "16x16"), fn=None, editable=True, button=False, width_text=200, width_button=80, spacer=0):
   44         """Create a text selection element for the frame.
   45 
   46         This consists of a horizontal layout with a static text element, a text control, and an optional button.
   47 
   48         @param box:                 The box element to pack the structure file selection GUI element into.
   49         @type box:                  wx.BoxSizer instance
   50         @param parent:              The parent GUI element.
   51         @type parent:               wx object
   52         @keyword text:              The static text.
   53         @type text:                 str
   54         @keyword default:           The default text of the control.
   55         @type default:              str
   56         @keyword tooltip:           The tooltip which appears on hovering over the text or input field.
   57         @type tooltip:              str
   58         @keyword tooltip_button:    The separate tooltip for the button.
   59         @type tooltip_button:       str
   60         @keyword button_text:       The text to display on the button.
   61         @type button_text:          str
   62         @keyword control:           The control class to use.
   63         @type control:              wx.TextCtrl derived class
   64         @keyword icon:              The path of the icon to use for the button.
   65         @type icon:                 str
   66         @keyword fn:                The function or method to execute when clicking on the button.  If this is a string, then an equivalent function will be searched for in the control object.
   67         @type fn:                   func or str
   68         @keyword editable:          A flag specifying if the control is editable or not.
   69         @type editable:             bool
   70         @keyword button:            A flag which if True will cause a button to appear.
   71         @type button:               bool
   72         @keyword width_text:        The width of the text element.
   73         @type width_text:           int
   74         @keyword width_button:      The width of the button.
   75         @type width_button:         int
   76         @keyword spacer:            The horizontal spacing between the elements.
   77         @type spacer:               int
   78         """
   79 
   80         # Horizontal packing for this element.
   81         sizer = wx.BoxSizer(wx.HORIZONTAL)
   82 
   83         # The label.
   84         self.label = wx.StaticText(parent, -1, text)
   85         self.label.SetMinSize((width_text, -1))
   86         self.label.SetFont(font.normal)
   87         sizer.Add(self.label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
   88 
   89         # The size for all elements, based on this text.
   90         size = self.label.GetSize()
   91         size_horizontal = size[1] + 8
   92 
   93         # Spacer.
   94         if dep_check.wx_classic:
   95             sizer.AddSpacer((spacer, -1))
   96         else:
   97             sizer.AddSpacer(spacer)
   98 
   99         # The text input field.
  100         self.field = control(parent, -1, str_to_gui(default))
  101         self.field.SetMinSize((-1, size_horizontal))
  102         self.field.SetFont(font.normal)
  103         self.field.SetEditable(editable)
  104         if not editable:
  105             colour = parent.GetBackgroundColour()
  106             self.field.SetOwnBackgroundColour(colour)
  107         sizer.Add(self.field, 1, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
  108 
  109         # Spacer.
  110         if dep_check.wx_classic:
  111             sizer.AddSpacer((spacer, -1))
  112         else:
  113             sizer.AddSpacer(spacer)
  114 
  115         # The button.
  116         if button:
  117             # Function is in the control class.
  118             if isinstance(fn, str):
  119                 # The function.
  120                 fn = getattr(field, fn)
  121 
  122             # Add the button.
  123             self.button = wx.lib.buttons.ThemedGenBitmapTextButton(parent, -1, None, str_to_gui(button_text))
  124             self.button.SetBitmapLabel(wx.Bitmap(icon, wx.BITMAP_TYPE_ANY))
  125             self.button.SetMinSize((width_button, size_horizontal))
  126             self.button.SetFont(font.normal)
  127             parent.Bind(wx.EVT_BUTTON, fn, self.button)
  128             sizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
  129 
  130         # No button, so add a spacer.
  131         else:
  132             if dep_check.wx_classic:
  133                 sizer.AddSpacer((width_button, -1))
  134             else:
  135                 sizer.AddSpacer(width_button)
  136 
  137         # Add the element to the box.
  138         box.Add(sizer, 0, wx.ALL|wx.EXPAND, 0)
  139 
  140         # Tooltip.
  141         if tooltip:
  142             self.label.SetToolTip(wx.ToolTip(tooltip))
  143             self.field.SetToolTip(wx.ToolTip(tooltip))
  144         if button and tooltip_button:
  145             self.button.SetToolTip(wx.ToolTip(tooltip_button))
  146 
  147 
  148     def Enable(self, enable=True):
  149         """Enable or disable the element for user input.
  150 
  151         @keyword enable:    The flag specifying if the element should be enabled or disabled.
  152         @type enable:       bool
  153         """
  154 
  155         # Call the control and button methods.
  156         self.field.Enable(enable)
  157         if hasattr(self, 'button'):
  158             self.button.Enable(enable)
  159 
  160 
  161     def GetValue(self):
  162         """Set the value of the control.
  163 
  164         @return:    The value of the text control.
  165         @rtype:     int
  166         """
  167 
  168         # Get the value from the text control.
  169         return self.field.GetValue()
  170 
  171 
  172     def SetValue(self, value):
  173         """Set the value of the control.
  174 
  175         @param value:   The value to set the text control to.
  176         @type value:    text
  177         """
  178 
  179         # Set the value of the text control.
  180         return self.field.SetValue(value)