Exemplo n.º 1
0
class Notification(QMainWindow):
    '''Transparent window containing an image or animated gif, used to display notifications.'''
    def __init__(self,
                 name,
                 notifier,
                 artPath,
                 link,
                 sound=None,
                 *args,
                 **kwargs):
        '''- name : a string
        - notifier : a Notifier object used to manage this notification
        - artPath : a string containing the path to the image file to be displayed
        - link : a string containing the link to be opened when left-clicking the notification
        - sound : a string containing the path to the object or None. Set to None for silent notifiations.'''
        super().__init__(*args, **kwargs)
        self.setCursor(QCursor(Qt.PointingHandCursor))
        self.name = name
        self.notifier = notifier
        self.link = link
        self.artPath = artPath
        self.isMovie = self.artPath.endswith(".gif")
        if sound == None:
            self.sound = None
        else:
            self.sound = QSoundEffect()
            self.sound.setSource(QUrl.fromLocalFile(sound))
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint
                            | Qt.Tool)
        self.setAttribute(Qt.WA_TranslucentBackground)
        imageLabel = QLabel()
        self.setCentralWidget(imageLabel)
        if self.isMovie:
            self.art = QMovie(self.artPath)
            self.art.jumpToNextFrame()
            imageLabel.setMovie(self.art)
            self.moveToBottomRight(self.art.frameRect().width(),
                                   self.art.frameRect().height())
        else:
            self.art = QPixmap(self.artPath)
            imageLabel.setPixmap(self.art)
            self.moveToBottomRight(self.art.width(), self.art.height())

    def moveToBottomRight(self, x, y):
        '''Moves the notification window to the bottom-right of the screen, above the taskbar'''
        screen = QDesktopWidget().availableGeometry()
        x_pos = screen.width() - x
        y_pos = screen.height() - y
        self.move(x_pos, y_pos)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.close()
            webbrowser.open_new_tab(self.link)
        elif event.button() == Qt.RightButton:
            self.close()

    def display(self, volume=1):
        '''Show the notification window and plays the sound.'''
        super().show()
        if self.isMovie:
            self.art.start()
        if self.sound:
            self.sound.setVolume(volume)
            self.sound.play()

    def close(self):
        '''Updates notifier and closes window'''
        super().close()
        self.notifier.update()