diff options
-rw-r--r-- | include/timeline/TimelineItem.h | 47 | ||||
-rw-r--r-- | src/timeline/TimelineItem.cc | 8 |
2 files changed, 47 insertions, 8 deletions
diff --git a/include/timeline/TimelineItem.h b/include/timeline/TimelineItem.h index 064dead4..6011dd44 100644 --- a/include/timeline/TimelineItem.h +++ b/include/timeline/TimelineItem.h @@ -17,6 +17,7 @@ #pragma once +#include <QAbstractTextDocumentLayout> #include <QDateTime> #include <QHBoxLayout> #include <QLabel> @@ -24,6 +25,8 @@ #include <QSettings> #include <QStyle> #include <QStyleOption> +#include <QTextBrowser> +#include <QTimer> #include "AvatarProvider.h" #include "RoomInfoListItem.h" @@ -39,6 +42,48 @@ class VideoItem; class FileItem; class Avatar; +class TextLabel : public QTextBrowser +{ + Q_OBJECT + +public: + TextLabel(const QString &text, QWidget *parent = 0) + : QTextBrowser(parent) + { + 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); + + setFixedHeight(document()->size().height()); + } + + QSize sizeHint() const override + { + ensurePolished(); + return document()->size().toSize(); + } + + void wheelEvent(QWheelEvent *event) override { event->ignore(); } + +private slots: + void adjustHeight(const QSizeF &size) { setFixedHeight(size.height()); } +}; + class TimelineItem : public QWidget { Q_OBJECT @@ -174,7 +219,7 @@ private: QLabel *timestamp_; QLabel *checkmark_; QLabel *userName_; - QLabel *body_; + TextLabel *body_; }; template<class Widget> diff --git a/src/timeline/TimelineItem.cc b/src/timeline/TimelineItem.cc index 6f126d3d..166199cd 100644 --- a/src/timeline/TimelineItem.cc +++ b/src/timeline/TimelineItem.cc @@ -18,7 +18,6 @@ #include <QContextMenuEvent> #include <QFontDatabase> #include <QMenu> -#include <QTextEdit> #include <QTimer> #include "Avatar.h" @@ -426,14 +425,9 @@ TimelineItem::generateBody(const QString &body) { QString content("<span>%1</span>"); - body_ = new QLabel(this); + body_ = new TextLabel(content.arg(replaceEmoji(body)), this); body_->setFont(font_); - body_->setWordWrap(true); - body_->setText(content.arg(replaceEmoji(body))); - body_->setMargin(0); - body_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction); - body_->setOpenExternalLinks(true); } // The username/timestamp is displayed along with the message body. |