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 | #include <QGridLayout> | 12 | #include <QGridLayout> |
| 13 | #include <QFrame> | 13 | #include <QFrame> |
| 14 | #include <QLayoutItem> | 14 | #include <QLayoutItem> |
| 15 | #include <QMainWindow> | ||
| 16 | #include <QPalette> | 15 | #include <QPalette> |
| 17 | #include <QScrollBar> | 16 | #include <QScrollBar> |
| 18 | #include <QSizePolicy> | 17 | #include <QSizePolicy> |
| 19 | #include <QTimer> | 18 | #include <QTimer> |
| 20 | #include <QWidget> | ||
| 21 | 19 | ||
| 22 | #include "Application.h" | 20 | #include "Application.h" |
| 23 | #include "FlowLayout.h" | 21 | #include "FlowLayout.h" |
| @@ -31,32 +29,40 @@ · 7 unmodified lines | |||
| 31 | const int NUM_MESSAGES_TO_TIME_STAMP = 20; | 29 | const int NUM_MESSAGES_TO_TIME_STAMP = 20; |
| 32 | 30 | ||
| 33 | const QRegularExpression regexLinks("((?:(?:ftp)|(?:https?))://\\S+)"); | 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 | ui(new Ui::ChatWindow), | 36 | ui(new Ui::ChatWindow), |
| 37 | numMessagesAfterLastTimeStamp(0), | 37 | numMessagesAfterLastTimeStamp(0), |
| 38 | _mousePressed(false), | 38 | _mousePressed(false), |
| 39 | _mouseStartPosition() | 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 | 43 | ui->setupUi(this); |
| 44 | // | ||
| 45 | titleBar = titleBarWidget(); | ||
| 46 | setTitleBarWidget(new QWidget()); | ||
| 47 | 44 | ||
| 48 | FlowLayout* flowLayout = new FlowLayout(0, 4, 4); | 45 | FlowLayout* flowLayout = new FlowLayout(0, 4, 4); |
| 49 | ui->usersWidget->setLayout(flowLayout); | 46 | ui->usersWidget->setLayout(flowLayout); |
| 50 | 47 | ||
| 51 | ui->messagesGridLayout->setColumnStretch(0, 1); | ||
| 52 | ui->messagesGridLayout->setColumnStretch(1, 3); | ||
| 53 | |||
| 54 | ui->messagePlainTextEdit->installEventFilter(this); | 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 | if (!AccountManager::getInstance().isLoggedIn()) { | 62 | if (!AccountManager::getInstance().isLoggedIn()) { |
| 57 | ui->connectingToXMPPLabel->setText(tr("You must be logged in to chat with others.")); | 63 | ui->connectingToXMPPLabel->setText(tr("You must be logged in to chat with others.")); |
| 58 | } | 64 | } |
| 59 | 65 | ||
| 60 | #ifdef HAVE_QXMPP | 66 | #ifdef HAVE_QXMPP |
| 61 | const QXmppClient& xmppClient = XmppClient::getInstance().getXMPPClient(); | 67 | const QXmppClient& xmppClient = XmppClient::getInstance().getXMPPClient(); |
| 62 | if (xmppClient.isConnected()) { | 68 | if (xmppClient.isConnected()) { |
| @@ -90,36 +96,17 @@ ChatWindow::~ChatWindow() { · 27 unmodified lines | |||
| 90 | delete ui; | 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 | void ChatWindow::keyPressEvent(QKeyEvent* event) { | 99 | void ChatWindow::keyPressEvent(QKeyEvent* event) { |
| 113 | QDockWidget::keyPressEvent(event); | ||
| 114 | if (event->key() == Qt::Key_Escape) { | 100 | if (event->key() == Qt::Key_Escape) { |
| 115 | hide(); | 101 | hide(); |
| 102 | } else { | ||
| 103 | FramelessDialog::keyPressEvent(event); | ||
| 116 | } | 104 | } |
| 117 | } | 105 | } |
| 118 | 106 | ||
| 119 | void ChatWindow::showEvent(QShowEvent* event) { | 107 | void ChatWindow::showEvent(QShowEvent* event) { |
| 120 | QDockWidget::showEvent(event); | 108 | FramelessDialog::showEvent(event); |
| 121 | if (!event->spontaneous()) { | 109 | if (!event->spontaneous()) { |
| 122 | activateWindow(); | ||
| 123 | ui->messagePlainTextEdit->setFocus(); | 110 | ui->messagePlainTextEdit->setFocus(); |
| 124 | } | 111 | } |
| 125 | } | 112 | } |
| @@ -142,18 +129,20 @@ bool ChatWindow::eventFilter(QObject* sender, QEvent* event) { · 16 unmodified lines | |||
| 142 | message.setBody(messageText); | 129 | message.setBody(messageText); |
| 143 | XmppClient::getInstance().getXMPPClient().sendPacket(message); | 130 | XmppClient::getInstance().getXMPPClient().sendPacket(message); |
| 144 | #endif | 131 | #endif |
| 145 | ui->messagePlainTextEdit->document()->clear(); | 132 | QTextCursor cursor = ui->messagePlainTextEdit->textCursor(); |
| 133 | cursor.select(QTextCursor::Document); | ||
| 134 | cursor.removeSelectedText(); | ||
| 146 | } | 135 | } |
| 147 | return true; | 136 | return true; |
| 148 | } | 137 | } |
| 149 | } else { | 138 | } else if (event->type() == QEvent::MouseButtonRelease) { |
| 150 | if (event->type() != QEvent::MouseButtonRelease) { | 139 | QVariant userVar = sender->property("user"); |
| 151 | return false; | 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 | #ifdef HAVE_QXMPP | 148 | #ifdef HAVE_QXMPP |
| @@ -176,16 +165,17 @@ void ChatWindow::addTimeStamp() { · 16 unmodified lines | |||
| 176 | timeString.chop(1); | 165 | timeString.chop(1); |
| 177 | if (!timeString.isEmpty()) { | 166 | if (!timeString.isEmpty()) { |
| 178 | QLabel* timeLabel = new QLabel(timeString); | 167 | QLabel* timeLabel = new QLabel(timeString); |
| 179 | timeLabel->setStyleSheet("color: palette(shadow);" | 168 | timeLabel->setStyleSheet("color: #333333;" |
| 180 | "background-color: palette(highlight);" | 169 | "background-color: white;" |
| 170 | "font-size: 14pt;" | ||
| 181 | "padding: 4px;"); | 171 | "padding: 4px;"); |
| 182 | timeLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); | 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); | 177 | ui->messagesVBoxLayout->addWidget(timeLabel); |
| 188 | ui->messagesGridLayout->parentWidget()->updateGeometry(); | 178 | ui->messagesVBoxLayout->parentWidget()->updateGeometry(); |
| 189 | 179 | ||
| 190 | Application::processEvents(); | 180 | Application::processEvents(); |
| 191 | numMessagesAfterLastTimeStamp = 0; | 181 | numMessagesAfterLastTimeStamp = 0; |
| @@ -262,6 +252,7 @@ void ChatWindow::participantsChanged() { · 70 unmodified lines | |||
| 262 | "padding-bottom: 2px;" | 252 | "padding-bottom: 2px;" |
| 263 | "padding-left: 2px;" | 253 | "padding-left: 2px;" |
| 264 | "border: 1px solid palette(shadow);" | 254 | "border: 1px solid palette(shadow);" |
| 255 | "font-size: 14pt;" | ||
| 265 | "font-weight: bold"); | 256 | "font-weight: bold"); |
| 266 | userLabel->setProperty("user", participantName); | 257 | userLabel->setProperty("user", participantName); |
| 267 | userLabel->setCursor(Qt::PointingHandCursor); | 258 | userLabel->setCursor(Qt::PointingHandCursor); |
| @@ -275,15 +266,11 @@ void ChatWindow::messageReceived(const QXmppMessage& message) { · 7 unmodified lines | |||
| 275 | return; | 266 | return; |
| 276 | } | 267 | } |
| 277 | 268 | ||
| 278 | QLabel* userLabel = new QLabel(getParticipantName(message.from())); | 269 | // Update background if this is a message from the current user |
| 279 | userLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | 270 | bool fromSelf = getParticipantName(message.from()) == AccountManager::getInstance().getUsername(); |
| 280 | userLabel->setStyleSheet("padding: 2px; font-weight: bold"); | ||
| 281 | userLabel->setAlignment(Qt::AlignTop | Qt::AlignRight); | ||
| 282 | |||
| 283 | ChatMessageArea* messageArea = new ChatMessageArea(); | ||
| 284 | 271 | ||
| 285 | messageArea->setOpenLinks(true); | 272 | // Create message area |
| 286 | messageArea->setOpenExternalLinks(true); | 273 | ChatMessageArea* messageArea = new ChatMessageArea(true); |
| 287 | messageArea->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); | 274 | messageArea->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); |
| 288 | messageArea->setTextInteractionFlags(Qt::TextBrowserInteraction); | 275 | messageArea->setTextInteractionFlags(Qt::TextBrowserInteraction); |
| 289 | messageArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 276 | messageArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| @@ -294,22 +281,30 @@ void ChatWindow::messageReceived(const QXmppMessage& message) { · 4 unmodified lines | |||
| 294 | "padding-left: 2px;" | 281 | "padding-left: 2px;" |
| 295 | "padding-top: 2px;" | 282 | "padding-top: 2px;" |
| 296 | "padding-right: 20px;" | 283 | "padding-right: 20px;" |
| 284 | "margin: 0px;" | ||
| 285 | "color: #333333;" | ||
| 286 | "font-size: 14pt;" | ||
| 297 | "background-color: rgba(0, 0, 0, 0%);" | 287 | "background-color: rgba(0, 0, 0, 0%);" |
| 298 | "border: 0;"); | 288 | "border: 0;"); |
| 299 | 289 | ||
| 300 | bool fromSelf = getParticipantName(message.from()) == AccountManager::getInstance().getUsername(); | 290 | QString userLabel = getParticipantName(message.from()); |
| 301 | if (fromSelf) { | 291 | if (fromSelf) { |
| 302 | userLabel->setStyleSheet(userLabel->styleSheet() + "; background-color: #e1e8ea"); | 292 | userLabel = "<b style=\"color: #4a6f91\">" + userLabel + ": </b>"; |
| 303 | messageArea->setStyleSheet(messageArea->styleSheet() + "; background-color: #e1e8ea"); | 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(); | 304 | bool atBottom = isNearBottom(); |
| 309 | ui->messagesGridLayout->addWidget(userLabel, ui->messagesGridLayout->rowCount(), 0); | ||
| 310 | ui->messagesGridLayout->addWidget(messageArea, ui->messagesGridLayout->rowCount() - 1, 1); | ||
| 311 | 305 | ||
| 312 | ui->messagesGridLayout->parentWidget()->updateGeometry(); | 306 | ui->messagesVBoxLayout->addWidget(messageArea); |
| 307 | ui->messagesVBoxLayout->parentWidget()->updateGeometry(); | ||
| 313 | Application::processEvents(); | 308 | Application::processEvents(); |
| 314 | 309 | ||
| 315 | if (atBottom || fromSelf) { | 310 | if (atBottom || fromSelf) { |
| @@ -326,26 +321,13 @@ void ChatWindow::messageReceived(const QXmppMessage& message) { · 10 unmodified lines | |||
| 326 | 321 | ||
| 327 | #endif | 322 | #endif |
| 328 | 323 | ||
| 329 | bool ChatWindow::isAtBottom() { | 324 | bool ChatWindow::isNearBottom() { |
| 330 | QScrollBar* verticalScrollBar = ui->messagesScrollArea->verticalScrollBar(); | 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 | // Scroll chat message area to bottom. | 329 | // Scroll chat message area to bottom. |
| 335 | void ChatWindow::scrollToBottom() { | 330 | void ChatWindow::scrollToBottom() { |
| 336 | QScrollBar* verticalScrollBar = ui->messagesScrollArea->verticalScrollBar(); | 331 | QScrollBar* verticalScrollBar = ui->messagesScrollArea->verticalScrollBar(); |
| 337 | verticalScrollBar->setSliderPosition(verticalScrollBar->maximum()); | 332 | verticalScrollBar->setValue(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); | ||
| 351 | } | 333 | } |