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 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   
 28  # relax module imports. 
 29  from relax_errors import RelaxError 
 30  from status import Status; status = Status() 
 31   
 32  # relax GUI module imports. 
 33  from gui.filedialog import RelaxFileDialog 
 34  from gui.fonts import font 
 35  from gui.misc import open_file 
 36  from gui import paths 
 37  from gui.string_conv import gui_to_str, str_to_gui 
 38   
 39   
40 -class Selector_file:
41 """Wizard GUI element for selecting files.""" 42
43 - 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):
44 """Build the file selection element. 45 46 @keyword name: The name of the element to use in titles, etc. 47 @type name: str 48 @keyword default: The default value of the element. 49 @type default: str 50 @keyword parent: The wizard GUI element. 51 @type parent: wx.Panel instance 52 @keyword sizer: The sizer to put the input field into. 53 @type sizer: wx.Sizer instance 54 @keyword desc: The text description. 55 @type desc: str 56 @keyword message: The file selector prompt string. 57 @type message: String 58 @keyword wildcard: The file wildcard pattern. For example for opening PDB files, this could be "PDB files (*.pdb)|*.pdb;*.PDB". 59 @type wildcard: String 60 @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. 61 @type style: long 62 @keyword tooltip: The tooltip which appears on hovering over all the GUI elements. 63 @type tooltip: str 64 @keyword divider: The position of the divider. 65 @type divider: int 66 @keyword padding: Spacing to the left and right of the widgets. 67 @type padding: int 68 @keyword spacer: The amount of spacing to add below the field in pixels. If None, a stretchable spacer will be used. 69 @type spacer: None or int 70 @keyword height_element: The height in pixels of the GUI element. 71 @type height_element: int 72 @keyword preview: A flag which if true will allow the file to be previewed. 73 @type preview: bool 74 @keyword read_only: A flag which if True means that the text of the element cannot be edited. 75 @type read_only: bool 76 """ 77 78 # Store the args. 79 self.name = name 80 81 # Argument translation. 82 if default == None: 83 default = wx.EmptyString 84 85 # Init. 86 sub_sizer = wx.BoxSizer(wx.HORIZONTAL) 87 88 # Left padding. 89 sub_sizer.AddSpacer(padding) 90 91 # The description. 92 text = wx.StaticText(parent, -1, desc, style=wx.ALIGN_LEFT) 93 text.SetFont(font.normal) 94 sub_sizer.Add(text, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 0) 95 96 # The divider. 97 if not divider: 98 raise RelaxError("The divider position has not been supplied.") 99 100 # Spacing. 101 x, y = text.GetSize() 102 sub_sizer.AddSpacer((divider - x, 0)) 103 104 # The input field. 105 self._field = wx.TextCtrl(parent, -1, default) 106 self._field.SetMinSize((-1, height_element)) 107 self._field.SetFont(font.normal) 108 sub_sizer.Add(self._field, 1, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 109 110 # The file selection object. 111 obj = RelaxFileDialog(parent, field=self._field, message=message, defaultFile=default, wildcard=wildcard, style=style) 112 113 # A little spacing. 114 sub_sizer.AddSpacer(5) 115 116 # The file selection button. 117 button = wx.BitmapButton(parent, -1, wx.Bitmap(paths.icon_16x16.open, wx.BITMAP_TYPE_ANY)) 118 button.SetMinSize((height_element, height_element)) 119 button.SetToolTipString("Select the file.") 120 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 121 parent.Bind(wx.EVT_BUTTON, obj.select_event, button) 122 123 # File preview. 124 if preview: 125 # A little spacing. 126 sub_sizer.AddSpacer(5) 127 128 # The preview button. 129 button = wx.BitmapButton(parent, -1, wx.Bitmap(paths.icon_16x16.document_preview, wx.BITMAP_TYPE_ANY)) 130 button.SetMinSize((height_element, height_element)) 131 button.SetToolTipString("Preview") 132 sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 133 parent.Bind(wx.EVT_BUTTON, self.preview_file, button) 134 135 # Right padding. 136 sub_sizer.AddSpacer(padding) 137 138 # Add to the main sizer (followed by stretchable spacing). 139 sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0) 140 141 # Spacing below the widget. 142 if spacer == None: 143 sizer.AddStretchSpacer() 144 else: 145 sizer.AddSpacer(spacer) 146 147 # Tooltip. 148 if tooltip: 149 text.SetToolTipString(tooltip) 150 self._field.SetToolTipString(tooltip)
151 152
153 - def Clear(self):
154 """Special method for clearing or resetting the GUI element.""" 155 156 # Clear the value from the TextCtrl. 157 self._field.Clear()
158 159
160 - def GetValue(self):
161 """Special method for returning the value of the GUI element. 162 163 @return: The string value. 164 @rtype: list of str 165 """ 166 167 # Convert and return the value from a TextCtrl. 168 return gui_to_str(self._field.GetValue())
169 170
171 - def SetValue(self, value):
172 """Special method for setting the value of the GUI element. 173 174 @param value: The value to set. 175 @type value: str 176 """ 177 178 # Convert and set the value for a TextCtrl. 179 self._field.SetValue(str_to_gui(value))
180 181
182 - def preview_file(self, event=None):
183 """Preview a file. 184 185 @keyword event: The wx event. 186 @type event: wx event 187 """ 188 189 # The file name. 190 file = gui_to_str(self._field.GetValue()) 191 192 # No file, so do nothing. 193 if file == None: 194 return 195 196 # Open the file as text. 197 open_file(file, force_text=True)
198