1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23  """Module containing a set of special GUI elements to be used in the relax wizards.""" 
 24   
 25   
 26  import wx 
 27   
 28   
 29  from relax_errors import RelaxError 
 30  from status import Status; status = Status() 
 31   
 32   
 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   
 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           
 79          self.name = name 
 80   
 81           
 82          if default == None: 
 83              default = wx.EmptyString 
 84   
 85           
 86          sub_sizer = wx.BoxSizer(wx.HORIZONTAL) 
 87   
 88           
 89          sub_sizer.AddSpacer(padding) 
 90   
 91           
 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           
 97          if not divider: 
 98              raise RelaxError("The divider position has not been supplied.") 
 99   
100           
101          x, y = text.GetSize() 
102          sub_sizer.AddSpacer((divider - x, 0)) 
103   
104           
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           
111          obj = RelaxFileDialog(parent, field=self._field, message=message, defaultFile=default, wildcard=wildcard, style=style) 
112   
113           
114          sub_sizer.AddSpacer(5) 
115   
116           
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           
124          if preview: 
125               
126              sub_sizer.AddSpacer(5) 
127   
128               
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           
136          sub_sizer.AddSpacer(padding) 
137   
138           
139          sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0) 
140   
141           
142          if spacer == None: 
143              sizer.AddStretchSpacer() 
144          else: 
145              sizer.AddSpacer(spacer) 
146   
147           
148          if tooltip: 
149              text.SetToolTipString(tooltip) 
150              self._field.SetToolTipString(tooltip) 
 151   
152   
154          """Special method for clearing or resetting the GUI element.""" 
155   
156           
157          self._field.Clear() 
 158   
159   
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           
168          return gui_to_str(self._field.GetValue()) 
 169   
170   
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           
179          self._field.SetValue(str_to_gui(value)) 
 180   
181   
183          """Preview a file. 
184   
185          @keyword event: The wx event. 
186          @type event:    wx event 
187          """ 
188   
189           
190          file = gui_to_str(self._field.GetValue()) 
191   
192           
193          if file == None: 
194              return 
195   
196           
197          open_file(file, force_text=True) 
  198