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