Fix line break issue on timeline with long words

fixes #193
This commit is contained in:
Konstantinos Sideris 2018-05-23 16:33:30 +03:00
parent 03c5f79543
commit 3cf7ab9f04
2 changed files with 47 additions and 8 deletions

View file

@ -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>

View file

@ -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.