Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/font/view.py
blob: 02ef45d8912874bb2f1d6485c9a2fc317ff70729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#Copyright (C) 2008, OLPC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

import gtk
import gobject
import pango
import pygtk
from gettext import gettext as _

from sugar.graphics import style

from jarabe.controlpanel.sectionview import SectionView
from jarabe.controlpanel.inlinealert import InlineAlert

_Small = _('8')
_Default =_('10')
_Large = _('24')
_seconds_label = _('%s ')
_MAX_DELAY = 1

class Font(SectionView):
    def __init__(self, model, alerts):
        SectionView.__init__(self)

        self._model = model
        self._font_size_sid = 0
        self._font_size_change_is_valid = True
        self._font_size_change_handler = None
        self._makechange = True
        self.restart_alerts = alerts

        self.set_border_width(style.DEFAULT_SPACING * 2)
        self.set_spacing(style.DEFAULT_SPACING)
        self._group = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
 
        separator = gtk.HSeparator()
        self.pack_start(separator, expand=False)
        separator.show()

        label_activation = gtk.Label(_('Font'))
        label_activation.set_alignment(0, 0)
        self.pack_start(label_activation, expand=False)
        label_activation.show()


        self._box_sliders = gtk.VBox()
        self._box_sliders.set_border_width(style.DEFAULT_SPACING)
        self._box_sliders.set_spacing(style.DEFAULT_SPACING * 2)

        self._font_size_slider = None
        self._font_size_alert = None
        self._setup_font()


        self.pack_start(self._box_sliders, expand=False)
        self._box_sliders.show()

             

        self.box_prevw = gtk.HBox()
        self.box_prevw.set_border_width(style.DEFAULT_SPACING)
        self.box_prevw.set_spacing(style.DEFAULT_SPACING * 2)

        self._setup_preview()


        self.pack_start(self.box_prevw, expand =False)
        self.box_prevw.show()
        self.setup()

    def _setup_font(self):

        """Here we create a HBox and in that we pack both the font size label and the scale with the marks as small, default and large """

        box_size = gtk.HBox(spacing=style.DEFAULT_SPACING)
        label_size = gtk.Label(_('Font size'))
        self._group.add_widget(label_size)
        label_size.set_alignment(1, 0.35)
        label_size.modify_fg(gtk.STATE_NORMAL, 
                             style.COLOR_SELECTION_GREY.get_gdk_color())
        box_size.pack_start(label_size, expand=False)
        self._group.add_widget(label_size)
        label_size.show()
        


        adj = gtk.Adjustment(value=1, lower=8, upper=24, step_incr=1, page_incr=100, page_size=0)
        self._font_size_slider = gtk.HScale(adj)
        self._font_size_slider.set_digits(0)
        self._font_size_slider.connect('value_changed', self._font_size_delay_format_cb,gtk.UPDATE_CONTINUOUS )

        self._font_size_slider.connect('value_changed', self.cb_font_size,gtk.UPDATE_CONTINUOUS )

        self._font_size_slider.add_mark(0.0,gtk.POS_BOTTOM,_('Small'))
        self._font_size_slider.add_mark(10.0,gtk.POS_BOTTOM,_('Default'))
        self._font_size_slider.add_mark(24.0,gtk.POS_BOTTOM,_('Large'))

        box_size.pack_start(self._font_size_slider)
        self._font_size_slider.show()
        self._box_sliders.pack_start(box_size, expand =False)
        box_size.show()



        self._font_size_alert = InlineAlert()
        label_size_error = gtk.Label()
        self._group.add_widget(label_size_error)

        delay_alert_box = gtk.HBox(spacing=style.DEFAULT_SPACING)
        delay_alert_box.pack_start(label_size_error, expand=False)
        label_size_error.show()
        delay_alert_box.pack_start(self._font_size_alert, expand=False)
        self._box_sliders.pack_start(delay_alert_box,expand=False)
        delay_alert_box.show()



        if 'font_size' in self.restart_alerts:
             self._font_size_alert.props.msg = self.restart_msg
             self._font_size_alert.show()

    def _setup_preview(self):

        """ Here we create another HBox within which we have font preview label and the text """
        box_preview=gtk.HBox(spacing=style.DEFAULT_SPACING)       
        label_activation = gtk.Label(_('Font preview:'))
        label_activation.set_alignment(1, 0.35)
        label_activation.modify_fg(gtk.STATE_NORMAL,style.COLOR_SELECTION_GREY.get_gdk_color())
        box_preview.pack_start(label_activation, expand=False)
        self._group.add_widget(label_activation)
        label_activation.show()


        self.label_font_activation = gtk.Label(_('The quick brown fox jumps over the lazy dog'))
            
        self.label_font_activation.set_alignment(1.5, 0.35)
        box_preview.pack_start(self.label_font_activation)
        

      

        self.label_font_activation.show()
       
        self.box_prevw.pack_start(box_preview, expand=False)
        box_preview.show()
      

 
    def setup(self):

        """ now we get the value of the slider with a restriction that it has to be integer and connect the handler to the _font_size_changed_cb(which will show the values on the scale at a point on the scale"""
        self._font_size_slider.set_value(self._model.get_font_size())
        self._font_size_change_is_valid=True
        self.needs_restart=False
        self._font_size_change_handler = self._font_size_slider.connect('value-changed', self._font_size_changed_cb)


    def undo(self):
        self._font_size_slider.disconnect(self._font_size_change_handler)
        self._model.undo()
        self._font_size_alert.hide()

    def _validate(self):
        if self._font_size_change_is_valid:
             self.props.is_valid =True
        else:
             self.props.is_valid = False


    def cb_font_size(self,label_font_activation,pos):
        self.label_font_activation.set_value_pos(pos)
 
    def _font_size_changed_cb(self, scale ,data=None):

        """ It will remove the event of restart if the value remains same and if different value then _font_size_sid will be reset"""
        if self._font_size_sid:
             gobject.source_remove(self._font_size_sid)
        self._font_size_sid = gobject.timeout_add(self._APPLY_TIMEOUT,self._font_size_timeout_cb, scale)
   
  


    def _font_size_timeout_cb(self,scale):

        """text will remain unchanged if the scale value and the model integer value remains same else it will show the restart message"""
        self._font_size_sid = 0
        if scale.get_value() == self._model.get_font_size():
             return
        try:
             self._model.set_font_size(scale.get_value())
        except ValueError, detail:
             self._font_size_alert.props.msg = detail
             self._font_size_change_is_valid = False

        else:
             self._font_size_alert.props.msg = self.restart_msg
             self._font_size_change_is_valid = True
             self.needs_restart=True
             self.restart_alerts.append('font_size')
        self._validate()
        self._font_size_alert.show()
        return False

    def _font_size_delay_format_cb(self, scale, value):
        """ It's value will be returned to setup(self) to place the slider position on the value returned by this"""
        if value == _MAX_DELAY:
             return _large
        elif value == 0:
             return _small
        elif value == 500:
             return _default
        else:
             return _seconds_label % (value / _MAX_DELAY)