mirror of
https://github.com/TagStudioDev/TagStudio.git
synced 2026-02-01 07:39:10 +00:00
feat(ui): clickable links in text fields (#924)
* feat: links in text fields are clickable * style: fix linting errors * style: remove unnecessary comment * feat: better url pattern * chore: disable Ruff error * style: add trailing line * style: maybe format it? that'd be smart * ui: chance text_label from RichText to Markdown --------- Co-authored-by: Travis Abendshien <46939827+CyanVoxel@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
# Created for TagStudio: https://github.com/CyanVoxel/TagStudio
|
||||
|
||||
|
||||
import re
|
||||
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import QHBoxLayout, QLabel
|
||||
|
||||
@@ -19,9 +21,25 @@ class TextWidget(FieldWidget):
|
||||
self.text_label = QLabel()
|
||||
self.text_label.setStyleSheet("font-size: 12px")
|
||||
self.text_label.setWordWrap(True)
|
||||
self.text_label.setTextInteractionFlags(Qt.TextInteractionFlag.TextSelectableByMouse)
|
||||
self.text_label.setTextFormat(Qt.TextFormat.MarkdownText)
|
||||
self.text_label.setOpenExternalLinks(True)
|
||||
self.text_label.setTextInteractionFlags(Qt.TextInteractionFlag.TextBrowserInteraction)
|
||||
self.base_layout.addWidget(self.text_label)
|
||||
self.set_text(text)
|
||||
|
||||
def set_text(self, text: str):
|
||||
text = linkify(text)
|
||||
self.text_label.setText(text)
|
||||
|
||||
|
||||
# Regex from https://stackoverflow.com/a/6041965
|
||||
def linkify(text: str):
|
||||
url_pattern = (
|
||||
r"(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-*]*[\w@?^=%&\/~+#-*])"
|
||||
)
|
||||
return re.sub(
|
||||
url_pattern,
|
||||
lambda url: f'<a href="{url.group(0)}">{url.group(0)}</a>',
|
||||
text,
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user