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