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