Package gui :: Package input_elements :: Module file
[hide private]
[frames] | no frames]

Source Code for Module gui.input_elements.file

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2012-2014 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  """Module containing a set of special GUI elements to be used in the relax wizards.""" 
 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.filedialog import RelaxFileDialog 
 33  from gui.fonts import font 
 34  from gui.misc import add_border, open_file 
 35  from gui.string_conv import gui_to_list, gui_to_str, list_to_gui, str_to_gui 
 36  from lib.errors import RelaxError 
 37  from status import Status; status = Status() 
 38   
 39   
40 -class File_element:
41 """A single file element for the multiple file input GUI element.""" 42
43 - def __init__(self, default='', parent=None, index=None, wildcard=wx.FileSelectorDefaultWildcardStr, style=wx.FD_DEFAULT_STYLE, padding=3, height_spacer=1, width_spacer=2, height_element=27, preview=True, can_be_none=False):
44 """Set up the file GUI element. 45 46 @keyword default: The default value of the element. 47 @type default: str 48 @keyword parent: The parent GUI element. 49 @type parent: wx.Panel instance 50 @keyword index: The index of the file element, to display its sequence number in the GUI element. 51 @type index: int 52 @keyword wildcard: The file wildcard pattern. For example for opening PDB files, this could be "PDB files (*.pdb)|*.pdb;*.PDB". 53 @type wildcard: String 54 @keyword style: The dialog style. To open a single file, set to wx.FD_OPEN. To open multiple files, set to wx.FD_OPEN|wx.FD_MULTIPLE. To save a single file, set to wx.FD_SAVE. To save multiple files, set to wx.FD_SAVE|wx.FD_MULTIPLE. 55 @type style: long 56 @keyword padding: Spacing to the left and right of the widgets. 57 @type padding: int 58 @keyword height_spacer: The amount of spacing to add below the field in pixels. 59 @type height_spacer: int 60 @keyword width_spacer: The amount of spacing to add horizontally between the TextCtrl and buttons in pixels. 61 @type width_spacer: int 62 @keyword height_element: The height in pixels of the GUI element. 63 @type height_element: int 64 @keyword preview: A flag which if true will allow the file to be previewed. 65 @type preview: bool 66 @keyword can_be_none: A flag which specifies if the element is allowed to have the None value. 67 @type can_be_none: bool 68 """ 69 70 # Store the arguments. 71 self.default = default 72 self.parent = parent 73 self.wildcard = wildcard 74 self.style = style 75 self.can_be_none = can_be_none 76 77 # A vertical sizer for the two elements of the file selection GUI elements and a spacer element. 78 self.sizer = wx.BoxSizer(wx.VERTICAL) 79 80 # Create a sizer for the elements. 81 sub_sizer = wx.BoxSizer(wx.HORIZONTAL) 82 83 # Left padding. 84 sub_sizer.AddSpacer(padding) 85 86 # The file index. 87 desc = str_to_gui("%i: " % (index+1)) 88 text = wx.StaticText(self.parent, -1, desc, style=wx.ALIGN_LEFT) 89 text.SetFont(font.normal_bold) 90 text.SetMinSize((35, -1)) 91 sub_sizer.Add(text, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0) 92 93 # A little spacing. 94 sub_sizer.AddSpacer(width_spacer) 95 96 # The input field. 97 self.field = wx.TextCtrl(self.parent, -1, self.default) 98 self.field.SetMinSize((-1, height_element)) 99 self.field.SetFont(font.normal) 100 sub_sizer.Add(self.field, 1, wx.EXPAND|wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 101 102 # A little spacing. 103 sub_sizer.AddSpacer(width_spacer) 104 105 # The file selection button. 106 button = wx.BitmapButton(self.parent, -1, wx.Bitmap(fetch_icon('oxygen.actions.document-open', "16x16"), wx.BITMAP_TYPE_ANY)) 107 button.SetMinSize((height_element, height_element)) 108 button.SetToolTipString("Select the file.") 109 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 110 self.parent.Bind(wx.EVT_BUTTON, self.select_file, button) 111 112 # File preview. 113 if preview: 114 # A little spacing. 115 sub_sizer.AddSpacer(width_spacer) 116 117 # The preview button. 118 button = wx.BitmapButton(self.parent, -1, wx.Bitmap(fetch_icon('oxygen.actions.document-preview', "16x16"), wx.BITMAP_TYPE_ANY)) 119 button.SetMinSize((height_element, height_element)) 120 button.SetToolTipString("Preview") 121 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 122 self.parent.Bind(wx.EVT_BUTTON, self.preview_file, button) 123 124 # Right padding. 125 sub_sizer.AddSpacer(padding) 126 127 # Add the sizer to the main sizer. 128 self.sizer.Add(sub_sizer, 1, wx.ALL|wx.EXPAND, 0) 129 130 # Add spacing. 131 self.sizer.AddSpacer(height_spacer)
132 133
134 - def GetValue(self):
135 """Return the file name. 136 137 @return: The file name. 138 @rtype: str 139 """ 140 141 # Return the current value. 142 return gui_to_str(self.field.GetValue())
143 144
145 - def SetValue(self, value):
146 """Set up the list of file. 147 148 @param value: The list of values to add to the list. 149 @type value: list of str or None 150 """ 151 152 # Set the value. 153 self.field.SetValue(str_to_gui(value))
154 155
156 - def preview_file(self, event=None):
157 """Preview a file. 158 159 @keyword event: The wx event. 160 @type event: wx event 161 """ 162 163 # The file name. 164 file = gui_to_str(self.field.GetValue()) 165 166 # No file, so do nothing. 167 if file == None: 168 return 169 170 # Open the file as text. 171 open_file(file, force_text=True)
172 173
174 - def select_file(self, event=None):
175 """Select a file. 176 177 @keyword event: The wx event. 178 @type event: wx event 179 """ 180 181 # The file selection object (initialised in this function and not __init__() so that the working directory is more logical). 182 dialog = RelaxFileDialog(self.parent, field=self.field, message="File selection", defaultFile=self.default, wildcard=self.wildcard, style=self.style) 183 184 # Show the dialog and catch if no file has been selected. 185 if status.show_gui: 186 dialog.select_event(event)
187 188 189
190 -class Selector_file:
191 """Wizard GUI element for selecting files.""" 192
193 - def __init__(self, name=None, default=None, parent=None, sizer=None, desc=None, message='File selection', wildcard=wx.FileSelectorDefaultWildcardStr, style=wx.FD_DEFAULT_STYLE, tooltip=None, divider=None, padding=0, spacer=None, height_element=27, preview=True, read_only=False):
194 """Build the file selection element. 195 196 @keyword name: The name of the element to use in titles, etc. 197 @type name: str 198 @keyword default: The default value of the element. 199 @type default: str 200 @keyword parent: The wizard GUI element. 201 @type parent: wx.Panel instance 202 @keyword sizer: The sizer to put the input field into. 203 @type sizer: wx.Sizer instance 204 @keyword desc: The text description. 205 @type desc: str 206 @keyword message: The file selector prompt string. 207 @type message: String 208 @keyword wildcard: The file wildcard pattern. For example for opening PDB files, this could be "PDB files (*.pdb)|*.pdb;*.PDB". 209 @type wildcard: String 210 @keyword style: The dialog style. To open a single file, set to wx.FD_OPEN. To open multiple files, set to wx.FD_OPEN|wx.FD_MULTIPLE. To save a single file, set to wx.FD_SAVE. To save multiple files, set to wx.FD_SAVE|wx.FD_MULTIPLE. 211 @type style: long 212 @keyword tooltip: The tooltip which appears on hovering over all the GUI elements. 213 @type tooltip: str 214 @keyword divider: The position of the divider. 215 @type divider: int 216 @keyword padding: Spacing to the left and right of the widgets. 217 @type padding: int 218 @keyword spacer: The amount of spacing to add below the field in pixels. If None, a stretchable spacer will be used. 219 @type spacer: None or int 220 @keyword height_element: The height in pixels of the GUI element. 221 @type height_element: int 222 @keyword preview: A flag which if true will allow the file to be previewed. 223 @type preview: bool 224 @keyword read_only: A flag which if True means that the text of the element cannot be edited. 225 @type read_only: bool 226 """ 227 228 # Store the args. 229 self.name = name 230 231 # Argument translation. 232 if default == None: 233 default = wx.EmptyString 234 235 # Init. 236 sub_sizer = wx.BoxSizer(wx.HORIZONTAL) 237 238 # Left padding. 239 sub_sizer.AddSpacer(padding) 240 241 # The description. 242 text = wx.StaticText(parent, -1, desc, style=wx.ALIGN_LEFT) 243 text.SetFont(font.normal) 244 sub_sizer.Add(text, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 0) 245 246 # The divider. 247 if not divider: 248 raise RelaxError("The divider position has not been supplied.") 249 250 # Spacing. 251 x, y = text.GetSize() 252 sub_sizer.AddSpacer((divider - x, 0)) 253 254 # The input field. 255 self._field = wx.TextCtrl(parent, -1, default) 256 self._field.SetMinSize((-1, height_element)) 257 self._field.SetFont(font.normal) 258 sub_sizer.Add(self._field, 1, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 259 260 # The file selection object. 261 obj = RelaxFileDialog(parent, field=self._field, message=message, defaultFile=default, wildcard=wildcard, style=style) 262 263 # A little spacing. 264 sub_sizer.AddSpacer(5) 265 266 # The file selection button. 267 button = wx.BitmapButton(parent, -1, wx.Bitmap(fetch_icon('oxygen.actions.document-open', "16x16"), wx.BITMAP_TYPE_ANY)) 268 button.SetMinSize((height_element, height_element)) 269 button.SetToolTipString("Select the file.") 270 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 271 parent.Bind(wx.EVT_BUTTON, obj.select_event, button) 272 273 # File preview. 274 if preview: 275 # A little spacing. 276 sub_sizer.AddSpacer(5) 277 278 # The preview button. 279 button = wx.BitmapButton(parent, -1, wx.Bitmap(fetch_icon('oxygen.actions.document-preview', "16x16"), wx.BITMAP_TYPE_ANY)) 280 button.SetMinSize((height_element, height_element)) 281 button.SetToolTipString("Preview") 282 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 283 parent.Bind(wx.EVT_BUTTON, self.preview_file, button) 284 285 # Right padding. 286 sub_sizer.AddSpacer(padding) 287 288 # Add to the main sizer (followed by stretchable spacing). 289 sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0) 290 291 # Spacing below the widget. 292 if spacer == None: 293 sizer.AddStretchSpacer() 294 else: 295 sizer.AddSpacer(spacer) 296 297 # Tooltip. 298 if tooltip: 299 text.SetToolTipString(tooltip) 300 self._field.SetToolTipString(tooltip)
301 302
303 - def Clear(self):
304 """Special method for clearing or resetting the GUI element.""" 305 306 # Clear the value from the TextCtrl. 307 self._field.Clear()
308 309
310 - def GetValue(self):
311 """Special method for returning the value of the GUI element. 312 313 @return: The string value. 314 @rtype: list of str 315 """ 316 317 # Convert and return the value from a TextCtrl. 318 return gui_to_str(self._field.GetValue())
319 320
321 - def SetValue(self, value):
322 """Special method for setting the value of the GUI element. 323 324 @param value: The value to set. 325 @type value: str 326 """ 327 328 # Convert and set the value for a TextCtrl. 329 self._field.SetValue(str_to_gui(value))
330 331
332 - def preview_file(self, event=None):
333 """Preview a file. 334 335 @keyword event: The wx event. 336 @type event: wx event 337 """ 338 339 # The file name. 340 file = gui_to_str(self._field.GetValue()) 341 342 # No file, so do nothing. 343 if file == None: 344 return 345 346 # Open the file as text. 347 open_file(file, force_text=True)
348 349 350
351 -class Selector_file_multiple:
352 """Wizard GUI element for selecting files.""" 353
354 - def __init__(self, name=None, default=None, parent=None, sizer=None, desc=None, message='File selection', wildcard=wx.FileSelectorDefaultWildcardStr, style=wx.FD_DEFAULT_STYLE, tooltip=None, divider=None, padding=0, spacer=None, height_element=27, preview=True, read_only=False, can_be_none=False):
355 """Build the file selection element. 356 357 @keyword name: The name of the element to use in titles, etc. 358 @type name: str 359 @keyword default: The default value of the element. 360 @type default: str 361 @keyword parent: The wizard GUI element. 362 @type parent: wx.Panel instance 363 @keyword sizer: The sizer to put the input field into. 364 @type sizer: wx.Sizer instance 365 @keyword desc: The text description. 366 @type desc: str 367 @keyword message: The file selector prompt string. 368 @type message: String 369 @keyword wildcard: The file wildcard pattern. For example for opening PDB files, this could be "PDB files (*.pdb)|*.pdb;*.PDB". 370 @type wildcard: String 371 @keyword style: The dialog style. To open a single file, set to wx.FD_OPEN. To open multiple files, set to wx.FD_OPEN|wx.FD_MULTIPLE. To save a single file, set to wx.FD_SAVE. To save multiple files, set to wx.FD_SAVE|wx.FD_MULTIPLE. 372 @type style: long 373 @keyword tooltip: The tooltip which appears on hovering over all the GUI elements. 374 @type tooltip: str 375 @keyword divider: The position of the divider. 376 @type divider: int 377 @keyword padding: Spacing to the left and right of the widgets. 378 @type padding: int 379 @keyword spacer: The amount of spacing to add below the field in pixels. If None, a stretchable spacer will be used. 380 @type spacer: None or int 381 @keyword height_element: The height in pixels of the GUI element. 382 @type height_element: int 383 @keyword preview: A flag which if true will allow the file to be previewed. 384 @type preview: bool 385 @keyword read_only: A flag which if True means that the text of the element cannot be edited. 386 @type read_only: bool 387 @keyword can_be_none: A flag which specifies if the element is allowed to have the None value. 388 @type can_be_none: bool 389 """ 390 391 # Store the args. 392 self.name = name 393 self.parent = parent 394 self.can_be_none = can_be_none 395 396 # Argument translation. 397 if default == None: 398 default = wx.EmptyString 399 400 # Init. 401 sub_sizer = wx.BoxSizer(wx.HORIZONTAL) 402 403 # Left padding. 404 sub_sizer.AddSpacer(padding) 405 406 # The description. 407 text = wx.StaticText(parent, -1, desc, style=wx.ALIGN_LEFT) 408 text.SetFont(font.normal) 409 sub_sizer.Add(text, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 0) 410 411 # The divider. 412 if not divider: 413 raise RelaxError("The divider position has not been supplied.") 414 415 # Spacing. 416 x, y = text.GetSize() 417 sub_sizer.AddSpacer((divider - x, 0)) 418 419 # The input field. 420 self._field = wx.TextCtrl(parent, -1, default) 421 self._field.SetMinSize((-1, height_element)) 422 self._field.SetFont(font.normal) 423 sub_sizer.Add(self._field, 1, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 424 425 # The file selection object. 426 obj = RelaxFileDialog(parent, field=self._field, message=message, defaultFile=default, wildcard=wildcard, style=style) 427 428 # A little spacing. 429 sub_sizer.AddSpacer(5) 430 431 # The edit button. 432 button = wx.BitmapButton(parent, -1, wx.Bitmap(fetch_icon('oxygen.actions.document-open', "16x16"), wx.BITMAP_TYPE_ANY)) 433 button.SetMinSize((height_element, height_element)) 434 button.SetToolTipString("Choose the file(s).") 435 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 436 parent.Bind(wx.EVT_BUTTON, self.open_dialog, button) 437 438 # Right padding. 439 sub_sizer.AddSpacer(padding) 440 441 # Add to the main sizer (followed by stretchable spacing). 442 sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0) 443 444 # Spacing below the widget. 445 if spacer == None: 446 sizer.AddStretchSpacer() 447 else: 448 sizer.AddSpacer(spacer) 449 450 # Tooltip. 451 if tooltip: 452 text.SetToolTipString(tooltip) 453 self._field.SetToolTipString(tooltip)
454 455
456 - def Clear(self):
457 """Special method for clearing or resetting the GUI element.""" 458 459 # Clear the value from the TextCtrl. 460 self._field.Clear()
461 462
463 - def GetValue(self):
464 """Special method for returning the value of the GUI element. 465 466 @return: The string value. 467 @rtype: list of str 468 """ 469 470 # The value. 471 value = self._field.GetValue() 472 473 # Handle single values. 474 value_set = False 475 try: 476 # Convert. 477 value = gui_to_str(value) 478 479 # Check that the conversion was successful. 480 if value == None and self.can_be_none: 481 value_set = True 482 else: 483 if value[0] != '[': 484 value_set = True 485 except: 486 pass 487 488 # Convert to a list, handling bad user behaviour. 489 if not value_set: 490 try: 491 value = gui_to_list(value) 492 493 # Set the value to None or an empty sequence. 494 except: 495 if self.can_be_none: 496 value = None 497 else: 498 value = [] 499 500 # Convert sequences to single values as needed. 501 if isinstance(value, list) and len(value) == 1: 502 value = value[0] 503 504 # Handle empty list and tuple values. 505 if len(value) == 0: 506 return None 507 508 # Return the value. 509 return value
510 511
512 - def SetValue(self, value):
513 """Special method for setting the value of the GUI element. 514 515 @param value: The value to set. 516 @type value: str 517 """ 518 519 # Handle single values. 520 if isinstance(value, list) and len(value) == 1: 521 value = value[0] 522 523 # Convert and set the value. 524 self._field.SetValue(list_to_gui(value))
525 526
527 - def open_dialog(self, event):
528 """Open a special dialog for inputting a list of text values. 529 530 @param event: The wx event. 531 @type event: wx event 532 """ 533 534 # Show the window. 535 self.selection_win_show() 536 537 # Extract the data from the selection window once closed. 538 self.selection_win_data() 539 540 # Destroy the window. 541 del self.sel_win
542 543
544 - def preview_file(self, event=None):
545 """Preview a file. 546 547 @keyword event: The wx event. 548 @type event: wx event 549 """ 550 551 # The file name. 552 file = gui_to_str(self._field.GetValue()) 553 554 # No file, so do nothing. 555 if file == None: 556 return 557 558 # Open the file as text. 559 open_file(file, force_text=True)
560 561
562 - def selection_win_data(self):
563 """Extract the data from the file list selection window.""" 564 565 # Get the value. 566 value = self.sel_win.GetValue() 567 568 # No sequence data. 569 if not len(value): 570 self.Clear() 571 572 # Set the values. 573 else: 574 self.SetValue(value)
575 576
577 - def selection_win_show(self):
578 """Show the file list selection window.""" 579 580 # Initialise the model selection window. 581 self.sel_win = Selector_file_window(parent=self.parent, name=self.name) 582 583 # Set the model selector window selections. 584 self.sel_win.SetValue(self.GetValue()) 585 586 # Show the model selector window. 587 if status.show_gui: 588 self.sel_win.ShowModal() 589 self.sel_win.Close()
590 591 592
593 -class Selector_file_window(wx.Dialog):
594 """The file list selection window.""" 595 596 # The window size. 597 SIZE = (800, 600) 598 599 # A border. 600 BORDER = 10 601 602 # Sizes. 603 SIZE_BUTTON = (150, 33) 604
605 - def __init__(self, parent=None, name='', spacing=10):
606 """Set up the file list selection window. 607 608 @keyword parent: The parent GUI element. 609 @type parent: wx.Window instance or None 610 @keyword name: The name of the window. 611 @type name: str 612 @keyword spacing: The spacing between elements in pixels. 613 @type spacing: int 614 """ 615 616 # Store the args. 617 self.name = name 618 self.spacing = spacing 619 620 # The title of the dialog. 621 title = "Multiple %s selection." % name 622 623 # Set up the dialog. 624 wx.Dialog.__init__(self, parent, id=-1, title=title) 625 626 # Initialise some values 627 self.width = self.SIZE[0] - 2*self.BORDER 628 629 # Set the frame properties. 630 self.SetSize(self.SIZE) 631 self.Centre() 632 self.SetFont(font.normal) 633 634 # The main box sizer. 635 main_sizer = wx.BoxSizer(wx.VERTICAL) 636 637 # Pack the sizer into the frame. 638 self.SetSizer(main_sizer) 639 640 # Build the central sizer, with borders. 641 sizer = add_border(main_sizer, border=self.BORDER, packing=wx.VERTICAL) 642 643 # Add the file list element. 644 self.add_file_list(sizer) 645 646 # Some spacing. 647 sizer.AddSpacer(self.BORDER) 648 649 # Add the bottom buttons. 650 self.add_buttons(sizer) 651 652 # Initialise the list of file selection elements to a single element. 653 self.elements = [] 654 self.add_element()
655 656
657 - def GetValue(self):
658 """Return the file names as a list. 659 660 @return: The list of file names. 661 @rtype: list of str 662 """ 663 664 # Init. 665 values = [] 666 667 # Loop over the entries. 668 for i in range(len(self.elements)): 669 values.append(self.elements[i].GetValue()) 670 671 # Return the file name list. 672 return values
673 674
675 - def SetValue(self, values):
676 """Set up the list of file names. 677 678 @param values: The list of file names to add. 679 @type values: list of str or None 680 """ 681 682 # No value. 683 if values == None: 684 return 685 686 # Single values. 687 if isinstance(values, str): 688 values = [values] 689 690 # Reset the elements. 691 self.delete_all() 692 693 # Loop over the file paths, creating the elements. 694 for i in range(len(values)): 695 # The first element already exists. 696 if i == 0: 697 self.elements[0].SetValue(values[i]) 698 699 # Otherwise create a new element. 700 else: 701 self.add_element(path=values[i])
702 703
704 - def add_buttons(self, sizer):
705 """Add the buttons to the sizer. 706 707 @param sizer: A sizer object. 708 @type sizer: wx.Sizer instance 709 """ 710 711 # Create a horizontal layout for the buttons. 712 button_sizer = wx.BoxSizer(wx.HORIZONTAL) 713 sizer.Add(button_sizer, 0, wx.ALIGN_CENTER|wx.ALL, 0) 714 715 # The add button. 716 button = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Add") 717 button.SetBitmapLabel(wx.Bitmap(fetch_icon('oxygen.actions.list-add-relax-blue', "22x22"), wx.BITMAP_TYPE_ANY)) 718 button.SetFont(font.normal) 719 button.SetToolTipString("Add a file selection item to the list.") 720 button.SetMinSize(self.SIZE_BUTTON) 721 button_sizer.Add(button, 0, wx.ADJUST_MINSIZE, 0) 722 self.Bind(wx.EVT_BUTTON, self.add_element, button) 723 724 # Spacer. 725 button_sizer.AddSpacer(20) 726 727 # The delete button. 728 button = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Delete") 729 button.SetBitmapLabel(wx.Bitmap(fetch_icon('oxygen.actions.list-remove', "22x22"), wx.BITMAP_TYPE_ANY)) 730 button.SetFont(font.normal) 731 button.SetToolTipString("Delete the last file selection item.") 732 button.SetMinSize(self.SIZE_BUTTON) 733 button_sizer.Add(button, 0, wx.ADJUST_MINSIZE, 0) 734 self.Bind(wx.EVT_BUTTON, self.delete, button) 735 736 # Spacer. 737 button_sizer.AddSpacer(20) 738 739 # The delete all button. 740 button = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Delete all") 741 button.SetBitmapLabel(wx.Bitmap(fetch_icon('oxygen.actions.edit-delete', "22x22"), wx.BITMAP_TYPE_ANY)) 742 button.SetFont(font.normal) 743 button.SetToolTipString("Delete all items.") 744 button.SetMinSize(self.SIZE_BUTTON) 745 button_sizer.Add(button, 0, wx.ADJUST_MINSIZE, 0) 746 self.Bind(wx.EVT_BUTTON, self.delete_all, button) 747 748 # Spacer. 749 button_sizer.AddSpacer(20) 750 751 # The Ok button. 752 button = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Ok") 753 button.SetBitmapLabel(wx.Bitmap(fetch_icon('oxygen.actions.dialog-ok', "22x22"), wx.BITMAP_TYPE_ANY)) 754 button.SetFont(font.normal) 755 button.SetMinSize(self.SIZE_BUTTON) 756 button_sizer.Add(button, 0, wx.ADJUST_MINSIZE, 0) 757 self.Bind(wx.EVT_BUTTON, self.close, button)
758 759
760 - def add_element(self, event=None, path=None):
761 """Add a new file selection element to the list. 762 763 @keyword event: The wx event. 764 @type event: wx event 765 @keyword path: The file path to set the element value to. 766 @type path: str or None 767 """ 768 769 # Initialise the element. 770 element = File_element(parent=self.panel, index=len(self.elements)) 771 772 # Set its value. 773 if path != None: 774 element.SetValue(path) 775 776 # Add the element's sizer to the main element sizer. 777 self.element_sizer.Add(element.sizer, 0, wx.ALL|wx.EXPAND, 0) 778 779 # Store the element. 780 self.elements.append(element) 781 782 # Reinitialise the scrolling for the panel, just in case the number of elements is bigger than the window. 783 self.panel.SetupScrolling(scroll_x=False, scroll_y=True) 784 785 # Redraw. 786 self.panel.Layout()
787 788
789 - def add_file_list(self, sizer):
790 """Initialise the control. 791 792 @param sizer: A sizer object. 793 @type sizer: wx.Sizer instance 794 """ 795 796 # Create a scrolled panel. 797 self.panel = scrolledpanel.ScrolledPanel(self, -1, name="file list") 798 799 # A sizer for the panel. 800 panel_sizer = wx.BoxSizer(wx.VERTICAL) 801 802 # Set the title. 803 title = "File list" 804 text = wx.StaticText(self.panel, -1, title, style=wx.TE_MULTILINE) 805 text.SetFont(font.subtitle) 806 panel_sizer.Add(text, 0, wx.ALIGN_LEFT, 0) 807 panel_sizer.AddSpacer(self.spacing) 808 809 # Create a sizer for the file selection elements. 810 self.element_sizer = wx.BoxSizer(wx.VERTICAL) 811 panel_sizer.Add(self.element_sizer, 1, wx.ALL|wx.EXPAND, 0) 812 813 # Set up and add the panel to the sizer. 814 self.panel.SetSizer(panel_sizer) 815 self.panel.SetAutoLayout(1) 816 self.panel.SetupScrolling(scroll_x=False, scroll_y=True) 817 sizer.Add(self.panel, 1, wx.ALL|wx.EXPAND, 0)
818 819
820 - def close(self, event):
821 """Close the window. 822 823 @param event: The wx event. 824 @type event: wx event 825 """ 826 827 # Close the window. 828 self.Close()
829 830
831 - def delete(self, event=None):
832 """Remove the last file selection item from the list. 833 834 @keyword event: The wx event. 835 @type event: wx event 836 """ 837 838 # Destroy the last subsizer. 839 self.elements[-1].sizer.DeleteWindows() 840 self.element_sizer.Remove(self.elements[-1].sizer) 841 842 # Destroy the Python structures. 843 self.elements.pop() 844 845 # If the list is empty, start again with a single blank element. 846 if not len(self.elements): 847 self.add_element() 848 849 # Redraw. 850 self.panel.Layout()
851 852
853 - def delete_all(self, event=None):
854 """Remove all file selection items from the list. 855 856 @keyword event: The wx event. 857 @type event: wx event 858 """ 859 860 # Destroy the subsizer. 861 for i in range(len(self.elements)): 862 self.elements[i].sizer.DeleteWindows() 863 self.element_sizer.Remove(self.elements[i].sizer) 864 865 # Destroy all Python structures. 866 del self.elements 867 868 # Reset the elements, starting again with a single blank element. 869 self.elements = [] 870 self.add_element() 871 872 # Redraw. 873 self.panel.Layout()
874