diff --git a/interface/src/ui/ChatMessageArea.cpp b/interface/src/ui/ChatMessageArea.cpp index f15b788990..929ad85d87 100644 --- a/interface/src/ui/ChatMessageArea.cpp +++ b/interface/src/ui/ChatMessageArea.cpp @@ -9,11 +9,12 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include "Application.h" #include "ChatMessageArea.h" #include #include -ChatMessageArea::ChatMessageArea() : QTextBrowser() { +ChatMessageArea::ChatMessageArea(bool useFixedHeight) : QTextBrowser(), _useFixedHeight(useFixedHeight) { connect(document()->documentLayout(), &QAbstractTextDocumentLayout::documentSizeChanged, this, &ChatMessageArea::updateLayout); } @@ -34,7 +35,15 @@ void ChatMessageArea::setHtml(const QString& html) { } void ChatMessageArea::updateLayout() { - setFixedHeight(document()->size().height()); + if (_useFixedHeight) { + setFixedHeight(document()->size().height()); + updateGeometry(); + emit sizeChanged(size()); + } +} + +void ChatMessageArea::setSize(const QSize& size) { + setFixedHeight(size.height()); updateGeometry(); } diff --git a/interface/src/ui/ChatMessageArea.h b/interface/src/ui/ChatMessageArea.h index 1c49c60b08..48cfc01aec 100644 --- a/interface/src/ui/ChatMessageArea.h +++ b/interface/src/ui/ChatMessageArea.h @@ -19,14 +19,19 @@ const int CHAT_MESSAGE_LINE_HEIGHT = 130; class ChatMessageArea : public QTextBrowser { Q_OBJECT public: - ChatMessageArea(); + ChatMessageArea(bool useFixedHeight = true); virtual void setHtml(const QString& html); public slots: void updateLayout(); + void setSize(const QSize& size); + +signals: + void sizeChanged(QSize newSize); protected: virtual void wheelEvent(QWheelEvent* event); + bool _useFixedHeight; }; diff --git a/interface/src/ui/ChatWindow.cpp b/interface/src/ui/ChatWindow.cpp index 1f0f884f30..c194e35fc1 100644 --- a/interface/src/ui/ChatWindow.cpp +++ b/interface/src/ui/ChatWindow.cpp @@ -240,13 +240,32 @@ void ChatWindow::messageReceived(const QXmppMessage& message) { return; } - QLabel* userLabel = new QLabel(getParticipantName(message.from())); - userLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - userLabel->setStyleSheet("padding: 2px; font-weight: bold"); - userLabel->setAlignment(Qt::AlignTop | Qt::AlignRight); + // Create username label + ChatMessageArea* userLabel = new ChatMessageArea(false); + userLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); + userLabel->setWordWrapMode(QTextOption::NoWrap); + userLabel->setLineWrapMode(QTextEdit::NoWrap); + userLabel->setTextInteractionFlags(Qt::NoTextInteraction); + userLabel->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + userLabel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + userLabel->setReadOnly(true); + userLabel->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); - ChatMessageArea* messageArea = new ChatMessageArea(); + userLabel->setStyleSheet("padding: 2px;" + "font-weight: bold;" + "background-color: rgba(0, 0, 0, 0%);" + "border: 0;"); + QTextBlockFormat format; + format.setLineHeight(130, QTextBlockFormat::ProportionalHeight); + QTextCursor cursor = userLabel->textCursor(); + cursor.setBlockFormat(format); + cursor.insertText(getParticipantName(message.from())); + + userLabel->setAlignment(Qt::AlignRight); + + // Create message area + ChatMessageArea* messageArea = new ChatMessageArea(true); messageArea->setOpenLinks(true); messageArea->setOpenExternalLinks(true); messageArea->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); @@ -262,10 +281,11 @@ void ChatWindow::messageReceived(const QXmppMessage& message) { "background-color: rgba(0, 0, 0, 0%);" "border: 0;"); + // Update background if this is a message from the current user bool fromSelf = getParticipantName(message.from()) == AccountManager::getInstance().getUsername(); if (fromSelf) { - userLabel->setStyleSheet(userLabel->styleSheet() + "; background-color: #e1e8ea"); - messageArea->setStyleSheet(messageArea->styleSheet() + "; background-color: #e1e8ea"); + userLabel->setStyleSheet(userLabel->styleSheet() + "background-color: #e1e8ea"); + messageArea->setStyleSheet(messageArea->styleSheet() + "background-color: #e1e8ea"); } messageArea->setHtml(message.body().replace(regexLinks, "\\1")); @@ -274,6 +294,12 @@ void ChatWindow::messageReceived(const QXmppMessage& message) { ui->messagesGridLayout->addWidget(userLabel, ui->messagesGridLayout->rowCount(), 0); ui->messagesGridLayout->addWidget(messageArea, ui->messagesGridLayout->rowCount() - 1, 1); + // Force the height of the username area to match the height of the message area + connect(messageArea, &ChatMessageArea::sizeChanged, userLabel, &ChatMessageArea::setSize); + + // Force initial height to match message area + userLabel->setFixedHeight(messageArea->size().height()); + ui->messagesGridLayout->parentWidget()->updateGeometry(); Application::processEvents();