Package gui :: Package spin_viewer :: Module containers
[hide private]
[frames] | no frames]

Source Code for Module gui.spin_viewer.containers

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2010-2011 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """The molecule, residue, and spin containers for the spin viewer.""" 
 25   
 26   
 27  # Python module imports. 
 28  from re import search 
 29  from string import replace, split 
 30  import wx 
 31   
 32  # relax module imports. 
 33  from generic_fns.mol_res_spin import return_spin 
 34   
 35  # GUI module imports. 
 36  from gui import paths 
 37  from gui.misc import str_to_gui 
 38   
 39   
 40   
41 -class Container(wx.Window):
42 """The molecule, residue, and spin container window.""" 43
44 - def __init__(self, gui, parent=None, id=None):
45 """Set up the container GUI element. 46 47 @param gui: The gui object. 48 @type gui: wx object 49 @keyword parent: The parent GUI element that this is to be attached to. 50 @type parent: wx object 51 @keyword id: The ID number. 52 @type id: int 53 """ 54 55 # Store the args. 56 self.gui = gui 57 58 # Execute the base class method. 59 wx.Window.__init__(self, parent, id, style=wx.BORDER_SUNKEN) 60 61 # Set a minimum size for the window. 62 self.SetMinSize((500, 500)) 63 64 # Add a sizer. 65 self.main_sizer = wx.BoxSizer(wx.VERTICAL) 66 self.SetSizer(self.main_sizer) 67 68 # Display the root panel. 69 self.container = Root(self.main_sizer, self) 70 71 # Resizing. 72 self.Bind(wx.EVT_SIZE, self._resize)
73 74
75 - def _resize(self, event):
76 """Resize the tree element. 77 78 @param event: The wx event. 79 @type event: wx event 80 """ 81 82 # Re-perform the window layout. 83 self.Layout() 84 self.Refresh()
85 86
87 - def display(self, info):
88 """Display the info for the selected container. 89 90 @param info: The information list consisting of the container type ('root', 'mol', 'res', or 'spin'), the molecule name, the residue number, the residue name, the spin number, and the spin name. The name and number information is dropped when not needed. 91 @type info: list of str and int 92 """ 93 94 # Destroy all the original contents. 95 self.main_sizer.Clear(deleteWindows=True) 96 97 # The root window display. 98 if info == 'root' or info == None: 99 self.container = Root(self.main_sizer, self) 100 101 # The molecule container display. 102 elif info['type'] == 'mol': 103 self.container = Molecule(self.main_sizer, self, mol_name=info['mol_name'], mol_id=info['id'], select=info['select']) 104 105 # The residue container display. 106 elif info['type'] == 'res': 107 self.container = Residue(self.main_sizer, self, mol_name=info['mol_name'], res_num=info['res_num'], res_name=info['res_name'], res_id=info['id'], select=info['select']) 108 109 # The spin container display. 110 elif info['type'] == 'spin': 111 self.container = Spin(self.main_sizer, self, mol_name=info['mol_name'], res_num=info['res_num'], res_name=info['res_name'], spin_num=info['spin_num'], spin_name=info['spin_name'], spin_id=info['id'], select=info['select']) 112 113 # Re-perform the window layout. 114 self.Layout() 115 self.Refresh()
116 117 118
119 -class Container_base:
120 """The base class for the molecule, residue, and spin containers.""" 121 122 # Some class variables. 123 border = 10 124
125 - def create_head_text(self, text):
126 """Generate the wx.StaticText object for header text. 127 128 @param text: The text of the subtitle. 129 @type text: str 130 @return: The subtitle object. 131 @rtype: wx.StaticText instance 132 """ 133 134 # Unicode. 135 text = unicode(text) 136 137 # Fix for the '&' character. 138 text = replace(text, '&', '&&') 139 140 # The object. 141 obj = wx.StaticText(self.parent, -1, text) 142 143 # Formatting. 144 obj.SetFont(wx.Font(pointSize=12, family=wx.FONTFAMILY_ROMAN, style=wx.ITALIC, weight=wx.NORMAL, face='Times')) 145 146 # Return the object. 147 return obj
148 149
150 - def create_subtitle(self, text):
151 """Generate the subtitle wx.StaticText object. 152 153 @param text: The text of the subtitle. 154 @type text: str 155 @return: The subtitle object. 156 @rtype: wx.StaticText instance 157 """ 158 159 # Unicode. 160 text = unicode(text) 161 162 # Fix for the '&' character. 163 text = replace(text, '&', '&&') 164 165 # The object. 166 obj = wx.StaticText(self.parent, -1, text) 167 168 # Formatting. 169 obj.SetFont(wx.Font(pointSize=16, family=wx.FONTFAMILY_ROMAN, style=wx.ITALIC, weight=wx.NORMAL, face='Times')) 170 171 # Return the object. 172 return obj
173 174
175 - def create_title(self, text):
176 """Generate the title wx.StaticText object. 177 178 @param text: The text of the subtitle. 179 @type text: str 180 @return: The subtitle object. 181 @rtype: wx.StaticText instance 182 """ 183 184 # The object. 185 title = wx.StaticText(self.parent, -1, text) 186 187 # Formatting. 188 title.SetFont(wx.Font(pointSize=32, family=wx.FONTFAMILY_ROMAN, style=wx.ITALIC, weight=wx.NORMAL, face='Times')) 189 190 # Return the object. 191 return title
192 193 194
195 -class Molecule(Container_base):
196 """The molecule container.""" 197
198 - def __init__(self, box, parent, mol_name=None, mol_id=None, select=True):
199 """Set up the molecule container. 200 201 @param box: The box sizer element to pack the contents into. 202 @type box: wx object 203 @param parent: The parent GUI element. 204 @type parent: wx object 205 @keyword mol_name: The name of the molecule. 206 @type mol_name: str 207 @keyword mol_id: The molecule ID string. 208 @type mol_id: str 209 @keyword select: The selection state. 210 @type select: bool 211 """ 212 213 # Store the args. 214 self.parent = parent 215 self.mol_name = mol_name 216 self.mol_id = mol_id 217 self.select = select 218 219 # Create the header. 220 sizer = self.header_molecule() 221 222 # Add to the sizer. 223 box.Add(sizer, 0, wx.ALL|wx.EXPAND, border=self.border) 224 225 # A divider. 226 line = wx.StaticLine(self.parent, -1, (25, 50)) 227 box.Add(line, 0, wx.EXPAND|wx.ALL, border=self.border)
228 229
230 - def header_molecule(self):
231 """Create the header for the molecule container. 232 233 @return: The sizer containing the header. 234 @rtype: wx.Sizer instance 235 """ 236 237 # A sizer for the header. 238 sizer = wx.BoxSizer(wx.HORIZONTAL) 239 240 # A sizer for the text component of the header. 241 text_sizer = wx.BoxSizer(wx.VERTICAL) 242 243 # The title 244 title = self.create_title("Molecule container") 245 text_sizer.Add(title, 0, wx.LEFT, 0) 246 247 # Spacer. 248 text_sizer.AddSpacer(30) 249 250 # The info grid. 251 grid_sizer = wx.FlexGridSizer(2, 2, 5, 50) 252 grid_sizer.Add(self.create_head_text("Molecule:"), 0, wx.ADJUST_MINSIZE, 0) 253 grid_sizer.Add(self.create_head_text(self.mol_name), 0, wx.ADJUST_MINSIZE, 0) 254 grid_sizer.Add(self.create_head_text("Molecule ID string:"), 0, wx.ADJUST_MINSIZE, 0) 255 grid_sizer.Add(self.create_head_text("'%s'" % self.mol_id), 0, wx.ADJUST_MINSIZE, 0) 256 text_sizer.Add(grid_sizer, 0, wx.LEFT, 0) 257 258 # Add the text sizer to the main header sizer. 259 sizer.Add(text_sizer, 1, wx.ALL|wx.EXPAND, 0) 260 261 # Stretch spacer. 262 sizer.AddStretchSpacer() 263 264 # The graphic. 265 if self.select: 266 path = paths.WIZARD_IMAGE_PATH + 'molecule.png' 267 else: 268 path = paths.WIZARD_IMAGE_PATH + 'molecule_grey.png' 269 image = wx.StaticBitmap(self.parent, -1, wx.Bitmap(path, wx.BITMAP_TYPE_ANY)) 270 sizer.Add(image, 0, wx.RIGHT, 0) 271 272 # Return the sizer. 273 return sizer
274 275 276
277 -class Residue(Container_base):
278 """The residue container.""" 279
280 - def __init__(self, box, parent, mol_name=None, res_num=None, res_name=None, res_id=None, select=True):
281 """Set up the residue container. 282 283 @param box: The box sizer element to pack the contents into. 284 @type box: wx object 285 @param parent: The parent GUI element. 286 @type parent: wx object 287 @keyword mol_name: The molecule name. 288 @type mol_name: str 289 @keyword res_num: The residue number. 290 @type res_num: str 291 @keyword res_name: The residue name. 292 @type res_name: str 293 @keyword res_id: The residue ID string. 294 @type res_id: str 295 @keyword select: The selection state. 296 @type select: bool 297 """ 298 299 # Store the args. 300 self.parent = parent 301 self.mol_name = mol_name 302 self.res_num = res_num 303 self.res_name = res_name 304 self.res_id = res_id 305 self.select = select 306 307 # Create the header. 308 sizer = self.header_residue() 309 310 # Add to the main sizer. 311 box.Add(sizer, 0, wx.ALL|wx.EXPAND, border=self.border) 312 313 # A divider. 314 line = wx.StaticLine(self.parent, -1, (25, 50)) 315 box.Add(line, 0, wx.EXPAND|wx.ALL, border=self.border)
316 317
318 - def header_residue(self):
319 """Create the header for the residue container. 320 321 @return: The sizer containing the header. 322 @rtype: wx.Sizer instance 323 """ 324 325 # A sizer for the header. 326 sizer = wx.BoxSizer(wx.HORIZONTAL) 327 328 # A sizer for the text component of the header. 329 text_sizer = wx.BoxSizer(wx.VERTICAL) 330 331 # The title 332 title = self.create_title("Residue container") 333 text_sizer.Add(title, 0, wx.LEFT, 0) 334 335 # Spacer. 336 text_sizer.AddSpacer(30) 337 338 # The info grid. 339 grid_sizer = wx.FlexGridSizer(4, 2, 5, 50) 340 grid_sizer.Add(self.create_head_text("Molecule:"), 0, wx.ADJUST_MINSIZE, 0) 341 grid_sizer.Add(self.create_head_text(self.mol_name), 0, wx.ADJUST_MINSIZE, 0) 342 grid_sizer.Add(self.create_head_text("Residue number:"), 0, wx.ADJUST_MINSIZE, 0) 343 grid_sizer.Add(self.create_head_text(self.res_num), 0, wx.ADJUST_MINSIZE, 0) 344 grid_sizer.Add(self.create_head_text("Residue name:"), 0, wx.ADJUST_MINSIZE, 0) 345 grid_sizer.Add(self.create_head_text(self.res_name), 0, wx.ADJUST_MINSIZE, 0) 346 grid_sizer.Add(self.create_head_text("Residue ID string:"), 0, wx.ADJUST_MINSIZE, 0) 347 grid_sizer.Add(self.create_head_text("'%s'" % self.res_id), 0, wx.ADJUST_MINSIZE, 0) 348 text_sizer.Add(grid_sizer, 0, wx.LEFT, 0) 349 350 # Add the text sizer to the main header sizer. 351 sizer.Add(text_sizer, 1, wx.ALL|wx.EXPAND, 0) 352 353 # Stretch spacer. 354 sizer.AddStretchSpacer() 355 356 # The graphic. 357 if self.select: 358 path = paths.WIZARD_IMAGE_PATH + 'residue.png' 359 else: 360 path = paths.WIZARD_IMAGE_PATH + 'residue_grey.png' 361 image = wx.StaticBitmap(self.parent, -1, wx.Bitmap(path, wx.BITMAP_TYPE_ANY)) 362 sizer.Add(image, 0, wx.RIGHT, 0) 363 364 # Return the sizer. 365 return sizer
366 367 368
369 -class Spin(Container_base):
370 """The spin container.""" 371
372 - def __init__(self, box, parent, mol_name=None, res_num=None, res_name=None, spin_num=None, spin_name=None, spin_id=None, select=True):
373 """Set up the spin container. 374 375 @param box: The box sizer element to pack the contents into. 376 @type box: wx object 377 @param parent: The parent GUI element. 378 @type parent: wx object 379 @keyword mol_name: The molecule name. 380 @type mol_name: str 381 @keyword res_num: The residue number. 382 @type res_num: str 383 @keyword res_name: The residue name. 384 @type res_name: str 385 @keyword spin_num: The spin number. 386 @type spin_num: str 387 @keyword spin_name: The spin name. 388 @type spin_name: str 389 @keyword spin_id: The spin ID string. 390 @type spin_id: str 391 @keyword select: The selection state. 392 @type select: bool 393 """ 394 395 # Store the args. 396 self.parent = parent 397 self.mol_name = mol_name 398 self.res_num = res_num 399 self.res_name = res_name 400 self.spin_num = spin_num 401 self.spin_name = spin_name 402 self.spin_id = spin_id 403 self.select = select 404 405 # Create the header. 406 sizer = self.header_spin() 407 408 # Add to the main sizer. 409 box.Add(sizer, 0, wx.ALL|wx.EXPAND, border=self.border) 410 411 # A divider. 412 line = wx.StaticLine(self.parent, -1, (25, 50)) 413 box.Add(line, 0, wx.EXPAND|wx.ALL, border=self.border) 414 415 # The spin container variables. 416 sizer2 = self.spin_vars() 417 box.Add(sizer2, 1, wx.ALL|wx.EXPAND, border=self.border)
418 419
420 - def header_spin(self):
421 """Create the header for the spin container. 422 423 @return: The sizer containing the header. 424 @rtype: wx.Sizer instance 425 """ 426 427 # A sizer for the header. 428 sizer = wx.BoxSizer(wx.HORIZONTAL) 429 430 # A sizer for the text component of the header. 431 text_sizer = wx.BoxSizer(wx.VERTICAL) 432 433 # The title 434 title = self.create_title("Spin container") 435 text_sizer.Add(title, 0, wx.LEFT, 0) 436 437 # Spacer. 438 text_sizer.AddSpacer(30) 439 440 # The info grid. 441 grid_sizer = wx.FlexGridSizer(6, 2, 5, 50) 442 grid_sizer.Add(self.create_head_text("Molecule:"), 0, wx.ADJUST_MINSIZE, 0) 443 grid_sizer.Add(self.create_head_text(self.mol_name), 0, wx.ADJUST_MINSIZE, 0) 444 grid_sizer.Add(self.create_head_text("Residue number:"), 0, wx.ADJUST_MINSIZE, 0) 445 grid_sizer.Add(self.create_head_text(self.res_num), 0, wx.ADJUST_MINSIZE, 0) 446 grid_sizer.Add(self.create_head_text("Residue name:"), 0, wx.ADJUST_MINSIZE, 0) 447 grid_sizer.Add(self.create_head_text(self.res_name), 0, wx.ADJUST_MINSIZE, 0) 448 grid_sizer.Add(self.create_head_text("Spin number:"), 0, wx.ADJUST_MINSIZE, 0) 449 grid_sizer.Add(self.create_head_text(self.spin_num), 0, wx.ADJUST_MINSIZE, 0) 450 grid_sizer.Add(self.create_head_text("Spin name:"), 0, wx.ADJUST_MINSIZE, 0) 451 grid_sizer.Add(self.create_head_text(self.spin_name), 0, wx.ADJUST_MINSIZE, 0) 452 grid_sizer.Add(self.create_head_text("Spin ID string:"), 0, wx.ADJUST_MINSIZE, 0) 453 grid_sizer.Add(self.create_head_text("'%s'" % self.spin_id), 0, wx.ADJUST_MINSIZE, 0) 454 text_sizer.Add(grid_sizer, 0, wx.LEFT, 0) 455 456 # Add the text sizer to the main header sizer. 457 sizer.Add(text_sizer, 1, wx.ALL|wx.EXPAND, 0) 458 459 # Stretch spacer. 460 sizer.AddStretchSpacer() 461 462 # The graphic. 463 if self.select: 464 path = paths.WIZARD_IMAGE_PATH + 'spin.png' 465 else: 466 path = paths.WIZARD_IMAGE_PATH + 'spin_grey.png' 467 image = wx.StaticBitmap(self.parent, -1, wx.Bitmap(path, wx.BITMAP_TYPE_ANY)) 468 sizer.Add(image, 0, wx.RIGHT, 0) 469 470 # Return the sizer. 471 return sizer
472 473
474 - def spin_vars(self, blacklist=[]):
475 """Create the variable table for the spin container. 476 477 @keyword blacklist: A list of spin container objects to blacklist from the variable display table. 478 @type blacklist: list of str 479 @return: The sizer containing the table. 480 @rtype: wx.Sizer instance 481 """ 482 483 # A sizer for the table. 484 sizer = wx.BoxSizer(wx.VERTICAL) 485 486 # The title 487 title = self.create_subtitle("Spin container contents") 488 sizer.Add(title, 0, wx.LEFT, 0) 489 490 # Add a spacer. 491 sizer.AddSpacer(20) 492 493 # The table. 494 table = wx.ListCtrl(self.parent, -1, style=wx.BORDER_SUNKEN|wx.LC_REPORT) 495 496 # The headers. 497 table.InsertColumn(0, "Variable") 498 table.InsertColumn(1, "Value") 499 table.InsertColumn(2, "Type") 500 501 # The widths. 502 table.SetColumnWidth(0, 300) 503 table.SetColumnWidth(1, 200) 504 table.SetColumnWidth(2, 200) 505 506 # The spin container. 507 spin = return_spin(self.spin_id) 508 509 # Add some names to the blacklist. 510 blacklist += ['is_empty'] 511 512 # Loop over the contents of the spin container. 513 for name in dir(spin): 514 # Skip special objects. 515 if search('^_', name): 516 continue 517 518 # Blacklisted names. 519 if name in blacklist: 520 continue 521 522 # Get the object. 523 obj = getattr(spin, name) 524 525 # The type. 526 obj_type = split(str(type(obj)), "'")[1] 527 528 # List types. 529 if obj_type in ['list', 'numpy.ndarray'] and len(obj) > 1: 530 # The first row. 531 table.Append((str_to_gui(name), str_to_gui("[%s," % obj[0]), str_to_gui(obj_type))) 532 533 # The rest of the rows. 534 for i in range(1, len(obj)-1): 535 table.Append((str_to_gui(''), str_to_gui(" %s," % obj[i]), str_to_gui(''))) 536 537 # The last row. 538 table.Append((str_to_gui(''), str_to_gui(" %s]" % obj[-1]), str_to_gui(''))) 539 540 # Dictionary types. 541 elif obj_type == 'dict': 542 # The keys. 543 keys = sorted(obj.keys()) 544 545 # Single entry (or None). 546 if len(keys) < 2: 547 table.Append((str_to_gui(name), str_to_gui(obj), str_to_gui(obj_type))) 548 continue 549 550 # The first row. 551 table.Append((str_to_gui(name), str_to_gui("{'%s': %s," % (keys[0], obj[keys[0]])), str_to_gui(obj_type))) 552 553 # The rest of the rows. 554 for i in range(1, len(keys)-1): 555 table.Append((str_to_gui(''), str_to_gui(" '%s': %s," % (keys[i], obj[keys[i]])), str_to_gui(''))) 556 557 # The last row. 558 table.Append((str_to_gui(''), str_to_gui(" '%s': %s}" % (keys[-1], obj[keys[-1]])), str_to_gui(''))) 559 560 # All other data types. 561 else: 562 table.Append((str_to_gui(name), str_to_gui(obj), str_to_gui(obj_type))) 563 564 # Add the table to the sizer. 565 sizer.Add(table, 1, wx.ALL|wx.EXPAND, 0) 566 567 # Return the sizer. 568 return sizer
569 570 571
572 -class Root(Container_base):
573 """The root container.""" 574
575 - def __init__(self, box, parent):
576 """Set up the root container. 577 578 @param box: The box sizer element to pack the contents into. 579 @type box: wx object 580 @param parent: The parent GUI element. 581 @type parent: wx object 582 """ 583 584 # Store the arg. 585 self.parent = parent 586 587 # A sizer for the header. 588 sizer = wx.BoxSizer(wx.VERTICAL) 589 590 # The title 591 title = self.create_title("The spin view window") 592 sizer.Add(title, 0, wx.LEFT, 0) 593 594 # Add to the sizer. 595 box.Add(sizer, 0, wx.ALL|wx.EXPAND, border=self.border)
596