-
-
Notifications
You must be signed in to change notification settings - Fork 465
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
Changes from 4 commits
f1e05eb
5bca276
31cff34
fe3bcc9
c377938
bdebe10
f939ce7
fedcfca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,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@?^=%&\/~+#-*])" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would recognising There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think compiling this regex with re.compile() once and storing it as a global might make it quicker, thought ?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah good point |
||
| return re.sub( | ||
| url_pattern, | ||
| lambda url: f'<a href="{url.group(0)}">{url.group(0)}</a>', | ||
| text, | ||
| flags=re.IGNORECASE | ||
| ) | ||
|
CyanVoxel marked this conversation as resolved.
Outdated
|
||
Uh oh!
There was an error while loading. Please reload this page.