From cc8c5f7e99733d1b4ea3df429469cdcecab445eb Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Thu, 2 Jun 2016 23:33:38 +0300 Subject: [PATCH] links in messages --- docs/plugin_api.md | 2 +- src/list_items.py | 35 +++++++++++++++++++++++++++++++---- src/mainscreen.py | 1 + 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/docs/plugin_api.md b/docs/plugin_api.md index b4c81db..d302393 100644 --- a/docs/plugin_api.md +++ b/docs/plugin_api.md @@ -49,5 +49,5 @@ Plugin's methods should not raise exceptions. #Examples -You can find example of a plugin in [official repo](https://github.com/ingvar1995/toxygen_plugins) +You can find examples in [official repo](https://github.com/ingvar1995/toxygen_plugins) diff --git a/src/list_items.py b/src/list_items.py index ea2f8ce..1011e8c 100644 --- a/src/list_items.py +++ b/src/list_items.py @@ -8,9 +8,12 @@ from file_transfers import TOX_FILE_TRANSFER_STATE from util import curr_directory, convert_time from messages import FILE_TRANSFER_MESSAGE_STATUS from widgets import DataLabel, create_menu +import cgi -class MessageEdit(QtGui.QTextEdit): +class MessageEdit(QtGui.QTextBrowser): + + # TODO: add anchor clicked with tox: support and unfocus links def __init__(self, text, width, parent=None): super(MessageEdit, self).__init__(parent) @@ -18,7 +21,8 @@ class MessageEdit(QtGui.QTextEdit): self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setWordWrapMode(QtGui.QTextOption.WrapAtWordBoundaryOrAnywhere) self.document().setTextWidth(width) - self.setPlainText(text) + self.setOpenExternalLinks(True) + self.setTextWithLinks(text) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPixelSize(14) @@ -28,10 +32,33 @@ class MessageEdit(QtGui.QTextEdit): self.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse) def contextMenuEvent(self, event): - menu = create_menu(self.createStandardContextMenu()) + menu = create_menu(self.createStandardContextMenu(event.pos())) + menu.popup(event.globalPos()) menu.exec_(event.globalPos()) del menu + def setTextWithLinks(self, text): + text = cgi.escape(text) + exp = QtCore.QRegExp( + '(' + '(?:\\b)((www\\.)|(http[s]?|ftp)://)' + '\\w+\\S+)' + '|(?:\\b)(file:///)([\\S| ]*)' + '|(?:\\b)(tox:[a-zA-Z\\d]{76}$)' + '|(?:\\b)(mailto:\\S+@\\S+\\.\\S+)' + '|(?:\\b)(tox:\\S+@\\S+)') + offset = exp.indexIn(text, 0) + while offset != -1: + url = exp.cap() + if exp.cap(2) == 'www.': + html = '{0}'.format(url) + else: + html = '{0}'.format(url) + text = text[:offset] + html + text[offset + len(exp.cap()):] + offset += len(html) + offset = exp.indexIn(text, offset) + self.setHtml(text) + class MessageItem(QtGui.QWidget): """ @@ -67,7 +94,7 @@ class MessageItem(QtGui.QWidget): if message_type == TOX_MESSAGE_TYPE['ACTION']: self.name.setStyleSheet("QLabel { color: #4169E1; }") self.message.setStyleSheet("QTextEdit { color: #4169E1; }") - else: + else: # TODO: replace with regex if text[0] == '>': self.message.setStyleSheet("QTextEdit { color: green; }") if text[-1] == '<': diff --git a/src/mainscreen.py b/src/mainscreen.py index 0cb6604..824156c 100644 --- a/src/mainscreen.py +++ b/src/mainscreen.py @@ -351,6 +351,7 @@ class MainWindow(QtGui.QMainWindow): self.account_name.setGeometry(QtCore.QRect(100, 30, self.width() - 520, 25)) self.account_status.setGeometry(QtCore.QRect(100, 50, self.width() - 520, 25)) + self.messageEdit.setFocus() self.profile.update() def keyPressEvent(self, event):