Skip to content

feat: clickable links in text fields #924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/tagstudio/qt/widgets/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -19,9 +21,22 @@ def __init__(self, title, text: str) -> None:
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.RichText)
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
)
Loading