screenlets.options.colour_option

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

# Source Code for Module screenlets.options.colour_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  Color options, these classes will display a text box.
     19  """
     20
     21  import gtk
     22
     23  from screenlets.options import _
     24  from base import Option
     25
     26 -class ColorOption(Option):
     27      """An Option for color options."""
     28 -    def on_import(self, strvalue):
     29          """Import (r, g, b, a) from comma-separated string."""
     30          # strip braces and spaces
     31          strvalue = strvalue.lstrip('(')
     32          strvalue = strvalue.rstrip(')')
     33          strvalue = strvalue.strip()
     34          # split value on commas
     35          tmpval = strvalue.split(',')
     36          outval = []
     37          for f in tmpval:
     38              # create list and again remove spaces
     39              outval.append(float(f.strip()))
     40          return outval
     41
     42 -    def on_export(self, value):
     43          """Export r, g, b, a to comma-separated string."""
     44          l = len(value)
     45          outval = ''
     46          for i in xrange(l):
     47              if type(value[i]) == float:
     48                  outval += "%0.5f" % value[i]
     49              else:
     50                  outval += str(value[i])
     51              if i < l-1:
     52                  outval += ','
     53          return outval
     54
     55 -    def generate_widget(self, value):
     56          """Generate a textbox for a color options"""
     57          self.widget = self.get_box_from_colour( value )
     58          self.set_value(value)
     59          return self.widget
     60
     61 -    def set_value(self, value):
     62          """Set the color value as required."""
     63          self.value = value
     64
     65 -    def has_changed(self, widget):
     66          """Executed when the widget event kicks off."""
     67          self.value = self.get_colour_from_box(self.widget)
     68          super(ColorOption, self).has_changed()
     69
     70 -    def get_box_from_colour(self, colour):
     71          """Turn a colour array into a colour widget"""
     72          result = gtk.ColorButton(gtk.gdk.Color(
     73              int(colour[0]*65535), int(colour[1]*65535), int(colour[2]*65535)))
     74          result.set_use_alpha(True)
     75          result.set_alpha(int(colour[3]*65535))
     76          result.connect("color-set", self.has_changed)
     77          return result
     78
     79 -    def get_colour_from_box(self, widget):
     80          """Turn a colour widget into a colour array"""
     81          colour = widget.get_color()
     82          return (
     83              colour.red/65535.0,
     84              colour.green/65535.0,
     85              colour.blue/65535.0,
     86              widget.get_alpha()/65535.0
     87          )
     88
     89
     90 -class ColorsOption(ColorOption):
     91      """Allow a list of colours to be created"""
     92 -    def on_import(self, value):
     93          """Importing multiple colours"""
     94          result = []
     95          for col in value.split(';'):
     96              if col:
     97                 result.append(super(ColorsOption, self).on_import(col))
     98          return result
     99
    100 -    def on_export(self, value):
    101          """Exporting multiple colours"""
    102          result = ""
    103          for col in value:
    104              result += super(ColorsOption, self).on_export(col)+';'
    105          return result
    106
    107 -    def generate_widget(self, value):
    108          """Generate a textbox for a color options"""
    109          self.widget = gtk.HBox()
    110          if type(value[0]) in [int, float]:
    111              value = [value]
    112          for col in value:
    113              self.add_colour_box(self.widget, col, False)
    114
    115          but = gtk.Button('Add', gtk.STOCK_ADD)
    116          but.show()
    117          but.connect("clicked", self.add_colour_box)
    118          self.widget.pack_end(but)
    119
    120          self.set_value(value)
    121          return self.widget
    122
    123 -    def del_colour_box(self, widget, event):
    124          """Remove a colour box from the array when right clicked"""
    125          if event.button == 3:
    126              if len(self.widget.get_children()) > 2:
    127                  self.widget.remove(widget)
    128                  self.has_changed(widget)
    129
    130 -    def add_colour_box(self, widget, col=None, update=True):
    131          """Add a new box for colours"""
    132          if not col:
    133              col = self.value[-1]
    134          new_box = self.get_box_from_colour( col )
    135          new_box.connect("button_press_event", self.del_colour_box)
    136          self.widget.pack_start(new_box, padding=1)
    137          new_box.show()
    138          if update:
    139              self.has_changed(widget)
    140
    141 -    def has_changed(self, widget):
    142          """The colour widgets have changed!"""
    143          self.value = []
    144          for c in self.widget.get_children():
    145              if type(c) == gtk.ColorButton:
    146                  self.value.append(self.get_colour_from_box( c ))
    147          super(ColorOption, self).has_changed()
    148