screenlets.options.file_option

Package screenlets :: Package options :: Module file_option |
[hide private]
[frames] | no frames]

# Source Code for Module screenlets.options.file_option

      1  #
      2  # Copyright (C) 2009 Martin Owens (DoctorMO) <doctormo@gmail.com>
      3  #
      4  # This program is free software: you can redistribute it and/or modify
      5  # it under the terms of the GNU General Public License as published by
      6  # the Free Software Foundation, either version 3 of the License, or
      7  # (at your option) any later version.
      8  #
      9  # This program is distributed in the hope that it will be useful,
     10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  # GNU General Public License for more details.
     13  #
     14  # You should have received a copy of the GNU General Public License
     15  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  #
     17  """
     18  File options, these classes will display a file select widget.
     19  """
     20
     21  import gtk
     22  import os
     23
     24  from screenlets.options import _
     25  from base import Option
     26
     27 -class FileOption(Option):
     28      """
     29      An Option-subclass for string-values that contain filenames. Adds
     30      a patterns-attribute that can contain a list of patterns to be shown
     31      in the assigned file selection dialog. The show_pixmaps-attribute
     32      can be set to True to make the filedialog show all image-types.
     33      supported by gtk.Pixmap. If the directory-attributue is true, the
     34      dialog will ony allow directories.
     35
     36      XXX - Some of this doen't yet work, unknown reason.
     37      """
     38      patterns = [ ( 'All Files', ['*'] ) ]
     39      image = False
     40      _gtk_file_mode = gtk.FILE_CHOOSER_ACTION_OPEN
     41      _opener_title = _("Choose File")
     42
     43 -    def on_import(self, strvalue):
     44          """When a file is imported from the config."""
     45          return strvalue
     46
     47 -    def on_export(self, value):
     48          """When a file is exported to the config."""
     49          return str(value)
     50
     51 -    def generate_widget(self, value):
     52          """Generate a special widget for file options"""
     53          dlg = self.generate_file_opener()
     54          self.widget = gtk.FileChooserButton(dlg)
     55          self.widget.set_title(self._opener_title)
     56          self.widget.set_size_request(180, 28)
     57          self.set_value(value)
     58          if self.realtime:
     59              self.widget.connect("selection-changed", self.has_changed)
     60          return self.widget
     61
     62 -    def generate_file_opener(self):
     63          """Generate a file opener widget"""
     64          dlg = gtk.FileChooserDialog(action=self._gtk_file_mode,
     65              buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
     66                       gtk.STOCK_OPEN, gtk.RESPONSE_OK),
     67          )
     68          dlg.set_keep_above(True)
     69          self.set_filters(dlg)
     70          return dlg
     71
     72 -    def set_filters(self, dlg):
     73          """Add file filters to the dialog widget"""
     74          if self.patterns:
     75              for filter in self.patterns:
     76                  fil = gtk.FileFilter()
     77                  fil.set_name("%s (%s)" % (filter[0], ','.join(filter[1])))
     78                  for pattern in filter[1]:
     79                      fil.add_pattern(pattern)
     80                  dlg.add_filter(fil)
     81
     82 -    def set_value(self, value):
     83          """Set the file value as required."""
     84          self.widget.set_filename(value)
     85          self.value = value
     86
     87 -    def has_changed(self, widget):
     88          """Executed when the widget event kicks off."""
     89          self.value = self.widget.get_filename()
     90          super(FileOption, self).has_changed()
     91
     92
     93 -class DirectoryOption(FileOption):
     94      """Directory is based on file widgets"""
     95      _gtk_file_mode = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER
     96      _opener_title = _("Choose Directory")
     97
     98
     99 -class ImageOption(FileOption):
    100      """Image is based on file widgets"""
    101      _opener_title = _("Choose Image")
    102
    103 -    def set_filters(self, dlg):
    104          """Add the standard pixbug formats"""
    105          flt = gtk.FileFilter()
    106          flt.add_pixbuf_formats()
    107          dlg.set_filter(flt)
    108
    109 -    def generate_widget(self, value):
    110          """Crazy image opener widget generation."""
    111          # create entry and button (entry is hidden)
    112          self._entry = gtk.Entry()
    113          self._entry.set_text(value)
    114          self._entry.set_editable(False)
    115          but = gtk.Button()
    116          # load preview image
    117          but.set_image(self.create_preview(value))
    118          but.connect('clicked', self.but_callback)
    119          # create widget
    120          self.widget = gtk.HBox()
    121          self.widget.add(self._entry)
    122          self.widget.add(but)
    123          but.show()
    124          self.widget.show()
    125          # add tooltips
    126          but.set_tooltip_text(_('Select Image ...'))
    127          but.set_tooltip_text(self.desc)
    128          return self.widget
    129
    130 -    def create_preview(self, filename):
    131          """Utililty method to reload preview image"""
    132          if filename and os.path.isfile(filename):
    133              pb = gtk.gdk.pixbuf_new_from_file_at_size(filename, 64, -1)
    134              if pb:
    135                  img = gtk.Image()
    136                  img.set_from_pixbuf(pb)
    137                  return img
    138          img = gtk.image_new_from_stock(gtk.STOCK_MISSING_IMAGE,
    139              gtk.ICON_SIZE_LARGE_TOOLBAR)
    140          img.set_size_request(64, 64)
    141          return img
    142
    143 -    def but_callback(self, widget):
    144          """Create button"""
    145          dlg = gtk.FileChooserDialog(buttons=(gtk.STOCK_CANCEL,
    146              gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    147          dlg.set_keep_above(True)
    148          dlg.set_filename(self._entry.get_text())
    149          prev = gtk.Image()
    150          box = gtk.VBox()
    151          box.set_size_request(150, -1)
    152          box.add(prev)
    153          prev.show()
    154          dlg.set_preview_widget_active(True)
    155          dlg.connect('selection-changed', self.preview_callback, dlg, prev)
    156          dlg.set_preview_widget(box)
    157          response = dlg.run()
    158          if response == gtk.RESPONSE_OK:
    159              self._entry.set_text(dlg.get_filename())
    160              widget.set_image(self.create_preview(dlg.get_filename()))
    161              self.has_changed(self.widget)
    162          dlg.destroy()
    163
    164 -    def preview_callback(self, widget, dlg, prev):
    165          """add preview widget to filechooser"""
    166          fname = dlg.get_preview_filename()
    167          if fname and os.path.isfile(fname):
    168              pb = gtk.gdk.pixbuf_new_from_file_at_size(fname, 150, -1)
    169              if pb:
    170                  prev.set_from_pixbuf(pb)
    171                  dlg.set_preview_widget_active(True)
    172              else:
    173                  dlg.set_preview_widget_active(False)
    174
    175 -    def has_changed(self, widget):
    176          """Executed when the widget event kicks off."""
    177          self.value = self._entry.get_text()
    178          super(FileOption, self).has_changed()
    179