2011-08-16 03:27:28 -04:00
|
|
|
import inspect
|
|
|
|
import threading
|
2011-08-27 13:51:50 -04:00
|
|
|
import time, os
|
2011-08-29 05:13:49 -04:00
|
|
|
import ostools
|
2011-08-16 03:27:28 -04:00
|
|
|
from PyQt4 import QtGui, QtCore
|
|
|
|
|
|
|
|
try:
|
|
|
|
import pynotify
|
|
|
|
except:
|
2011-08-20 05:01:11 -04:00
|
|
|
pynotify = None
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
class DefaultToast(object):
|
2011-08-27 13:51:50 -04:00
|
|
|
def __init__(self, machine, title, msg, icon):
|
|
|
|
self.machine = machine
|
|
|
|
self.title = title
|
|
|
|
self.msg = msg
|
|
|
|
self.icon = icon
|
2011-08-16 03:27:28 -04:00
|
|
|
def show(self):
|
|
|
|
print self.title, self.msg, self.icon
|
2011-08-27 13:51:50 -04:00
|
|
|
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"
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
class ToastMachine(object):
|
|
|
|
class __Toast__(object):
|
2011-08-25 08:30:43 -04:00
|
|
|
def __init__(self, machine, title, msg, time=3000, icon="", importance=0):
|
|
|
|
self.machine = machine
|
2011-08-16 03:27:28 -04:00
|
|
|
self.title = title
|
|
|
|
self.msg = msg
|
|
|
|
self.time = time
|
2011-08-27 13:51:50 -04:00
|
|
|
if icon:
|
|
|
|
icon = os.path.abspath(icon)
|
2011-08-16 03:27:28 -04:00
|
|
|
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):
|
2011-08-25 08:30:43 -04:00
|
|
|
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()
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
def realShow(self):
|
2011-08-27 13:51:50 -04:00
|
|
|
self.machine.displaying = True
|
2011-08-16 03:27:28 -04:00
|
|
|
t = None
|
2011-08-25 08:30:43 -04:00
|
|
|
for (k,v) in self.machine.types.iteritems():
|
|
|
|
if self.machine.type == k:
|
2011-08-27 13:51:50 -04:00
|
|
|
args = inspect.getargspec(v.__init__).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)
|
2011-08-16 03:27:28 -04:00
|
|
|
# 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:
|
2011-08-25 08:30:43 -04:00
|
|
|
if 'default' in self.machine.types:
|
2011-08-27 13:51:50 -04:00
|
|
|
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)
|
2011-08-16 03:27:28 -04:00
|
|
|
else:
|
2011-08-27 13:51:50 -04:00
|
|
|
t = self.machine.types['default'](self.machine, self.title, self.msg, self.icon)
|
2011-08-16 03:27:28 -04:00
|
|
|
else:
|
|
|
|
t = DefaultToast(self.title, self.msg, self.icon)
|
|
|
|
t.show()
|
2011-08-25 08:30:43 -04:00
|
|
|
|
|
|
|
def __init__(self, parent, name, on=True, type="default",
|
|
|
|
types=({'default' : DefaultToast,
|
|
|
|
'libnotify': pynotify.Notification}
|
|
|
|
if pynotify else
|
|
|
|
{'default' : DefaultToast}),
|
|
|
|
extras={}):
|
|
|
|
self.parent = parent
|
2011-08-16 03:27:28 -04:00
|
|
|
self.name = name
|
2011-08-25 08:30:43 -04:00
|
|
|
self.on = on
|
2011-08-16 03:27:28 -04:00
|
|
|
types.update(extras)
|
|
|
|
self.types = types
|
2011-08-25 08:30:43 -04:00
|
|
|
self.type = "default"
|
2011-08-16 03:27:28 -04:00
|
|
|
self.quit = False
|
2011-08-27 13:51:50 -04:00
|
|
|
self.displaying = False
|
2011-08-16 03:27:28 -04:00
|
|
|
|
2011-08-25 08:30:43 -04:00
|
|
|
self.setCurrentType(type)
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
self.toasts = []
|
|
|
|
|
2011-08-27 13:51:50 -04:00
|
|
|
def Toast(self, title, msg, icon="", time=3000):
|
|
|
|
return self.__Toast__(self, title, msg, time=time, icon=icon)
|
2011-08-25 08:30:43 -04:00
|
|
|
|
|
|
|
def setEnabled(self, on):
|
|
|
|
self.on = (on is True)
|
|
|
|
|
|
|
|
def currentType(self):
|
|
|
|
return self.type
|
|
|
|
|
2011-09-13 00:18:18 -04:00
|
|
|
def availableTypes(self):
|
2011-08-25 08:30:43 -04:00
|
|
|
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()
|
2011-08-27 13:51:50 -04:00
|
|
|
except pytwmn.ERROR, e:
|
2011-08-25 08:30:43 -04:00
|
|
|
print "Problem initilizing pytwmn: " + str(e)
|
|
|
|
return
|
|
|
|
#self.type = type = "default"
|
|
|
|
self.type = type
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
def appName(self):
|
|
|
|
if inspect.ismethod(self.name) or inspect.isfunction(self.name):
|
|
|
|
return self.name()
|
|
|
|
else:
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def showNext(self):
|
2011-08-27 13:51:50 -04:00
|
|
|
if not self.displaying and self.toasts:
|
|
|
|
self.toasts.sort(key=lambda x: x.importance)
|
|
|
|
self.toasts[0].realShow()
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
def showAll(self):
|
|
|
|
while self.toasts:
|
|
|
|
self.showNext()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not self.quit:
|
2011-08-25 08:30:43 -04:00
|
|
|
if self.on and self.toasts:
|
2011-08-16 03:27:28 -04:00
|
|
|
self.showNext()
|
|
|
|
|
|
|
|
|
2011-08-27 13:51:50 -04:00
|
|
|
class PesterToast(QtGui.QWidget, DefaultToast):
|
|
|
|
def __init__(self, machine, title, msg, icon, time=3000, parent=None):
|
2011-08-29 05:28:29 -04:00
|
|
|
QtGui.QWidget.__init__(self, parent)
|
2011-08-27 13:51:50 -04:00
|
|
|
|
|
|
|
self.machine = machine
|
|
|
|
self.time = time
|
|
|
|
|
2011-08-29 05:28:29 -04:00
|
|
|
if ostools.isWin32():
|
|
|
|
self.setWindowFlags(QtCore.Qt.ToolTip)
|
|
|
|
else:
|
2011-08-29 13:28:41 -04:00
|
|
|
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.X11BypassWindowManagerHint | QtCore.Qt.ToolTip)
|
2011-08-27 13:51:50 -04:00
|
|
|
|
|
|
|
self.m_animation = QtCore.QParallelAnimationGroup()
|
|
|
|
anim = QtCore.QPropertyAnimation(self)
|
|
|
|
anim.setTargetObject(self)
|
|
|
|
self.m_animation.addAnimation(anim)
|
|
|
|
anim.setEasingCurve(QtCore.QEasingCurve.OutBounce)
|
|
|
|
anim.setDuration(1000)
|
|
|
|
self.connect(anim, QtCore.SIGNAL('finished()'),
|
|
|
|
self, QtCore.SLOT('reverseTrigger()'))
|
|
|
|
|
|
|
|
self.m_animation.setDirection(QtCore.QAnimationGroup.Forward)
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
self.title = QtGui.QLabel(title, self)
|
|
|
|
self.msg = QtGui.QLabel(msg, self)
|
2011-08-27 13:51:50 -04:00
|
|
|
self.content = msg
|
|
|
|
if icon:
|
|
|
|
self.icon = QtGui.QLabel("")
|
|
|
|
self.icon.setPixmap(QtGui.QPixmap(icon).scaledToWidth(30))
|
|
|
|
else:
|
|
|
|
self.icon = QtGui.QLabel("")
|
|
|
|
self.icon.setPixmap(QtGui.QPixmap(30, 30))
|
|
|
|
self.icon.pixmap().fill(QtGui.QColor(0,0,0,0))
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
layout_0 = QtGui.QVBoxLayout()
|
2011-08-27 13:51:50 -04:00
|
|
|
layout_0.setMargin(0)
|
|
|
|
layout_0.setContentsMargins(0, 0, 0, 0)
|
|
|
|
|
|
|
|
if self.icon:
|
|
|
|
layout_1 = QtGui.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)
|
2011-08-16 03:27:28 -04:00
|
|
|
layout_0.addWidget(self.msg)
|
|
|
|
|
2011-08-27 13:51:50 -04:00
|
|
|
self.setMaximumWidth(self.parent().theme["toasts/width"])
|
|
|
|
self.msg.setMaximumWidth(self.parent().theme["toasts/width"])
|
|
|
|
self.title.setMinimumHeight(self.parent().theme["toasts/title/minimumheight"])
|
|
|
|
|
2011-08-16 03:27:28 -04:00
|
|
|
self.setLayout(layout_0)
|
|
|
|
|
2011-08-27 13:51:50 -04:00
|
|
|
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"]))
|
|
|
|
|
2011-08-27 17:19:39 -04:00
|
|
|
p = QtGui.QApplication.desktop().availableGeometry(self).bottomRight()
|
|
|
|
o = QtGui.QApplication.desktop().screenGeometry(self).bottomRight()
|
|
|
|
anim.setStartValue(p.y() - o.y())
|
2011-08-27 13:51:50 -04:00
|
|
|
anim.setEndValue(100)
|
|
|
|
self.connect(anim, QtCore.SIGNAL('valueChanged(QVariant)'),
|
|
|
|
self, QtCore.SLOT('updateBottomLeftAnimation(QVariant)'))
|
|
|
|
|
|
|
|
self.byebye = False
|
2011-08-16 03:27:28 -04:00
|
|
|
|
2011-08-27 13:51:50 -04:00
|
|
|
@QtCore.pyqtSlot()
|
2011-08-16 03:27:28 -04:00
|
|
|
def show(self):
|
2011-08-27 13:51:50 -04:00
|
|
|
self.m_animation.start()
|
|
|
|
|
|
|
|
@QtCore.pyqtSlot()
|
|
|
|
def done(self):
|
|
|
|
QtGui.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, QtCore.SLOT('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)
|
|
|
|
self.disconnect(anim, QtCore.SIGNAL('finished()'),
|
|
|
|
self, QtCore.SLOT('reverseTrigger()'))
|
|
|
|
self.connect(anim, QtCore.SIGNAL('finished()'),
|
|
|
|
self, QtCore.SLOT('done()'))
|
|
|
|
self.m_animation.start()
|
|
|
|
|
|
|
|
@QtCore.pyqtSlot(QtCore.QVariant)
|
|
|
|
def updateBottomLeftAnimation(self, value):
|
|
|
|
p = QtGui.QApplication.desktop().availableGeometry(self).bottomRight()
|
|
|
|
val = float(self.height())/100
|
|
|
|
self.move(p.x()-self.width(), p.y() - (value.toInt()[0] * val) +1)
|
|
|
|
self.layout().setSpacing(0)
|
|
|
|
QtGui.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)
|
2011-08-29 13:28:41 -04:00
|
|
|
if curspace == -1:
|
|
|
|
break
|
|
|
|
if (metric.width(text[:lastspace]) > maxwidth) or \
|
|
|
|
len(text[:lastspace]) < 1:
|
|
|
|
for i in xrange(len(text)):
|
|
|
|
if metric.width(text[:i]) > maxwidth:
|
|
|
|
lastspace = i-1
|
|
|
|
break
|
2011-08-27 13:51:50 -04:00
|
|
|
ret.append(text[:lastspace])
|
|
|
|
text = text[lastspace+1:]
|
|
|
|
ret.append(text)
|
|
|
|
return "\n".join(ret)
|
|
|
|
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
class PesterToastMachine(ToastMachine, QtCore.QObject):
|
2011-08-25 08:30:43 -04:00
|
|
|
def __init__(self, parent, name, on=True, type="default",
|
2011-08-20 05:01:11 -04:00
|
|
|
types=({'default' : DefaultToast,
|
|
|
|
'libnotify' : pynotify.Notification}
|
|
|
|
if pynotify else
|
|
|
|
{'default' : DefaultToast}),
|
2011-08-16 03:27:28 -04:00
|
|
|
extras={}):
|
2011-08-25 08:30:43 -04:00
|
|
|
ToastMachine.__init__(self, parent, name, on, type, types, extras)
|
2011-08-16 03:27:28 -04:00
|
|
|
QtCore.QObject.__init__(self, parent)
|
|
|
|
|
2011-08-25 08:30:43 -04:00
|
|
|
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)
|
|
|
|
|
2011-08-16 03:27:28 -04:00
|
|
|
@QtCore.pyqtSlot()
|
|
|
|
def showNext(self):
|
|
|
|
ToastMachine.showNext(self)
|
|
|
|
|
|
|
|
def run(self):
|
2011-08-27 13:51:50 -04:00
|
|
|
pass
|
|
|
|
#~ self.timer = QtCore.QTimer(self)
|
|
|
|
#~ self.timer.setInterval(1000)
|
|
|
|
#~ self.connect(self.timer, QtCore.SIGNAL('timeout()'),
|
|
|
|
#~ self, QtCore.SLOT('showNext()'))
|
|
|
|
#~ if self.on:
|
|
|
|
#~ self.timer.start()
|