From 31e9281e8c3439462ed58ec53558b1c47f74c471 Mon Sep 17 00:00:00 2001
From: Walter Bender <walter@sugarlabs.org>
Date: Tue, 20 Jul 2010 14:50:47 -0400
Subject: [PATCH] new color selector for cpsection
---
extensions/cpsection/aboutme/view.py | 182 ++++++++++++++++++++++------------
1 files changed, 117 insertions(+), 65 deletions(-)
diff --git a/extensions/cpsection/aboutme/view.py b/extensions/cpsection/aboutme/view.py
index cabd66a..caf6d29 100644
a
|
b
|
from sugar.graphics.xocolor import XoColor |
25 | 25 | from jarabe.controlpanel.sectionview import SectionView |
26 | 26 | from jarabe.controlpanel.inlinealert import InlineAlert |
27 | 27 | |
| 28 | _DIRECTION_LEFT = 0 |
| 29 | _DIRECTION_TOP = 1 |
| 30 | _DIRECTION_CENTER = 2 |
| 31 | _DIRECTION_BOTTOM = 3 |
| 32 | _DIRECTION_RIGHT = 4 |
| 33 | |
| 34 | |
28 | 35 | class EventIcon(gtk.EventBox): |
29 | | __gtype_name__ = "SugarEventIcon" |
30 | | def __init__(self, **kwargs): |
| 36 | __gtype_name__ = "SugarEventIcon" |
| 37 | |
| 38 | def __init__(self, **kwargs): |
31 | 39 | gtk.EventBox.__init__(self) |
32 | 40 | |
33 | | self.icon = Icon(pixel_size = style.XLARGE_ICON_SIZE, **kwargs) |
34 | | |
| 41 | self.icon = Icon(pixel_size=style.XLARGE_ICON_SIZE, **kwargs) |
| 42 | |
35 | 43 | self.set_visible_window(False) |
36 | 44 | self.set_app_paintable(True) |
37 | 45 | self.set_events(gtk.gdk.BUTTON_PRESS_MASK) |
… |
… |
class EventIcon(gtk.EventBox): |
39 | 47 | self.add(self.icon) |
40 | 48 | self.icon.show() |
41 | 49 | |
| 50 | |
42 | 51 | class ColorPicker(EventIcon): |
43 | 52 | __gsignals__ = { |
44 | 53 | 'color-changed': (gobject.SIGNAL_RUN_FIRST, |
45 | 54 | gobject.TYPE_NONE, |
46 | | ([str])) |
47 | | } |
48 | | def __init__(self, xocolor=None): |
| 55 | ([object])) |
| 56 | } |
| 57 | |
| 58 | def __init__(self, direction): |
49 | 59 | EventIcon.__init__(self) |
50 | | self.icon.props.xo_color = xocolor |
| 60 | |
51 | 61 | self.icon.props.icon_name = 'computer-xo' |
| 62 | self._direction = direction |
| 63 | self._color = None |
| 64 | |
52 | 65 | self.icon.props.pixel_size = style.XLARGE_ICON_SIZE |
53 | | self.connect('button_press_event', self.__pressed_cb) |
54 | 66 | |
55 | | def __pressed_cb(self, button, event): |
56 | | self._set_random_colors() |
| 67 | self.connect('button_press_event', self.__pressed_cb, direction) |
| 68 | |
| 69 | def update(self, color): |
| 70 | if self._direction == _DIRECTION_CENTER: |
| 71 | self._color = color |
| 72 | elif self._direction == _DIRECTION_LEFT: |
| 73 | self._color = XoColor(color.get_prev_fill_color()) |
| 74 | elif self._direction == _DIRECTION_RIGHT: |
| 75 | self._color = XoColor(color.get_prev_stroke_color()) |
| 76 | elif self._direction == _DIRECTION_TOP: |
| 77 | self._color = XoColor(color.get_next_fill_color()) |
| 78 | else: |
| 79 | self._color = XoColor(color.get_next_stroke_color()) |
| 80 | self.icon.props.xo_color = self._color |
| 81 | |
| 82 | def __pressed_cb(self, button, event, direction): |
| 83 | if direction != _DIRECTION_CENTER: |
| 84 | self.emit('color-changed', self._color) |
57 | 85 | |
58 | | def _set_random_colors(self): |
59 | | xocolor = XoColor() |
60 | | self.icon.props.xo_color = xocolor |
61 | | self.emit('color-changed', xocolor.to_string()) |
62 | 86 | |
63 | 87 | class AboutMe(SectionView): |
| 88 | |
64 | 89 | def __init__(self, model, alerts): |
65 | 90 | SectionView.__init__(self) |
66 | 91 | |
… |
… |
class AboutMe(SectionView): |
69 | 94 | self._nick_sid = 0 |
70 | 95 | self._color_valid = True |
71 | 96 | self._nick_valid = True |
72 | | self._color_change_handler = None |
73 | | self._nick_change_handler = None |
| 97 | self._handlers = [] |
74 | 98 | |
75 | 99 | self.set_border_width(style.DEFAULT_SPACING * 2) |
76 | 100 | self.set_spacing(style.DEFAULT_SPACING) |
77 | 101 | self._group = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL) |
78 | 102 | |
| 103 | self._color_label = gtk.HBox(spacing=style.DEFAULT_SPACING) |
| 104 | self._color_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
| 105 | self._color_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
| 106 | self._color_alert = None |
| 107 | |
| 108 | self._pickers = { |
| 109 | _DIRECTION_CENTER: ColorPicker(_DIRECTION_CENTER), |
| 110 | _DIRECTION_LEFT: ColorPicker(_DIRECTION_LEFT), |
| 111 | _DIRECTION_RIGHT: ColorPicker(_DIRECTION_RIGHT), |
| 112 | _DIRECTION_TOP: ColorPicker(_DIRECTION_TOP), |
| 113 | _DIRECTION_BOTTOM: ColorPicker(_DIRECTION_BOTTOM) |
| 114 | } |
| 115 | |
| 116 | self._setup_color() |
| 117 | initial_color = XoColor(self._model.get_color_xo()) |
| 118 | self._update_pickers(initial_color) |
| 119 | |
79 | 120 | self._nick_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
80 | 121 | self._nick_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
81 | 122 | self._nick_entry = None |
82 | 123 | self._nick_alert = None |
83 | 124 | self._setup_nick() |
84 | | |
85 | | self._color_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
86 | | self._color_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
87 | | self._color_picker = None |
88 | | self._color_alert = None |
89 | | self._setup_color() |
90 | | |
91 | 125 | self.setup() |
92 | 126 | |
93 | 127 | def _setup_nick(self): |
94 | | label_entry = gtk.Label(_('Name:')) |
95 | | label_entry.modify_fg(gtk.STATE_NORMAL, |
96 | | style.COLOR_SELECTION_GREY.get_gdk_color()) |
97 | | self._group.add_widget(label_entry) |
98 | | label_entry.set_alignment(1, 0.5) |
99 | | self._nick_box.pack_start(label_entry, expand=False) |
100 | | label_entry.show() |
101 | | |
102 | | self._nick_entry = gtk.Entry() |
103 | | self._nick_entry.modify_bg(gtk.STATE_INSENSITIVE, |
| 128 | self._nick_entry = gtk.Entry() |
| 129 | self._nick_entry.modify_bg(gtk.STATE_INSENSITIVE, |
104 | 130 | style.COLOR_WHITE.get_gdk_color()) |
105 | | self._nick_entry.modify_base(gtk.STATE_INSENSITIVE, |
| 131 | self._nick_entry.modify_base(gtk.STATE_INSENSITIVE, |
106 | 132 | style.COLOR_WHITE.get_gdk_color()) |
107 | 133 | self._nick_entry.set_width_chars(25) |
108 | 134 | self._nick_box.pack_start(self._nick_entry, expand=False) |
109 | | self._nick_entry.show() |
| 135 | self._nick_entry.show() |
110 | 136 | |
111 | 137 | label_entry_error = gtk.Label() |
112 | 138 | self._group.add_widget(label_entry_error) |
… |
… |
class AboutMe(SectionView): |
119 | 145 | self._nick_alert.props.msg = self.restart_msg |
120 | 146 | self._nick_alert.show() |
121 | 147 | |
122 | | self.pack_start(self._nick_box, False) |
| 148 | self._center_in_panel = gtk.Alignment(0.5) |
| 149 | self._center_in_panel.add(self._nick_box) |
| 150 | self.pack_start(self._center_in_panel, False) |
123 | 151 | self.pack_start(self._nick_alert_box, False) |
124 | 152 | self._nick_box.show() |
125 | 153 | self._nick_alert_box.show() |
126 | | |
127 | | def _setup_color(self): |
| 154 | self._center_in_panel.show() |
| 155 | |
| 156 | def _setup_color(self): |
128 | 157 | label_color = gtk.Label(_('Click to change your color:')) |
129 | | label_color.modify_fg(gtk.STATE_NORMAL, |
| 158 | label_color.modify_fg(gtk.STATE_NORMAL, |
130 | 159 | style.COLOR_SELECTION_GREY.get_gdk_color()) |
131 | 160 | self._group.add_widget(label_color) |
132 | | self._color_box.pack_start(label_color, expand=False) |
| 161 | self._color_label.pack_start(label_color, expand=False) |
133 | 162 | label_color.show() |
134 | | |
135 | | self._color_picker = ColorPicker() |
136 | | self._color_box.pack_start(self._color_picker, expand=False) |
137 | | self._color_picker.show() |
| 163 | |
| 164 | for direction in sorted(self._pickers.keys()): |
| 165 | if direction == _DIRECTION_CENTER: |
| 166 | left_separator = gtk.SeparatorToolItem() |
| 167 | left_separator.show() |
| 168 | self._color_box.pack_start(left_separator, expand=False) |
| 169 | |
| 170 | picker = self._pickers[direction] |
| 171 | picker.show() |
| 172 | self._color_box.pack_start(picker, expand=False) |
| 173 | |
| 174 | if direction == _DIRECTION_CENTER: |
| 175 | right_separator = gtk.SeparatorToolItem() |
| 176 | right_separator.show() |
| 177 | self._color_box.pack_start(right_separator, expand=False) |
138 | 178 | |
139 | 179 | label_color_error = gtk.Label() |
140 | 180 | self._group.add_widget(label_color_error) |
… |
… |
class AboutMe(SectionView): |
147 | 187 | self._color_alert.props.msg = self.restart_msg |
148 | 188 | self._color_alert.show() |
149 | 189 | |
150 | | self.pack_start(self._color_box, False) |
151 | | self.pack_start(self._color_alert_box, False) |
| 190 | self._center_in_panel = gtk.Alignment(0.5) |
| 191 | self._center_in_panel.add(self._color_box) |
| 192 | self.pack_start(self._color_label, False) |
| 193 | self.pack_start(self._center_in_panel, False) |
| 194 | self.pack_start(self._color_alert_box, False) |
| 195 | self._color_label.show() |
152 | 196 | self._color_box.show() |
153 | 197 | self._color_alert_box.show() |
154 | | |
| 198 | self._center_in_panel.show() |
| 199 | |
155 | 200 | def setup(self): |
156 | 201 | self._nick_entry.set_text(self._model.get_nick()) |
157 | | color = XoColor(self._model.get_color_xo()) |
158 | | self._color_picker.icon.props.xo_color = color |
159 | | |
160 | 202 | self._color_valid = True |
161 | 203 | self._nick_valid = True |
162 | 204 | self.needs_restart = False |
163 | | self._nick_change_handler = self._nick_entry.connect( \ |
164 | | 'changed', self.__nick_changed_cb) |
165 | | self._color_change_handler = self._color_picker.connect( \ |
166 | | 'color-changed', self.__color_changed_cb) |
| 205 | |
| 206 | def connect(widget, signal, cb): |
| 207 | self._handlers.append((widget, widget.connect(signal, cb))) |
| 208 | |
| 209 | connect(self._nick_entry, 'changed', self.__nick_changed_cb) |
| 210 | for picker in self._pickers.values(): |
| 211 | connect(picker, 'color-changed', self.__color_changed_cb) |
167 | 212 | |
168 | 213 | def undo(self): |
169 | | self._color_picker.disconnect(self._color_change_handler) |
170 | | self._nick_entry.disconnect(self._nick_change_handler) |
| 214 | for widget, handler in self._handlers: |
| 215 | widget.disconnect(handler) |
171 | 216 | self._model.undo() |
172 | 217 | self._nick_alert.hide() |
173 | | self._color_alert.hide() |
| 218 | self._color_alert.hide() |
| 219 | |
| 220 | def _update_pickers(self, color): |
| 221 | for picker in self._pickers.values(): |
| 222 | picker.update(color) |
174 | 223 | |
175 | 224 | def _validate(self): |
176 | 225 | if self._nick_valid and self._color_valid: |
… |
… |
class AboutMe(SectionView): |
178 | 227 | else: |
179 | 228 | self.props.is_valid = False |
180 | 229 | |
181 | | def __nick_changed_cb(self, widget, data=None): |
| 230 | def __nick_changed_cb(self, widget, data=None): |
182 | 231 | if self._nick_sid: |
183 | 232 | gobject.source_remove(self._nick_sid) |
184 | | self._nick_sid = gobject.timeout_add(self._APPLY_TIMEOUT, |
| 233 | self._nick_sid = gobject.timeout_add(self._APPLY_TIMEOUT, |
185 | 234 | self.__nick_timeout_cb, widget) |
186 | 235 | |
187 | | def __nick_timeout_cb(self, widget): |
| 236 | def __nick_timeout_cb(self, widget): |
188 | 237 | self._nick_sid = 0 |
189 | 238 | |
190 | 239 | if widget.get_text() == self._model.get_nick(): |
… |
… |
class AboutMe(SectionView): |
193 | 242 | self._model.set_nick(widget.get_text()) |
194 | 243 | except ValueError, detail: |
195 | 244 | self._nick_alert.props.msg = detail |
196 | | self._nick_valid = False |
| 245 | self._nick_valid = False |
197 | 246 | else: |
198 | 247 | self._nick_alert.props.msg = self.restart_msg |
199 | | self._nick_valid = True |
| 248 | self._nick_valid = True |
200 | 249 | self.needs_restart = True |
201 | 250 | self.restart_alerts.append('nick') |
202 | 251 | self._validate() |
203 | 252 | self._nick_alert.show() |
204 | 253 | return False |
205 | 254 | |
206 | | def __color_changed_cb(self, colorpicker, xocolor): |
207 | | self._model.set_color_xo(xocolor) |
| 255 | def __color_changed_cb(self, colorpicker, color): |
| 256 | self._model.set_color_xo(color.to_string()) |
208 | 257 | self.needs_restart = True |
209 | 258 | self._color_alert.props.msg = self.restart_msg |
210 | 259 | self._color_valid = True |
… |
… |
class AboutMe(SectionView): |
212 | 261 | |
213 | 262 | self._validate() |
214 | 263 | self._color_alert.show() |
| 264 | |
| 265 | self._update_pickers(color) |
| 266 | |
215 | 267 | return False |