-
Notifications
You must be signed in to change notification settings - Fork 50
/
toast.py
401 lines (352 loc) · 15 KB
/
toast.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import inspect
import threading
import time, os
import ostools
from PyQt5 import QtGui, QtCore, QtWidgets
import logging
try:
import pynotify
except:
pynotify = None
class DefaultToast(QtWidgets.QWidget):
def __init__(self, parent, **kwds):
super().__init__(parent)
self.machine = kwds.get('machine')
self.title = kwds.get('title')
self.msg = kwds.get('msg')
self.icon = kwds.get('icon')
def show(self):
print(self.title, self.msg, self.icon)
self.done()
def done(self):
t = self.machine.toasts[0]
if t.title == self.title and t.msg == self.msg and t.icon == self.icon:
self.machine.toasts.pop(0)
self.machine.displaying = False
print("Done")
class ToastMachine(object):
class __Toast__(object):
def __init__(self, machine, title, msg, time=3000, icon="", importance=0):
self.machine = machine
self.title = title
self.msg = msg
self.time = time
if icon:
icon = os.path.abspath(icon)
self.icon = icon
self.importance = importance
if inspect.ismethod(self.title) or inspect.isfunction(self.title):
self.title = self.title()
def titleM(self, title=None):
if title:
self.title = title
if inspect.ismethod(self.title) or inspect.isfunction(self.title):
self.title = self.title()
else: return self.title
def msgM(self, msg=None):
if msg: self.msg = msg
else: return self.msg
def timeM(self, time=None):
if time: self.time = time
else: return self.time
def iconM(self, icon=None):
if icon: self.icon = icon
else: return self.icon
def importanceM(self, importance=None):
if importance != None: self.importance = importance
else: return self.importance
def show(self):
if self.machine.on:
# Use libnotify's queue if using libnotify
if self.machine.type == "libnotify" or self.machine.type == "twmn":
self.realShow()
elif self.machine.toasts:
self.machine.toasts.append(self)
else:
self.machine.toasts.append(self)
self.realShow()
def realShow(self):
self.machine.displaying = True
t = None
for (k,v) in self.machine.types.items():
if self.machine.type == k:
try:
args = inspect.getargspec(v.__init__).args
except:
args = []
extras = {}
if 'parent' in args:
extras['parent'] = self.machine.parent
if 'time' in args:
extras['time'] = self.time
if k == "libnotify" or k == "twmn":
t = v(self.title, self.msg, self.icon, **extras)
else:
t = v(self.machine, self.title, self.msg, self.icon, **extras)
# Use libnotify's urgency setting
if k == "libnotify":
if self.importance < 0:
t.set_urgency(pynotify.URGENCY_CRITICAL)
elif self.importance == 0:
t.set_urgency(pynotify.URGENCY_NORMAL)
elif self.importance > 0:
t.set_urgency(pynotify.URGENCY_LOW)
break
if not t:
if 'default' in self.machine.types:
if 'parent' in inspect.getargspec(self.machine.types['default'].__init__).args:
t = self.machine.types['default'](self.machine, self.title, self.msg, self.icon, self.machine.parent)
else:
t = self.machine.types['default'](self.machine, self.title, self.msg, self.icon)
else:
t = DefaultToast(self.title, self.msg, self.icon)
t.show()
def __init__(self, parent, name, on=True, type="default",
types=({'default' : DefaultToast,
'libnotify': pynotify.Notification}
if pynotify else
{'default' : DefaultToast}),
extras={}):
self.parent = parent
self.name = name
self.on = on
types.update(extras)
self.types = types
self.type = "default"
self.quit = False
self.displaying = False
self.setCurrentType(type)
self.toasts = []
def Toast(self, title, msg, icon="", time=3000):
return self.__Toast__(self, title, msg, time=time, icon=icon)
def setEnabled(self, on):
self.on = (on is True)
def currentType(self):
return self.type
def availableTypes(self):
return sorted(self.types.keys())
def setCurrentType(self, type):
if type in self.types:
if type == "libnotify":
if not pynotify or not pynotify.init("ToastMachine"):
print("Problem initilizing pynotify")
return
#self.type = type = "default"
elif type == "twmn":
from libs import pytwmn
try:
pytwmn.init()
except pytwmn.ERROR as e:
print("Problem initilizing pytwmn: " + str(e))
return
#self.type = type = "default"
self.type = type
def appName(self):
if inspect.ismethod(self.name) or inspect.isfunction(self.name):
return self.name()
else:
return self.name
def showNext(self):
if not self.displaying and self.toasts:
self.toasts.sort(key=lambda x: x.importance)
self.toasts[0].realShow()
def showAll(self):
while self.toasts:
self.showNext()
def run(self):
while not self.quit:
if self.on and self.toasts:
self.showNext()
class PesterToast(DefaultToast):
def __init__(self, machine, title, msg, icon, time=3000, parent=None):
logging.info(isinstance(parent, QtWidgets.QWidget))
kwds = dict(machine=machine, title=title, msg=msg, icon=icon)
super().__init__(parent, **kwds)
self.machine = machine
self.time = time
if ostools.isWin32():
self.setWindowFlags(QtCore.Qt.ToolTip)
else:
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.X11BypassWindowManagerHint | QtCore.Qt.ToolTip)
self.m_animation = QtCore.QParallelAnimationGroup()
anim = QtCore.QPropertyAnimation(self, finished=self.reverseTrigger)
anim.setTargetObject(self)
self.m_animation.addAnimation(anim)
anim.setEasingCurve(QtCore.QEasingCurve.OutBounce)
anim.setDuration(1000)
self.m_animation.setDirection(QtCore.QAnimationGroup.Forward)
self.title = QtWidgets.QLabel(title, self)
self.msg = QtWidgets.QLabel(msg, self)
self.content = msg
if icon:
self.icon = QtWidgets.QLabel("")
self.icon.setPixmap(QtGui.QPixmap(icon).scaledToWidth(30))
else:
self.icon = QtWidgets.QLabel("")
self.icon.setPixmap(QtGui.QPixmap(30, 30))
self.icon.pixmap().fill(QtGui.QColor(0,0,0,0))
layout_0 = QtWidgets.QVBoxLayout()
layout_0.setContentsMargins(0, 0, 0, 0)
if self.icon:
layout_1 = QtWidgets.QGridLayout()
layout_1.addWidget(self.icon, 0,0, 1,1)
layout_1.addWidget(self.title, 0,1, 1,7)
layout_1.setAlignment(self.msg, QtCore.Qt.AlignTop)
layout_0.addLayout(layout_1)
else:
layout_0.addWidget(self.title)
layout_0.addWidget(self.msg)
self.setMaximumWidth(self.parent().theme["toasts/width"])
self.msg.setMaximumWidth(self.parent().theme["toasts/width"])
self.title.setMinimumHeight(self.parent().theme["toasts/title/minimumheight"])
self.setLayout(layout_0)
self.setGeometry(0,0, self.parent().theme["toasts/width"], self.parent().theme["toasts/height"])
self.setStyleSheet(self.parent().theme["toasts/style"])
self.title.setStyleSheet(self.parent().theme["toasts/title/style"])
if self.icon:
self.icon.setStyleSheet(self.parent().theme["toasts/icon/style"])
self.msg.setStyleSheet(self.parent().theme["toasts/content/style"])
self.layout().setSpacing(0)
self.msg.setText(PesterToast.wrapText(self.msg.font(), str(self.msg.text()), self.parent().theme["toasts/width"], self.parent().theme["toasts/content/style"]))
p = QtWidgets.QApplication.desktop().availableGeometry(self).bottomRight()
o = QtWidgets.QApplication.desktop().screenGeometry(self).bottomRight()
anim.setStartValue(p.y() - o.y())
anim.setEndValue(100)
anim.valueChanged.connect(self.updateBottomLeftAnimation)
self.byebye = False
@QtCore.pyqtSlot()
def show(self):
self.m_animation.start()
@QtCore.pyqtSlot()
def done(self):
QtWidgets.QWidget.hide(self)
t = self.machine.toasts[0]
if t.title == str(self.title.text()) and \
t.msg == str(self.content):
self.machine.toasts.pop(0)
self.machine.displaying = False
if self.machine.on:
self.machine.showNext()
del self
@QtCore.pyqtSlot()
def reverseTrigger(self):
if self.time >= 0:
QtCore.QTimer.singleShot(self.time, self.reverseStart)
@QtCore.pyqtSlot()
def reverseStart(self):
if not self.byebye:
self.byebye = True
anim = self.m_animation.animationAt(0)
self.m_animation.setDirection(QtCore.QAnimationGroup.Backward)
anim.setEasingCurve(QtCore.QEasingCurve.InCubic)
anim.finished.disconnect(self.reverseTrigger)
anim.finished.connect(self.done)
self.m_animation.start()
@QtCore.pyqtSlot(QtCore.QVariant)
def updateBottomLeftAnimation(self, value):
p = QtWidgets.QApplication.desktop().availableGeometry(self).bottomRight()
val = float(self.height())/100
self.move(p.x()-self.width(), p.y() - (value * val) +1)
self.layout().setSpacing(0)
QtWidgets.QWidget.show(self)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.RightButton:
self.reverseStart()
elif event.button() == QtCore.Qt.LeftButton:
pass
@staticmethod
def wrapText(font, text, maxwidth, css=""):
ret = []
metric = QtGui.QFontMetrics(font)
if "padding" in css:
if css[css.find("padding")+7] != "-":
colon = css.find(":", css.find("padding"))
semicolon = css.find(";", css.find("padding"))
if semicolon < 0:
stuff = css[colon+1:]
else:
stuff = css[colon+1:semicolon]
stuff = stuff.replace("px", "").lstrip().rstrip()
stuff = stuff.split(" ")
if len(stuff) == 1:
maxwidth -= int(stuff[0])*2
elif len(stuff) == 2:
maxwidth -= int(stuff[1])*2
elif len(stuff) == 3:
maxwidth -= int(stuff[1])*2
elif len(stuff) == 4:
maxwidth -= int(stuff[1]) + int(stuff[3])
else:
if "padding-left" in css:
colon = css.find(":", css.find("padding-left"))
semicolon = css.find(";", css.find("padding-left"))
if semicolon < 0:
stuff = css[colon+1:]
else:
stuff = css[colon+1:semicolon]
stuff = stuff.replace("px", "").lstrip().rstrip()
if stuff.isdigit():
maxwidth -= int(stuff)
if "padding-right" in css:
colon = css.find(":", css.find("padding-right"))
semicolon = css.find(";", css.find("padding-right"))
if semicolon < 0:
stuff = css[colon+1:]
else:
stuff = css[colon+1:semicolon]
stuff = stuff.replace("px", "").lstrip().rstrip()
if stuff.isdigit():
maxwidth -= int(stuff)
if metric.width(text) < maxwidth:
return text
while metric.width(text) > maxwidth:
lastspace = text.find(" ")
curspace = lastspace
while metric.width(text, curspace) < maxwidth:
lastspace = curspace
curspace = text.find(" ", lastspace+1)
if curspace == -1:
break
if (metric.width(text[:lastspace]) > maxwidth) or \
len(text[:lastspace]) < 1:
for i in range(len(text)):
if metric.width(text[:i]) > maxwidth:
lastspace = i-1
break
ret.append(text[:lastspace])
text = text[lastspace+1:]
ret.append(text)
return "\n".join(ret)
class PesterToastMachine(ToastMachine, QtCore.QObject):
def __init__(self, parent, name, on=True, type="default",
types=({'default' : DefaultToast,
'libnotify' : pynotify.Notification}
if pynotify else
{'default' : DefaultToast}),
extras={}):
ToastMachine.__init__(self, parent, name, on, type, types, extras)
QtCore.QObject.__init__(self, parent)
def setEnabled(self, on):
oldon = self.on
ToastMachine.setEnabled(self, on)
if oldon != self.on:
self.parent.config.set('notify', self.on)
if self.on:
self.timer.start()
else:
self.timer.stop()
def setCurrentType(self, type):
oldtype = self.type
ToastMachine.setCurrentType(self, type)
if oldtype != self.type:
self.parent.config.set('notifyType', self.type)
@QtCore.pyqtSlot()
def showNext(self):
ToastMachine.showNext(self)
def run(self):
pass
#~ self.timer = QtCore.QTimer(self)
#~ self.timer.setInterval(1000)
#~ self.timer.timeout.connect(self.showNext)
#~ if self.on:
#~ self.timer.start()