UI/NotificationWidget.py

Mon, 25 Mar 2013 03:11:06 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Mon, 25 Mar 2013 03:11:06 +0100
branch
Py2 comp.
changeset 2525
8b507a9a2d40
parent 2302
f29e9405c851
child 3057
10516539f238
permissions
-rw-r--r--

Script changes: Future import added, super calls modified and unicode behavior for str.

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

# Copyright (c) 2012 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a Notification widget.
"""

from __future__ import unicode_literals    # __IGNORE_WARNING__

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(NotificationWidget, self).__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(NotificationWidget, self).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