Package gui :: Package analyses :: Package elements :: Module model_list
[hide private]
[frames] | no frames]

Source Code for Module gui.analyses.elements.model_list

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