forked from mivoligo/Zeegaree-Lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeegaree_lite.py
executable file
·337 lines (240 loc) · 11.1 KB
/
zeegaree_lite.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2013-2014 Michał Prędotka
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from gi.repository import Unity, Notify
from PySide import QtCore, QtGui, QtDeclarative
import subprocess
import os
import datetime
NOTIFICATION_ICON = os.path.join(os.path.dirname(__file__), "./images/z-l_128.png")
TRAY_ICON = os.path.join(os.path.dirname(__file__), "./images/mono_color_32.png")
UNITY_LAUNCHER = Unity.LauncherEntry.get_for_desktop_id("zeegaree-lite.desktop")
HOME = os.path.expanduser("~")
FIRST_FILE = HOME + "/.config/zeegaree-lite/firstrun.txt"
class Notification(QtCore.QObject):
""" Notification about Timer events
Notification uses Ubuntu Notify and sound played with paplay"""
@QtCore.Slot(str, str, str)
def somethingFinished(self, subject, body, soundfile):
timerSound = os.path.join(os.path.dirname(__file__), soundfile)
Notify.init("Timer finished")
notification = Notify.Notification.new (subject, body, NOTIFICATION_ICON)
notification.set_urgency(Notify.Urgency.CRITICAL)
notification.show()
subprocess.Popen(["paplay", timerSound])
class Launcher(QtCore.QObject):
""" Stuff specific to Unity Launcher """
@QtCore.Slot(str)
def setUrgent(self, value):
""" Setting urgent state of icon in Unity launcher """
UNITY_LAUNCHER.set_property("urgent", value)
@QtCore.Slot(float)
def getTimerProgress(self, value):
""" Display timer progress """
UNITY_LAUNCHER.set_property("progress", value)
if value > 0:
UNITY_LAUNCHER.set_property("progress_visible", True)
else:
UNITY_LAUNCHER.set_property("progress_visible", False)
class SaveClass(QtGui.QMainWindow):
""" Save Lap times and/or Split times from Stopwatch """
@QtCore.Slot(str, str)
def getSomethingToSave(self, file_title, text_to_write):
now = datetime.datetime.now()
file_name = file_title+" "+str(now.strftime("%x %X")+".txt")
fileName, filtr = QtGui.QFileDialog.getSaveFileName(self,
"Save "+file_title,
file_name,
"Text Files (*.txt);;All Files (*)")
if fileName:
laptimefile = open(fileName, "w+")
laptimefile.write(text_to_write)
laptimefile.close()
class FirstRun(QtCore.QObject):
""" Create file on first run """
@QtCore.Slot()
def createTimerFile(self):
firstfile = open(FIRST_FILE, "w+")
firstfile.write("Timer_clicked")
firstfile.close()
@QtCore.Slot(result = str)
def checkTimerFile(self):
if os.path.exists(FIRST_FILE):
firstfile = open(FIRST_FILE, "r")
textcheck = firstfile.readline()
firstfile.close()
if textcheck == "Timer_clicked":
return "Timer clicked"
class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, icon, parent=None):
QtGui.QSystemTrayIcon.__init__(self, icon, parent)
menu = QtGui.QMenu(parent)
self.setContextMenu(menu)
self.showMainWindow = QtGui.QAction("Show", self, triggered = self.onShowMainWindow)
self.showMainWindow.setVisible(False)
self.hideMainWindow =QtGui.QAction("Hide", self)
self.hideMainWindow.setVisible(True)
self.hideMainWindow.triggered.connect(self.onHideMainWindow)
self.stopwatchMenuTitle = QtGui.QAction("Stopwatch", self)
self.stopwatchMenuTitle.setDisabled(True)
self.stopwatchMenuStart = QtGui.QAction("Start stopwatch", self, triggered = self.onStopwatchStart)
self.stopwatchMenuPause = QtGui.QAction("Pause stopwatch", self, triggered = self.onStopwatchPause)
self.stopwatchMenuPause.setVisible(False)
self.stopwatchMenuResume = QtGui.QAction("Resume stopwatch", self, triggered = self.onStopwatchStart)
self.stopwatchMenuResume.setVisible(False)
self.stopwatchMenuLap = QtGui.QAction("Lap", self, triggered = self.onStopwatchLap)
self.stopwatchMenuLap.setVisible(False)
self.stopwatchMenuSplit = QtGui.QAction("Split", self, triggered = self.onStopwatchSplit)
self.stopwatchMenuSplit.setVisible(False)
self.stopwatchMenuReset = QtGui.QAction("Reset stopwatch", self, triggered = self.onStopwatchReset)
self.stopwatchMenuReset.setVisible(False)
self.timerMenuTitle = QtGui.QAction("Timer", self)
self.timerMenuTitle.setDisabled(True)
self.timerMenuSet = QtGui.QAction("Set timer", self, triggered = self.onTimerSet)
self.timerMenuStop = QtGui.QAction("Stop timer", self, triggered = self.onTimerStop)
self.timerMenuStop.setVisible(False)
self.timerMenuReset = QtGui.QAction("Reset timer", self, triggered = self.onTimerReset)
self.timerMenuReset.setVisible(False)
menu.addAction(self.showMainWindow)
menu.addAction(self.hideMainWindow)
menu.addSeparator()
menu.addAction(self.stopwatchMenuTitle)
menu.addAction(self.stopwatchMenuStart)
menu.addAction(self.stopwatchMenuPause)
menu.addAction(self.stopwatchMenuResume)
menu.addAction(self.stopwatchMenuLap)
menu.addAction(self.stopwatchMenuSplit)
menu.addAction(self.stopwatchMenuReset)
menu.addSeparator()
menu.addAction(self.timerMenuTitle)
menu.addAction(self.timerMenuSet)
menu.addAction(self.timerMenuStop)
menu.addAction(self.timerMenuReset)
menu.addSeparator()
menu.addAction(QtGui.QAction("Quit", self, triggered= app.exit))
def iconActivated(self, reason):
if reason == QtGui.QSystemTrayIcon.Trigger:
trayIcon.showMainWindow.setVisible(False)
trayIcon.hideMainWindow.setVisible(True)
view.show()
view.activateWindow()
def onShowMainWindow(self):
self.showMainWindow.setVisible(False)
self.hideMainWindow.setVisible(True)
view.show()
view.activateWindow()
def onHideMainWindow(self):
self.hideMainWindow.setVisible(False)
self.showMainWindow.setVisible(True)
view.hide()
def onStopwatchStart(self):
self.stopwatchMenuStart.setVisible(False)
self.stopwatchMenuPause.setVisible(True)
self.stopwatchMenuLap.setVisible(True)
self.stopwatchMenuSplit.setVisible(True)
self.stopwatchMenuReset.setVisible(True)
rootObject.startStopwatch()
@QtCore.Slot()
def onStopwatchStartFromQML(self):
trayIcon.stopwatchMenuStart.setVisible(False)
trayIcon.stopwatchMenuResume.setVisible(False)
trayIcon.stopwatchMenuPause.setVisible(True)
trayIcon.stopwatchMenuLap.setVisible(True)
trayIcon.stopwatchMenuSplit.setVisible(True)
trayIcon.stopwatchMenuReset.setVisible(True)
def onStopwatchPause(self):
self.stopwatchMenuPause.setVisible(False)
self.stopwatchMenuResume.setVisible(True)
rootObject.pauseStopwatch()
@QtCore.Slot()
def onStopwatchPauseFromQML(self):
trayIcon.stopwatchMenuPause.setVisible(False)
trayIcon.stopwatchMenuResume.setVisible(True)
def onStopwatchReset(self):
self.stopwatchMenuStart.setVisible(True)
self.stopwatchMenuPause.setVisible(False)
self.stopwatchMenuResume.setVisible(False)
self.stopwatchMenuLap.setVisible(False)
self.stopwatchMenuSplit.setVisible(False)
self.stopwatchMenuReset.setVisible(False)
rootObject.resetStopwatch()
@QtCore.Slot()
def onStopwatchResetFromQML(self):
trayIcon.stopwatchMenuStart.setVisible(True)
trayIcon.stopwatchMenuPause.setVisible(False)
trayIcon.stopwatchMenuResume.setVisible(False)
trayIcon.stopwatchMenuLap.setVisible(False)
trayIcon.stopwatchMenuSplit.setVisible(False)
trayIcon.stopwatchMenuReset.setVisible(False)
def onStopwatchLap(self):
rootObject.getLap()
def onStopwatchSplit(self):
rootObject.getSplit()
def onTimerSet(self):
self.showMainWindow.setVisible(False)
self.hideMainWindow.setVisible(True)
view.show()
view.activateWindow()
view.raise_()
rootObject.showTimer()
@QtCore.Slot()
def onTimerStartFromQML(self):
trayIcon.timerMenuSet.setVisible(False)
trayIcon.timerMenuStop.setVisible(True)
trayIcon.timerMenuReset.setVisible(True)
@QtCore.Slot()
def onTimerStopFromQML(self):
trayIcon.timerMenuSet.setVisible(True)
trayIcon.timerMenuStop.setVisible(False)
trayIcon.timerMenuReset.setVisible(False)
def onTimerStop(self):
self.timerMenuSet.setVisible(True)
self.timerMenuStop.setVisible(False)
self.timerMenuReset.setVisible(False)
rootObject.stopTimer()
def onTimerReset(self):
self.timerMenuSet.setVisible(True)
self.timerMenuStop.setVisible(False)
self.timerMenuReset.setVisible(False)
rootObject.resetTimer()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
try:
os.makedirs(HOME + "/.config/zeegaree-lite")
except OSError:
pass
view = QtDeclarative.QDeclarativeView()
view.setSource(QtCore.QUrl(os.path.join(os.path.dirname(__file__),'main.qml')))
# Fit root object to be able to resize window
view.setResizeMode(view.SizeRootObjectToView)
view.setMinimumSize(QtCore.QSize(510, 400))
view.setWindowTitle("Zeegaree Lite")
# Get the root object of the user interface.
rootObject = view.rootObject()
icon = QtGui.QIcon(TRAY_ICON)
trayIcon = SystemTrayIcon(icon)
notification = Notification()
launcher = Launcher()
somethingtosave = SaveClass()
firstrun = FirstRun()
trayicon = SystemTrayIcon(trayIcon)
context = view.rootContext()
context.setContextProperty("notification", notification)
context.setContextProperty("launcher", launcher)
context.setContextProperty("somethingtosave", somethingtosave)
context.setContextProperty("firstrun", firstrun)
context.setContextProperty("trayicon", trayicon)
trayIcon.show()
trayIcon.activated.connect(trayicon.iconActivated)
view.show()
sys.exit(app.exec_())