Gnome SHELL Generic Monitor

Gnome SHELL Generic Monitor Git Source Tree

Root/examples/timer.py

1#!/usr/bin/python3
2
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16#
17
18'''
19Display two timers with interactive behaviour :
20 * One timer is white and background become red after one hour
21 * Second timer is blue
22
23 * Click : start/stop timer
24 * Double click : reset timer
25 * Right click : switch timer
26'''
27
28import time
29from threading import Thread
30from signal import signal, SIGINT
31import sys
32from genericmonitor import *
33
34class TimerThread(Thread,GenericMonitor):
35
36 def stop(self):
37 self._stopLoop = True
38 self.stopMainLoop()
39
40 def _displayTimerValue(self):
41 curValue = self.timers[self.curTimer]
42 text = '%02d:%02d' % (int(curValue/60)%60, curValue%60)
43 if curValue >= (60*60):
44 text = '%02d:%s' % (int(curValue/(60*60)), text)
45 if self.curTimer == 0:
46 if self.timerPaused and curValue:
47 style = 'color:black;background-color:white'
48 else:
49 style = 'color:white'
50 if curValue >= (60*60):
51 style += ';background-color:red'
52 else:
53 style = 'color:#215D9C'
54
55 self.textWidget.setText(text)
56 self.textWidget.setStyle(style)
57
58 self.notify(self.monitorGroup)
59
60 def run(self):
61 self.setupMonitor()
62 self.timers = [0, 0]
63 self.curTimer = 0
64 self.timerPaused = False
65 self._stopLoop = False
66
67 self.textWidget = GenericMonitorTextWidget('')
68 signals = {'on-click':'signal','on-dblclick':'signal','on-rightclick':'signal'}
69 self.monitorItem = GenericMonitorItem('timer', [self.textWidget], signals, box='right')
70 self.monitorGroup = GenericMonitorGroup('Timer', self.monitorItem)
71
72 while not self._stopLoop:
73 time.sleep(1)
74 if not self.timerPaused:
75 self.timers[self.curTimer] += 1
76 self._displayTimerValue()
77
78 def _forMe(self, sender):
79 return sender == self.monitorItem.getFullName()
80
81 def onClick(self, sender):
82 if not self._forMe(sender): return
83 self.timerPaused = not self.timerPaused
84 self._displayTimerValue()
85
86 def onRightClick(self, sender):
87 if not self._forMe(sender): return
88 if self.curTimer == 0:
89 self.curTimer = 1
90 else:
91 self.curTimer = 0
92 self.timers[self.curTimer] = 0
93 self.timerPaused = False
94 self._displayTimerValue()
95
96 def onDblClick(self, sender):
97 if not self._forMe(sender): return
98 self.timers[self.curTimer] = 0
99 self.timerPaused = True
100 self._displayTimerValue()
101
102 def onActivate(self):
103 super().onActivate()
104 self.timerPaused = self._lastState
105 self._displayTimerValue()
106
107 def onDeactivate(self):
108 super().onDeactivate()
109 self._lastState = self.timerPaused
110 if not self.timerPaused and self.curTimer == 0:
111 self.timerPaused = True
112
113def signalHandler(signal_received, frame):
114 timerThread.stop()
115 timerThread.join()
116 groups = ['Timer']
117 timerThread.deleteGroups(groups)
118 sys.exit(0)
119
120timerThread = TimerThread()
121timerThread.start()
122
123signal(SIGINT, signalHandler)
124
125timerThread.runMainLoop()

Archive Download this file

Branches

Tags