summary refs log tree commit diff
path: root/src/timeline/TimelineItem.cpp
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-09-26 15:17:14 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-09-26 15:17:14 +0300
commitc64a1bf7592cac57032080c36e4514d4de2a8a95 (patch)
tree529d3f77c1d1639776b2cf47a3e06d33a04d0b64 /src/timeline/TimelineItem.cpp
parentLint (diff)
downloadnheko-c64a1bf7592cac57032080c36e4514d4de2a8a95.tar.xz
Move TextLabel into its own file
Diffstat (limited to 'src/timeline/TimelineItem.cpp')
-rw-r--r--src/timeline/TimelineItem.cpp86
1 files changed, 6 insertions, 80 deletions
diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp

index b62437cd..9558c397 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp
@@ -28,6 +28,7 @@ #include "Olm.h" #include "ui/Avatar.h" #include "ui/Painter.h" +#include "ui/TextLabel.h" #include "timeline/TimelineItem.h" #include "timeline/widgets/AudioItem.h" @@ -41,86 +42,6 @@ constexpr int MSG_RIGHT_MARGIN = 7; constexpr int MSG_PADDING = 20; -TextLabel::TextLabel(QWidget *parent) - : TextLabel(QString(), parent) -{} - -TextLabel::TextLabel(const QString &text, QWidget *parent) - : QTextBrowser(parent) -{ - document()->setDefaultStyleSheet(QString("a {color: %1; }").arg(utils::linkColor())); - - setText(text); - setOpenExternalLinks(true); - - // Make it look and feel like an ordinary label. - setReadOnly(true); - setFrameStyle(QFrame::NoFrame); - QPalette pal = palette(); - pal.setColor(QPalette::Base, Qt::transparent); - setPalette(pal); - - // Wrap anywhere but prefer words, adjust minimum height on the fly. - setLineWrapMode(QTextEdit::WidgetWidth); - setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); - connect(document()->documentLayout(), - &QAbstractTextDocumentLayout::documentSizeChanged, - this, - &TextLabel::adjustHeight); - document()->setDocumentMargin(0); - - 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<User>(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::focusOutEvent(QFocusEvent *e) -{ - QTextBrowser::focusOutEvent(e); - - QTextCursor cursor = textCursor(); - cursor.clearSelection(); - setTextCursor(cursor); -} - -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) : QWidget(parent) { @@ -680,6 +601,11 @@ TimelineItem::generateBody(const QString &body) body_ = new TextLabel(replaceEmoji(body), this); body_->setFont(font_); body_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction); + + connect(body_, &TextLabel::userProfileTriggered, this, [](const QString &user_id) { + MainWindow::instance()->openUserProfile(user_id, + ChatPage::instance()->currentRoom()); + }); } // The username/timestamp is displayed along with the message body.