Showing the changes to interface/src/ui/ChatWindow.cpp (1 of 41 files). Show all
Unified · Split
| @@ -12,12 +12,10 @@ · 11 unmodified lines | ||
| 12 | 12 | #include <QGridLayout> |
| 13 | 13 | #include <QFrame> |
| 14 | 14 | #include <QLayoutItem> |
| 15 | −#include <QMainWindow> | |
| 16 | 15 | #include <QPalette> |
| 17 | 16 | #include <QScrollBar> |
| 18 | 17 | #include <QSizePolicy> |
| 19 | 18 | #include <QTimer> |
| 20 | −#include <QWidget> | |
| 21 | 19 | |
| 22 | 20 | #include "Application.h" |
| 23 | 21 | #include "FlowLayout.h" |
| @@ -31,32 +29,40 @@ · 7 unmodified lines | ||
| 31 | 29 | const int NUM_MESSAGES_TO_TIME_STAMP = 20; |
| 32 | 30 | |
| 33 | 31 | const QRegularExpression regexLinks("((?:(?:ftp)|(?:https?))://\\S+)"); |
| 32 | +const QRegularExpression regexHifiLinks("([#@]\\S+)"); | |
| 34 | 33 | |
| 35 | −ChatWindow::ChatWindow() : | |
| 34 | +ChatWindow::ChatWindow(QWidget* parent) : | |
| 35 | + FramelessDialog(parent, 0, POSITION_RIGHT), | |
| 36 | 36 | ui(new Ui::ChatWindow), |
| 37 | 37 | numMessagesAfterLastTimeStamp(0), |
| 38 | 38 | _mousePressed(false), |
| 39 | 39 | _mouseStartPosition() |
| 40 | 40 | { |
| 41 | − ui->setupUi(this); | |
| 41 | + setAttribute(Qt::WA_DeleteOnClose, false); | |
| 42 | 42 | |
| 43 | − // remove the title bar (see the Qt docs on setTitleBarWidget), but we keep it for undocking | |
| 44 | − // | |
| 45 | − titleBar = titleBarWidget(); | |
| 46 | − setTitleBarWidget(new QWidget()); | |
| 43 | + ui->setupUi(this); | |
| 47 | 44 | |
| 48 | 45 | FlowLayout* flowLayout = new FlowLayout(0, 4, 4); |
| 49 | 46 | ui->usersWidget->setLayout(flowLayout); |
| 50 | 47 | |
| 51 | − ui->messagesGridLayout->setColumnStretch(0, 1); | |
| 52 | − ui->messagesGridLayout->setColumnStretch(1, 3); | |
| 53 | − | |
| 54 | 48 | ui->messagePlainTextEdit->installEventFilter(this); |
| 49 | + ui->messagePlainTextEdit->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); | |
| 50 | + | |
| 51 | + QTextCursor cursor(ui->messagePlainTextEdit->textCursor()); | |
| 52 | + | |
| 53 | + cursor.movePosition(QTextCursor::Start); | |
| 54 | + | |
| 55 | + QTextBlockFormat format = cursor.blockFormat(); | |
| 56 | + format.setLineHeight(130, QTextBlockFormat::ProportionalHeight); | |
| 57 | + | |
| 58 | + cursor.setBlockFormat(format); | |
| 59 | + | |
| 60 | + ui->messagePlainTextEdit->setTextCursor(cursor); | |
| 55 | 61 | |
| 56 | 62 | if (!AccountManager::getInstance().isLoggedIn()) { |
| 57 | 63 | ui->connectingToXMPPLabel->setText(tr("You must be logged in to chat with others.")); |
| 58 | 64 | } |
| 59 | − | |
| 65 | + | |
| 60 | 66 | #ifdef HAVE_QXMPP |
| 61 | 67 | const QXmppClient& xmppClient = XmppClient::getInstance().getXMPPClient(); |
| 62 | 68 | if (xmppClient.isConnected()) { |
| @@ -90,36 +96,17 @@ ChatWindow::~ChatWindow() { · 27 unmodified lines | ||
| 90 | 96 | delete ui; |
| 91 | 97 | } |
| 92 | 98 | |
| 93 | −void ChatWindow::mousePressEvent(QMouseEvent *e) { | |
| 94 | − if (e->button() == Qt::LeftButton && isFloating()) { | |
| 95 | − _mousePressed = true; | |
| 96 | − _mouseStartPosition = e->pos(); | |
| 97 | − } | |
| 98 | −} | |
| 99 | − | |
| 100 | −void ChatWindow::mouseMoveEvent(QMouseEvent *e) { | |
| 101 | − if (_mousePressed) { | |
| 102 | − move(mapToParent(e->pos() - _mouseStartPosition)); | |
| 103 | − } | |
| 104 | −} | |
| 105 | − | |
| 106 | −void ChatWindow::mouseReleaseEvent( QMouseEvent *e ) { | |
| 107 | − if ( e->button() == Qt::LeftButton ) { | |
| 108 | − _mousePressed = false; | |
| 109 | − } | |
| 110 | −} | |
| 111 | − | |
| 112 | 99 | void ChatWindow::keyPressEvent(QKeyEvent* event) { |
| 113 | − QDockWidget::keyPressEvent(event); | |
| 114 | 100 | if (event->key() == Qt::Key_Escape) { |
| 115 | 101 | hide(); |
| 102 | + } else { | |
| 103 | + FramelessDialog::keyPressEvent(event); | |
| 116 | 104 | } |
| 117 | 105 | } |
| 118 | 106 | |
| 119 | 107 | void ChatWindow::showEvent(QShowEvent* event) { |
| 120 | − QDockWidget::showEvent(event); | |
| 108 | + FramelessDialog::showEvent(event); | |
| 121 | 109 | if (!event->spontaneous()) { |
| 122 | − activateWindow(); | |
| 123 | 110 | ui->messagePlainTextEdit->setFocus(); |
| 124 | 111 | } |
| 125 | 112 | } |
| @@ -142,18 +129,20 @@ bool ChatWindow::eventFilter(QObject* sender, QEvent* event) { · 16 unmodified lines | ||
| 142 | 129 | message.setBody(messageText); |
| 143 | 130 | XmppClient::getInstance().getXMPPClient().sendPacket(message); |
| 144 | 131 | #endif |
| 145 | − ui->messagePlainTextEdit->document()->clear(); | |
| 132 | + QTextCursor cursor = ui->messagePlainTextEdit->textCursor(); | |
| 133 | + cursor.select(QTextCursor::Document); | |
| 134 | + cursor.removeSelectedText(); | |
| 146 | 135 | } |
| 147 | 136 | return true; |
| 148 | 137 | } |
| 149 | − } else { | |
| 150 | − if (event->type() != QEvent::MouseButtonRelease) { | |
| 151 | − return false; | |
| 138 | + } else if (event->type() == QEvent::MouseButtonRelease) { | |
| 139 | + QVariant userVar = sender->property("user"); | |
| 140 | + if (userVar.isValid()) { | |
| 141 | + Menu::getInstance()->goToUser("@" + userVar.toString()); | |
| 142 | + return true; | |
| 152 | 143 | } |
| 153 | − QString user = sender->property("user").toString(); | |
| 154 | − Menu::getInstance()->goToUser(user); | |
| 155 | 144 | } |
| 156 | − return false; | |
| 145 | + return FramelessDialog::eventFilter(sender, event); | |
| 157 | 146 | } |
| 158 | 147 | |
| 159 | 148 | #ifdef HAVE_QXMPP |
| @@ -176,16 +165,17 @@ void ChatWindow::addTimeStamp() { · 16 unmodified lines | ||
| 176 | 165 | timeString.chop(1); |
| 177 | 166 | if (!timeString.isEmpty()) { |
| 178 | 167 | QLabel* timeLabel = new QLabel(timeString); |
| 179 | − timeLabel->setStyleSheet("color: palette(shadow);" | |
| 180 | − "background-color: palette(highlight);" | |
| 168 | + timeLabel->setStyleSheet("color: #333333;" | |
| 169 | + "background-color: white;" | |
| 170 | + "font-size: 14pt;" | |
| 181 | 171 | "padding: 4px;"); |
| 182 | 172 | timeLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
| 183 | − timeLabel->setAlignment(Qt::AlignHCenter); | |
| 173 | + timeLabel->setAlignment(Qt::AlignLeft); | |
| 184 | 174 | |
| 185 | − bool atBottom = isAtBottom(); | |
| 175 | + bool atBottom = isNearBottom(); | |
| 186 | 176 | |
| 187 | − ui->messagesGridLayout->addWidget(timeLabel, ui->messagesGridLayout->rowCount(), 0, 1, 2); | |
| 188 | − ui->messagesGridLayout->parentWidget()->updateGeometry(); | |
| 177 | + ui->messagesVBoxLayout->addWidget(timeLabel); | |
| 178 | + ui->messagesVBoxLayout->parentWidget()->updateGeometry(); | |
| 189 | 179 | |
| 190 | 180 | Application::processEvents(); |
| 191 | 181 | numMessagesAfterLastTimeStamp = 0; |
| @@ -262,6 +252,7 @@ void ChatWindow::participantsChanged() { · 70 unmodified lines | ||
| 262 | 252 | "padding-bottom: 2px;" |
| 263 | 253 | "padding-left: 2px;" |
| 264 | 254 | "border: 1px solid palette(shadow);" |
| 255 | + "font-size: 14pt;" | |
| 265 | 256 | "font-weight: bold"); |
| 266 | 257 | userLabel->setProperty("user", participantName); |
| 267 | 258 | userLabel->setCursor(Qt::PointingHandCursor); |
| @@ -275,15 +266,11 @@ void ChatWindow::messageReceived(const QXmppMessage& message) { · 7 unmodified lines | ||
| 275 | 266 | return; |
| 276 | 267 | } |
| 277 | 268 | |
| 278 | − QLabel* userLabel = new QLabel(getParticipantName(message.from())); | |
| 279 | − userLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
| 280 | − userLabel->setStyleSheet("padding: 2px; font-weight: bold"); | |
| 281 | − userLabel->setAlignment(Qt::AlignTop | Qt::AlignRight); | |
| 282 | − | |
| 283 | − ChatMessageArea* messageArea = new ChatMessageArea(); | |
| 269 | + // Update background if this is a message from the current user | |
| 270 | + bool fromSelf = getParticipantName(message.from()) == AccountManager::getInstance().getUsername(); | |
| 284 | 271 | |
| 285 | − messageArea->setOpenLinks(true); | |
| 286 | − messageArea->setOpenExternalLinks(true); | |
| 272 | + // Create message area | |
| 273 | + ChatMessageArea* messageArea = new ChatMessageArea(true); | |
| 287 | 274 | messageArea->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); |
| 288 | 275 | messageArea->setTextInteractionFlags(Qt::TextBrowserInteraction); |
| 289 | 276 | messageArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| @@ -294,22 +281,30 @@ void ChatWindow::messageReceived(const QXmppMessage& message) { · 4 unmodified lines | ||
| 294 | 281 | "padding-left: 2px;" |
| 295 | 282 | "padding-top: 2px;" |
| 296 | 283 | "padding-right: 20px;" |
| 284 | + "margin: 0px;" | |
| 285 | + "color: #333333;" | |
| 286 | + "font-size: 14pt;" | |
| 297 | 287 | "background-color: rgba(0, 0, 0, 0%);" |
| 298 | 288 | "border: 0;"); |
| 299 | 289 | |
| 300 | − bool fromSelf = getParticipantName(message.from()) == AccountManager::getInstance().getUsername(); | |
| 290 | + QString userLabel = getParticipantName(message.from()); | |
| 301 | 291 | if (fromSelf) { |
| 302 | − userLabel->setStyleSheet(userLabel->styleSheet() + "; background-color: #e1e8ea"); | |
| 303 | − messageArea->setStyleSheet(messageArea->styleSheet() + "; background-color: #e1e8ea"); | |
| 292 | + userLabel = "<b style=\"color: #4a6f91\">" + userLabel + ": </b>"; | |
| 293 | + messageArea->setStyleSheet(messageArea->styleSheet() + "background-color: #e1e8ea"); | |
| 294 | + } else { | |
| 295 | + userLabel = "<b>" + userLabel + ": </b>"; | |
| 304 | 296 | } |
| 305 | 297 | |
| 306 | − messageArea->setHtml(message.body().replace(regexLinks, "<a href=\"\\1\">\\1</a>")); | |
| 298 | + messageArea->document()->setDefaultStyleSheet("a { text-decoration: none; font-weight: bold; color: #267077;}"); | |
| 299 | + QString messageText = message.body().toHtmlEscaped(); | |
| 300 | + messageText = messageText.replace(regexLinks, "<a href=\"\\1\">\\1</a>"); | |
| 301 | + messageText = messageText.replace(regexHifiLinks, "<a href=\"hifi://\\1\">\\1</a>"); | |
| 302 | + messageArea->setHtml(userLabel + messageText); | |
| 307 | 303 | |
| 308 | − bool atBottom = isAtBottom(); | |
| 309 | − ui->messagesGridLayout->addWidget(userLabel, ui->messagesGridLayout->rowCount(), 0); | |
| 310 | − ui->messagesGridLayout->addWidget(messageArea, ui->messagesGridLayout->rowCount() - 1, 1); | |
| 304 | + bool atBottom = isNearBottom(); | |
| 311 | 305 | |
| 312 | − ui->messagesGridLayout->parentWidget()->updateGeometry(); | |
| 306 | + ui->messagesVBoxLayout->addWidget(messageArea); | |
| 307 | + ui->messagesVBoxLayout->parentWidget()->updateGeometry(); | |
| 313 | 308 | Application::processEvents(); |
| 314 | 309 | |
| 315 | 310 | if (atBottom || fromSelf) { |
| @@ -326,26 +321,13 @@ void ChatWindow::messageReceived(const QXmppMessage& message) { · 10 unmodified lines | ||
| 326 | 321 | |
| 327 | 322 | #endif |
| 328 | 323 | |
| 329 | −bool ChatWindow::isAtBottom() { | |
| 324 | +bool ChatWindow::isNearBottom() { | |
| 330 | 325 | QScrollBar* verticalScrollBar = ui->messagesScrollArea->verticalScrollBar(); |
| 331 | − return verticalScrollBar->sliderPosition() == verticalScrollBar->maximum(); | |
| 326 | + return verticalScrollBar->value() >= verticalScrollBar->maximum() - Ui::AUTO_SCROLL_THRESHOLD; | |
| 332 | 327 | } |
| 333 | 328 | |
| 334 | 329 | // Scroll chat message area to bottom. |
| 335 | 330 | void ChatWindow::scrollToBottom() { |
| 336 | 331 | QScrollBar* verticalScrollBar = ui->messagesScrollArea->verticalScrollBar(); |
| 337 | − verticalScrollBar->setSliderPosition(verticalScrollBar->maximum()); | |
| 338 | −} | |
| 339 | − | |
| 340 | −void ChatWindow::togglePinned() { | |
| 341 | − QMainWindow* mainWindow = Application::getInstance()->getWindow(); | |
| 342 | − mainWindow->removeDockWidget(this); | |
| 343 | − if (ui->togglePinnedButton->isChecked()) { | |
| 344 | − mainWindow->addDockWidget(ui->togglePinnedButton->isChecked() ? Qt::RightDockWidgetArea : Qt::NoDockWidgetArea, this); | |
| 345 | − } | |
| 346 | − if (!this->toggleViewAction()->isChecked()) { | |
| 347 | − this->toggleViewAction()->trigger(); | |
| 348 | − } | |
| 349 | − this->setFloating(!ui->togglePinnedButton->isChecked()); | |
| 350 | − setTitleBarWidget(ui->togglePinnedButton->isChecked()?new QWidget():titleBar); | |
| 332 | + verticalScrollBar->setValue(verticalScrollBar->maximum()); | |
| 351 | 333 | } |