Ticket #1592: 0001-added-prev-next-fill-and-stroke-color-to-selector.patch

File 0001-added-prev-next-fill-and-stroke-color-to-selector.patch, 12.0 KB (added by walter, 14 years ago)

cleaned up version as per feedback

  • extensions/cpsection/aboutme/view.py

    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  
    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, get_next_stroke_color, \
     25    get_prev_stroke_color, get_next_fill_color, get_prev_fill_color
    2426
    2527from jarabe.controlpanel.sectionview import SectionView
    2628from jarabe.controlpanel.inlinealert import InlineAlert
    2729
     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
    2837class EventIcon(gtk.EventBox):
    29     __gtype_name__ = "SugarEventIcon"   
    30     def __init__(self, **kwargs):         
     38    __gtype_name__ = "SugarEventIcon"
     39
     40    def __init__(self, **kwargs):
    3141        gtk.EventBox.__init__(self)
    3242
    33         self.icon = Icon(pixel_size = style.XLARGE_ICON_SIZE, **kwargs)
    34        
     43        self.icon = Icon(pixel_size=style.XLARGE_ICON_SIZE, **kwargs)
     44
    3545        self.set_visible_window(False)
    3646        self.set_app_paintable(True)
    3747        self.set_events(gtk.gdk.BUTTON_PRESS_MASK)
    class EventIcon(gtk.EventBox): 
    3949        self.add(self.icon)
    4050        self.icon.show()
    4151
     52
    4253class ColorPicker(EventIcon):
    4354    __gsignals__ = {
    4455        'color-changed': (gobject.SIGNAL_RUN_FIRST,
    4556                          gobject.TYPE_NONE,
    46                           ([str]))
    47     }
    48     def __init__(self, xocolor=None):
     57                          ([object]))
     58        }
     59
     60    def __init__(self, picker):
    4961        EventIcon.__init__(self)
    50         self.icon.props.xo_color = xocolor
     62
    5163        self.icon.props.icon_name = 'computer-xo'
     64        self._picker = picker
     65        self._color = None
     66
    5267        self.icon.props.pixel_size = style.XLARGE_ICON_SIZE
    53         self.connect('button_press_event', self.__pressed_cb)
    5468
    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)
    5787
    58     def _set_random_colors(self):
    59         xocolor = XoColor()
    60         self.icon.props.xo_color = xocolor
    61         self.emit('color-changed', xocolor.to_string())
    6288
    6389class AboutMe(SectionView):
     90
    6491    def __init__(self, model, alerts):
    6592        SectionView.__init__(self)
    6693
    class AboutMe(SectionView): 
    6996        self._nick_sid = 0
    7097        self._color_valid = True
    7198        self._nick_valid = True
    72         self._color_change_handler = None
    73         self._nick_change_handler = None
     99        self._handlers = []
    74100
    75101        self.set_border_width(style.DEFAULT_SPACING * 2)
    76102        self.set_spacing(style.DEFAULT_SPACING)
    77103        self._group = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
    78104
     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
    79122        self._nick_box = gtk.HBox(spacing=style.DEFAULT_SPACING)
    80123        self._nick_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING)
    81124        self._nick_entry = None
    82125        self._nick_alert = None
    83126        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 
    91127        self.setup()
    92128
    93129    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,
    104132                                   style.COLOR_WHITE.get_gdk_color())
    105         self._nick_entry.modify_base(gtk.STATE_INSENSITIVE, 
     133        self._nick_entry.modify_base(gtk.STATE_INSENSITIVE,
    106134                                     style.COLOR_WHITE.get_gdk_color())
    107135        self._nick_entry.set_width_chars(25)
    108136        self._nick_box.pack_start(self._nick_entry, expand=False)
    109         self._nick_entry.show()       
     137        self._nick_entry.show()
    110138
    111139        label_entry_error = gtk.Label()
    112140        self._group.add_widget(label_entry_error)
    class AboutMe(SectionView): 
    119147            self._nick_alert.props.msg = self.restart_msg
    120148            self._nick_alert.show()
    121149
    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)
    123153        self.pack_start(self._nick_alert_box, False)
    124154        self._nick_box.show()
    125155        self._nick_alert_box.show()
    126    
    127     def _setup_color(self):               
     156        self._center_in_panel.show()
     157
     158    def _setup_color(self):
    128159        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,
    130161                              style.COLOR_SELECTION_GREY.get_gdk_color())
    131162        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)
    133164        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)
    138180
    139181        label_color_error = gtk.Label()
    140182        self._group.add_widget(label_color_error)
    class AboutMe(SectionView): 
    147189            self._color_alert.props.msg = self.restart_msg
    148190            self._color_alert.show()
    149191
    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()
    152198        self._color_box.show()
    153199        self._color_alert_box.show()
    154    
     200        self._center_in_panel.show()
     201
    155202    def setup(self):
    156203        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 
    160204        self._color_valid = True
    161205        self._nick_valid = True
    162206        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)
    167214
    168215    def undo(self):
    169         self._color_picker.disconnect(self._color_change_handler)
    170         self._nick_entry.disconnect(self._nick_change_handler)
    171216        self._model.undo()
    172217        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)
    174223
    175224    def _validate(self):
    176225        if self._nick_valid and self._color_valid:
    class AboutMe(SectionView): 
    178227        else:
    179228            self.props.is_valid = False
    180229
    181     def __nick_changed_cb(self, widget, data=None):       
     230    def __nick_changed_cb(self, widget, data=None):
    182231        if self._nick_sid:
    183232            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,
    185234                                             self.__nick_timeout_cb, widget)
    186235
    187     def __nick_timeout_cb(self, widget):       
     236    def __nick_timeout_cb(self, widget):
    188237        self._nick_sid = 0
    189238
    190239        if widget.get_text() == self._model.get_nick():
    class AboutMe(SectionView): 
    193242            self._model.set_nick(widget.get_text())
    194243        except ValueError, detail:
    195244            self._nick_alert.props.msg = detail
    196             self._nick_valid = False           
     245            self._nick_valid = False
    197246        else:
    198247            self._nick_alert.props.msg = self.restart_msg
    199             self._nick_valid = True 
     248            self._nick_valid = True
    200249            self.needs_restart = True
    201250            self.restart_alerts.append('nick')
    202251        self._validate()
    203252        self._nick_alert.show()
    204253        return False
    205254
    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())
    208257        self.needs_restart = True
    209258        self._color_alert.props.msg = self.restart_msg
    210259        self._color_valid = True
    class AboutMe(SectionView): 
    212261
    213262        self._validate()
    214263        self._color_alert.show()
     264
     265        self._update_pickers(color)
     266
    215267        return False