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, 14 years ago)

latest version of cpsection/aboutme/view.py

  • 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  
    11# Copyright (C) 2008, OLPC
     2# Copyright (C) 2010, Sugar Labs
    23#
    34# This program is free software; you can redistribute it and/or modify
    45# it under the terms of the GNU General Public License as published by
    from gettext import gettext as _ 
    2021
    2122from sugar.graphics.icon import Icon
    2223from sugar.graphics import style
    23 from sugar.graphics.xocolor import XoColor
     24from sugar.graphics.xocolor import XoColor, colors
    2425
    2526from jarabe.controlpanel.sectionview import SectionView
    2627from jarabe.controlpanel.inlinealert import InlineAlert
    2728
     29_STROKE_COLOR = 0
     30_FILL_COLOR = 1
     31
     32
     33def _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
     47def _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
     61def _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
     75def _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
     89def _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
     96def _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
     103def _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
    28113
    29114class EventIcon(gtk.EventBox):
    30115    __gtype_name__ = "SugarEventIcon"
    class ColorPicker(EventIcon): 
    46131    __gsignals__ = {
    47132        'color-changed': (gobject.SIGNAL_RUN_FIRST,
    48133                          gobject.TYPE_NONE,
    49                           ([str]))
    50     }
     134                          ([object]))
     135        }
    51136
    52     def __init__(self, xocolor=None):
     137    def __init__(self, picker):
    53138        EventIcon.__init__(self)
    54         self.icon.props.xo_color = xocolor
     139
    55140        self.icon.props.icon_name = 'computer-xo'
     141        self._picker = picker
     142        self._color = None
     143
    56144        self.icon.props.pixel_size = style.XLARGE_ICON_SIZE
    57         self.connect('button_press_event', self.__pressed_cb)
    58145
    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
    61160
    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)
    66164
    67165
    68166class AboutMe(SectionView):
    class AboutMe(SectionView): 
    75173        self._nick_sid = 0
    76174        self._color_valid = True
    77175        self._nick_valid = True
    78         self._color_change_handler = None
    79         self._nick_change_handler = None
     176        self._handlers = []
    80177
    81178        self.set_border_width(style.DEFAULT_SPACING * 2)
    82179        self.set_spacing(style.DEFAULT_SPACING)
    83180        self._group = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
    84181
    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)
    91183        self._color_box = gtk.HBox(spacing=style.DEFAULT_SPACING)
    92184        self._color_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING)
    93         self._color_picker = None
    94185        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
    95195        self._setup_color()
     196        initial_color = XoColor(self._model.get_color_xo())
     197        self._update_pickers(initial_color)
    96198
     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()
    97204        self.setup()
    98205
    99206    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 
    108207        self._nick_entry = gtk.Entry()
    109208        self._nick_entry.modify_bg(gtk.STATE_INSENSITIVE,
    110209                                   style.COLOR_WHITE.get_gdk_color())
    class AboutMe(SectionView): 
    125224            self._nick_alert.props.msg = self.restart_msg
    126225            self._nick_alert.show()
    127226
    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)
    129230        self.pack_start(self._nick_alert_box, False)
    130231        self._nick_box.show()
    131232        self._nick_alert_box.show()
     233        self._center_in_panel.show()
    132234
    133235    def _setup_color(self):
    134236        label_color = gtk.Label(_('Click to change your color:'))
    135237        label_color.modify_fg(gtk.STATE_NORMAL,
    136238                              style.COLOR_SELECTION_GREY.get_gdk_color())
    137239        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)
    139241        label_color.show()
    140242
    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)
    144257
    145258        label_color_error = gtk.Label()
    146259        self._group.add_widget(label_color_error)
    class AboutMe(SectionView): 
    153266            self._color_alert.props.msg = self.restart_msg
    154267            self._color_alert.show()
    155268
    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)
    157273        self.pack_start(self._color_alert_box, False)
     274        self._color_label.show()
    158275        self._color_box.show()
    159276        self._color_alert_box.show()
     277        self._center_in_panel.show()
    160278
    161279    def setup(self):
    162280        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 = color
    165 
    166281        self._color_valid = True
    167282        self._nick_valid = True
    168283        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)
    173291
    174292    def undo(self):
    175         self._color_picker.disconnect(self._color_change_handler)
    176         self._nick_entry.disconnect(self._nick_change_handler)
    177293        self._model.undo()
    178294        self._nick_alert.hide()
    179295        self._color_alert.hide()
    180296
     297    def _update_pickers(self, color):
     298        for picker in self._pickers.values():
     299            picker.update(color)
     300
    181301    def _validate(self):
    182302        if self._nick_valid and self._color_valid:
    183303            self.props.is_valid = True
    class AboutMe(SectionView): 
    209329        self._nick_alert.show()
    210330        return False
    211331
    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())
    214334        self.needs_restart = True
    215335        self._color_alert.props.msg = self.restart_msg
    216336        self._color_valid = True
    class AboutMe(SectionView): 
    218338
    219339        self._validate()
    220340        self._color_alert.show()
     341
     342        self._update_pickers(color)
     343
    221344        return False