This repository was archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.py
More file actions
111 lines (90 loc) · 3.17 KB
/
Copy pathtimer.py
File metadata and controls
111 lines (90 loc) · 3.17 KB
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
from PySide2.QtCore import QObject, Signal, Slot, Property, QThread
from time import sleep
from datetime import timedelta, datetime
from models import Database, Task
class TimerWorker(QThread):
on_start = Signal(object)
on_stop = Signal(object)
def __init__(self):
QThread.__init__(self)
self.running_flag = False
self.seconds = 0
self.date_start = datetime.now()
self.date_stop = datetime.now()
def run(self):
self.running_flag = True
self.date_start = datetime.now()
while self.running_flag:
general_datetime = str(timedelta(seconds=self.seconds))
self.on_start.emit(general_datetime)
self.seconds += 1
sleep(1)
def stop(self):
self.running_flag = False
self.seconds = 0
self.date_stop = datetime.now()
dates = {
"start": self.date_start,
"stop": self.date_stop
}
self.on_stop.emit(dates)
class Timer(QObject):
def __init__(self):
QObject.__init__(self)
self._text = "0:00:00"
self._description = "No Description"
self._start_visibility = True
self._stop_visibility = False
self.thread = TimerWorker()
self.thread.on_start.connect(self.on_start)
self.thread.on_stop.connect(self.on_stop)
def _get_start_visibility(self):
return self._start_visibility
def _get_stop_visibility(self):
return self._stop_visibility
def _get_text(self):
return self._text
def _get_description(self):
return self._description
def _set_text(self, new_text):
self._text = new_text
self.on_text.emit()
def _set_description(self, new_description):
self._description = new_description
def _set_start_visibility(self, new_visibility):
self._start_visibility = new_visibility
self.on_start_visibility.emit()
def _set_stop_visibility(self, new_visibility):
self._stop_visibility = new_visibility
self.on_stop_visibility.emit()
@Slot(object)
def on_start(self, value):
self._set_text(str(value))
@Slot(object)
def on_stop(self, dates):
db = Database()
new_task = Task(description=self._get_description(),
date_start=dates["start"],
date_stop=dates["stop"])
db.session.add(new_task)
db.session.commit()
@Slot()
def start_clock(self):
self._set_start_visibility(False)
self._set_stop_visibility(True)
self.thread.start()
@Slot(str)
def stop_clock(self, description):
self._set_start_visibility(True)
self._set_stop_visibility(False)
self._set_description(description)
self.thread.stop()
self.thread.quit()
# Signal definition
on_text = Signal()
on_start_visibility = Signal()
on_stop_visibility = Signal()
# Property definition
text = Property(str, _get_text, notify=on_text)
start_visibility = Property(bool, _get_start_visibility, notify=on_start_visibility)
stop_visibility = Property(bool, _get_stop_visibility, notify=on_stop_visibility)