From 9c06ba5d25e8007f7d21971edce4c9791be009ed Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Wed, 12 Sep 2018 20:54:44 +0300 Subject: [PATCH] Open user profile on matrix.to links --- CHANGELOG.md | 2 ++ src/timeline/TimelineItem.cpp | 41 +++++++++++++++++++++++++++++++++++ src/timeline/TimelineItem.h | 10 +++++++++ 3 files changed, 53 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d8aac84..5e97fbbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## [Unreleased] ### Features - Support for sending & receiving markdown formatted messages. (#283) +- Context menu option to show the raw text message of an event. (#437) +- Clicking on a user pill link will open the user profile. ### Improvements - Update Polish translation (#430) diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp index 5bec4458..1654e451 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp @@ -16,6 +16,7 @@ */ #include +#include #include #include #include @@ -35,6 +36,7 @@ #include "timeline/widgets/VideoItem.h" #include "dialogs/RawMessage.h" +#include "mtx/identifiers.hpp" constexpr int MSG_RIGHT_MARGIN = 7; constexpr int MSG_PADDING = 20; @@ -61,8 +63,47 @@ TextLabel::TextLabel(const QString &text, QWidget *parent) &TextLabel::adjustHeight); document()->setDocumentMargin(0); + setFocusPolicy(Qt::NoFocus); setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); setFixedHeight(0); + + connect(this, &TextLabel::linkActivated, this, [](const QUrl &url) { + auto parts = url.toString().split('/'); + auto defaultHandler = [](const QUrl &url) { QDesktopServices::openUrl(url); }; + + if (url.host() != "matrix.to" || parts.isEmpty()) + return defaultHandler(url); + + try { + using namespace mtx::identifiers; + parse(parts.last().toStdString()); + } catch (const std::exception &) { + return defaultHandler(url); + } + + auto user_id = parts.last(); + auto room_id = ChatPage::instance()->currentRoom(); + + MainWindow::instance()->openUserProfile(user_id, room_id); + }); +} + +void +TextLabel::mousePressEvent(QMouseEvent *e) +{ + link_ = (e->button() & Qt::LeftButton) ? anchorAt(e->pos()) : QString(); + QTextBrowser::mousePressEvent(e); +} + +void +TextLabel::mouseReleaseEvent(QMouseEvent *e) +{ + if (e->button() & Qt::LeftButton && !link_.isEmpty() && anchorAt(e->pos()) == link_) { + emit linkActivated(link_); + return; + } + + QTextBrowser::mouseReleaseEvent(e); } StatusIndicator::StatusIndicator(QWidget *parent) diff --git a/src/timeline/TimelineItem.h b/src/timeline/TimelineItem.h index fb961b6c..32d586f5 100644 --- a/src/timeline/TimelineItem.h +++ b/src/timeline/TimelineItem.h @@ -105,8 +105,18 @@ public: void wheelEvent(QWheelEvent *event) override { event->ignore(); } +protected: + void mousePressEvent(QMouseEvent *e) override; + void mouseReleaseEvent(QMouseEvent *e) override; + private slots: void adjustHeight(const QSizeF &size) { setFixedHeight(size.height()); } + +signals: + void linkActivated(const QUrl &link); + +private: + QString link_; }; class UserProfileFilter : public QObject