source: misc/preminder/preminder.py @ 17:a039c34dabc6

Revision 17:a039c34dabc6, 5.4 KB checked in by Eriol, 5 years ago (diff)

Added forgotten timer: check if not PPP/PHP Day

Line 
1#!/usr/bin/env python
2#
3# preminder.py
4# A reminder using GTK shaped windows
5# http://mornie.org/blog/2007/04/26/A-reminder-using-GTK-shaped-window/
6#
7# Copyright (C) 2007 Daniele Tricoli aka Eriol <eriol@mornie.org>
8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License
11# as published by the Free Software Foundation; either version 2
12# of the License, or (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22#
23#
24# The original pizza image (published under GNU Free Documentation License)
25# was downloaded from: http://commons.wikimedia.org/wiki/Image:Spinach_pizza.jpg
26# And was modified by me with Gimp :)
27
28import pygtk
29pygtk.require('2.0')
30
31import gobject
32import gtk
33import pango
34
35from datetime import datetime, time
36from time import sleep
37
38from dateutil import rrule
39
40IMAGE_FILE = 'pizza.png'
41START_CHECK_HOUR = time(9, 0)
42FINISH_CHECK_HOUR = time(21, 0)
43TOTAL_HOURS = FINISH_CHECK_HOUR.hour - START_CHECK_HOUR.hour
44AUTO_HIDE_AFTER = 10 # seconds
45CHECK_TIMER = 10 #seconds
46DIPLAYING_TEXT = 'Today is a PPP/PHP Day!'
47
48def u(t):
49    if t < 0: return 0
50    else: return 1
51
52def setTimer(t):
53    return u(t) - 0.5 * u(t-TOTAL_HOURS-3) - 0.25 * u(t-TOTAL_HOURS-1)
54
55class PReminder:
56
57    def __init__(self):
58        self.window = gtk.Window(gtk.WINDOW_POPUP)
59        self.window.set_events(self.window.get_events() | gtk.gdk.BUTTON_PRESS_MASK)
60        self.window.connect("button_press_event", self.hide)
61
62        self.timerShow = None
63        self.pieces = 0
64
65        colormap = gtk.gdk.colormap_get_system()
66        black = colormap.alloc_color('black')
67        white = colormap.alloc_color('white')
68
69        image = gtk.Image()
70        image.set_from_file(IMAGE_FILE)
71
72        pixbuf = image.get_pixbuf()
73        self.width, self.height = pixbuf.get_width(), pixbuf.get_height()
74        self.pixelpiece = self.width / TOTAL_HOURS
75        self.pixmap, self.mask = pixbuf.render_pixmap_and_mask()
76
77        self.bgc = self.pixmap.new_gc(foreground=black)
78        self.wgc = self.pixmap.new_gc(foreground=white)
79        self.wmgc = self.mask.new_gc(foreground=white)
80        self.bmgc = self.mask.new_gc(foreground=black)
81
82        area = gtk.DrawingArea()
83        self.pangolayout = area.create_pango_layout('')
84
85        self.draw()
86
87        self.fixed = gtk.Fixed()
88        self.fixed.set_size_request(self.width, self.height)
89        self.fixed.put(self.image, 0, 0)
90        self.window.add(self.fixed)
91
92        self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
93
94        self.check()
95
96    def setText(self, text):
97        markup = '<span size="xx-large" foreground="black">%s</span>'
98        self.pangolayout.set_markup(markup % text)
99        self.draw()
100
101    def setPiece(self, x):
102        self.pieces = x
103        self.draw()
104
105    def draw(self):
106        w = self.width, self.pangolayout.get_pixel_size()[0]
107        offset = (max(w) - min(w)) / 2.
108
109        if self.width < self.pangolayout.get_pixel_size()[0]:
110            offset = -offset
111
112        self.mask.draw_rectangle(self.bmgc,
113                                 True,
114                                 0, 0,
115                                 self.pieces * self.pixelpiece,
116                                 self.height)
117        textwidth = self.pangolayout.get_pixel_size()[0]
118        self.pixmap.draw_layout(self.bgc,
119                                int(offset),
120                                int(self.height / 2.),
121                                self.pangolayout)
122        self.mask.draw_layout(self.wmgc,
123                              int(offset),
124                              int(self.height / 2.),
125                              self.pangolayout)
126
127        self.image = gtk.Image()
128        self.image.set_from_pixmap(self.pixmap, self.mask)
129        self.image.show()
130
131        self.window.shape_combine_mask(self.mask, 0, 0)
132
133    def hide(self, *args):
134        self.window.hide()
135        if self.timerShow:
136            gobject.source_remove(self.timerShow)
137
138    def show(self):
139        self.window.show()
140        self.fixed.show()
141
142    def check(self):
143        today = datetime.today()
144        lastThursday = rrule.rrule(rrule.MONTHLY,
145                                   byweekday=rrule.TH(-1),
146                                   count=1)[0].date()
147
148        if today.date() == lastThursday:
149            now = today.time()
150            if START_CHECK_HOUR <= now <= FINISH_CHECK_HOUR:
151                timerModulator = setTimer(now.hour - START_CHECK_HOUR.hour)
152                timer = int(timerModulator * 60 * 60 * 1000)
153                self.setText(DIPLAYING_TEXT)
154                self.setPiece(now.hour - START_CHECK_HOUR.hour)
155
156                self.show()
157                self.timerShow = gobject.timeout_add(AUTO_HIDE_AFTER * 1000,
158                                                     self.hide)
159        else:
160            timer = CHECK_TIMER * 1000
161
162        self.timerID = gobject.timeout_add(timer, self.check)
163
164    def main(self):
165        gtk.main()
166
167if __name__ == "__main__":
168    x = PReminder()
169    try:
170        x.main()
171    except KeyboardInterrupt:
172        lambda: 0
Note: See TracBrowser for help on using the repository browser.