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