2020-05-04 10:09:59 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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 3 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, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
'''
|
|
|
|
Display two timers with interactive behaviour :
|
|
|
|
* One timer is white and background become red after one hour
|
|
|
|
* Second timer is blue
|
|
|
|
|
|
|
|
* Click : start/stop timer
|
|
|
|
* Double click : reset timer
|
|
|
|
* Right click : switch timer
|
|
|
|
'''
|
|
|
|
|
|
|
|
import time
|
|
|
|
from threading import Thread
|
|
|
|
from signal import signal, SIGINT
|
|
|
|
import sys
|
2020-05-07 08:42:14 +02:00
|
|
|
from genericmonitor import GenericMonitor, GenericMonitorGroup, GenericMonitorItem
|
2020-05-04 10:09:59 +02:00
|
|
|
|
2020-05-07 08:42:14 +02:00
|
|
|
class TimerThread(Thread,GenericMonitor):
|
2020-05-04 10:09:59 +02:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._stopLoop = True
|
2020-05-07 08:42:14 +02:00
|
|
|
self.stopMainLoop()
|
2020-05-04 10:09:59 +02:00
|
|
|
|
|
|
|
def _displayTimerValue(self):
|
2020-05-07 08:42:14 +02:00
|
|
|
item = GenericMonitorItem('timer', onClick='signal', box='right')
|
|
|
|
group = GenericMonitorGroup('Timer', item)
|
2020-05-04 10:09:59 +02:00
|
|
|
curValue = self.timers[self.curTimer]
|
|
|
|
item.text = '%02d:%02d' % (int(curValue/60)%60, curValue%60)
|
|
|
|
if curValue >= (60*60):
|
|
|
|
item.text = '%02d:%s' % (int(curValue/(60*60)), item.text)
|
|
|
|
if self.curTimer == 0:
|
|
|
|
style = 'color:white'
|
|
|
|
if curValue > (60*60):
|
|
|
|
style += ';background-color:red'
|
|
|
|
else:
|
|
|
|
style = 'color:#215D9C'
|
|
|
|
item.style = style
|
2020-05-07 08:42:14 +02:00
|
|
|
self.notify(group)
|
2020-05-04 10:09:59 +02:00
|
|
|
|
|
|
|
def run(self):
|
2020-05-07 08:42:14 +02:00
|
|
|
self.setupMonitor()
|
2020-05-04 10:09:59 +02:00
|
|
|
self.timers = [0, 0]
|
|
|
|
self.curTimer = 0
|
|
|
|
self.timerPaused = False
|
|
|
|
self._stopLoop = False
|
|
|
|
while not self._stopLoop:
|
|
|
|
time.sleep(1)
|
|
|
|
if not self.timerPaused:
|
|
|
|
self.timers[self.curTimer] += 1
|
|
|
|
self._displayTimerValue()
|
|
|
|
|
|
|
|
def _forMe(self, sender):
|
|
|
|
return sender == 'timer@Timer'
|
|
|
|
|
|
|
|
def onClick(self, sender):
|
|
|
|
if not self._forMe(sender): return
|
|
|
|
self.timerPaused = not self.timerPaused
|
|
|
|
self._displayTimerValue()
|
|
|
|
|
|
|
|
def onRightClick(self, sender):
|
|
|
|
if not self._forMe(sender): return
|
|
|
|
if self.curTimer == 0:
|
|
|
|
self.curTimer = 1
|
|
|
|
else:
|
|
|
|
self.curTimer = 0
|
|
|
|
self.timers[self.curTimer] = 0
|
|
|
|
self.timerPaused = False
|
|
|
|
self._displayTimerValue()
|
|
|
|
|
|
|
|
def onDblClick(self, sender):
|
|
|
|
if not self._forMe(sender): return
|
|
|
|
self.timers[self.curTimer] = 0
|
|
|
|
self.timerPaused = True
|
|
|
|
self._displayTimerValue()
|
|
|
|
|
2020-05-07 08:42:14 +02:00
|
|
|
def onActivate(self):
|
|
|
|
super().onActivate()
|
|
|
|
self.timerPaused = self._lastState
|
|
|
|
self._displayTimerValue()
|
2020-05-04 10:09:59 +02:00
|
|
|
|
2020-05-07 08:42:14 +02:00
|
|
|
def onDeactivate(self):
|
|
|
|
super().onDeactivate()
|
|
|
|
self._lastState = self.timerPaused
|
|
|
|
if not self.timerPaused and self.curTimer == 0:
|
|
|
|
self.timerPaused = True
|
2020-05-04 10:09:59 +02:00
|
|
|
|
|
|
|
def signalHandler(signal_received, frame):
|
|
|
|
timerThread.stop()
|
|
|
|
timerThread.join()
|
2020-05-07 08:42:14 +02:00
|
|
|
groups = {'groups':['Timer']}
|
|
|
|
timerThread.deleteGroups(groups)
|
2020-05-04 10:09:59 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
timerThread = TimerThread()
|
|
|
|
timerThread.start()
|
|
|
|
|
|
|
|
signal(SIGINT, signalHandler)
|
|
|
|
|
2020-05-07 08:42:14 +02:00
|
|
|
timerThread.runMainLoop()
|