2023-02-12 19:21:14 -05:00
|
|
|
"""FIXME: not sure how this works exactly but it seems kinda broken!!"""
|
2022-03-19 19:48:19 -04:00
|
|
|
import os
|
2022-10-07 16:51:40 -04:00
|
|
|
|
|
|
|
# import time
|
2022-03-19 19:48:19 -04:00
|
|
|
import inspect
|
|
|
|
import logging
|
|
|
|
|
2022-08-19 07:12:58 -04:00
|
|
|
try:
|
|
|
|
from PyQt6 import QtCore, QtGui, QtWidgets
|
|
|
|
except ImportError:
|
|
|
|
print("PyQt5 fallback (toast.py)")
|
|
|
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
2022-03-19 19:48:19 -04:00
|
|
|
|
2021-12-01 12:29:17 -05:00
|
|
|
import ostools
|
2022-03-19 19:48:19 -04:00
|
|
|
|
2021-12-01 12:29:17 -05:00
|
|
|
_datadir = ostools.getDataDir()
|
2022-10-07 16:51:40 -04:00
|
|
|
PchumLog = logging.getLogger("pchumLogger")
|
2011-08-16 03:27:28 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
# try:
|
2022-03-19 19:48:19 -04:00
|
|
|
# import pynotify
|
2022-10-07 16:51:40 -04:00
|
|
|
# except:
|
2022-03-19 19:48:19 -04:00
|
|
|
# pynotify = None
|
|
|
|
|
|
|
|
# Pynotify is broken.
|
|
|
|
pynotify = None
|
2011-08-16 03:27:28 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2023-01-14 16:59:59 -05:00
|
|
|
class DefaultToast:
|
2023-02-12 19:21:14 -05:00
|
|
|
def __init__(self, machine, title, msg, icon, parent=None):
|
2016-11-13 01:14:39 -05:00
|
|
|
self.machine = machine
|
2022-10-07 16:51:40 -04:00
|
|
|
self.title = title
|
|
|
|
self.msg = msg
|
|
|
|
self.icon = icon
|
|
|
|
|
2011-08-16 03:27:28 -04:00
|
|
|
def show(self):
|
2021-03-23 17:36:43 -04:00
|
|
|
print(self.title, self.msg, self.icon)
|
2011-08-27 13:51:50 -04:00
|
|
|
self.done()
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-08-27 13:51:50 -04:00
|
|
|
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
|
2021-08-10 16:45:48 -04:00
|
|
|
PchumLog.info("Done")
|
2011-08-16 03:27:28 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2023-01-14 16:59:59 -05:00
|
|
|
class ToastMachine:
|
|
|
|
class __Toast__:
|
2011-08-25 08:30:43 -04:00
|
|
|
def __init__(self, machine, title, msg, time=3000, icon="", importance=0):
|
2022-10-07 16:51:40 -04:00
|
|
|
self.machine = machine
|
|
|
|
self.title = title
|
|
|
|
self.msg = msg
|
|
|
|
self.time = time
|
2011-08-27 13:51:50 -04:00
|
|
|
if icon:
|
|
|
|
icon = os.path.abspath(icon)
|
2022-10-07 16:51:40 -04:00
|
|
|
self.icon = icon
|
2011-08-16 03:27:28 -04:00
|
|
|
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()
|
2022-10-07 16:51:40 -04:00
|
|
|
else:
|
|
|
|
return self.title
|
|
|
|
|
2011-08-16 03:27:28 -04:00
|
|
|
def msgM(self, msg=None):
|
2022-10-07 16:51:40 -04:00
|
|
|
if msg:
|
|
|
|
self.msg = msg
|
|
|
|
else:
|
|
|
|
return self.msg
|
|
|
|
|
2011-08-16 03:27:28 -04:00
|
|
|
def timeM(self, time=None):
|
2022-10-07 16:51:40 -04:00
|
|
|
if time:
|
|
|
|
self.time = time
|
|
|
|
else:
|
|
|
|
return self.time
|
|
|
|
|
2011-08-16 03:27:28 -04:00
|
|
|
def iconM(self, icon=None):
|
2022-10-07 16:51:40 -04:00
|
|
|
if icon:
|
|
|
|
self.icon = icon
|
|
|
|
else:
|
|
|
|
return self.icon
|
|
|
|
|
2011-08-16 03:27:28 -04:00
|
|
|
def importanceM(self, importance=None):
|
2023-02-13 14:26:05 -05:00
|
|
|
if importance:
|
2022-10-07 16:51:40 -04:00
|
|
|
self.importance = importance
|
|
|
|
else:
|
|
|
|
return self.importance
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
def show(self):
|
2011-08-25 08:30:43 -04:00
|
|
|
if self.machine.on:
|
|
|
|
# Use libnotify's queue if using libnotify
|
2023-02-14 13:55:23 -05:00
|
|
|
if self.machine.type in ("libnotify", "twmn"):
|
2011-08-25 08:30:43 -04:00
|
|
|
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
|
2023-02-09 14:52:26 -05:00
|
|
|
for k, v in self.machine.types.items():
|
2011-08-25 08:30:43 -04:00
|
|
|
if self.machine.type == k:
|
2013-02-26 00:52:38 -05:00
|
|
|
try:
|
2023-05-04 14:08:33 -04:00
|
|
|
args = inspect.getfullargspec(v.__init__).args
|
2013-02-26 00:52:38 -05:00
|
|
|
except:
|
|
|
|
args = []
|
|
|
|
|
2011-08-27 13:51:50 -04:00
|
|
|
extras = {}
|
2022-10-07 16:51:40 -04:00
|
|
|
if "parent" in args:
|
|
|
|
extras["parent"] = self.machine.parent
|
|
|
|
if "time" in args:
|
|
|
|
extras["time"] = self.time
|
2023-02-14 13:55:23 -05:00
|
|
|
if k in ("libnotify", "twmn"):
|
2011-08-27 13:51:50 -04:00
|
|
|
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:
|
2022-10-07 16:51:40 -04:00
|
|
|
if "default" in self.machine.types:
|
|
|
|
if (
|
|
|
|
"parent"
|
2023-05-04 14:08:33 -04:00
|
|
|
in inspect.getfullargspec(
|
2022-10-07 16:51:40 -04:00
|
|
|
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:
|
2022-10-07 16:51:40 -04:00
|
|
|
t = self.machine.types["default"](
|
|
|
|
self.machine, self.title, self.msg, self.icon
|
|
|
|
)
|
2011-08-16 03:27:28 -04:00
|
|
|
else:
|
2023-02-12 19:21:14 -05:00
|
|
|
t = DefaultToast(self.machine, self.title, self.msg, self.icon)
|
2011-08-16 03:27:28 -04:00
|
|
|
t.show()
|
2011-08-25 08:30:43 -04:00
|
|
|
|
2022-10-07 16:51:40 -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
|
|
|
|
self.name = name
|
|
|
|
self.on = on
|
2011-08-16 03:27:28 -04:00
|
|
|
types.update(extras)
|
2022-10-07 16:51:40 -04:00
|
|
|
self.types = types
|
|
|
|
self.type = "default"
|
|
|
|
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):
|
2022-10-07 16:51:40 -04:00
|
|
|
self.on = on is True
|
2011-08-25 08:30:43 -04:00
|
|
|
|
|
|
|
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"):
|
2021-08-10 16:45:48 -04:00
|
|
|
PchumLog.info("Problem initilizing pynotify")
|
2011-08-25 08:30:43 -04:00
|
|
|
return
|
2022-10-07 16:51:40 -04:00
|
|
|
# self.type = type = "default"
|
2011-08-25 08:30:43 -04:00
|
|
|
elif type == "twmn":
|
2021-03-24 17:34:55 -04:00
|
|
|
import pytwmn
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2011-08-25 08:30:43 -04:00
|
|
|
try:
|
|
|
|
pytwmn.init()
|
2023-02-12 19:34:13 -05:00
|
|
|
except Exception:
|
2023-02-12 19:21:14 -05:00
|
|
|
PchumLog.exception("Problem initilizing pytwmn.")
|
2011-08-25 08:30:43 -04:00
|
|
|
return
|
2022-10-07 16:51:40 -04:00
|
|
|
# self.type = type = "default"
|
2011-08-25 08:30:43 -04:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2021-03-23 17:36:43 -04:00
|
|
|
class PesterToast(QtWidgets.QWidget, DefaultToast):
|
2011-08-27 13:51:50 -04:00
|
|
|
def __init__(self, machine, title, msg, icon, time=3000, parent=None):
|
2023-02-12 19:21:14 -05:00
|
|
|
# FIXME: Not sure how this works exactly either xd, can't we init the parents seperately?
|
2023-05-03 13:29:09 -04:00
|
|
|
kwds = {
|
|
|
|
"parent": parent,
|
|
|
|
"machine": machine,
|
|
|
|
"title": title,
|
|
|
|
"msg": msg,
|
|
|
|
"icon": icon,
|
|
|
|
}
|
2023-02-12 19:21:14 -05:00
|
|
|
super().__init__(**kwds)
|
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():
|
2022-06-26 22:18:37 -04:00
|
|
|
self.setWindowFlags(QtCore.Qt.WindowType.ToolTip)
|
2011-08-29 05:28:29 -04:00
|
|
|
else:
|
2022-10-07 16:51:40 -04:00
|
|
|
self.setWindowFlags(
|
|
|
|
QtCore.Qt.WindowType.WindowStaysOnTopHint
|
|
|
|
| QtCore.Qt.WindowType.X11BypassWindowManagerHint
|
|
|
|
| QtCore.Qt.WindowType.ToolTip
|
|
|
|
)
|
2011-08-27 13:51:50 -04:00
|
|
|
|
|
|
|
self.m_animation = QtCore.QParallelAnimationGroup()
|
2016-11-13 01:14:39 -05:00
|
|
|
anim = QtCore.QPropertyAnimation(self)
|
2011-08-27 13:51:50 -04:00
|
|
|
anim.setTargetObject(self)
|
|
|
|
self.m_animation.addAnimation(anim)
|
2022-06-26 22:18:37 -04:00
|
|
|
anim.setEasingCurve(QtCore.QEasingCurve.Type.OutBounce)
|
2011-08-27 13:51:50 -04:00
|
|
|
anim.setDuration(1000)
|
2021-03-23 17:36:43 -04:00
|
|
|
anim.finished.connect(self.reverseTrigger)
|
2011-08-27 13:51:50 -04:00
|
|
|
|
2022-06-26 22:18:37 -04:00
|
|
|
self.m_animation.setDirection(QtCore.QAbstractAnimation.Direction.Forward)
|
2011-08-16 03:27:28 -04:00
|
|
|
|
2021-03-23 17:36:43 -04:00
|
|
|
self.title = QtWidgets.QLabel(title, self)
|
|
|
|
self.msg = QtWidgets.QLabel(msg, self)
|
2011-08-27 13:51:50 -04:00
|
|
|
self.content = msg
|
|
|
|
if icon:
|
2021-03-23 17:36:43 -04:00
|
|
|
self.icon = QtWidgets.QLabel("")
|
2022-06-30 03:59:34 -04:00
|
|
|
iconPixmap = QtGui.QPixmap(icon).scaledToWidth(30)
|
|
|
|
self.icon.setPixmap(iconPixmap)
|
2022-10-07 16:51:40 -04:00
|
|
|
# else:
|
2022-06-30 03:59:34 -04:00
|
|
|
# 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
|
|
|
|
2021-03-23 17:36:43 -04:00
|
|
|
layout_0 = QtWidgets.QVBoxLayout()
|
|
|
|
layout_0.setContentsMargins(0, 0, 0, 0)
|
2011-08-27 13:51:50 -04:00
|
|
|
|
|
|
|
if self.icon:
|
2021-03-23 17:36:43 -04:00
|
|
|
layout_1 = QtWidgets.QGridLayout()
|
2022-10-07 16:51:40 -04:00
|
|
|
layout_1.addWidget(self.icon, 0, 0, 1, 1)
|
|
|
|
layout_1.addWidget(self.title, 0, 1, 1, 7)
|
2022-06-26 22:18:37 -04:00
|
|
|
layout_1.setAlignment(self.msg, QtCore.Qt.AlignmentFlag.AlignTop)
|
2011-08-27 13:51:50 -04:00
|
|
|
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)
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
self.setGeometry(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
self.parent().theme["toasts/width"],
|
|
|
|
self.parent().theme["toasts/height"],
|
|
|
|
)
|
2011-08-27 13:51:50 -04:00
|
|
|
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)
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
self.msg.setText(
|
|
|
|
PesterToast.wrapText(
|
|
|
|
self.msg.font(),
|
2023-02-19 15:10:35 -05:00
|
|
|
self.msg.text(),
|
2022-10-07 16:51:40 -04:00
|
|
|
self.parent().theme["toasts/width"],
|
|
|
|
self.parent().theme["toasts/content/style"],
|
|
|
|
)
|
|
|
|
)
|
2022-06-30 03:59:34 -04:00
|
|
|
|
|
|
|
screens = QtWidgets.QApplication.screens()
|
2022-10-07 16:51:40 -04:00
|
|
|
screen = screens[0] # Should be the main one right???
|
2022-06-30 03:59:34 -04:00
|
|
|
# This 100% doesn't work with multiple screens.
|
|
|
|
p = screen.availableGeometry().bottomRight()
|
|
|
|
o = screen.geometry().bottomRight()
|
|
|
|
anim.setStartValue(p.y() - o.y())
|
|
|
|
anim.setEndValue(100)
|
|
|
|
anim.valueChanged[QtCore.QVariant].connect(self.updateBottomLeftAnimation)
|
2011-08-27 13:51:50 -04:00
|
|
|
|
|
|
|
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):
|
2021-03-23 17:36:43 -04:00
|
|
|
QtWidgets.QWidget.hide(self)
|
2011-08-27 13:51:50 -04:00
|
|
|
t = self.machine.toasts[0]
|
2023-02-19 15:10:35 -05:00
|
|
|
if t.title == self.title.text() and t.msg == self.content:
|
2011-08-27 13:51:50 -04:00
|
|
|
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:
|
2021-03-24 14:20:47 -04:00
|
|
|
QtCore.QTimer.singleShot(self.time, self.reverseStart)
|
2011-08-27 13:51:50 -04:00
|
|
|
|
|
|
|
@QtCore.pyqtSlot()
|
|
|
|
def reverseStart(self):
|
|
|
|
if not self.byebye:
|
|
|
|
self.byebye = True
|
|
|
|
anim = self.m_animation.animationAt(0)
|
2022-06-26 22:18:37 -04:00
|
|
|
self.m_animation.setDirection(QtCore.QAbstractAnimation.Direction.Backward)
|
|
|
|
anim.setEasingCurve(QtCore.QEasingCurve.Type.InCubic)
|
2021-03-23 17:36:43 -04:00
|
|
|
anim.finished.disconnect(self.reverseTrigger)
|
|
|
|
anim.finished.connect(self.done)
|
2011-08-27 13:51:50 -04:00
|
|
|
self.m_animation.start()
|
|
|
|
|
|
|
|
@QtCore.pyqtSlot(QtCore.QVariant)
|
|
|
|
def updateBottomLeftAnimation(self, value):
|
2022-10-07 16:51:40 -04:00
|
|
|
# p = QtWidgets.QApplication.desktop().availableGeometry(self).bottomRight()
|
2022-06-30 04:15:06 -04:00
|
|
|
screens = QtWidgets.QApplication.screens()
|
|
|
|
screen = screens[0] # Main window?
|
|
|
|
p = screen.availableGeometry().bottomRight()
|
2022-10-07 16:51:40 -04:00
|
|
|
val = (self.height()) / 100
|
2021-11-29 17:09:32 -05:00
|
|
|
# Does type casting this to an int have any negative consequences?
|
2022-10-07 16:51:40 -04:00
|
|
|
self.move(int(p.x() - self.width()), int(p.y() - (value * val) + 1))
|
2011-08-27 13:51:50 -04:00
|
|
|
self.layout().setSpacing(0)
|
2021-03-23 17:36:43 -04:00
|
|
|
QtWidgets.QWidget.show(self)
|
2011-08-27 13:51:50 -04:00
|
|
|
|
|
|
|
def mousePressEvent(self, event):
|
2022-06-26 22:18:37 -04:00
|
|
|
if event.button() == QtCore.Qt.MouseButton.RightButton:
|
2011-08-27 13:51:50 -04:00
|
|
|
self.reverseStart()
|
2022-06-26 22:18:37 -04:00
|
|
|
elif event.button() == QtCore.Qt.MouseButton.LeftButton:
|
2011-08-27 13:51:50 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def wrapText(font, text, maxwidth, css=""):
|
|
|
|
ret = []
|
|
|
|
metric = QtGui.QFontMetrics(font)
|
|
|
|
if "padding" in css:
|
2022-10-07 16:51:40 -04:00
|
|
|
if css[css.find("padding") + 7] != "-":
|
2011-08-27 13:51:50 -04:00
|
|
|
colon = css.find(":", css.find("padding"))
|
|
|
|
semicolon = css.find(";", css.find("padding"))
|
|
|
|
if semicolon < 0:
|
2022-10-07 16:51:40 -04:00
|
|
|
stuff = css[colon + 1 :]
|
2011-08-27 13:51:50 -04:00
|
|
|
else:
|
2022-10-07 16:51:40 -04:00
|
|
|
stuff = css[colon + 1 : semicolon]
|
2011-08-27 13:51:50 -04:00
|
|
|
stuff = stuff.replace("px", "").lstrip().rstrip()
|
|
|
|
stuff = stuff.split(" ")
|
|
|
|
if len(stuff) == 1:
|
2022-10-07 16:51:40 -04:00
|
|
|
maxwidth -= int(stuff[0]) * 2
|
2011-08-27 13:51:50 -04:00
|
|
|
elif len(stuff) == 2:
|
2022-10-07 16:51:40 -04:00
|
|
|
maxwidth -= int(stuff[1]) * 2
|
2011-08-27 13:51:50 -04:00
|
|
|
elif len(stuff) == 3:
|
2022-10-07 16:51:40 -04:00
|
|
|
maxwidth -= int(stuff[1]) * 2
|
2011-08-27 13:51:50 -04:00
|
|
|
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:
|
2022-10-07 16:51:40 -04:00
|
|
|
stuff = css[colon + 1 :]
|
2011-08-27 13:51:50 -04:00
|
|
|
else:
|
2022-10-07 16:51:40 -04:00
|
|
|
stuff = css[colon + 1 : semicolon]
|
2011-08-27 13:51:50 -04:00
|
|
|
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:
|
2022-10-07 16:51:40 -04:00
|
|
|
stuff = css[colon + 1 :]
|
2011-08-27 13:51:50 -04:00
|
|
|
else:
|
2022-10-07 16:51:40 -04:00
|
|
|
stuff = css[colon + 1 : semicolon]
|
2011-08-27 13:51:50 -04:00
|
|
|
stuff = stuff.replace("px", "").lstrip().rstrip()
|
|
|
|
if stuff.isdigit():
|
|
|
|
maxwidth -= int(stuff)
|
|
|
|
|
2022-06-26 22:18:37 -04:00
|
|
|
if metric.horizontalAdvance(text) < maxwidth:
|
2011-08-27 13:51:50 -04:00
|
|
|
return text
|
2022-06-26 22:18:37 -04:00
|
|
|
while metric.horizontalAdvance(text) > maxwidth:
|
2011-08-27 13:51:50 -04:00
|
|
|
lastspace = text.find(" ")
|
|
|
|
curspace = lastspace
|
2022-06-26 22:18:37 -04:00
|
|
|
while metric.horizontalAdvance(text, curspace) < maxwidth:
|
2011-08-27 13:51:50 -04:00
|
|
|
lastspace = curspace
|
2022-10-07 16:51:40 -04:00
|
|
|
curspace = text.find(" ", lastspace + 1)
|
2011-08-29 13:28:41 -04:00
|
|
|
if curspace == -1:
|
|
|
|
break
|
2022-10-07 16:51:40 -04:00
|
|
|
if (metric.horizontalAdvance(text[:lastspace]) > maxwidth) or len(
|
|
|
|
text[:lastspace]
|
|
|
|
) < 1:
|
2021-03-23 17:36:43 -04:00
|
|
|
for i in range(len(text)):
|
2022-06-26 22:18:37 -04:00
|
|
|
if metric.horizontalAdvance(text[:i]) > maxwidth:
|
2022-10-07 16:51:40 -04:00
|
|
|
lastspace = i - 1
|
2011-08-29 13:28:41 -04:00
|
|
|
break
|
2011-08-27 13:51:50 -04:00
|
|
|
ret.append(text[:lastspace])
|
2022-10-07 16:51:40 -04:00
|
|
|
text = text[lastspace + 1 :]
|
2011-08-27 13:51:50 -04:00
|
|
|
ret.append(text)
|
|
|
|
return "\n".join(ret)
|
|
|
|
|
2011-08-16 03:27:28 -04:00
|
|
|
|
|
|
|
class PesterToastMachine(ToastMachine, QtCore.QObject):
|
2022-10-07 16:51:40 -04:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
parent,
|
|
|
|
name,
|
|
|
|
on=True,
|
|
|
|
type="default",
|
|
|
|
types=(
|
|
|
|
{"default": DefaultToast, "libnotify": pynotify.Notification}
|
|
|
|
if pynotify
|
|
|
|
else {"default": DefaultToast}
|
|
|
|
),
|
|
|
|
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:
|
2022-10-07 16:51:40 -04:00
|
|
|
self.parent.config.set("notify", self.on)
|
2011-08-25 08:30:43 -04:00
|
|
|
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:
|
2022-10-07 16:51:40 -04:00
|
|
|
self.parent.config.set("notifyType", self.type)
|
2011-08-25 08:30:43 -04:00
|
|
|
|
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
|
2022-10-07 16:51:40 -04:00
|
|
|
# ~ self.timer = QtCore.QTimer(self)
|
|
|
|
# ~ self.timer.setInterval(1000)
|
|
|
|
# ~ self.connect(self.timer, QtCore.SIGNAL('timeout()'),
|
|
|
|
# ~ self, QtCore.SLOT('showNext()'))
|
|
|
|
# ~ if self.on:
|
2022-10-08 08:30:56 -04:00
|
|
|
# ~ self.timer.start()
|