Package gui :: Module filedialog
[hide private]
[frames] | no frames]

Source Code for Module gui.filedialog

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009 Michael Bieri                                            # 
  4  # Copyright (C) 2010-2011 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  # Module docstring. 
 25  """relax specific file and directory dialogs.""" 
 26   
 27  # Python module imports. 
 28  from os import chdir, getcwd 
 29  import wx 
 30   
 31  # relax module imports. 
 32  from status import Status; status = Status() 
 33   
 34  # relax GUI module imports. 
 35  from gui.misc import gui_to_str, str_to_gui 
 36   
 37   
38 -class RelaxDirDialog(wx.DirDialog):
39 """relax specific replacement directory dialog for selecting directories.""" 40
41 - def __init__(self, parent, message=wx.DirSelectorPromptStr, defaultPath=wx.EmptyString, style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON, pos=wx.DefaultPosition, size=wx.DefaultSize, name=wx.DirDialogNameStr):
42 """Setup the class and store the field. 43 44 @param parent: The parent wx window object. 45 @type parent: Window 46 @keyword message: The path selector prompt string. 47 @type message: String 48 @keyword defaultPath: The default directory to open in. 49 @type defaultPath: String 50 @keyword style: The dialog style. 51 @type style: long 52 @keyword pos: The window position. 53 @type pos: Point 54 @keyword size: The default window size. 55 @type size: Size 56 @keyword name: The title for the dialog. 57 @type name: String 58 """ 59 60 # No path supplied, so use the current working directory. 61 if defaultPath == wx.EmptyString: 62 defaultPath = getcwd() 63 64 # Initialise the base class. 65 super(RelaxDirDialog, self).__init__(parent, message=message, defaultPath=defaultPath, style=style, pos=pos, size=size, name=name)
66 67
68 - def get_path(self):
69 """Return the selected path. 70 71 @return: The name of the selected path. 72 @rtype: str 73 """ 74 75 # The path. 76 path = gui_to_str(self.GetPath()) 77 78 # Change the current working directory. 79 chdir(path) 80 81 # Return the path. 82 return path
83 84 85
86 -class RelaxFileDialog(wx.FileDialog):
87 """relax specific replacement file dialog for opening and closing files. 88 89 This class provides the select() method so that this class can be used with a wx event. 90 """ 91
92 - def __init__(self, parent, field=None, message=wx.FileSelectorPromptStr, defaultDir=wx.EmptyString, defaultFile=wx.EmptyString, wildcard=wx.FileSelectorDefaultWildcardStr, style=wx.FD_DEFAULT_STYLE, pos=wx.DefaultPosition):
93 """Setup the class and store the field. 94 95 @param parent: The parent wx window object. 96 @type parent: Window 97 @keyword field: The field to update with the file selection. 98 @type field: wx object or None 99 @keyword message: The file selector prompt string. 100 @type message: String 101 @keyword defaultDir: The directory to open in. 102 @type defaultDir: String 103 @keyword defaultFile: The file to default selection to. 104 @type defaultFile: String 105 @keyword wildcard: The file wildcard pattern. For example for opening PDB files, this could be "PDB files (*.pdb)|*.pdb;*.PDB". 106 @type wildcard: String 107 @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. 108 @type style: long 109 @keyword pos: The window position. 110 @type pos: Point 111 """ 112 113 # Store the args. 114 self.field = field 115 self.style = style 116 117 # No directory supplied, so use the current working directory. 118 if defaultDir == wx.EmptyString: 119 defaultDir = getcwd() 120 121 # Initialise the base class. 122 super(RelaxFileDialog, self).__init__(parent, message=message, defaultDir=defaultDir, defaultFile=defaultFile, wildcard=wildcard, style=style, pos=pos)
123 124
125 - def get_file(self):
126 """Return the selected file. 127 128 @return: The name of the selected file(s). 129 @rtype: str or list of str 130 """ 131 132 # The multiple files. 133 if self.style in [wx.FD_OPEN|wx.FD_MULTIPLE, wx.FD_SAVE|wx.FD_MULTIPLE]: 134 file = self.GetPaths() 135 136 # The single file. 137 else: 138 file = self.GetPath() 139 140 # Change the current working directory. 141 chdir(self.GetDirectory()) 142 143 # Return the file. 144 return file
145 146
147 - def select_event(self, event):
148 """The file selector GUI element. 149 150 @param event: The wx event. 151 @type event: wx event 152 """ 153 154 # Show the dialog, and return if nothing was selected. 155 if status.show_gui and self.ShowModal() != wx.ID_OK: 156 return 157 158 # Get the selected file. 159 file = self.get_file() 160 161 # Update the field. 162 self.field.SetValue(str_to_gui(file)) 163 164 # Scroll the text to the end. 165 self.field.SetInsertionPoint(len(file))
166