From 1b4afcb93715e1ec3eb79b16beaaabd3120a39a5 Mon Sep 17 00:00:00 2001 From: krepa098 Date: Wed, 10 Sep 2014 16:03:53 +0200 Subject: [PATCH] detect URLs and show them as hyperlink --- widget/chatareawidget.cpp | 16 ++++++++++++++-- widget/chatareawidget.h | 7 +++++-- widget/tool/chataction.cpp | 20 +++++++++++++++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/widget/chatareawidget.cpp b/widget/chatareawidget.cpp index bd261f3bf..a6762c673 100644 --- a/widget/chatareawidget.cpp +++ b/widget/chatareawidget.cpp @@ -19,14 +19,21 @@ #include #include #include +#include ChatAreaWidget::ChatAreaWidget(QWidget *parent) : - QTextEdit(parent) + QTextBrowser(parent) { setReadOnly(true); viewport()->setCursor(Qt::ArrowCursor); setContextMenuPolicy(Qt::CustomContextMenu); setUndoRedoEnabled(false); + + setOpenExternalLinks(false); + setOpenLinks(false); + setAcceptRichText(false); + + connect(this, &ChatAreaWidget::anchorClicked, this, &ChatAreaWidget::onAnchorClicked); } ChatAreaWidget::~ChatAreaWidget() @@ -67,7 +74,12 @@ void ChatAreaWidget::mouseReleaseEvent(QMouseEvent * event) } } -void ChatAreaWidget::insertMessage(ChatAction* msgAction) +void ChatAreaWidget::onAnchorClicked(const QUrl &url) +{ + QDesktopServices::openUrl(url); +} + +void ChatAreaWidget::insertMessage(ChatAction *msgAction) { if (msgAction == nullptr) return; diff --git a/widget/chatareawidget.h b/widget/chatareawidget.h index cf9026877..e42f576f1 100644 --- a/widget/chatareawidget.h +++ b/widget/chatareawidget.h @@ -17,12 +17,12 @@ #ifndef CHATAREAWIDGET_H #define CHATAREAWIDGET_H -#include +#include #include class ChatAction; -class ChatAreaWidget : public QTextEdit +class ChatAreaWidget : public QTextBrowser { Q_OBJECT public: @@ -36,6 +36,9 @@ signals: protected: void mouseReleaseEvent(QMouseEvent * event); +public slots: + void onAnchorClicked(const QUrl& url); + private: QList messages; bool lockSliderToBottom; diff --git a/widget/tool/chataction.cpp b/widget/tool/chataction.cpp index f362e2b81..cb59290ae 100644 --- a/widget/tool/chataction.cpp +++ b/widget/tool/chataction.cpp @@ -22,7 +22,7 @@ QString ChatAction::toHtmlChars(const QString &str) { - static QList> replaceList = {{"&","&"}, {" "," "}, {">",">"}, {"<","<"}}; + static QList> replaceList = {{"&","&"}, {">",">"}, {"<","<"}}; QString res = str; for (auto &it : replaceList) @@ -71,6 +71,24 @@ MessageAction::MessageAction(const QString &author, const QString &message, cons { QString message_ = SmileyPack::getInstance().smileyfied(toHtmlChars(message)); + // detect urls + QRegExp exp("(www\\.|http[s]?:\\/\\/|ftp:\\/\\/)\\S+"); + int offset = 0; + while ((offset = exp.indexIn(message_, offset)) != -1) + { + QString url = exp.cap(); + + // add scheme if not specified + if (exp.cap(1) == "www.") + url.prepend("http://"); + + QString htmledUrl = QString("%1").arg(url); + message_.replace(offset, exp.cap().length(), htmledUrl); + + offset += htmledUrl.length(); + } + + // detect text quotes QStringList messageLines = message_.split("\n"); message_ = ""; for (QString& s : messageLines)