Package gui :: Package components :: Module free_file_format
[hide private]
[frames] | no frames]

Source Code for Module gui.components.free_file_format

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009 Michael Bieri                                            # 
  4  # Copyright (C) 2010-2012 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax 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 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax 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 relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Python module imports. 
 25  import wx 
 26  from wx.lib import buttons 
 27   
 28  # relax module imports. 
 29  from data import Relax_data_store; ds = Relax_data_store() 
 30  from status import Status; status = Status() 
 31   
 32  # relax GUI module imports. 
 33  from gui.fonts import font 
 34  from gui.icons import relax_icons 
 35  from gui.input_elements.value import Value 
 36  from gui.message import Question 
 37  from gui import paths 
 38  from gui.string_conv import gui_to_int, int_to_gui, str_to_gui 
 39  from gui.wizard import Wiz_page 
 40   
 41   
42 -class Free_file_format:
43 """GUI element for the free file format. 44 45 This is used for specifying the columns used for the molecule name, residue name and number, spin name and number and data and error columns. 46 """ 47 48 size_square_button = (33, 33) 49
50 - def __init__(self, sizer, parent, padding=10, spacer=3, data_cols=False, save=True, reset=True):
51 """Build the free format file settings widget. 52 53 @param sizer: The sizer to put the GUI element into. 54 @type sizer: wx.Sizer instance 55 @param parent: The parent wx GUI element. 56 @type parent: wx object 57 @keyword padding: The size of the padding between the wx.StaticBoxSizer border and the internal elements, in pixels. 58 @type padding: int 59 @keyword spacer: The horizontal spacing between the elements, in pixels. 60 @type spacer: int 61 @keyword data_cols: A flag which if True causes the data and error column elements to be displayed. 62 @type data_cols: bool 63 @keyword save: A flag which if True will cause the save button to be displayed. 64 @type save: bool 65 @keyword reset: A flag which if True will cause the reset button to be displayed. 66 @type reset: bool 67 """ 68 69 # Store the args. 70 self.parent = parent 71 72 # A static box to hold all the widgets. 73 box = wx.StaticBox(self.parent, -1, "Free format file settings") 74 box.SetFont(font.subtitle) 75 76 # Init. 77 main_sizer = wx.StaticBoxSizer(box, wx.HORIZONTAL) 78 field_sizer = wx.BoxSizer(wx.VERTICAL) 79 button_sizer = wx.BoxSizer(wx.VERTICAL) 80 81 # The border of the widget. 82 border = wx.BoxSizer() 83 84 # Place the box sizer inside the border. 85 border.Add(main_sizer, 1, wx.ALL|wx.EXPAND, 0) 86 87 # Add to the main sizer (followed by stretchable spacing). 88 sizer.Add(border, 0, wx.EXPAND) 89 sizer.AddStretchSpacer() 90 91 # Calculate the divider position. 92 divider = self.parent._div_left - border.GetMinSize()[0] / 2 - padding 93 94 # The columns. 95 self.spin_id_col = Value(name='spin_id_col', parent=self.parent, value_type='int', sizer=field_sizer, desc="spin ID column", divider=divider, padding=padding, spacer=spacer, can_be_none=True) 96 self.mol_name_col = Value(name='mol_name_col', parent=self.parent, value_type='int', sizer=field_sizer, desc="Molecule name column:", divider=divider, padding=padding, spacer=spacer, can_be_none=True) 97 self.res_num_col = Value(name='res_num_col', parent=self.parent, value_type='int', sizer=field_sizer, desc="Residue number column:", divider=divider, padding=padding, spacer=spacer, can_be_none=True) 98 self.res_name_col = Value(name='res_name_col', parent=self.parent, value_type='int', sizer=field_sizer, desc="Residue name column:", divider=divider, padding=padding, spacer=spacer, can_be_none=True) 99 self.spin_num_col = Value(name='spin_num_col', parent=self.parent, value_type='int', sizer=field_sizer, desc="Spin number column:", divider=divider, padding=padding, spacer=spacer, can_be_none=True) 100 self.spin_name_col = Value(name='spin_name_col', parent=self.parent, value_type='int', sizer=field_sizer, desc="Spin name column:", divider=divider, padding=padding, spacer=spacer, can_be_none=True) 101 if data_cols: 102 self.data_col = Value(name='data_col', parent=self.parent, value_type='int', sizer=field_sizer, desc="Data column:", divider=divider, padding=padding, spacer=spacer, can_be_none=True) 103 self.error_col = Value(name='error_col', parent=self.parent, value_type='int', sizer=field_sizer, desc="Error column:", divider=divider, padding=padding, spacer=spacer, can_be_none=True) 104 105 # The column separator. 106 self.sep = Value(name='sep', parent=self.parent, element_type='combo', value_type='str', sizer=field_sizer, desc="Column separator:", combo_choices=["white space", ",", ";", ":", ""], divider=divider, padding=padding, spacer=spacer, read_only=False, can_be_none=True) 107 108 # Add the field sizer to the main sizer. 109 main_sizer.Add(field_sizer, 1, wx.ALL|wx.EXPAND, 0) 110 111 # Set the values. 112 self.set_vals() 113 114 # Buttons! 115 if save or reset: 116 # Add a save button. 117 if save: 118 # Build the button. 119 button = buttons.ThemedGenBitmapTextButton(self.parent, -1, None, "") 120 button.SetBitmapLabel(wx.Bitmap(paths.icon_22x22.save, wx.BITMAP_TYPE_ANY)) 121 button.SetFont(font.normal) 122 button.SetToolTipString("Save the free file format settings within the relax data store.") 123 button.SetMinSize(self.size_square_button) 124 125 # Add the button. 126 button_sizer.Add(button, 0, wx.ADJUST_MINSIZE, 0) 127 128 # Padding. 129 button_sizer.AddSpacer(padding) 130 131 # Bind the click event. 132 self.parent.Bind(wx.EVT_BUTTON, self.save, button) 133 134 # Add a reset button. 135 if reset: 136 # Build the button. 137 button = buttons.ThemedGenBitmapTextButton(self.parent, -1, None, "") 138 button.SetBitmapLabel(wx.Bitmap(paths.icon_22x22.edit_delete, wx.BITMAP_TYPE_ANY)) 139 button.SetFont(font.normal) 140 button.SetToolTipString("Reset the free file format settings to the original values.") 141 button.SetMinSize(self.size_square_button) 142 143 # Add the button. 144 button_sizer.Add(button, 0, wx.ADJUST_MINSIZE, 0) 145 146 # Bind the click event. 147 self.parent.Bind(wx.EVT_BUTTON, self.reset, button) 148 149 # Add the button sizer to the widget (with spacing). 150 main_sizer.AddSpacer(padding) 151 main_sizer.Add(button_sizer, 0, wx.ALL, 0)
152 153
154 - def GetValue(self):
155 """Return the free file format settings as a keyword dictionary. 156 157 @return: The dictionary of free file format settings. 158 @rtype: dict 159 """ 160 161 # Initialise. 162 settings = {} 163 164 # Get the column numbers. 165 settings['spin_id_col'] = gui_to_int(self.spin_id_col.GetValue()) 166 settings['mol_name_col'] = gui_to_int(self.mol_name_col.GetValue()) 167 settings['res_num_col'] = gui_to_int(self.res_num_col.GetValue()) 168 settings['res_name_col'] = gui_to_int(self.res_name_col.GetValue()) 169 settings['spin_num_col'] = gui_to_int(self.spin_num_col.GetValue()) 170 settings['spin_name_col'] = gui_to_int(self.spin_name_col.GetValue()) 171 if hasattr(self, 'data_col'): 172 settings['data_col'] = gui_to_int(self.data_col.GetValue()) 173 if hasattr(self, 'error_col'): 174 settings['error_col'] = gui_to_int(self.error_col.GetValue()) 175 176 # The column separator. 177 settings['sep'] = str(self.sep.GetValue()) 178 if settings['sep'] == 'white space': 179 settings['sep'] = None 180 181 # Return the settings. 182 return settings
183 184
185 - def SetValue(self, key, value):
186 """Special method for setting the value of the GUI element corresponding to the key. 187 188 @param key: The key corresponding to the desired GUI element. This can be one of ['spin_id_col', 'mol_name_col', 'res_num_col', 'res_name_col', 'spin_num_col', 'spin_name_col', 'data_col', 'error_col', 'sep']. 189 @type key: str 190 @param value: The value that the specific GUI element's SetValue() method expects. 191 @type value: unknown 192 """ 193 194 # Get the element. 195 obj = getattr(self, key) 196 197 # Convert the data. 198 if key == 'sep': 199 value = str_to_gui(value) 200 else: 201 value = int_to_gui(value) 202 203 # Set the value. 204 obj.SetValue(value)
205 206
207 - def reset(self, event):
208 """Reset the free file format widget contents to the original values. 209 210 @param event: The wx event. 211 @type event: wx event 212 """ 213 214 # Ask a question. 215 if status.show_gui and Question('Would you really like to reset the free file format settings?', parent=self.parent).ShowModal() == wx.ID_NO: 216 return 217 218 # First reset. 219 ds.relax_gui.free_file_format.reset() 220 221 # Then update the values. 222 self.set_vals()
223 224
225 - def save(self, event):
226 """Save the free file format widget contents into the relax data store. 227 228 @param event: The wx event. 229 @type event: wx event 230 """ 231 232 # Get the column numbers. 233 ds.relax_gui.free_file_format.spin_id_col = gui_to_int(self.spin_id_col.GetValue()) 234 ds.relax_gui.free_file_format.mol_name_col = gui_to_int(self.mol_name_col.GetValue()) 235 ds.relax_gui.free_file_format.res_num_col = gui_to_int(self.res_num_col.GetValue()) 236 ds.relax_gui.free_file_format.res_name_col = gui_to_int(self.res_name_col.GetValue()) 237 ds.relax_gui.free_file_format.spin_num_col = gui_to_int(self.spin_num_col.GetValue()) 238 ds.relax_gui.free_file_format.spin_name_col = gui_to_int(self.spin_name_col.GetValue()) 239 240 # The data and error. 241 if hasattr(self, 'data_col'): 242 ds.relax_gui.free_file_format.data_col = gui_to_int(self.data_col.GetValue()) 243 if hasattr(self, 'error_col'): 244 ds.relax_gui.free_file_format.error_col = gui_to_int(self.error_col.GetValue()) 245 246 # The column separator. 247 ds.relax_gui.free_file_format.sep = str(self.sep.GetValue()) 248 if ds.relax_gui.free_file_format.sep == 'white space': 249 ds.relax_gui.free_file_format.sep = None
250 251
252 - def set_vals(self):
253 """Set the free file format widget contents to the values from the relax data store.""" 254 255 # The column numbers. 256 self.spin_id_col.SetValue(int_to_gui(ds.relax_gui.free_file_format.spin_id_col)) 257 self.mol_name_col.SetValue(int_to_gui(ds.relax_gui.free_file_format.mol_name_col)) 258 self.res_num_col.SetValue(int_to_gui(ds.relax_gui.free_file_format.res_num_col)) 259 self.res_name_col.SetValue(int_to_gui(ds.relax_gui.free_file_format.res_name_col)) 260 self.spin_num_col.SetValue(int_to_gui(ds.relax_gui.free_file_format.spin_num_col)) 261 self.spin_name_col.SetValue(int_to_gui(ds.relax_gui.free_file_format.spin_name_col)) 262 if hasattr(self, 'data_col'): 263 self.data_col.SetValue(int_to_gui(ds.relax_gui.free_file_format.data_col)) 264 if hasattr(self, 'error_col'): 265 self.error_col.SetValue(int_to_gui(ds.relax_gui.free_file_format.error_col)) 266 267 # The column separator. 268 if not ds.relax_gui.free_file_format.sep: 269 self.sep.SetValue(str_to_gui("white space")) 270 else: 271 self.sep.SetValue(str_to_gui(ds.relax_gui.free_file_format.sep))
272 273 274
275 -class Free_file_format_window(wx.Dialog, Wiz_page):
276 """The free file format setting window.""" 277 278 # The window size. 279 SIZE = (500, 550) 280 281 # A border. 282 BORDER = 10 283 284 # Sizes. 285 SIZE_BUTTON = (100, 33) 286
287 - def __init__(self, parent=None):
288 """Set up the window.""" 289 290 # Execute the base __init__() method. 291 wx.Dialog.__init__(self, parent, id=-1, title="Free file format", style=wx.DEFAULT_FRAME_STYLE) 292 293 # The sizes. 294 self._main_size = self.SIZE[0] - 2*self.BORDER 295 self._div_left = self._main_size / 2 296 297 # Set up the window icon. 298 self.SetIcons(relax_icons) 299 300 # The main sizer. 301 self.main_sizer = self.build_frame() 302 303 # The heading. 304 text = wx.StaticText(self, -1, "Settings for the free file format") 305 text.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, "")) 306 self.main_sizer.Add(text, 0, wx.ALIGN_CENTER_HORIZONTAL, 5) 307 self.main_sizer.AddStretchSpacer() 308 309 # The relax logo. 310 bmp = wx.StaticBitmap(self, -1, wx.Bitmap(paths.IMAGE_PATH+'relax.gif', wx.BITMAP_TYPE_ANY)) 311 self.main_sizer.Add(bmp, 0, wx.ALIGN_CENTER_HORIZONTAL, 5) 312 self.main_sizer.AddStretchSpacer() 313 314 # The centre section. 315 self.add_centre(self.main_sizer) 316 317 # The bottom buttons. 318 self.add_buttons(self.main_sizer) 319 320 # Set the window size. 321 self.SetSize(self.SIZE) 322 self.SetMinSize(self.SIZE) 323 324 # Centre the window. 325 self.Center()
326 327
328 - def add_buttons(self, sizer):
329 """Add the buttons to the sizer. 330 331 @param sizer: A sizer object. 332 @type sizer: wx.Sizer instance 333 """ 334 335 # Create a horizontal layout for the buttons. 336 button_sizer = wx.BoxSizer(wx.HORIZONTAL) 337 sizer.Add(button_sizer, 0, wx.ALIGN_CENTER|wx.ALL, 0) 338 339 # The save button. 340 button = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Save") 341 button.SetBitmapLabel(wx.Bitmap(paths.icon_22x22.save, wx.BITMAP_TYPE_ANY)) 342 button.SetFont(font.normal) 343 button.SetToolTipString("Save the free file format settings within the relax data store.") 344 button.SetMinSize(self.SIZE_BUTTON) 345 button_sizer.Add(button, 0, wx.ADJUST_MINSIZE, 0) 346 self.Bind(wx.EVT_BUTTON, self.save, button) 347 348 # Spacer. 349 button_sizer.AddSpacer(20) 350 351 # The reset button. 352 button = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Reset") 353 button.SetBitmapLabel(wx.Bitmap(paths.icon_22x22.edit_delete, wx.BITMAP_TYPE_ANY)) 354 button.SetFont(font.normal) 355 button.SetToolTipString("Reset the free file format settings to the original values.") 356 button.SetMinSize(self.SIZE_BUTTON) 357 button_sizer.Add(button, 0, wx.ADJUST_MINSIZE, 0) 358 self.Bind(wx.EVT_BUTTON, self.reset, button) 359 360 # Spacer. 361 button_sizer.AddSpacer(20) 362 363 # The cancel button. 364 button = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Cancel") 365 button.SetBitmapLabel(wx.Bitmap(paths.icon_22x22.dialog_cancel, wx.BITMAP_TYPE_ANY)) 366 button.SetFont(font.normal) 367 button.SetMinSize(self.SIZE_BUTTON) 368 button_sizer.Add(button, 0, wx.ADJUST_MINSIZE, 0) 369 self.Bind(wx.EVT_BUTTON, self.cancel, button)
370 371
372 - def add_centre(self, sizer):
373 """Add the centre of the free file format settings window. 374 375 @param sizer: A sizer object. 376 @type sizer: wx.Sizer instance 377 """ 378 379 # The widget. 380 self._element = Free_file_format(sizer, parent=self, data_cols=True, save=False, reset=False) 381 382 # Spacing. 383 self.main_sizer.AddStretchSpacer()
384 385
386 - def build_frame(self):
387 """Create the main part of the frame, returning the central sizer.""" 388 389 # The sizers. 390 sizer1 = wx.BoxSizer(wx.HORIZONTAL) 391 sizer2 = wx.BoxSizer(wx.VERTICAL) 392 central_sizer = wx.BoxSizer(wx.VERTICAL) 393 394 # Left and right borders. 395 sizer1.AddSpacer(self.BORDER) 396 sizer1.Add(sizer2, 1, wx.EXPAND|wx.ALL, 0) 397 sizer1.AddSpacer(self.BORDER) 398 399 # Top and bottom borders. 400 sizer2.AddSpacer(self.BORDER) 401 sizer2.Add(central_sizer, 1, wx.EXPAND|wx.ALL, 0) 402 sizer2.AddSpacer(self.BORDER) 403 404 # Set the sizer for the frame. 405 self.SetSizer(sizer1) 406 407 # Return the central sizer. 408 return central_sizer
409 410
411 - def cancel(self, event):
412 """Close the window. 413 414 @param event: The wx event. 415 @type event: wx event 416 """ 417 418 # Destroy the window. 419 self.Destroy()
420 421
422 - def reset(self, event):
423 """Reset the free file format settings. 424 425 @param event: The wx event. 426 @type event: wx event 427 """ 428 429 # Execute the base class method. 430 self._element.reset(event)
431 432
433 - def save(self, event):
434 """Save the free file format widget contents into the relax data store. 435 436 @param event: The wx event. 437 @type event: wx event 438 """ 439 440 # Execute the base class method. 441 self._element.save(event) 442 443 # Destroy the window. 444 self.Destroy()
445