summary refs log tree commit diff
path: root/include
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-05-23 16:33:30 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-05-23 16:33:30 +0300
commit3cf7ab9f042e50fc19748e1e09eb5af355f451ca (patch)
tree1a6097d7795272dd6947ac597a4959420c2dd91d /include
parentUse std::string for the "creator" key in m.room.member (diff)
downloadnheko-3cf7ab9f042e50fc19748e1e09eb5af355f451ca.tar.xz
Fix line break issue on timeline with long words
fixes #193
Diffstat (limited to 'include')
-rw-r--r--include/timeline/TimelineItem.h47
1 files changed, 46 insertions, 1 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>