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-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  # 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.string_conv 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, field=None, 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 field: The field to update with the file selection. 47 @type field: wx object or None 48 @keyword message: The path selector prompt string. 49 @type message: String 50 @keyword defaultPath: The default directory to open in. 51 @type defaultPath: String 52 @keyword style: The dialog style. 53 @type style: long 54 @keyword pos: The window position. 55 @type pos: Point 56 @keyword size: The default window size. 57 @type size: Size 58 @keyword name: The title for the dialog. 59 @type name: String 60 """ 61 62 # Store the args. 63 self.field = field 64 65 # No path supplied, so use the current working directory. 66 if defaultPath == wx.EmptyString: 67 defaultPath = getcwd() 68 69 # Initialise the base class. 70 super(RelaxDirDialog, self).__init__(parent, message=message, defaultPath=defaultPath, style=style, pos=pos, size=size, name=name)
71 72
73 - def get_path(self):
74 """Return the selected path. 75 76 @return: The name of the selected path. 77 @rtype: str 78 """ 79 80 # The path. 81 path = gui_to_str(self.GetPath()) 82 83 # Change the current working directory. 84 chdir(path) 85 86 # Return the path. 87 return path
88 89
90 - def select_event(self, event):
91 """The path selector GUI element. 92 93 @param event: The wx event. 94 @type event: wx event 95 """ 96 97 # Show the dialog, and return if nothing was selected. 98 if status.show_gui and self.ShowModal() != wx.ID_OK: 99 return 100 101 # Get the selected path. 102 path = self.get_path() 103 104 # Update the field. 105 self.field.SetValue(str_to_gui(path)) 106 107 # Scroll the text to the end. 108 self.field.SetInsertionPoint(len(path))
109 110 111
112 -class RelaxFileDialog(wx.FileDialog):
113 """relax specific replacement file dialog for opening and closing files. 114 115 This class provides the select() method so that this class can be used with a wx event. 116 """ 117
118 - 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):
119 """Setup the class and store the field. 120 121 @param parent: The parent wx window object. 122 @type parent: Window 123 @keyword field: The field to update with the file selection. 124 @type field: wx object or None 125 @keyword message: The file selector prompt string. 126 @type message: String 127 @keyword defaultDir: The directory to open in. 128 @type defaultDir: String 129 @keyword defaultFile: The file to default selection to. 130 @type defaultFile: String 131 @keyword wildcard: The file wildcard pattern. For example for opening PDB files, this could be "PDB files (*.pdb)|*.pdb;*.PDB". 132 @type wildcard: String 133 @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. 134 @type style: long 135 @keyword pos: The window position. 136 @type pos: Point 137 """ 138 139 # Store the args. 140 self.field = field 141 self.style = style 142 143 # No directory supplied, so use the current working directory. 144 if defaultDir == wx.EmptyString: 145 defaultDir = getcwd() 146 147 # Initialise the base class. 148 super(RelaxFileDialog, self).__init__(parent, message=message, defaultDir=defaultDir, defaultFile=defaultFile, wildcard=wildcard, style=style, pos=pos)
149 150
151 - def get_file(self):
152 """Return the selected file. 153 154 @return: The name of the selected file(s). 155 @rtype: str or list of str 156 """ 157 158 # The multiple files. 159 if self.style in [wx.FD_OPEN|wx.FD_MULTIPLE, wx.FD_SAVE|wx.FD_MULTIPLE]: 160 file = self.GetPaths() 161 162 # The single file. 163 else: 164 file = self.GetPath() 165 166 # Change the current working directory. 167 chdir(self.GetDirectory()) 168 169 # Return the file. 170 return file
171 172
173 - def select_event(self, event):
174 """The file selector GUI element. 175 176 @param event: The wx event. 177 @type event: wx event 178 """ 179 180 # Show the dialog, and return if nothing was selected. 181 if status.show_gui and self.ShowModal() != wx.ID_OK: 182 return 183 184 # Get the selected file. 185 file = self.get_file() 186 187 # Update the field. 188 self.field.SetValue(str_to_gui(file)) 189 190 # Scroll the text to the end. 191 self.field.SetInsertionPoint(len(file))
192