From 6b2536916fb547374215b2eef9ab8f6714fc43bc Mon Sep 17 00:00:00 2001 From: "dries.k" Date: Sun, 28 Aug 2022 21:24:41 +0200 Subject: [PATCH] track underMouse --- gotify_tray/gui/MainApplication.py | 1 + gotify_tray/gui/widgets/ImagePopup.py | 33 +++++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/gotify_tray/gui/MainApplication.py b/gotify_tray/gui/MainApplication.py index 3bba6ba..c99b802 100644 --- a/gotify_tray/gui/MainApplication.py +++ b/gotify_tray/gui/MainApplication.py @@ -313,6 +313,7 @@ class MainApplication(QtWidgets.QApplication): def image_popup_callback(self, link: str, pos: QtCore.QPoint): if (filename := self.cache.lookup(link)) or (filename := self.downloader.get_filename(link)): # TODO: preload links self.image_popup = ImagePopup(filename, pos, link) + self.image_popup.show() else: # TODO logger.warning(f"Image {link} is not in the cache") diff --git a/gotify_tray/gui/widgets/ImagePopup.py b/gotify_tray/gui/widgets/ImagePopup.py index 63fdd32..b8799bf 100644 --- a/gotify_tray/gui/widgets/ImagePopup.py +++ b/gotify_tray/gui/widgets/ImagePopup.py @@ -20,18 +20,31 @@ class ImagePopup(QtWidgets.QLabel): self.setWindowFlags(QtCore.Qt.WindowType.ToolTip) self.installEventFilter(self) - - pixmap = QtGui.QPixmap(filename).scaled( - settings.value("ImagePopup/w", type=int), - settings.value("ImagePopup/h", type=int), - aspectRatioMode=QtCore.Qt.AspectRatioMode.KeepAspectRatio, - transformMode=QtCore.Qt.TransformationMode.SmoothTransformation - ) - self.setPixmap(pixmap) - self.move(pos - QtCore.QPoint(pixmap.width()/2, pixmap.height()/2)) - self.show() + # Prevent leaving the pop-up open when moving quickly out of the widget + self.popup_timer = QtCore.QTimer() + self.popup_timer.timeout.connect(self.check_mouse) + + pixmap = QtGui.QPixmap(filename).scaled( + settings.value("ImagePopup/w", type=int), + settings.value("ImagePopup/h", type=int), + aspectRatioMode=QtCore.Qt.AspectRatioMode.KeepAspectRatio, + transformMode=QtCore.Qt.TransformationMode.SmoothTransformation, + ) + self.setPixmap(pixmap) + self.move(pos - QtCore.QPoint(15, 15)) + + self.popup_timer.start(500) + + def check_mouse(self): + if not self.underMouse(): + self.close() + + def close(self): + self.popup_timer.stop() + super(ImagePopup, self).close() + def eventFilter(self, object: QtCore.QObject, event: QtCore.QEvent) -> bool: if event.type() == QtCore.QEvent.Type.Leave: # Close the pop-up on mouse leave