basic image pop-up on hover
This commit is contained in:
46
gotify_tray/gui/widgets/ImagePopup.py
Normal file
46
gotify_tray/gui/widgets/ImagePopup.py
Normal 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)
|
||||
Reference in New Issue
Block a user