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