"Fossies" - the Fresh Open Source Software Archive

Member "relax-5.0.0/gui/input_elements/spin_id.py" (2 Dec 2019, 10559 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 "spin_id.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) 2010-2012 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 """GUI element for the user input of spin IDs."""
   24 
   25 # Python module imports.
   26 from copy import deepcopy
   27 import wx
   28 
   29 # relax module imports.
   30 import dep_check
   31 from gui.fonts import font
   32 from gui.string_conv import gui_to_str, str_to_gui
   33 from lib.errors import RelaxError
   34 from pipe_control.mol_res_spin import id_string_doc
   35 
   36 
   37 class Spin_id:
   38     """GUI element for the input of spin ID strings."""
   39 
   40     def __init__(self, name=None, default=None, parent=None, element_type='default', sizer=None, desc="spin ID string", combo_choices=None, combo_data=None, tooltip=None, divider=None, padding=0, spacer=None, height_element=27, can_be_none=True):
   41         """Set up the base spin ID element.
   42 
   43         @keyword name:              The name of the element to use in titles, etc.
   44         @type name:                 str
   45         @keyword default:           The default value.
   46         @type default:              str or None
   47         @keyword parent:            The parent GUI element.
   48         @type parent:               wx.Panel instance
   49         @keyword element_type:      The type of GUI element to create.  This currently only supports the 'default' type.
   50         @type element_type:         str
   51         @keyword sizer:             The sizer to put the input field widget into.
   52         @type sizer:                wx.Sizer instance
   53         @keyword desc:              The text description.
   54         @type desc:                 str
   55         @keyword combo_choices:     The list of choices to present to the user.
   56         @type combo_choices:        list of str
   57         @keyword combo_data:        The data returned by a call to GetValue().  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.
   58         @type combo_data:           list
   59         @keyword tooltip:           The tooltip which appears on hovering over the text or input field.
   60         @type tooltip:              str
   61         @keyword choices:           The list of choices to present to the user.
   62         @type choices:              list of str
   63         @keyword divider:           The optional position of the divider.  If None, the class variable _div_left will be used.
   64         @type divider:              None or int
   65         @keyword padding:           Spacing to the left and right of the widgets.
   66         @type padding:              int
   67         @keyword spacer:            The amount of spacing to add below the field in pixels.  If None, a stretchable spacer will be used.
   68         @type spacer:               None or int
   69         @keyword height_element:    The height in pixels of the GUI element.
   70         @type height_element:       int
   71         @keyword can_be_none:       A flag which specifies if the element is allowed to have the None value.
   72         @type can_be_none:          bool
   73         """
   74 
   75         # Check the element type.
   76         types = ['default']
   77         if element_type not in types:
   78             raise RelaxError("The %s element type '%s' must be one of %s." % (name, element_type, types))
   79 
   80         # Store the args.
   81         self.name = name
   82         self.default = default
   83         self.can_be_none = can_be_none
   84 
   85         # The combo choices, if not supplied.
   86         if combo_choices == None or combo_choices == []:
   87             combo_choices = ['@N', '@N*', '@C', '@C*', '@H', '@H*', '@O', '@O*', '@P', '@P*']
   88 
   89         # Init.
   90         sub_sizer = wx.BoxSizer(wx.HORIZONTAL)
   91 
   92         # Left padding.
   93         sub_sizer.AddSpacer(padding)
   94 
   95         # The description.
   96         text = wx.StaticText(parent, -1, desc, style=wx.ALIGN_LEFT)
   97         text.SetFont(font.normal)
   98         sub_sizer.Add(text, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 0)
   99 
  100         # The divider.
  101         if not divider:
  102             raise RelaxError("The divider position has not been supplied.")
  103 
  104         # Spacing.
  105         x, y = text.GetSize()
  106         if dep_check.wx_classic:
  107             sub_sizer.AddSpacer((divider - x, 0))
  108         else:
  109             sub_sizer.AddSpacer(divider - x)
  110 
  111         # The input field.
  112         style = wx.CB_DROPDOWN
  113         self._field = wx.ComboBox(parent, -1, '', style=style)
  114 
  115         # Update the choices.
  116         self.UpdateChoices(combo_choices=combo_choices, combo_data=combo_data, combo_default=default)
  117 
  118         # Set up the input field.
  119         self._field.SetMinSize((50, height_element))
  120         self._field.SetFont(font.normal)
  121         sub_sizer.Add(self._field, 1, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0)
  122 
  123         # Right padding.
  124         sub_sizer.AddSpacer(padding)
  125 
  126         # Add to the main sizer.
  127         sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0)
  128 
  129         # Spacing below the widget.
  130         if spacer == None:
  131             sizer.AddStretchSpacer()
  132         else:
  133             sizer.AddSpacer(spacer)
  134 
  135         # Initialise the tooltip string, if not supplied.
  136         if tooltip == None:
  137             tooltip = ''
  138 
  139         # Add the ID string documentation to the tooltip.
  140         for type, element in id_string_doc.element_loop():
  141             if type == 'paragraph':
  142                 # Initial spacing.
  143                 tooltip += '\n\n'
  144 
  145                 # The text.
  146                 tooltip += element
  147 
  148         # Set the tooltip.
  149         text.SetToolTip(wx.ToolTip(tooltip))
  150         self._field.SetToolTip(wx.ToolTip(tooltip))
  151 
  152 
  153     def Clear(self):
  154         """Special method for clearing or resetting the GUI element."""
  155 
  156         # Clear the value.
  157         self._field.Clear()
  158         self._field.SetValue('')
  159 
  160 
  161     def GetValue(self):
  162         """Special method for returning the value of the GUI element.
  163 
  164         @return:    The spin ID value.
  165         @rtype:     str or None
  166         """
  167 
  168         # An element selected from the list.
  169         sel_index = self._field.GetSelection()
  170         if sel_index == wx.NOT_FOUND:
  171             value = None
  172         else:
  173             value = gui_to_str(self._field.GetClientData(sel_index))
  174 
  175         # A non-list value.
  176         if value == None:
  177             value = gui_to_str(self._field.GetValue())
  178 
  179         # Return the value.
  180         return value
  181 
  182 
  183     def SetValue(self, value):
  184         """Special method for setting the value of the GUI element.
  185 
  186         @param value:   The spin ID to set.
  187         @type value:    str or None
  188         """
  189 
  190         # Loop until the proper client data is found.
  191         found = False
  192         for i in range(self._field.GetCount()):
  193             if self._field.GetClientData(i) == value:
  194                 self._field.SetSelection(i)
  195                 found = True
  196                 break
  197 
  198         # No value found.
  199         if not found:
  200             self._field.SetSelection(wx.NOT_FOUND)
  201             self._field.SetValue(str_to_gui(value))
  202 
  203 
  204     def UpdateChoices(self, combo_choices=None, combo_data=None, combo_default=None):
  205         """Special wizard method for updating the list of choices in a ComboBox type element.
  206 
  207         @keyword combo_choices: The list of choices to present to the user.
  208         @type combo_choices:    list of str
  209         @keyword combo_data:    The data returned by a call to GetValue().  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.
  210         @type combo_data:       list
  211         @keyword combo_default: The default value of the ComboBox.  This is only used if the element_type is set to 'combo'.
  212         @type combo_default:    str or None
  213         """
  214 
  215         # Store the current selection's client data to restore at the end.
  216         sel_index = self._field.GetSelection()
  217         if sel_index == wx.NOT_FOUND:
  218             sel = None
  219         else:
  220             sel = self._field.GetClientData(sel_index)
  221 
  222         # First clear all data.
  223         self.Clear()
  224 
  225         # Set the data if needed.
  226         if combo_data == None:
  227             combo_data = deepcopy(combo_choices)
  228 
  229         # Loop over the choices and data, adding both to the end.
  230         for i in range(len(combo_choices)):
  231             self._field.Insert(str_to_gui(combo_choices[i]), i, combo_data[i])
  232 
  233         # Set the default selection.
  234         if sel == None and combo_default != None:
  235             # Translate if needed.
  236             if combo_default in combo_choices:
  237                 string = combo_default
  238                 set_sel = True
  239             elif combo_default not in combo_data:
  240                 string = combo_default
  241                 set_sel = False
  242             else:
  243                 string = combo_choices[combo_data.index(combo_default)]
  244                 set_sel = True
  245 
  246             # Set the selection.
  247             if set_sel:
  248                 self._field.SetStringSelection(string)
  249 
  250             # Set the value.
  251             else:
  252                 self._field.SetValue(string)
  253 
  254         # Restore the selection.
  255         else:
  256             for j in range(self._field.GetCount()):
  257                 if self._field.GetClientData(j) == sel:
  258                     self._field.SetSelection(j)