From 5a031a488d2c51440297a9ec8cd4c67c7fcd7175 Mon Sep 17 00:00:00 2001
From: Walter Bender <walter@sugarlabs.org>
Date: Thu, 5 Aug 2010 07:37:36 -0400
Subject: [PATCH] added prev next fill and stroke color to selector
---
extensions/cpsection/aboutme/view.py | 184 ++++++++++++++++++++++------------
1 files changed, 118 insertions(+), 66 deletions(-)
diff --git a/extensions/cpsection/aboutme/view.py b/extensions/cpsection/aboutme/view.py
index cabd66a..5c8e65d 100644
a
|
b
|
|
1 | 1 | # Copyright (C) 2008, OLPC |
| 2 | # Copyright (C) 2010, Sugar Labs |
2 | 3 | # |
3 | 4 | # This program is free software; you can redistribute it and/or modify |
4 | 5 | # it under the terms of the GNU General Public License as published by |
… |
… |
from gettext import gettext as _ |
20 | 21 | |
21 | 22 | from sugar.graphics.icon import Icon |
22 | 23 | from sugar.graphics import style |
23 | | from sugar.graphics.xocolor import XoColor |
| 24 | from sugar.graphics.xocolor import XoColor, get_next_stroke_color, \ |
| 25 | get_prev_stroke_color, get_next_fill_color, get_prev_fill_color |
24 | 26 | |
25 | 27 | from jarabe.controlpanel.sectionview import SectionView |
26 | 28 | from jarabe.controlpanel.inlinealert import InlineAlert |
27 | 29 | |
| 30 | _PREV_FILL_COLOR = 0 |
| 31 | _NEXT_FILL_COLOR = 1 |
| 32 | _CURRENT_COLOR = 2 |
| 33 | _NEXT_STROKE_COLOR = 3 |
| 34 | _PREV_STROKE_COLOR = 4 |
| 35 | |
| 36 | |
28 | 37 | class EventIcon(gtk.EventBox): |
29 | | __gtype_name__ = "SugarEventIcon" |
30 | | def __init__(self, **kwargs): |
| 38 | __gtype_name__ = "SugarEventIcon" |
| 39 | |
| 40 | def __init__(self, **kwargs): |
31 | 41 | gtk.EventBox.__init__(self) |
32 | 42 | |
33 | | self.icon = Icon(pixel_size = style.XLARGE_ICON_SIZE, **kwargs) |
34 | | |
| 43 | self.icon = Icon(pixel_size=style.XLARGE_ICON_SIZE, **kwargs) |
| 44 | |
35 | 45 | self.set_visible_window(False) |
36 | 46 | self.set_app_paintable(True) |
37 | 47 | self.set_events(gtk.gdk.BUTTON_PRESS_MASK) |
… |
… |
class EventIcon(gtk.EventBox): |
39 | 49 | self.add(self.icon) |
40 | 50 | self.icon.show() |
41 | 51 | |
| 52 | |
42 | 53 | class ColorPicker(EventIcon): |
43 | 54 | __gsignals__ = { |
44 | 55 | 'color-changed': (gobject.SIGNAL_RUN_FIRST, |
45 | 56 | gobject.TYPE_NONE, |
46 | | ([str])) |
47 | | } |
48 | | def __init__(self, xocolor=None): |
| 57 | ([object])) |
| 58 | } |
| 59 | |
| 60 | def __init__(self, picker): |
49 | 61 | EventIcon.__init__(self) |
50 | | self.icon.props.xo_color = xocolor |
| 62 | |
51 | 63 | self.icon.props.icon_name = 'computer-xo' |
| 64 | self._picker = picker |
| 65 | self._color = None |
| 66 | |
52 | 67 | self.icon.props.pixel_size = style.XLARGE_ICON_SIZE |
53 | | self.connect('button_press_event', self.__pressed_cb) |
54 | 68 | |
55 | | def __pressed_cb(self, button, event): |
56 | | self._set_random_colors() |
| 69 | self.connect('button_press_event', self.__pressed_cb, picker) |
| 70 | |
| 71 | def update(self, color): |
| 72 | if self._picker == _PREV_FILL_COLOR: |
| 73 | self._color = XoColor(get_prev_fill_color(color)) |
| 74 | elif self._picker == _PREV_STROKE_COLOR: |
| 75 | self._color = XoColor(get_prev_stroke_color(color)) |
| 76 | elif self._picker == _NEXT_FILL_COLOR: |
| 77 | self._color = XoColor(get_next_fill_color(color)) |
| 78 | elif self._picker == _NEXT_STROKE_COLOR: |
| 79 | self._color = XoColor(get_next_stroke_color(color)) |
| 80 | else: |
| 81 | self._color = color |
| 82 | self.icon.props.xo_color = self._color |
| 83 | |
| 84 | def __pressed_cb(self, button, event, picker): |
| 85 | if picker != _CURRENT_COLOR: |
| 86 | self.emit('color-changed', self._color) |
57 | 87 | |
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 | 88 | |
63 | 89 | class AboutMe(SectionView): |
| 90 | |
64 | 91 | def __init__(self, model, alerts): |
65 | 92 | SectionView.__init__(self) |
66 | 93 | |
… |
… |
class AboutMe(SectionView): |
69 | 96 | self._nick_sid = 0 |
70 | 97 | self._color_valid = True |
71 | 98 | self._nick_valid = True |
72 | | self._color_change_handler = None |
73 | | self._nick_change_handler = None |
| 99 | self._handlers = [] |
74 | 100 | |
75 | 101 | self.set_border_width(style.DEFAULT_SPACING * 2) |
76 | 102 | self.set_spacing(style.DEFAULT_SPACING) |
77 | 103 | self._group = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL) |
78 | 104 | |
| 105 | self._color_label = gtk.HBox(spacing=style.DEFAULT_SPACING) |
| 106 | self._color_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
| 107 | self._color_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
| 108 | self._color_alert = None |
| 109 | |
| 110 | self._pickers = { |
| 111 | _PREV_FILL_COLOR: ColorPicker(_PREV_FILL_COLOR), |
| 112 | _NEXT_FILL_COLOR: ColorPicker(_NEXT_FILL_COLOR), |
| 113 | _CURRENT_COLOR: ColorPicker(_CURRENT_COLOR), |
| 114 | _NEXT_STROKE_COLOR: ColorPicker(_NEXT_STROKE_COLOR), |
| 115 | _PREV_STROKE_COLOR: ColorPicker(_PREV_STROKE_COLOR) |
| 116 | } |
| 117 | |
| 118 | self._setup_color() |
| 119 | initial_color = XoColor(self._model.get_color_xo()) |
| 120 | self._update_pickers(initial_color) |
| 121 | |
79 | 122 | self._nick_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
80 | 123 | self._nick_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
81 | 124 | self._nick_entry = None |
82 | 125 | self._nick_alert = None |
83 | 126 | 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 | 127 | self.setup() |
92 | 128 | |
93 | 129 | 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, |
| 130 | self._nick_entry = gtk.Entry() |
| 131 | self._nick_entry.modify_bg(gtk.STATE_INSENSITIVE, |
104 | 132 | style.COLOR_WHITE.get_gdk_color()) |
105 | | self._nick_entry.modify_base(gtk.STATE_INSENSITIVE, |
| 133 | self._nick_entry.modify_base(gtk.STATE_INSENSITIVE, |
106 | 134 | style.COLOR_WHITE.get_gdk_color()) |
107 | 135 | self._nick_entry.set_width_chars(25) |
108 | 136 | self._nick_box.pack_start(self._nick_entry, expand=False) |
109 | | self._nick_entry.show() |
| 137 | self._nick_entry.show() |
110 | 138 | |
111 | 139 | label_entry_error = gtk.Label() |
112 | 140 | self._group.add_widget(label_entry_error) |
… |
… |
class AboutMe(SectionView): |
119 | 147 | self._nick_alert.props.msg = self.restart_msg |
120 | 148 | self._nick_alert.show() |
121 | 149 | |
122 | | self.pack_start(self._nick_box, False) |
| 150 | self._center_in_panel = gtk.Alignment(0.5) |
| 151 | self._center_in_panel.add(self._nick_box) |
| 152 | self.pack_start(self._center_in_panel, False) |
123 | 153 | self.pack_start(self._nick_alert_box, False) |
124 | 154 | self._nick_box.show() |
125 | 155 | self._nick_alert_box.show() |
126 | | |
127 | | def _setup_color(self): |
| 156 | self._center_in_panel.show() |
| 157 | |
| 158 | def _setup_color(self): |
128 | 159 | label_color = gtk.Label(_('Click to change your color:')) |
129 | | label_color.modify_fg(gtk.STATE_NORMAL, |
| 160 | label_color.modify_fg(gtk.STATE_NORMAL, |
130 | 161 | style.COLOR_SELECTION_GREY.get_gdk_color()) |
131 | 162 | self._group.add_widget(label_color) |
132 | | self._color_box.pack_start(label_color, expand=False) |
| 163 | self._color_label.pack_start(label_color, expand=False) |
133 | 164 | 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() |
| 165 | |
| 166 | for p in sorted(self._pickers.keys()): |
| 167 | if p == _CURRENT_COLOR: |
| 168 | left_separator = gtk.SeparatorToolItem() |
| 169 | left_separator.show() |
| 170 | self._color_box.pack_start(left_separator, expand=False) |
| 171 | |
| 172 | picker = self._pickers[p] |
| 173 | picker.show() |
| 174 | self._color_box.pack_start(picker, expand=False) |
| 175 | |
| 176 | if p == _CURRENT_COLOR: |
| 177 | right_separator = gtk.SeparatorToolItem() |
| 178 | right_separator.show() |
| 179 | self._color_box.pack_start(right_separator, expand=False) |
138 | 180 | |
139 | 181 | label_color_error = gtk.Label() |
140 | 182 | self._group.add_widget(label_color_error) |
… |
… |
class AboutMe(SectionView): |
147 | 189 | self._color_alert.props.msg = self.restart_msg |
148 | 190 | self._color_alert.show() |
149 | 191 | |
150 | | self.pack_start(self._color_box, False) |
151 | | self.pack_start(self._color_alert_box, False) |
| 192 | self._center_in_panel = gtk.Alignment(0.5) |
| 193 | self._center_in_panel.add(self._color_box) |
| 194 | self.pack_start(self._color_label, False) |
| 195 | self.pack_start(self._center_in_panel, False) |
| 196 | self.pack_start(self._color_alert_box, False) |
| 197 | self._color_label.show() |
152 | 198 | self._color_box.show() |
153 | 199 | self._color_alert_box.show() |
154 | | |
| 200 | self._center_in_panel.show() |
| 201 | |
155 | 202 | def setup(self): |
156 | 203 | 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 | 204 | self._color_valid = True |
161 | 205 | self._nick_valid = True |
162 | 206 | 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) |
| 207 | |
| 208 | def connect(widget, signal, cb): |
| 209 | self._handlers.append((widget, widget.connect(signal, cb))) |
| 210 | |
| 211 | connect(self._nick_entry, 'changed', self.__nick_changed_cb) |
| 212 | for picker in self._pickers.values(): |
| 213 | connect(picker, 'color-changed', self.__color_changed_cb) |
167 | 214 | |
168 | 215 | def undo(self): |
169 | | self._color_picker.disconnect(self._color_change_handler) |
170 | | self._nick_entry.disconnect(self._nick_change_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 |