UI/NotificationWidget.py

Wed, 02 Jan 2013 10:31:48 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 02 Jan 2013 10:31:48 +0100
changeset 2302
f29e9405c851
parent 2190
abd65b78425e
child 2525
8b507a9a2d40
child 3012
d177226027e2
child 3163
9f50365a0870
permissions
-rw-r--r--

Updated copyright for 2013.

# -*- coding: utf-8 -*-

# Copyright (c) 2012 - 2013 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()

eric ide

mercurial