render images
This commit is contained in:
@@ -16,6 +16,8 @@ from gotify_tray.tasks import (
|
||||
GetApplicationsTask,
|
||||
GetApplicationMessagesTask,
|
||||
GetMessagesTask,
|
||||
ProcessMessageTask,
|
||||
ProcessMessagesTask,
|
||||
ServerConnectionWatchdogTask,
|
||||
)
|
||||
from gotify_tray.gui.themes import set_theme
|
||||
@@ -74,6 +76,8 @@ class MainApplication(QtWidgets.QApplication):
|
||||
self.application_model = ApplicationModel()
|
||||
|
||||
self.main_window = MainWindow(self.application_model, self.messages_model)
|
||||
self.main_window.show() # The initial .show() is necessary to get the correct sizes when adding MessageWigets
|
||||
self.main_window.hide()
|
||||
|
||||
self.refresh_applications()
|
||||
|
||||
@@ -195,43 +199,39 @@ class MainApplication(QtWidgets.QApplication):
|
||||
),
|
||||
)
|
||||
|
||||
def get_messages_finished_callback(self, page: gotify.GotifyPagedMessagesModel):
|
||||
"""Process messages before inserting them into the main window
|
||||
"""
|
||||
|
||||
def insert_helper(row: int, message: gotify.GotifyMessageModel):
|
||||
if item := self.application_model.itemFromId(message.appid):
|
||||
self.insert_message(
|
||||
row, message, item.data(ApplicationItemDataRole.ApplicationRole),
|
||||
)
|
||||
self.processEvents()
|
||||
|
||||
self.process_messages_task = ProcessMessagesTask(page)
|
||||
self.process_messages_task.message_processed.connect(insert_helper)
|
||||
self.process_messages_task.start()
|
||||
|
||||
def application_selection_changed_callback(
|
||||
self, item: Union[ApplicationModelItem, ApplicationAllMessagesItem]
|
||||
):
|
||||
self.messages_model.clear()
|
||||
|
||||
if isinstance(item, ApplicationModelItem):
|
||||
|
||||
def get_application_messages_callback(
|
||||
page: gotify.GotifyPagedMessagesModel,
|
||||
):
|
||||
for i, message in enumerate(page.messages):
|
||||
self.insert_message(
|
||||
i, message, item.data(ApplicationItemDataRole.ApplicationRole),
|
||||
)
|
||||
|
||||
self.get_application_messages_task = GetApplicationMessagesTask(
|
||||
item.data(ApplicationItemDataRole.ApplicationRole).id,
|
||||
self.gotify_client,
|
||||
)
|
||||
self.get_application_messages_task.success.connect(
|
||||
get_application_messages_callback
|
||||
self.get_messages_finished_callback
|
||||
)
|
||||
self.get_application_messages_task.start()
|
||||
|
||||
elif isinstance(item, ApplicationAllMessagesItem):
|
||||
|
||||
def get_messages_callback(page: gotify.GotifyPagedMessagesModel):
|
||||
for i, message in enumerate(page.messages):
|
||||
if item := self.application_model.itemFromId(message.appid):
|
||||
self.insert_message(
|
||||
i,
|
||||
message,
|
||||
item.data(ApplicationItemDataRole.ApplicationRole),
|
||||
)
|
||||
|
||||
self.get_messages_task = GetMessagesTask(self.gotify_client)
|
||||
self.get_messages_task.success.connect(get_messages_callback)
|
||||
self.get_messages_task.success.connect(self.get_messages_finished_callback)
|
||||
self.get_messages_task.start()
|
||||
|
||||
def add_message_to_model(self, message: gotify.GotifyMessageModel):
|
||||
@@ -240,14 +240,27 @@ class MainApplication(QtWidgets.QApplication):
|
||||
if selected_application_item := self.application_model.itemFromIndex(
|
||||
application_index
|
||||
):
|
||||
if isinstance(selected_application_item, ApplicationModelItem):
|
||||
# A single application is selected
|
||||
if (
|
||||
message.appid
|
||||
== selected_application_item.data(
|
||||
ApplicationItemDataRole.ApplicationRole
|
||||
).id
|
||||
|
||||
def insert_message_helper():
|
||||
if isinstance(selected_application_item, ApplicationModelItem):
|
||||
# A single application is selected
|
||||
if (
|
||||
message.appid
|
||||
== selected_application_item.data(
|
||||
ApplicationItemDataRole.ApplicationRole
|
||||
).id
|
||||
):
|
||||
self.insert_message(
|
||||
0,
|
||||
message,
|
||||
application_item.data(
|
||||
ApplicationItemDataRole.ApplicationRole
|
||||
),
|
||||
)
|
||||
elif isinstance(
|
||||
selected_application_item, ApplicationAllMessagesItem
|
||||
):
|
||||
# "All messages' is selected
|
||||
self.insert_message(
|
||||
0,
|
||||
message,
|
||||
@@ -255,13 +268,15 @@ class MainApplication(QtWidgets.QApplication):
|
||||
ApplicationItemDataRole.ApplicationRole
|
||||
),
|
||||
)
|
||||
elif isinstance(selected_application_item, ApplicationAllMessagesItem):
|
||||
# "All messages' is selected
|
||||
self.insert_message(
|
||||
0,
|
||||
message,
|
||||
application_item.data(ApplicationItemDataRole.ApplicationRole),
|
||||
)
|
||||
|
||||
self.process_message_task = ProcessMessageTask(message)
|
||||
self.process_message_task.finished.connect(insert_message_helper)
|
||||
self.process_message_task.start()
|
||||
else:
|
||||
logger.error(
|
||||
f"App id {message.appid} could not be found. Refreshing applications."
|
||||
)
|
||||
self.refresh_applications()
|
||||
|
||||
def new_message_callback(self, message: gotify.GotifyMessageModel):
|
||||
self.add_message_to_model(message)
|
||||
|
||||
@@ -104,7 +104,9 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
def insert_message_widget(
|
||||
self, message_item: MessagesModelItem, image_path: str = ""
|
||||
):
|
||||
message_widget = MessageWidget(message_item, image_path=image_path)
|
||||
message_widget = MessageWidget(
|
||||
self.listView_messages, message_item, image_path=image_path
|
||||
)
|
||||
self.listView_messages.setIndexWidget(
|
||||
self.messages_model.indexFromItem(message_item), message_widget
|
||||
)
|
||||
|
||||
@@ -4,9 +4,11 @@ from PyQt6 import QtCore, QtGui, QtWidgets
|
||||
|
||||
from ..models.MessagesModel import MessageItemDataRole, MessagesModelItem
|
||||
from ..designs.widget_message import Ui_Form
|
||||
from gotify_tray.database import Downloader
|
||||
from gotify_tray.database import Settings
|
||||
from gotify_tray.utils import convert_links
|
||||
from gotify_tray.utils import convert_links, get_image
|
||||
from gotify_tray.gui.themes import get_theme_file
|
||||
from gotify_tray.gotify.models import GotifyMessageModel
|
||||
|
||||
|
||||
settings = Settings("gotify-tray")
|
||||
@@ -16,13 +18,18 @@ class MessageWidget(QtWidgets.QWidget, Ui_Form):
|
||||
deletion_requested = QtCore.pyqtSignal(MessagesModelItem)
|
||||
image_popup = QtCore.pyqtSignal(str, QtCore.QPoint)
|
||||
|
||||
def __init__(self, message_item: MessagesModelItem, image_path: str = ""):
|
||||
def __init__(
|
||||
self,
|
||||
parent: QtWidgets.QWidget,
|
||||
message_item: MessagesModelItem,
|
||||
image_path: str = "",
|
||||
):
|
||||
super(MessageWidget, self).__init__()
|
||||
self.parent = parent
|
||||
self.setupUi(self)
|
||||
self.setAutoFillBackground(True)
|
||||
|
||||
self.message_item = message_item
|
||||
message = message_item.data(MessageItemDataRole.MessageRole)
|
||||
message: GotifyMessageModel = message_item.data(MessageItemDataRole.MessageRole)
|
||||
|
||||
# Fonts
|
||||
self.set_fonts()
|
||||
@@ -31,14 +38,20 @@ class MessageWidget(QtWidgets.QWidget, Ui_Form):
|
||||
self.label_title.setText(message.title)
|
||||
self.label_date.setText(message.date.strftime("%Y-%m-%d, %H:%M"))
|
||||
|
||||
if (
|
||||
markdown := message.get("extras", {})
|
||||
.get("client::display", {})
|
||||
.get("contentType")
|
||||
) == "text/markdown":
|
||||
if markdown := (
|
||||
message.get("extras", {}).get("client::display", {}).get("contentType")
|
||||
== "text/markdown"
|
||||
):
|
||||
self.label_message.setTextFormat(QtCore.Qt.TextFormat.MarkdownText)
|
||||
|
||||
self.label_message.setText(convert_links(message.message))
|
||||
# If the message is only an image URL, then instead of showing the message,
|
||||
# download the image and show it in the message label
|
||||
if image_url := get_image(message.message):
|
||||
downloader = Downloader()
|
||||
filename = downloader.get_filename(image_url)
|
||||
self.set_message_image(filename)
|
||||
else:
|
||||
self.label_message.setText(convert_links(message.message))
|
||||
|
||||
# Show the application icon
|
||||
if image_path:
|
||||
@@ -91,6 +104,21 @@ class MessageWidget(QtWidgets.QWidget, Ui_Form):
|
||||
self.pb_delete.setIcon(QtGui.QIcon(get_theme_file("trashcan.svg")))
|
||||
self.pb_delete.setIconSize(QtCore.QSize(24, 24))
|
||||
|
||||
def set_message_image(self, filename: str):
|
||||
pixmap = QtGui.QPixmap(filename)
|
||||
|
||||
# Make sure the image fits within the listView
|
||||
W = self.parent.width() - self.label_image.width() - 50
|
||||
if pixmap.width() > W:
|
||||
pixmap = pixmap.scaled(
|
||||
W,
|
||||
0.5 * self.parent.height(),
|
||||
aspectRatioMode=QtCore.Qt.AspectRatioMode.KeepAspectRatio,
|
||||
transformMode=QtCore.Qt.TransformationMode.SmoothTransformation,
|
||||
)
|
||||
|
||||
self.label_message.setPixmap(pixmap)
|
||||
|
||||
def link_hovered_callback(self, link: str):
|
||||
if not settings.value("ImagePopup/enabled", type=bool):
|
||||
return
|
||||
|
||||
@@ -94,6 +94,7 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
|
||||
|
||||
def add_message_widget(self):
|
||||
self.message_widget = MessageWidget(
|
||||
self,
|
||||
MessagesModelItem(
|
||||
GotifyMessageModel(
|
||||
{
|
||||
|
||||
@@ -5,9 +5,10 @@ import time
|
||||
from PyQt6 import QtCore
|
||||
from PyQt6.QtCore import pyqtSignal
|
||||
|
||||
from gotify_tray.database import Settings
|
||||
from gotify_tray.database import Downloader, Settings
|
||||
from gotify_tray.gotify.api import GotifyClient
|
||||
from gotify_tray.gotify.models import GotifyVersionModel
|
||||
from gotify_tray.utils import get_image
|
||||
|
||||
from . import gotify
|
||||
|
||||
@@ -199,3 +200,28 @@ class ImportSettingsTask(BaseTask):
|
||||
def task(self):
|
||||
settings.load(self.path)
|
||||
self.success.emit()
|
||||
|
||||
|
||||
class ProcessMessageTask(BaseTask):
|
||||
def __init__(self, message: gotify.GotifyMessageModel):
|
||||
super(ProcessMessageTask, self).__init__()
|
||||
self.message = message
|
||||
|
||||
def task(self):
|
||||
if image_url := get_image(self.message.message):
|
||||
downloader = Downloader()
|
||||
downloader.get_filename(image_url)
|
||||
|
||||
|
||||
class ProcessMessagesTask(BaseTask):
|
||||
message_processed = pyqtSignal(int, gotify.GotifyMessageModel)
|
||||
def __init__(self, page: gotify.GotifyPagedMessagesModel):
|
||||
super(ProcessMessagesTask, self).__init__()
|
||||
self.page = page
|
||||
|
||||
def task(self):
|
||||
downloader = Downloader()
|
||||
for i, message in enumerate(self.page.messages):
|
||||
if image_url := get_image(message.message):
|
||||
downloader.get_filename(image_url)
|
||||
self.message_processed.emit(i, message)
|
||||
|
||||
@@ -4,6 +4,7 @@ import re
|
||||
import subprocess
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def verify_server(force_new: bool = False, enable_import: bool = True) -> bool:
|
||||
@@ -44,6 +45,25 @@ def convert_links(text):
|
||||
return _link.sub(replace, text)
|
||||
|
||||
|
||||
def get_image(s: str) -> Optional[str]:
|
||||
"""If `s` contains only an image URL, this function returns that URL.
|
||||
This function also extracts a URL in the `` markdown image format.
|
||||
"""
|
||||
s = s.strip()
|
||||
|
||||
# Return True if 's' is a url and has an image extension
|
||||
RE = r'(?:(https://|http://)|(www\.))(\S+\b/?)([!"#$%&\'()*+,\-./:;<=>?@[\\\]^_`{|}~]*).(jpg|jpeg|png|bmp|gif)(\s|$)'
|
||||
if re.compile(RE, re.I).fullmatch(s) is not None:
|
||||
return s
|
||||
|
||||
# Return True if 's' has the markdown image format
|
||||
RE = r'!\[[^\]]*\]\((.*?)\s*("(?:.*[^"])")?\s*\)'
|
||||
if re.compile(RE, re.I).fullmatch(s) is not None:
|
||||
return re.compile(RE, re.I).findall(s)[0][0]
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_abs_path(s) -> str:
|
||||
h = Path(__file__).parent.parent
|
||||
p = Path(s)
|
||||
|
||||
Reference in New Issue
Block a user