diff -r 5149cec53130 -r abd65b78425e UI/NotificationWidget.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UI/NotificationWidget.py Sun Nov 04 16:55:20 2012 +0100 @@ -0,0 +1,135 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing a Notification widget. +""" + +from PyQt4.QtCore import Qt, QTimer, QPoint +from PyQt4.QtGui import QWidget, QPixmap + +from .Ui_NotificationWidget import Ui_NotificationWidget + +import Globals + + +class NotificationWidget(QWidget, Ui_NotificationWidget): + """ + Class implementing a Notification widget. + """ + def __init__(self, parent=None, setPosition=False): + """ + Constructor + + @param parent reference to the parent widget (QWidget) + @param setPosition flag indicating to set the display + position interactively (boolean) + """ + super().__init__(parent) + self.setupUi(self) + + self.__timeout = 5000 + self.__icon = QPixmap() + self.__heading = "" + self.__text = "" + self.__dragPosition = QPoint() + + self.__settingPosition = setPosition + + flags = Qt.Tool | \ + Qt.FramelessWindowHint | \ + Qt.WindowStaysOnTopHint | \ + Qt.X11BypassWindowManagerHint + if Globals.isWindowsPlatform(): + flags |= Qt.ToolTip + self.setWindowFlags(flags) + + self.__timer = QTimer(self) + self.__timer.setSingleShot(True) + self.__timer.timeout.connect(self.close) + + if self.__settingPosition: + self.setCursor(Qt.OpenHandCursor) + + def setPixmap(self, icon): + """ + Public method to set the icon for the notification. + + @param icon icon to be used (QPixmap) + """ + self.__icon = QPixmap(icon) + + def setHeading(self, heading): + """ + Public method to set the heading for the notification. + + @param heading heading to be used (string) + """ + self.__heading = heading + + def setText(self, text): + """ + Public method to set the text for the notification. + + @param text text to be used (string) + """ + self.__text = text + + def setTimeout(self, timeout): + """ + Public method to set the timeout for the notification. + + @param timeout timeout to be used in seconds (integer) + """ + self.__timeout = timeout * 1000 + + def show(self): + """ + Public method to show the notification. + """ + self.icon.setPixmap(self.__icon) + self.heading.setText(self.__heading) + self.text.setText(self.__text) + + if not self.__settingPosition: + self.__timer.stop() + self.__timer.setInterval(self.__timeout) + self.__timer.start() + + super().show() + + def mousePressEvent(self, evt): + """ + Protected method to handle presses of a mouse button. + + @param evt reference to the mouse event (QMouseEvent) + """ + if not self.__settingPosition: + self.close() + return + + if evt.button() == Qt.LeftButton: + self.__dragPosition = evt.globalPos() - self.frameGeometry().topLeft() + self.setCursor(Qt.ClosedHandCursor) + evt.accept() + + def mouseReleaseEvent(self, evt): + """ + Protected method to handle releases of a mouse button. + + @param evt reference to the mouse event (QMouseEvent) + """ + if self.__settingPosition and evt.button() == Qt.LeftButton: + self.setCursor(Qt.OpenHandCursor) + + def mouseMoveEvent(self, evt): + """ + Protected method to handle dragging the window. + + @param evt reference to the mouse event (QMouseEvent) + """ + if evt.buttons() & Qt.LeftButton: + self.move(evt.globalPos() - self.__dragPosition) + evt.accept()