Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mmm_modules/timer.py
blob: 0a4c104392f39d3a18aacbf236c7d02e4776780a (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
# Copyright 2007 World Wide Workshop Foundation
#
# 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
#
# If you find this activity useful or end up using parts of it in one of your
# own creations we would love to hear from you at info@WorldWideWorkshop.org !
#

from gi.repository import Gtk, GObject, Pango

import os
from time import time

cwd = os.path.normpath(os.path.join(os.path.split(__file__)[0], '..'))

if os.path.exists(os.path.join(cwd, 'mamamedia_icons')):
    # Local, no shared code, version
    mmmpath = cwd
    iconpath = os.path.join(mmmpath, 'mamamedia_icons')
else:
    propfile = os.path.expanduser("~/.sugar/default/org.worldwideworkshop.olpc.MMMPath")

    if os.path.exists(propfile):
        mmmpath = file(propfile, 'rb').read()
    else:
        mmmpath = cwd
    iconpath = os.path.join(mmmpath, 'icons')

from utils import load_image

class TimerWidget (Gtk.HBox):
    __gsignals__ = {'timer_toggle' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (bool,)),}
    def __init__ (self, bg_color="#DD4040", fg_color="#4444FF", lbl_color="#DD4040", can_stop=True):
        Gtk.HBox.__init__(self)
        self.counter = Gtk.EventBox()
        self.counter.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse(bg_color))
        self.counter.set_size_request(120, -1)
        hb = Gtk.HBox()
        self.counter.add(hb)
        self.lbl_time = Gtk.Label()
        self.lbl_time.modify_fg(Gtk.StateType.NORMAL, Gdk.color_parse(lbl_color))
        self.pack_start(self.lbl_time, False, True, 0)
        self.time_label = Gtk.Label("--:--")
        self.time_label.modify_fg(Gtk.StateType.NORMAL, Gdk.color_parse(fg_color))
        hb.pack_start(self.time_label, False, False, 5)
        self.prepare_icons()
        self.icon = Gtk.Image()
        self.icon.set_from_pixbuf(self.icons[1])
        hb.pack_end(self.icon, False, False, 5)
        self.pack_start(self.counter, False, True, 0)
        self.connect("button-press-event", self.process_click)
        self.start_time = 0
        self.timer_id = None
        self.finished = False
        self.can_stop = can_stop

    def set_label (self, label):
        self.lbl_time.set_label(label)

    def prepare_icons (self):
        self.icons = []
        self.icons.append(load_image(os.path.join(iconpath,"circle-x.svg")))
        self.icons.append(load_image(os.path.join(iconpath,"circle-check.svg")))


    def set_can_stop (self, can_stop):
        self.can_stop = can_stop
    
    def modify_bg(self, state, color):
        self.foreach(lambda x: x is not self.counter and x.modify_bg(state, color))

    def reset (self, auto_start=True):
        self.set_sensitive(True)
        self.finished = False
        self.stop()
        self.start_time = 0
        if auto_start:
            self.start()

    def start (self):
        if self.finished:
            return
        self.set_sensitive(True)
        self.icon.set_from_pixbuf(self.icons[0])
        if self.start_time is None:
            self.start_time = time()
        else:
            self.start_time = time() - self.start_time
        self.do_tick()
        if self.timer_id is None:
            self.timer_id = GObject.timeout_add(1000, self.do_tick)
        self.emit('timer_toggle', True)

    def stop (self, finished=False):
        if not self.can_stop and not finished:
            return
        self.icon.set_from_pixbuf(self.icons[1])
        if self.timer_id is not None:
            GObject.source_remove(self.timer_id)
            self.timer_id = None
            self.start_time = time() - self.start_time
        if not finished:
            self.time_label.set_text("--:--")
        else:
            self.finished = True
        self.emit('timer_toggle', False)
        
    def process_click (self, btn, event):
        if self.timer_id is None:
            self.start()
        else:
            self.stop()

    def is_running (self):
        return self.timer_id is not None

    def ellapsed (self):
        if self.is_running():
            return time() - self.start_time
        else:
            return self.start_time

    def is_reset (self):
        return not self.is_running() and self.start_time == 0

    def do_tick (self):
        t = time() - self.start_time
        if t > 5999:
            # wrap timer
            t = 0
            self.start_time = time()
        self.time_label.set_text("%0.2i:%0.2i" % (t/60, t%60))
        return True

    def _freeze (self):
        return (self.start_time, time(), self.finished, self.timer_id is None)

    def _thaw (self, obj):
        self.start_time, t, finished, stopped = obj
        if self.start_time is not None:
            if not stopped:
                self.start_time = t - self.start_time
                self.start()
                return
            self.start_time = time() - self.start_time
            self.do_tick()
        self.stop(finished)