Ticket #1592: 0001-enhanced-color-selector-with-code-clean-as-per-sugge.patch
File 0001-enhanced-color-selector-with-code-clean-as-per-sugge.patch, 12.3 KB (added by walter, 13 years ago) |
---|
-
extensions/cpsection/aboutme/view.py
From dc118f3abac852343d3d8f1ec02379358307f177 Mon Sep 17 00:00:00 2001 From: Walter Bender <walter@sugarlabs.org> Date: Mon, 16 Aug 2010 09:44:48 -0400 Subject: [PATCH] enhanced color selector with code clean as per suggestions from tomeu --- extensions/cpsection/aboutme/view.py | 215 ++++++++++++++++++++++++++------- 1 files changed, 169 insertions(+), 46 deletions(-) diff --git a/extensions/cpsection/aboutme/view.py b/extensions/cpsection/aboutme/view.py index 9e07bd1..49843f2 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, colors 24 25 25 26 from jarabe.controlpanel.sectionview import SectionView 26 27 from jarabe.controlpanel.inlinealert import InlineAlert 27 28 29 _STROKE_COLOR = 0 30 _FILL_COLOR = 1 31 32 33 def _get_next_stroke_color(color): 34 """ Return the next color pair in the list that shares the same fill 35 as color. """ 36 current_index = _get_current_index(color) 37 if current_index == -1: 38 return "%s,%s" % (color.stroke, color.fill) 39 next_index = _next_index(current_index) 40 while(colors[next_index][_FILL_COLOR] != \ 41 colors[current_index][_FILL_COLOR]): 42 next_index = _next_index(next_index) 43 return "%s,%s" % (colors[next_index][_STROKE_COLOR], 44 colors[next_index][_FILL_COLOR]) 45 46 47 def _get_previous_stroke_color(color): 48 """ Return the previous color pair in the list that shares the same fill 49 as color. """ 50 current_index = _get_current_index(color) 51 if current_index == -1: 52 return "%s,%s" % (color.stroke, color.fill) 53 previous_index = _previous_index(current_index) 54 while (colors[previous_index][_FILL_COLOR] != \ 55 colors[current_index][_FILL_COLOR]): 56 previous_index = _previous_index(previous_index) 57 return "%s,%s" % (colors[previous_index][_STROKE_COLOR], 58 colors[previous_index][_FILL_COLOR]) 59 60 61 def _get_next_fill_color(color): 62 """ Return the next color pair in the list that shares the same stroke 63 as color. """ 64 current_index = _get_current_index(color) 65 if current_index == -1: 66 return "%s,%s" % (color.stroke, color.fill) 67 next_index = _next_index(current_index) 68 while (colors[next_index][_STROKE_COLOR] != \ 69 colors[current_index][_STROKE_COLOR]): 70 next_index = _next_index(next_index) 71 return "%s,%s" % (colors[next_index][_STROKE_COLOR], 72 colors[next_index][_FILL_COLOR]) 73 74 75 def _get_previous_fill_color(color): 76 """ Return the previous color pair in the list that shares the same stroke 77 as color. """ 78 current_index = _get_current_index(color) 79 if current_index == -1: 80 return "%s,%s" % (color.stroke, color.fill) 81 previous_index = _previous_index(current_index) 82 while (colors[previous_index][_STROKE_COLOR] != \ 83 colors[current_index][_STROKE_COLOR]): 84 previous_index = _previous_index(previous_index) 85 return "%s,%s" % (colors[previous_index][_STROKE_COLOR], 86 colors[previous_index][_FILL_COLOR]) 87 88 89 def _next_index(current_index): 90 next_index = current_index + 1 91 if next_index == len(colors): 92 next_index = 0 93 return next_index 94 95 96 def _previous_index(current_index): 97 previous_index = current_index - 1 98 if previous_index < 0: 99 previous_index = len(colors)-1 100 return previous_index 101 102 103 def _get_current_index(color): 104 return colors.index([color.stroke, color.fill]) 105 106 107 _PREVIOUS_FILL_COLOR = 0 108 _NEXT_FILL_COLOR = 1 109 _CURRENT_COLOR = 2 110 _NEXT_STROKE_COLOR = 3 111 _PREVIOUS_STROKE_COLOR = 4 112 28 113 29 114 class EventIcon(gtk.EventBox): 30 115 __gtype_name__ = "SugarEventIcon" … … class ColorPicker(EventIcon): 46 131 __gsignals__ = { 47 132 'color-changed': (gobject.SIGNAL_RUN_FIRST, 48 133 gobject.TYPE_NONE, 49 ([ str]))50 }134 ([object])) 135 } 51 136 52 def __init__(self, xocolor=None):137 def __init__(self, picker): 53 138 EventIcon.__init__(self) 54 self.icon.props.xo_color = xocolor 139 55 140 self.icon.props.icon_name = 'computer-xo' 141 self._picker = picker 142 self._color = None 143 56 144 self.icon.props.pixel_size = style.XLARGE_ICON_SIZE 57 self.connect('button_press_event', self.__pressed_cb)58 145 59 def __pressed_cb(self, button, event): 60 self._set_random_colors() 146 self.connect('button_press_event', self.__pressed_cb, picker) 147 148 def update(self, color): 149 if self._picker == _PREVIOUS_FILL_COLOR: 150 self._color = XoColor(_get_previous_fill_color(color)) 151 elif self._picker == _PREVIOUS_STROKE_COLOR: 152 self._color = XoColor(_get_previous_stroke_color(color)) 153 elif self._picker == _NEXT_FILL_COLOR: 154 self._color = XoColor(_get_next_fill_color(color)) 155 elif self._picker == _NEXT_STROKE_COLOR: 156 self._color = XoColor(_get_next_stroke_color(color)) 157 else: 158 self._color = color 159 self.icon.props.xo_color = self._color 61 160 62 def _set_random_colors(self): 63 xocolor = XoColor() 64 self.icon.props.xo_color = xocolor 65 self.emit('color-changed', xocolor.to_string()) 161 def __pressed_cb(self, button, event, picker): 162 if picker != _CURRENT_COLOR: 163 self.emit('color-changed', self._color) 66 164 67 165 68 166 class AboutMe(SectionView): … … class AboutMe(SectionView): 75 173 self._nick_sid = 0 76 174 self._color_valid = True 77 175 self._nick_valid = True 78 self._color_change_handler = None 79 self._nick_change_handler = None 176 self._handlers = [] 80 177 81 178 self.set_border_width(style.DEFAULT_SPACING * 2) 82 179 self.set_spacing(style.DEFAULT_SPACING) 83 180 self._group = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL) 84 181 85 self._nick_box = gtk.HBox(spacing=style.DEFAULT_SPACING) 86 self._nick_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING) 87 self._nick_entry = None 88 self._nick_alert = None 89 self._setup_nick() 90 182 self._color_label = gtk.HBox(spacing=style.DEFAULT_SPACING) 91 183 self._color_box = gtk.HBox(spacing=style.DEFAULT_SPACING) 92 184 self._color_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING) 93 self._color_picker = None94 185 self._color_alert = None 186 187 self._pickers = { 188 _PREVIOUS_FILL_COLOR: ColorPicker(_PREVIOUS_FILL_COLOR), 189 _NEXT_FILL_COLOR: ColorPicker(_NEXT_FILL_COLOR), 190 _CURRENT_COLOR: ColorPicker(_CURRENT_COLOR), 191 _NEXT_STROKE_COLOR: ColorPicker(_NEXT_STROKE_COLOR), 192 _PREVIOUS_STROKE_COLOR: ColorPicker(_PREVIOUS_STROKE_COLOR) 193 } 194 95 195 self._setup_color() 196 initial_color = XoColor(self._model.get_color_xo()) 197 self._update_pickers(initial_color) 96 198 199 self._nick_box = gtk.HBox(spacing=style.DEFAULT_SPACING) 200 self._nick_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING) 201 self._nick_entry = None 202 self._nick_alert = None 203 self._setup_nick() 97 204 self.setup() 98 205 99 206 def _setup_nick(self): 100 label_entry = gtk.Label(_('Name:'))101 label_entry.modify_fg(gtk.STATE_NORMAL,102 style.COLOR_SELECTION_GREY.get_gdk_color())103 self._group.add_widget(label_entry)104 label_entry.set_alignment(1, 0.5)105 self._nick_box.pack_start(label_entry, expand=False)106 label_entry.show()107 108 207 self._nick_entry = gtk.Entry() 109 208 self._nick_entry.modify_bg(gtk.STATE_INSENSITIVE, 110 209 style.COLOR_WHITE.get_gdk_color()) … … class AboutMe(SectionView): 125 224 self._nick_alert.props.msg = self.restart_msg 126 225 self._nick_alert.show() 127 226 128 self.pack_start(self._nick_box, False) 227 self._center_in_panel = gtk.Alignment(0.5) 228 self._center_in_panel.add(self._nick_box) 229 self.pack_start(self._center_in_panel, False) 129 230 self.pack_start(self._nick_alert_box, False) 130 231 self._nick_box.show() 131 232 self._nick_alert_box.show() 233 self._center_in_panel.show() 132 234 133 235 def _setup_color(self): 134 236 label_color = gtk.Label(_('Click to change your color:')) 135 237 label_color.modify_fg(gtk.STATE_NORMAL, 136 238 style.COLOR_SELECTION_GREY.get_gdk_color()) 137 239 self._group.add_widget(label_color) 138 self._color_ box.pack_start(label_color, expand=False)240 self._color_label.pack_start(label_color, expand=False) 139 241 label_color.show() 140 242 141 self._color_picker = ColorPicker() 142 self._color_box.pack_start(self._color_picker, expand=False) 143 self._color_picker.show() 243 for picker_index in sorted(self._pickers.keys()): 244 if picker_index == _CURRENT_COLOR: 245 left_separator = gtk.SeparatorToolItem() 246 left_separator.show() 247 self._color_box.pack_start(left_separator, expand=False) 248 249 picker = self._pickers[picker_index] 250 picker.show() 251 self._color_box.pack_start(picker, expand=False) 252 253 if picker_index == _CURRENT_COLOR: 254 right_separator = gtk.SeparatorToolItem() 255 right_separator.show() 256 self._color_box.pack_start(right_separator, expand=False) 144 257 145 258 label_color_error = gtk.Label() 146 259 self._group.add_widget(label_color_error) … … class AboutMe(SectionView): 153 266 self._color_alert.props.msg = self.restart_msg 154 267 self._color_alert.show() 155 268 156 self.pack_start(self._color_box, False) 269 self._center_in_panel = gtk.Alignment(0.5) 270 self._center_in_panel.add(self._color_box) 271 self.pack_start(self._color_label, False) 272 self.pack_start(self._center_in_panel, False) 157 273 self.pack_start(self._color_alert_box, False) 274 self._color_label.show() 158 275 self._color_box.show() 159 276 self._color_alert_box.show() 277 self._center_in_panel.show() 160 278 161 279 def setup(self): 162 280 self._nick_entry.set_text(self._model.get_nick()) 163 color = XoColor(self._model.get_color_xo())164 self._color_picker.icon.props.xo_color = color165 166 281 self._color_valid = True 167 282 self._nick_valid = True 168 283 self.needs_restart = False 169 self._nick_change_handler = self._nick_entry.connect( \ 170 'changed', self.__nick_changed_cb) 171 self._color_change_handler = self._color_picker.connect( \ 172 'color-changed', self.__color_changed_cb) 284 285 def connect(widget, signal, cb): 286 self._handlers.append((widget, widget.connect(signal, cb))) 287 288 connect(self._nick_entry, 'changed', self.__nick_changed_cb) 289 for picker in self._pickers.values(): 290 connect(picker, 'color-changed', self.__color_changed_cb) 173 291 174 292 def undo(self): 175 self._color_picker.disconnect(self._color_change_handler)176 self._nick_entry.disconnect(self._nick_change_handler)177 293 self._model.undo() 178 294 self._nick_alert.hide() 179 295 self._color_alert.hide() 180 296 297 def _update_pickers(self, color): 298 for picker in self._pickers.values(): 299 picker.update(color) 300 181 301 def _validate(self): 182 302 if self._nick_valid and self._color_valid: 183 303 self.props.is_valid = True … … class AboutMe(SectionView): 209 329 self._nick_alert.show() 210 330 return False 211 331 212 def __color_changed_cb(self, colorpicker, xocolor):213 self._model.set_color_xo( xocolor)332 def __color_changed_cb(self, colorpicker, color): 333 self._model.set_color_xo(color.to_string()) 214 334 self.needs_restart = True 215 335 self._color_alert.props.msg = self.restart_msg 216 336 self._color_valid = True … … class AboutMe(SectionView): 218 338 219 339 self._validate() 220 340 self._color_alert.show() 341 342 self._update_pickers(color) 343 221 344 return False