screenlets.options.list_option

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

# Source Code for Module screenlets.options.list_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  List options, these classes will display all sorts of crazy shit.
     19  """
     20
     21  import gtk
     22
     23  from screenlets.options import _
     24  from base import Option
     25
     26 -class ListOption(Option):
     27      """An Option for string options."""
     28 -    def on_import(self, strvalue):
     29          """When a list is imported from the config."""
     30          return eval(strvalue)
     31
     32 -    def on_export(self, value):
     33          """When a list is exported to the config."""
     34          return str(value)
     35
     36 -    def generate_widget(self, value):
     37          """Generate some widgets for a list."""
     38          self._entry = gtk.Entry()
     39          self._entry.set_editable(False)
     40          self.set_value(value)
     41          self._entry.show()
     42          img = gtk.Image()
     43          img.set_from_stock(gtk.STOCK_EDIT, 1)
     44          but = gtk.Button()
     45          but.set_image(img)
     46          but.show()
     47          but.connect("clicked", self.open_listeditor)
     48          but.set_tooltip_text(_('Open List-Editor ...'))
     49          self._entry.set_tooltip_text(self.desc)
     50          self.widget = gtk.HBox()
     51          self.widget.add(self._entry)
     52          self.widget.add(but)
     53          return self.widget
     54
     55 -    def open_listeditor(self, event):
     56          # open dialog
     57          dlg = ListOptionDialog()
     58          # read string from entry and import it through option-class
     59          # (this is needed to always have an up-to-date value)
     60          dlg.set_list(self.on_import(self._entry.get_text()))
     61          resp = dlg.run()
     62          if resp == gtk.RESPONSE_OK:
     63              # set text in entry
     64              self._entry.set_text(str(dlg.get_list()))
     65              # manually call the options-callback
     66              if self.realtime:
     67                  self.has_changed(dlg)
     68          dlg.destroy()
     69
     70 -    def set_value(self, value):
     71          """Set the list string value as required."""
     72          self._entry.set_text(str(value))
     73          self.value = value
     74
     75 -    def has_changed(self, widget):
     76          """Executed when the widget event kicks off."""
     77          self.value = widget.get_list()
     78          super(ListOption, self).has_changed()
     79
     80
     81 -class ListOptionDialog(gtk.Dialog):
     82      """An editing dialog used for editing options of the ListOption-type."""
     83      model = None
     84      tree = None
     85      buttonbox = None
     86
     87      # call gtk.Dialog.__init__
     88 -    def __init__ (self):
     89          super(ListOptionDialog, self).__init__(
     90              "Edit List",
     91              flags=gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR,
     92              buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
     93              gtk.STOCK_OK, gtk.RESPONSE_OK)
     94          )
     95          # set size
     96          self.resize(300, 370)
     97          self.set_keep_above(True)
     98          # init vars
     99          self.model = gtk.ListStore(str)
    100          # create UI
    101          self.create_ui()
    102
    103 -    def create_ui (self):
    104          """Create the user-interface for this dialog."""
    105          # create outer hbox (tree|buttons)
    106          hbox = gtk.HBox()
    107          hbox.set_border_width(10)
    108          hbox.set_spacing(10)
    109          # create tree
    110          self.tree = gtk.TreeView(model=self.model)
    111          self.tree.set_headers_visible(False)
    112          self.tree.set_reorderable(True)
    113          #self.tree.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_HORIZONTAL)
    114          col = gtk.TreeViewColumn('')
    115          cell = gtk.CellRendererText()
    116          #cell.set_property('cell-background', 'cyan')
    117          cell.set_property('foreground', 'black')
    118          col.pack_start(cell, False)
    119          col.set_attributes(cell, text=0)
    120          self.tree.append_column(col)
    121          self.tree.show()
    122          hbox.pack_start(self.tree, True, True)
    123          #sep = gtk.VSeparator()
    124          #sep.show()
    125          #hbox.add(sep)
    126          # create  buttons
    127          self.buttonbox = bb = gtk.VButtonBox()
    128          self.buttonbox.set_layout(gtk.BUTTONBOX_START)
    129          b1 = gtk.Button(stock=gtk.STOCK_ADD)
    130          b2 = gtk.Button(stock=gtk.STOCK_EDIT)
    131          b3 = gtk.Button(stock=gtk.STOCK_REMOVE)
    132          b1.connect('clicked', self.button_callback, 'add')
    133          b2.connect('clicked', self.button_callback, 'edit')
    134          b3.connect('clicked', self.button_callback, 'remove')
    135          bb.add(b1)
    136          bb.add(b2)
    137          bb.add(b3)
    138          self.buttonbox.show_all()
    139          #hbox.add(self.buttonbox)
    140          hbox.pack_end(self.buttonbox, False)
    141          # add everything to outer hbox and show it
    142          hbox.show()
    143          self.vbox.add(hbox)
    144
    145 -    def set_list (self, lst):
    146          """Set the list to be edited in this editor."""
    147          for el in lst:
    148              self.model.append([el])
    149
    150 -    def get_list (self):
    151          """Return the list that is currently being edited in this editor."""
    152          lst = []
    153          for i in self.model:
    154              lst.append(i[0])
    155          return lst
    156
    157 -    def remove_selected_item (self):
    158          """Remove the currently selected item."""
    159          sel = self.tree.get_selection()
    160          if sel:
    161              it = sel.get_selected()[1]
    162              if it:
    163                  print self.model.get_value(it, 0)
    164                  self.model.remove(it)
    165
    166 -    def entry_dialog (self, default = ''):
    167          """Show entry-dialog and return string."""
    168          entry = gtk.Entry()
    169          entry.set_text(default)
    170          entry.show()
    171          dlg = gtk.Dialog("Add/Edit Item", flags=gtk.DIALOG_DESTROY_WITH_PARENT,
    172              buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK,
    173              gtk.RESPONSE_OK))
    174          dlg.set_keep_above(True)
    175          dlg.vbox.add(entry)
    176          resp = dlg.run()
    177          ret = None
    178          if resp == gtk.RESPONSE_OK:
    179              ret = entry.get_text()
    180          dlg.destroy()
    181          return ret
    182
    183 -    def button_callback (self, widget, id):
    184          print "PRESS: %s" % id
    185          if id == 'remove':
    186              self.remove_selected_item()
    187          if id == 'add':
    188              new = self.entry_dialog()
    189              if new != None:
    190                  self.model.append([new])
    191          if id == 'edit':
    192              sel = self.tree.get_selection()
    193              if sel:
    194                  it = sel.get_selected()[1]
    195                  if it:
    196                      new = self.entry_dialog(self.model.get_value(it, 0))
    197                      if new != None:
    198                          #self.model.append([new])
    199                          self.model.set_value(it, 0, new)
    200
    201  # TEST>-
    202  """dlg = ListOptionDialog()
    203  dlg.set_list(['test1', 'afarew34s', 'fhjh23faj', 'yxcdfs58df', 'hsdf7jsdfh'])
    204  dlg.run()
    205  print "RESULT: " + str(dlg.get_list())
    206  dlg.destroy()
    207  import sys
    208  sys.exit(1)"""
    209  # /TEST
    210