~ubuntu-branches/ubuntu/trusty/screenlets/trusty

« back to all changes in this revision

Viewing changes to src/lib/options/colour_option.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2011-07-02 11:06:42 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110702110642-d0oqdiy8sye8cli2
Tags: 0.1.4-0ubuntu1
* New upstream release.
* debian/patches:
 - 90-disable-resize-grip.patch : Drop, merged upstream.
 - 91_use_webkit.patch: Drop, merged upstream.
* debian/rules:
 - Only fix the permission on 1 file.
* debian/copyright:
 - Update copyright holders.

Show diffs side-by-side

added added

removed removed

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