Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/deviceicon/clock.py
blob: 12947bf2613785f2adc945726a5a4af36a0f171d (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# Copyright (C) 2008 Martin Dengler
#
# 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

from gettext import gettext as _

import logging
import math
import time

import gconf
import gobject
import gtk
import gtk.gdk
import pango
import pangocairo

import jarabe.frame

from jarabe.frame.frameinvoker import FrameWidgetInvoker
from sugar.graphics import style
from sugar.graphics.palette import Palette
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.xocolor import XoColor



DEFAULT_TEXT_FONT = "Bitstream Vera Sans"
DEFAULT_TIME_FORMAT_12H = _("%l:%M %p")
DEFAULT_TIME_FORMAT_24H = _("%H:%M")
DEFAULT_TIME_FORMAT = DEFAULT_TIME_FORMAT_12H


class TextIcon(gtk.Image):
    def __init__(self, *args, **kwargs):
        gtk.Image.__init__(self, *args, **kwargs)
        client = gconf.client_get_default()
        mycolor = XoColor(client.get_string('/desktop/sugar/user/color'))
        self._fill_rgba = style.Color(mycolor.fill).get_rgba()
        self._stroke_rgba = style.Color(mycolor.stroke).get_rgba()

        self.add_events(gtk.gdk.EXPOSURE_MASK)
        self.connect("expose-event", self.my_expose_event)

        self.add_events(gtk.gdk.VISIBILITY_NOTIFY_MASK)
        self.connect('event', self.__event_cb)

    def __event_cb(self, *args, **kwargs):
        logging.debug("DigitalClock TextIcon event args: %s; kwargs %s" % (args, kwargs))

    def my_expose_event(self, widget_, event):
        cr = self.window.cairo_create()
        x, y, w_, h_ = self.allocation
        cr.translate(x, y)
        self.texticon_draw(cr)

    def draw_rounded_rectangle(self, cr, x, y, width, height, radius=9.0):
        """Draw a rounded rectangle path based on Guillaume Seguin's code"""
        offset = radius / 3
        x0 = x + offset
        y0 = y + offset
        x1 = x + width - offset
        y1 = y + height - offset
        cr.new_path()

        pi = math.pi

        cr.arc      (x0 + radius, y1 - radius, radius, pi / 2, pi)
        cr.line_to  (x0, y0 + radius)
        cr.arc      (x0 + radius, y0 + radius, radius, pi, 3 * pi / 2)
        cr.line_to  (x1 - radius, y0)
        cr.arc      (x1 - radius, y0 + radius, radius, 3 * pi / 2, 2 * pi)
        cr.line_to  (x1, y1 - radius)
        cr.arc      (x1 - radius, y1 - radius, radius, 0, pi / 2)

        cr.close_path()

    def write(self, cr, text, x=0, y=0, font=None, reverse_video=False):
        pcr = pangocairo.CairoContext(cr)
        layout = pcr.create_layout()
        if font is None:
            font = DEFAULT_TEXT_FONT
        layout.set_font_description(pango.FontDescription(font))
        layout.set_markup(text)

        padding = 10
        w, h = layout.get_pixel_size()
        w += 2 * padding

        fg = self._stroke_rgba
        bg = self._fill_rgba

        if reverse_video:
            fg, bg = bg, fg

            cr.save()
            cr.set_source_rgba(*fg)
            self.draw_rounded_rectangle(cr, x, y, w, h)
            cr.fill()
            cr.set_line_width(padding * 0.4)
            cr.set_source_rgba(*bg)
            self.draw_rounded_rectangle(cr, x, y, w, h)
            cr.stroke()
            cr.restore()

        cr.save()

        cr.move_to(x + padding, y)

        pcr.layout_path(layout)
        cr.set_source_rgba(*fg)
        cr.set_line_width(padding * 0.5)
        cr.stroke_preserve()
        cr.set_source_rgba(*bg)
        cr.fill()
        cr.restore()

        self.set_size_request(w, h)

    def texticon_draw(self, cr):
        """
        draw the widget on the provided cairo surface

        Should be overridden by subclasses; example:

        def texticon_draw(self, cr):
            self.write(cr, time.strftime(_("%m/%d %H:%M"), time.localtime()))
        """
        raise Exception("TextIcon.texticon_draw(): subclasses must"
                        " override this method")


class DigitalClock(TextIcon):

    gconf_dir = "/desktop/sugar/desktop/clock"
    gconf_keys = {
        "time_format":   ("strftime_format_string", DEFAULT_TIME_FORMAT_12H),
        "font_size":     ("font_size", 32),
        "reverse_video": ("reverse_video", True),
        }

    def __init__(self, *args, **kwargs):
        TextIcon.__init__(self, *args, **kwargs)

        self.__update_sigid = None
        self._setup()
 
        client = gconf.client_get_default()
        client.add_dir(DigitalClock.gconf_dir, gconf.CLIENT_PRELOAD_ONELEVEL)
        for keyname, default_ in DigitalClock.gconf_keys.values():
            client.notify_add(DigitalClock.gconf_dir + "/" + keyname,
                              self.__gconf_changed_cb)

    def _setup(self):
        client = gconf.client_get_default()
        for attr, (keyname, default) in DigitalClock.gconf_keys.iteritems():
            keypath = DigitalClock.gconf_dir + "/" + keyname
            if client.get(keypath) is None:
                client.set_value(keypath, default)
            setattr(self, attr, client.get_value(keypath))

        if "%S" in self.time_format:
            self.update_interval = 1000
        else:
            self.update_interval = 60000

    def __gconf_changed_cb(self, *args, **kwargs):
        self._setup()

    def texticon_draw(self, cr):
        time_string = "..."
        font_string = DEFAULT_TEXT_FONT + " 32"
        reverse_video = True
        try:
            time_string = time.strftime(self.time_format, time.localtime())
            font_string = "%s %s" % (DEFAULT_TEXT_FONT, self.font_size)
            reverse_video = self.reverse_video
        except Exception, msg:
            logging.debug("DigitalClock: failed trying to use"
                          " time/font string/reverse video settings:"
                          " %s/%s/%s: %s"
                          % (time_string, font_string, reverse_video, msg))
        time_string = time_string.strip()
        self.write(cr,
                   time_string,
                   font=font_string,
                   reverse_video=reverse_video)

    def my_expose_event(self, widget, event):
        TextIcon.my_expose_event(self, widget, event)
        if jarabe.frame.get_view().visible and self.__update_sigid is None:
            delay = self.update_interval
            self.__update_sigid = gobject.timeout_add(delay,
                                                      self.__update_clock)

    def __update_clock(self):
        if jarabe.frame.get_view().visible:
            self.window.invalidate_rect(self.allocation, True)
        else:
            self.__update_sigid = None
        return jarabe.frame.get_view().visible

    def set_time_format(self, time_str):
        self.time_format = time_str
        client = gconf.client_get_default()
        client.set_value(DigitalClock.gconf_dir + "/" +
                         DigitalClock.gconf_keys["time_format"][0],
                         time_str)
        if jarabe.frame.get_view().visible:
            self.window.invalidate_rect(self.allocation, True)


class DigitalClockTrayItem(ToolButton):

    FRAME_POSITION_RELATIVE = 50 # all the way on the right

    def __init__(self):
        ToolButton.__init__(self)

        self.clock = DigitalClock()

        self.hbox = gtk.HBox()
        self.hbox.add(self.clock)
        self.set_icon_widget(self.hbox)
        self.hbox.show_all()

        self.set_palette_invoker(FrameWidgetInvoker(self))
        self.palette = DigitalClockPalette(_(""), self.clock)
        self.palette.set_group_id("frame")
        self.clock.connect("expose-event", self.palette.update)


class DigitalClockPalette(Palette):

    def __init__(self, primary_text, model):
        Palette.__init__(self, primary_text)

        self.model = model
        vbox = gtk.VBox()

        self.button_hidden = gtk.RadioButton(group=None, label="hidden")

        self.button_12h = gtk.RadioButton(group=self.button_hidden,
                                          label=_("12 Hour"))
        self.button_12h.show()

        self.button_24h = gtk.RadioButton(group=self.button_hidden,
                                          label=_("24 Hour"))
        self.button_24h.show()

        self.buttons = (self.button_hidden, self.button_12h, self.button_24h)
        self.button_signals = {}

        for button in self.buttons:
            vbox.pack_start(button)

        vbox.show()
        self.set_content(vbox)

        #FIXME: track model state?
        self.initialize_toggle_states()

        for button in self.buttons:
            self.button_signals[button] = button.connect(
                "toggled", self.__hour_toggled_cb)

    def update(self, *args_, **kwargs_):
        local_time = time.localtime()
        self.props.primary_text = time.strftime(_("%x"), local_time)

    def initialize_toggle_states(self):
        time_format = self.model.time_format
        button_to_activate = None

        if time_format == DEFAULT_TIME_FORMAT_24H:
            button_to_activate = self.button_24h
        elif time_format == DEFAULT_TIME_FORMAT_12H:
            button_to_activate = self.button_12h
        else:
            button_to_activate = self.button_hidden

        if not button_to_activate.get_active():
            for button, signal_id in self.button_signals.iteritems():
                button.signal_handler_block(signal_id)
            for button in self.buttons:
                if button.get_active() != button == button_to_activate:
                    button.set_active(button == button_to_activate)
            for button, signal_id in self.button_signals.iteritems():
                button.signal_handler_unblock(signal_id)

    def __hour_setting_updated(self):
        time_format = self.model.time_format
        if self.button_24h.get_active():
            time_format = DEFAULT_TIME_FORMAT_24H
        elif self.button_12h.get_active():
            time_format = DEFAULT_TIME_FORMAT_12H

        logging.debug("clock: __h_s_u: time_format %s --> %s" % (self.model.time_format, time_format))
        logging.debug("clock: __h_s_u: active is %s/%s/%s" % (
                self.button_hidden.get_active(),
                self.button_12h.get_active(),
                self.button_24h.get_active()))

        if time_format != self.model.time_format:
            self.model.set_time_format(time_format)

    def __hour_toggled_cb(self, widget):
        if widget.get_active():
            self.__hour_setting_updated()



def setup(tray):
    tray.add_device(DigitalClockTrayItem())