From 34e9431889ab0ead43abec2732fd14b972ce71ae Mon Sep 17 00:00:00 2001
From: Walter Bender <walter@sugarlabs.org>
Date: Wed, 26 May 2010 19:36:27 -0400
Subject: [PATCH] code cleanup as per Sascha's feedback
---
extensions/cpsection/touchpad/model.py | 18 ++------
extensions/cpsection/touchpad/view.py | 75 ++++++++++++--------------------
2 files changed, 33 insertions(+), 60 deletions(-)
diff --git a/extensions/cpsection/touchpad/model.py b/extensions/cpsection/touchpad/model.py
index 4e29651..b02b2cc 100644
a
|
b
|
from gettext import gettext as _ |
18 | 18 | from os import system, path, remove |
19 | 19 | import gconf |
20 | 20 | |
21 | | _CAPACITIVE = 0 |
22 | | _RESISTIVE = 1 |
23 | 21 | _flag_path = '/home/olpc/.olpc-pentablet-mode' |
24 | 22 | _node_path = '/sys/devices/platform/i8042/serio1/ptmode' |
25 | 23 | |
… |
… |
def get_touchpad(): |
31 | 29 | _file_handle.close() |
32 | 30 | |
33 | 31 | if _text[0] == '1': |
34 | | return _RESISTIVE |
| 32 | return 'resistive' |
35 | 33 | else: |
36 | | return _CAPACITIVE |
| 34 | return 'capacitive' |
37 | 35 | |
38 | 36 | def set_touchpad(touchpad): |
39 | 37 | """Set the touchpad mode.""" |
40 | | if touchpad == _CAPACITIVE: |
| 38 | if touchpad == 'capacitive': |
41 | 39 | if path.exists(_flag_path): |
42 | 40 | remove(_flag_path) |
43 | 41 | system("echo 0 > %s" % (_node_path)) |
44 | 42 | else: |
45 | | system("touch %s" % (_flag_path)) |
| 43 | _file_handle = open(_flag_path, "w") |
| 44 | _file_handle.close() |
46 | 45 | system("echo 1 > %s" % (_node_path)) |
47 | 46 | return |
48 | 47 | |
49 | | def print_touchpad(): |
50 | | """Print the future touchpad mode.""" |
51 | | if get_touchpad == _CAPACITIVE: |
52 | | print _('Touchpad set to finger mode.') |
53 | | else: |
54 | | print _('Touchpad set to stylus mode.') |
55 | | |
56 | 48 | def get_color_xo(): |
57 | 49 | """Get xo color""" |
58 | 50 | client = gconf.client_get_default() |
diff --git a/extensions/cpsection/touchpad/view.py b/extensions/cpsection/touchpad/view.py
index 19fb686..7339a43 100644
a
|
b
|
from sugar.graphics import style |
24 | 24 | from sugar.graphics.xocolor import XoColor |
25 | 25 | |
26 | 26 | from jarabe.controlpanel.sectionview import SectionView |
27 | | from jarabe.controlpanel.inlinealert import InlineAlert |
28 | | |
29 | | _CAPACITIVE = 0 |
30 | | _RESISTIVE = 1 |
31 | 27 | |
32 | 28 | class TouchpadEventIcon(gtk.EventBox): |
33 | | """A subclass of the Sugar Event Icon""" |
| 29 | """Clickable icon used for capacitive and resistive buttons""" |
34 | 30 | __gtype_name__ = "SugarEventIcon" |
35 | 31 | |
36 | 32 | def __init__(self, **kwargs): |
… |
… |
class TouchpadEventIcon(gtk.EventBox): |
46 | 42 | self.add(self.icon) |
47 | 43 | self.icon.show() |
48 | 44 | |
49 | | class TouchpadPicker(TouchpadEventIcon): |
50 | | """A class for the touchpad selection buttons""" |
| 45 | class _TouchpadPicker(TouchpadEventIcon): |
| 46 | """The touchpad selection buttons""" |
51 | 47 | __gsignals__ = { |
52 | 48 | 'touchpad-changed': (gobject.SIGNAL_RUN_FIRST, |
53 | 49 | gobject.TYPE_NONE, |
… |
… |
class TouchpadPicker(TouchpadEventIcon): |
58 | 54 | """Create icons for the touchpad mode buttons.""" |
59 | 55 | TouchpadEventIcon.__init__(self) |
60 | 56 | |
61 | | if touchpad_mode == _CAPACITIVE: |
| 57 | if touchpad_mode == 'capacitive': |
62 | 58 | self.icon.props.icon_name = 'capacitive' |
63 | 59 | else: |
64 | 60 | self.icon.props.icon_name = 'resistive' |
… |
… |
class TouchpadPicker(TouchpadEventIcon): |
70 | 66 | |
71 | 67 | def update(self, touchpad, xo_color): |
72 | 68 | """Update the button states.""" |
73 | | if self._touchpad_mode == _CAPACITIVE: |
| 69 | if self._touchpad_mode == 'capacitive': |
74 | 70 | self.icon.props.icon_name = 'capacitive' |
75 | | if touchpad == _CAPACITIVE: |
| 71 | if touchpad == 'capacitive': |
76 | 72 | self.icon.props.xo_color = xo_color |
77 | 73 | else: |
78 | 74 | self.icon.props.xo_color = XoColor('#666666,#FFFFFF') |
79 | | elif self._touchpad_mode == _RESISTIVE: |
| 75 | elif self._touchpad_mode == 'resistive': |
80 | 76 | self.icon.props.icon_name = 'resistive' |
81 | | if touchpad == _RESISTIVE: |
| 77 | if touchpad == 'resistive': |
82 | 78 | self.icon.props.xo_color = xo_color |
83 | 79 | else: |
84 | 80 | self.icon.props.xo_color = XoColor('#666666,#FFFFFF') |
… |
… |
class TouchpadPicker(TouchpadEventIcon): |
88 | 84 | self.emit('touchpad-changed', touchpad_mode) |
89 | 85 | |
90 | 86 | class Touchpad(SectionView): |
91 | | """A class for the touchpad selection panel""" |
| 87 | """The touchpad selection panel""" |
92 | 88 | |
93 | 89 | def __init__(self, model, alerts): |
94 | | """ Create the touchpad panel.""" |
| 90 | """ Create the touchpad selection panel.""" |
95 | 91 | SectionView.__init__(self) |
96 | 92 | |
97 | 93 | self._model = model |
… |
… |
class Touchpad(SectionView): |
102 | 98 | self.set_spacing(style.DEFAULT_SPACING) |
103 | 99 | self._group = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL) |
104 | 100 | |
105 | | self._touchpad_label = gtk.HBox(spacing=style.DEFAULT_SPACING) |
106 | | self._touchpad_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
107 | | self._touchpad_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
108 | | self._touchpad_alert = None |
| 101 | self._label_box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
| 102 | self._box = gtk.HBox(spacing=style.DEFAULT_SPACING) |
109 | 103 | |
110 | 104 | self._pickers = { |
111 | | _CAPACITIVE: TouchpadPicker(_CAPACITIVE), |
112 | | _RESISTIVE: TouchpadPicker(_RESISTIVE)} |
| 105 | 'capacitive': _TouchpadPicker('capacitive'), |
| 106 | 'resistive': _TouchpadPicker('resistive')} |
113 | 107 | |
114 | | self._setup_touchpad() |
| 108 | self._setup_widgets() |
115 | 109 | self._initial_value = self._model.get_touchpad() |
116 | 110 | self._update_pickers(self._initial_value) |
117 | 111 | |
118 | 112 | self.setup() |
119 | 113 | |
120 | | def _setup_touchpad(self): |
121 | | """Layout the panel: label, buttons, alert""" |
122 | | label_touchpad = gtk.Label(_('Click to change your touchpad:')) |
123 | | label_touchpad.modify_fg(gtk.STATE_NORMAL, |
| 114 | def _setup_widgets(self): |
| 115 | """Layout the panel: label, buttons""" |
| 116 | label = gtk.Label(_('Click to change your touchpad:')) |
| 117 | label.modify_fg(gtk.STATE_NORMAL, |
124 | 118 | style.COLOR_SELECTION_GREY.get_gdk_color()) |
125 | | self._group.add_widget(label_touchpad) |
126 | | self._touchpad_label.pack_start(label_touchpad, expand=False) |
127 | | label_touchpad.show() |
| 119 | self._group.add_widget(label) |
| 120 | self._label_box.pack_start(label, expand=False) |
| 121 | label.show() |
128 | 122 | |
129 | 123 | for touchpad_mode in sorted(self._pickers.keys()): |
130 | 124 | picker = self._pickers[touchpad_mode] |
131 | 125 | picker.show() |
132 | | self._touchpad_box.pack_start(picker, expand=False) |
133 | | |
134 | | label_touchpad_error = gtk.Label() |
135 | | self._group.add_widget(label_touchpad_error) |
136 | | self._touchpad_alert_box.pack_start(label_touchpad_error, expand=False) |
137 | | label_touchpad_error.show() |
138 | | |
139 | | self._touchpad_alert = InlineAlert() |
140 | | self._touchpad_alert_box.pack_start(self._touchpad_alert) |
| 126 | self._box.pack_start(picker, expand=False) |
141 | 127 | |
142 | 128 | self._center_in_panel = gtk.Alignment(0.5) |
143 | | self._center_in_panel.add(self._touchpad_box) |
144 | | self.pack_start(self._touchpad_label, False) |
145 | | self.pack_start(self._center_in_panel, False) |
146 | | self.pack_start(self._touchpad_alert_box, False) |
147 | | self._touchpad_label.show() |
148 | | self._touchpad_box.show() |
149 | | self._touchpad_alert_box.show() |
| 129 | self._center_in_panel.add(self._box) |
| 130 | self.pack_start(self._label_box, False) |
| 131 | self.pack_start(self._center_in_panel, False) |
| 132 | self._label_box.show() |
| 133 | self._box.show() |
150 | 134 | self._center_in_panel.show() |
151 | 135 | |
152 | 136 | def setup(self): |
… |
… |
class Touchpad(SectionView): |
161 | 145 | |
162 | 146 | def undo(self): |
163 | 147 | """Undo any changes.""" |
164 | | for widget, handler in self._handlers: |
165 | | widget.disconnect(handler) |
166 | 148 | self._model.set_touchpad(self._initial_value) |
167 | | self._touchpad_alert.hide() |
168 | 149 | |
169 | 150 | def _update_pickers(self, touchpad): |
170 | 151 | """Update the buttons to reflect selection""" |