"Fossies" - the Fresh Open Source Software Archive

Member "relax-5.0.0/gui/analyses/elements/model_list.py" (2 Dec 2019, 13150 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 "model_list.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-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 """Auto-analysis GUI element for the control of lists of models."""
   25 
   26 # Python module imports.
   27 import wx
   28 from wx.lib import scrolledpanel
   29 import wx.lib.mixins.listctrl
   30 
   31 # relax module imports.
   32 import dep_check
   33 from graphics import fetch_icon
   34 from gui.fonts import font
   35 from gui.message import Question
   36 from gui.misc import add_border
   37 from gui.string_conv import list_to_gui, str_to_gui
   38 from status import Status; status = Status()
   39 
   40 
   41 class Model_list:
   42     """The model list GUI element."""
   43 
   44     # Class variables.
   45     border = 10
   46     """The border width, in pixels."""
   47 
   48     desc = None
   49     """The short description for the GUI element."""
   50 
   51     model_desc = []
   52     """The short description for each model."""
   53 
   54     models = []
   55     """The list of names of the model."""
   56 
   57     params = []
   58     """The list of parameters of each model in string form."""
   59 
   60     warning = None
   61     """A warning string which if set will present a warning message to the user prior to allowing them to modify the list of models."""
   62 
   63     red_flag = False
   64     """A flag which if True will cause the flag icon to turn red if the model list has been modified."""
   65 
   66     size = wx.Size(1024, 750)
   67     """The initial size of the window."""
   68 
   69     tooltip = None
   70     """The tooltip string to add to the text and field wx GUI elements."""
   71 
   72     tooltip_button = None
   73     """The separate tooltip string to add to the button wx GUI element."""
   74 
   75 
   76     def __init__(self, parent, box):
   77         """Build the combo box list widget for a list of list selections.
   78 
   79         @param parent:      The parent GUI element.
   80         @type parent:       wx object instance
   81         @param box:         The sizer to put the combo box widget into.
   82         @type box:          wx.Sizer instance
   83         """
   84 
   85         # Store some args.
   86         self.parent = parent
   87 
   88         # Initialise all models as being selected, and create a list with the separators removed.
   89         self.select = []
   90         self.models_stripped = []
   91         for model in self.models:
   92             if model != None:
   93                 self.select.append(True)
   94                 self.models_stripped.append(model)
   95 
   96         # Initialise the model selection window.
   97         self.model_win = Model_sel_window(self.models, self.params, self.model_desc, size=self.size, border=self.border)
   98 
   99         # Horizontal packing for this element.
  100         sizer = wx.BoxSizer(wx.HORIZONTAL)
  101 
  102         # Add a label.
  103         label = self.parent.add_static_text(sizer, self.parent, text=self.desc, width=self.parent.width_text)
  104 
  105         # Spacer.
  106         if dep_check.wx_classic:
  107             sizer.AddSpacer((self.parent.spacer_horizontal, -1))
  108         else:
  109             sizer.AddSpacer(self.parent.spacer_horizontal)
  110 
  111         # The text input field.
  112         self.field = self.parent.add_text_control(sizer, self.parent, text=list_to_gui(self.GetValue()), editable=False)
  113 
  114         # Spacer.
  115         if dep_check.wx_classic:
  116             sizer.AddSpacer((self.parent.spacer_horizontal, -1))
  117         else:
  118             sizer.AddSpacer(self.parent.spacer_horizontal)
  119 
  120         # Add the button.
  121         self.button = self.parent.add_button_open(sizer, self.parent, icon=fetch_icon("oxygen.actions.flag-blue", "16x16"), text="Modify", fn=self.modify, width=self.parent.width_button, height=label.GetSize()[1]+8)
  122 
  123         # Tooltip.
  124         if self.tooltip:
  125             label.SetToolTip(wx.ToolTip(self.tooltip))
  126             self.field.SetToolTip(wx.ToolTip(self.tooltip))
  127         if self.tooltip_button:
  128             self.button.SetToolTip(wx.ToolTip(self.tooltip_button))
  129 
  130         # Add the contents to the main box.
  131         box.Add(sizer, 0, wx.ALL|wx.EXPAND, 0)
  132 
  133 
  134     def Enable(self, enable=True):
  135         """Enable or disable the element.
  136 
  137         @keyword enable:    The flag specifying if the element should be enabled or disabled.
  138         @type enable:       bool
  139         """
  140 
  141         # Call the control and button's method.
  142         self.field.Enable(enable)
  143         self.button.Enable(enable)
  144 
  145 
  146     def GetValue(self):
  147         """Return the list of models.
  148 
  149         @return:    The list of models.
  150         @rtype:     list of str
  151         """
  152 
  153         # Initialise.
  154         model_list = []
  155 
  156         # Add the models if they are selected.
  157         for i in range(len(self.select)):
  158             if self.select[i]:
  159                 model_list.append(self.models_stripped[i])
  160 
  161         # Return the list.
  162         return model_list
  163 
  164 
  165     def set_value(self, value):
  166         """Store the list of models.
  167 
  168         @param value:   The list of models.
  169         @type value:    list of str
  170         """
  171 
  172         # First set all models as being deselected.
  173         for i in range(len(self.select)):
  174             self.select[i] = False
  175 
  176         # Select all models in the list.
  177         for model in value:
  178             # The model index.
  179             index = self.models_stripped.index(model)
  180 
  181             # Set the selected flag.
  182             self.select[index] = True
  183 
  184         # Update the button.
  185         self.update_button()
  186 
  187         # Update the GUI element.
  188         self.field.SetValue(list_to_gui(self.GetValue()))
  189 
  190 
  191     def modify(self, event=None):
  192         """Modify the model selection.
  193 
  194         @keyword event: The wx event.
  195         @type event:    wx event
  196         """
  197 
  198         # First state that this should not be done.
  199         if self.warning and status.show_gui and not Question(self.warning, title="Warning - do not change!", size=(420, 210), default=False).ShowModal() == wx.ID_YES:
  200             return
  201 
  202         # Set the model selector window selections.
  203         self.model_win.set_selection(self.select)
  204 
  205         # Show the model selector window.
  206         if status.show_gui:
  207             self.model_win.ShowModal()
  208             self.model_win.Close()
  209 
  210         # Set the values.
  211         self.select = self.model_win.get_selection()
  212 
  213         # Update the button.
  214         self.update_button()
  215 
  216         # Update the GUI element.
  217         self.field.SetValue(list_to_gui(self.GetValue()))
  218 
  219 
  220     def update_button(self):
  221         """Update the button bitmap as needed."""
  222 
  223         # Nothing to do.
  224         if not self.red_flag:
  225             return
  226 
  227         # Change the flag to red to indicate to the user that changing the models is a bad thing!
  228         if False in self.select:
  229             self.button.SetBitmapLabel(wx.Bitmap(fetch_icon("oxygen.actions.flag-red", "16x16"), wx.BITMAP_TYPE_ANY))
  230 
  231         # Otherwise set it to blue (in case all models are selected again).
  232         else:
  233             self.button.SetBitmapLabel(wx.Bitmap(fetch_icon("oxygen.actions.flag-blue", "16x16"), wx.BITMAP_TYPE_ANY))
  234 
  235 
  236 
  237 class Model_sel_window(wx.Dialog):
  238     """The model selector window object."""
  239 
  240     def __init__(self, models, params, desc, size=None, border=None):
  241         """Set up the model selector window.
  242 
  243         @param models:      The list of models.
  244         @type models:       list of str
  245         @param params:      The list of parameters corresponding to the models.
  246         @type params:       list of str
  247         @param desc:        The description for each model.
  248         @type desc:         list of str
  249         @keyword size:      The initial size of the window.
  250         @type size:         wx.Size instance
  251         @keyword border:    The border width, in pixels.
  252         @type border:       int
  253         """
  254 
  255         # Set up the dialog.
  256         wx.Dialog.__init__(self, None, id=-1, title="Model list selector", size=size, style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
  257 
  258         # Initialise some values
  259         desc_flag = False
  260         if len(desc):
  261             desc_flag = True
  262 
  263         # Set the frame properties.
  264         self.SetFont(font.normal)
  265 
  266         # The main sizer.
  267         main_sizer = wx.BoxSizer(wx.VERTICAL)
  268 
  269         # Pack the sizer into the frame.
  270         self.SetSizer(main_sizer)
  271 
  272         # Build the central sizer, with borders.
  273         sizer = add_border(main_sizer, border=border, packing=wx.VERTICAL)
  274 
  275         # A scrolled panel for the text.
  276         panel = scrolledpanel.ScrolledPanel(self, -1)
  277         panel.SetAutoLayout(1)
  278         panel.SetupScrolling()
  279         sizer.Add(panel, 1, wx.ALL|wx.EXPAND, 0)
  280 
  281         # A sizer for the panel.
  282         panel_sizer = wx.BoxSizer(wx.HORIZONTAL)
  283         panel.SetSizer(panel_sizer)
  284 
  285         # Add a list control.
  286         cols = 2
  287         if desc_flag:
  288             cols += 1
  289         self.grid_sizer = wx.FlexGridSizer(len(models)+2, cols, 3, 30)
  290 
  291         # The headers (and then a space).
  292         titles = ["Model", "Parameters"]
  293         if desc_flag:
  294             titles.append("Description")
  295         for title in titles:
  296             text = wx.StaticText(panel, -1, str_to_gui(title))
  297             text.SetFont(font.subtitle)
  298             self.grid_sizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL)
  299         for i in range(len(titles)):
  300             self.grid_sizer.Add(wx.StaticText(panel, -1, ""))
  301 
  302         # Add the models and parameters.
  303         self.model_selection = []
  304         for i in range(len(models)):
  305             # No model - i.e. a separator.
  306             if models[i] == None:
  307                 for i in range(len(titles)):
  308                     self.grid_sizer.Add(wx.StaticText(panel, -1, ""))
  309                 continue
  310 
  311             # Create a checkbox for the model.
  312             check_box = wx.CheckBox(panel, -1, str_to_gui(models[i]))
  313             self.model_selection.append(check_box)
  314             self.grid_sizer.Add(check_box, 0, wx.ALIGN_CENTER_VERTICAL)
  315 
  316             # Set all selections to True.
  317             self.model_selection[-1].SetValue(True)
  318 
  319             # Add the parameter text.
  320             text = wx.StaticText(panel, -1, str_to_gui(params[i]))
  321             text.SetFont(font.normal)
  322             self.grid_sizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL)
  323 
  324             # Add the description.
  325             if desc_flag:
  326                 text = wx.StaticText(panel, -1, str_to_gui(desc[i]))
  327                 text.SetFont(font.normal)
  328                 self.grid_sizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL)
  329 
  330         # Add the table to the sizer.
  331         panel_sizer.Add(self.grid_sizer, 1, wx.ALL|wx.EXPAND, 0)
  332 
  333         # Set up the window.
  334         self.SetMinSize(wx.Size(600, 300))
  335         self.Centre()
  336 
  337 
  338     def get_selection(self):
  339         """Return the selection as a list of booleans.
  340 
  341         @return:    The list of models selected.
  342         @rtype:     list of bool
  343         """
  344 
  345         # Init.
  346         select = []
  347 
  348         # Loop over the entries.
  349         for i in range(len(self.model_selection)):
  350             select.append(self.model_selection[i].GetValue())
  351 
  352         # Return the list.
  353         return select
  354 
  355 
  356     def set_selection(self, select):
  357         """Set the selection.
  358 
  359         @param select:  The list of selections.
  360         @type select:   list of bool
  361         """
  362 
  363         # Loop over the entries.
  364         for i in range(len(self.model_selection)):
  365             self.model_selection[i].SetValue(select[i])
  366 
  367 
  368 
  369 class ModelSelListCtrl(wx.ListCtrl, wx.lib.mixins.listctrl.CheckListCtrlMixin):
  370     """A special list control with checkboxes."""
  371 
  372     def __init__(self, parent):
  373         """Initialise the control.
  374 
  375         @param parent:  The parent window.
  376         @type parent:   wx.Frame instance
  377         """
  378 
  379         # Execute the list control __init__() method.
  380         wx.ListCtrl.__init__(self, parent, -1, style=wx.BORDER_SUNKEN|wx.LC_REPORT)
  381 
  382         # Execute the CheckListCtrlMixin __init__() method.
  383         wx.lib.mixins.listctrl.CheckListCtrlMixin.__init__(self)