basic image pop-up on hover

This commit is contained in:
dries.k
2022-08-28 15:22:41 +02:00
parent 548b49f610
commit 28fef2c9de
9 changed files with 126 additions and 18 deletions

View File

@@ -0,0 +1,46 @@
from PyQt6 import QtCore, QtGui, QtWidgets
from gotify_tray.database import Settings
settings = Settings("gotify-tray")
class ImagePopup(QtWidgets.QLabel):
def __init__(self, filename: str, pos: QtCore.QPoint, link: str = None):
"""Create and show a pop-up image under the cursor
Args:
filename (str): The path to the image to display
pos (QtCore.QPoint): The location at which the image should be displayed
link (str, optional): The URL of the image. Defaults to None.
"""
super(ImagePopup, self).__init__()
self.link = link
self.setWindowFlags(QtCore.Qt.WindowType.ToolTip)
self.installEventFilter(self)
self.setPixmap(
QtGui.QPixmap(filename).scaled(
settings.value("ImagePopup/w", type=int),
settings.value("ImagePopup/h", type=int),
QtCore.Qt.AspectRatioMode.KeepAspectRatio,
)
)
self.move(pos - QtCore.QPoint(30, 30))
self.show()
def eventFilter(self, object: QtCore.QObject, event: QtCore.QEvent) -> bool:
if event.type() == QtCore.QEvent.Type.Leave:
# Close the pop-up on mouse leave
self.close()
elif (
event.type() == QtCore.QEvent.Type.MouseButtonPress
and event.button() == QtCore.Qt.MouseButton.LeftButton
and self.link
):
# Open the image URL on left click
QtGui.QDesktopServices.openUrl(QtCore.QUrl(self.link))
return super().eventFilter(object, event)