2
# Copyright (C) 2009 Martin Owens (DoctorMO) <doctormo@gmail.com>
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.
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.
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
19
Color options, these classes will display a text box.
24
from screenlets.options import _
25
from base import Option
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(',')
39
# create list and again remove spaces
40
outval.append(float(f.strip()))
43
def on_export(self, value):
44
"""Export r, g, b, a to comma-separated string."""
48
if type(value[i]) == float:
49
outval += "%0.5f" % value[i]
51
outval += str(value[i])
56
def generate_widget(self, value):
57
"""Generate a textbox for a color options"""
58
self.widget = self.get_box_from_colour( value )
62
def set_value(self, value):
63
"""Set the color value as required."""
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()
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)
80
def get_colour_from_box(self, widget):
81
"""Turn a colour widget into a colour array"""
82
colour = widget.get_color()
87
widget.get_alpha()/65535.0
91
class ColorsOption(ColorOption):
92
"""Allow a list of colours to be created"""
93
def on_import(self, value):
94
"""Importing multiple colours"""
96
for col in value.split(';'):
98
result.append(super(ColorsOption, self).on_import(col))
101
def on_export(self, value):
102
"""Exporting multiple colours"""
105
result += super(ColorsOption, self).on_export(col)+';'
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]:
114
self.add_colour_box(self.widget, col, False)
116
but = gtk.Button('Add', gtk.STOCK_ADD)
118
but.connect("clicked", self.add_colour_box)
119
self.widget.pack_end(but)
121
self.set_value(value)
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)
131
def add_colour_box(self, widget, col=None, update=True):
132
"""Add a new box for colours"""
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)
140
self.has_changed(widget)
142
def has_changed(self, widget):
143
"""The colour widgets have changed!"""
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()