2017-12-31 00:50:07 +01:00
|
|
|
#include "channelview.hpp"
|
2017-11-04 13:17:35 +01:00
|
|
|
#include "debug/log.hpp"
|
2017-09-12 19:06:16 +02:00
|
|
|
#include "messages/limitedqueuesnapshot.hpp"
|
2017-06-11 09:31:45 +02:00
|
|
|
#include "messages/message.hpp"
|
2017-09-12 19:06:16 +02:00
|
|
|
#include "messages/messageref.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/channelmanager.hpp"
|
|
|
|
#include "singletons/settingsmanager.hpp"
|
|
|
|
#include "singletons/thememanager.hpp"
|
|
|
|
#include "singletons/windowmanager.hpp"
|
2017-04-19 15:25:05 +02:00
|
|
|
#include "ui_accountpopupform.h"
|
2017-10-11 10:34:04 +02:00
|
|
|
#include "util/benchmark.hpp"
|
2017-06-11 09:31:45 +02:00
|
|
|
#include "util/distancebetweenpoints.hpp"
|
2017-11-12 17:21:50 +01:00
|
|
|
#include "widgets/split.hpp"
|
2017-12-23 22:17:38 +01:00
|
|
|
#include "widgets/tooltipwidget.hpp"
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-02-01 16:28:28 +01:00
|
|
|
#include <QDebug>
|
2017-07-26 09:08:19 +02:00
|
|
|
#include <QDesktopServices>
|
2017-02-07 00:03:15 +01:00
|
|
|
#include <QGraphicsBlurEffect>
|
2017-01-11 01:08:20 +01:00
|
|
|
#include <QPainter>
|
2017-06-06 17:18:23 +02:00
|
|
|
|
|
|
|
#include <math.h>
|
2017-09-12 19:06:16 +02:00
|
|
|
#include <algorithm>
|
2017-02-07 00:03:15 +01:00
|
|
|
#include <chrono>
|
2017-01-18 01:04:54 +01:00
|
|
|
#include <functional>
|
2017-09-12 19:06:16 +02:00
|
|
|
#include <memory>
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2018-01-05 10:41:21 +01:00
|
|
|
#define LAYOUT_WIDTH \
|
|
|
|
(this->width() - (this->scrollBar.isVisible() ? 16 : 4) * this->getDpiMultiplier())
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
using namespace chatterino::messages;
|
|
|
|
|
2017-01-18 21:30:23 +01:00
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
|
|
|
|
2017-12-17 02:18:13 +01:00
|
|
|
ChannelView::ChannelView(BaseWidget *parent)
|
2017-09-16 00:05:06 +02:00
|
|
|
: BaseWidget(parent)
|
2017-06-11 11:36:42 +02:00
|
|
|
, scrollBar(this)
|
2017-09-23 19:23:10 +02:00
|
|
|
, userPopupWidget(std::shared_ptr<twitch::TwitchChannel>())
|
2017-01-01 02:30:42 +01:00
|
|
|
{
|
2017-08-17 17:02:10 +02:00
|
|
|
#ifndef Q_OS_MAC
|
2017-09-15 17:23:49 +02:00
|
|
|
// this->setAttribute(Qt::WA_OpaquePaintEvent);
|
2017-08-17 17:02:10 +02:00
|
|
|
#endif
|
2017-06-11 11:36:42 +02:00
|
|
|
this->setMouseTracking(true);
|
2017-01-22 12:46:35 +01:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
QObject::connect(&singletons::SettingManager::getInstance(),
|
|
|
|
&singletons::SettingManager::wordTypeMaskChanged, this,
|
2017-09-16 00:05:06 +02:00
|
|
|
&ChannelView::wordTypeMaskChanged);
|
2017-01-26 04:26:40 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->scrollBar.getCurrentValueChanged().connect([this] {
|
2017-06-06 17:18:23 +02:00
|
|
|
// Whenever the scrollbar value has been changed, re-render the ChatWidgetView
|
2017-08-12 12:09:26 +02:00
|
|
|
this->layoutMessages();
|
2017-12-28 00:48:21 +01:00
|
|
|
this->goToBottom->setVisible(this->enableScrollingToBottom && this->scrollBar.isVisible() &&
|
|
|
|
!this->scrollBar.isAtBottom());
|
2017-09-21 17:34:41 +02:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
this->queueUpdate();
|
2017-06-06 17:18:23 +02:00
|
|
|
});
|
2017-09-17 02:13:57 +02:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::WindowManager &windowManager = singletons::WindowManager::getInstance();
|
2017-12-17 02:18:13 +01:00
|
|
|
|
2017-09-17 02:13:57 +02:00
|
|
|
this->repaintGifsConnection =
|
|
|
|
windowManager.repaintGifs.connect([&] { this->updateGifEmotes(); });
|
2018-01-01 23:29:54 +01:00
|
|
|
this->layoutConnection = windowManager.layout.connect([&](Channel *channel) {
|
|
|
|
if (channel == nullptr || this->channel.get() == channel) {
|
|
|
|
this->layoutMessages();
|
|
|
|
}
|
|
|
|
});
|
2017-09-21 17:34:41 +02:00
|
|
|
|
|
|
|
this->goToBottom = new RippleEffectLabel(this, 0);
|
2018-01-05 00:01:31 +01:00
|
|
|
this->goToBottom->setStyleSheet("background-color: rgba(0,0,0,0.66); color: #FFF;");
|
|
|
|
this->goToBottom->getLabel().setText("More messages below");
|
2017-09-21 17:34:41 +02:00
|
|
|
this->goToBottom->setVisible(false);
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
this->managedConnections.emplace_back(
|
|
|
|
singletons::FontManager::getInstance().fontChanged.connect([this] {
|
|
|
|
this->layoutMessages(); //
|
|
|
|
}));
|
2017-12-17 03:26:23 +01:00
|
|
|
|
2018-01-05 03:14:46 +01:00
|
|
|
connect(goToBottom, &RippleEffectLabel::clicked, this, [this] {
|
|
|
|
QTimer::singleShot(180, [this] {
|
|
|
|
this->scrollBar.scrollToBottom(singletons::SettingManager::getInstance()
|
|
|
|
.enableSmoothScrollingNewMessages.getValue());
|
|
|
|
});
|
|
|
|
});
|
2017-10-11 10:34:04 +02:00
|
|
|
|
|
|
|
this->updateTimer.setInterval(1000 / 60);
|
|
|
|
this->updateTimer.setSingleShot(true);
|
|
|
|
connect(&this->updateTimer, &QTimer::timeout, this, [this] {
|
|
|
|
if (this->updateQueued) {
|
2018-01-05 00:15:03 +01:00
|
|
|
this->updateQueued = false;
|
|
|
|
this->repaint();
|
|
|
|
this->updateTimer.start();
|
2017-10-11 10:34:04 +02:00
|
|
|
}
|
|
|
|
});
|
2018-01-05 11:22:51 +01:00
|
|
|
|
|
|
|
this->pauseTimeout.setSingleShot(true);
|
2017-01-22 12:46:35 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
ChannelView::~ChannelView()
|
2017-01-22 12:46:35 +01:00
|
|
|
{
|
2017-12-31 22:58:35 +01:00
|
|
|
QObject::disconnect(&singletons::SettingManager::getInstance(),
|
|
|
|
&singletons::SettingManager::wordTypeMaskChanged, this,
|
|
|
|
&ChannelView::wordTypeMaskChanged);
|
2017-12-28 16:26:35 +01:00
|
|
|
this->messageAppendedConnection.disconnect();
|
|
|
|
this->messageRemovedConnection.disconnect();
|
|
|
|
this->repaintGifsConnection.disconnect();
|
|
|
|
this->layoutConnection.disconnect();
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
2017-01-03 21:19:33 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
void ChannelView::queueUpdate()
|
2017-01-03 21:19:33 +01:00
|
|
|
{
|
2018-01-05 00:15:03 +01:00
|
|
|
if (this->updateTimer.isActive()) {
|
|
|
|
this->updateQueued = true;
|
|
|
|
return;
|
|
|
|
}
|
2017-10-11 10:34:04 +02:00
|
|
|
|
2018-01-05 00:15:03 +01:00
|
|
|
this->repaint();
|
2017-10-11 10:34:04 +02:00
|
|
|
|
2018-01-05 00:15:03 +01:00
|
|
|
this->updateTimer.start();
|
2017-10-11 10:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelView::layoutMessages()
|
|
|
|
{
|
|
|
|
this->actuallyLayoutMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelView::actuallyLayoutMessages()
|
|
|
|
{
|
2017-10-27 22:05:08 +02:00
|
|
|
// BENCH(timer)
|
2017-12-22 15:13:42 +01:00
|
|
|
auto messagesSnapshot = this->getMessagesSnapshot();
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
if (messagesSnapshot.getLength() == 0) {
|
2017-06-11 11:36:42 +02:00
|
|
|
this->scrollBar.setVisible(false);
|
2017-10-11 10:34:04 +02:00
|
|
|
|
|
|
|
return;
|
2017-02-03 19:31:51 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
bool redrawRequired = false;
|
2017-06-11 11:36:42 +02:00
|
|
|
bool showScrollbar = false;
|
2017-02-03 19:31:51 +01:00
|
|
|
|
2017-06-06 17:18:23 +02:00
|
|
|
// Bool indicating whether or not we were showing all messages
|
|
|
|
// True if one of the following statements are true:
|
|
|
|
// The scrollbar was not visible
|
|
|
|
// The scrollbar was visible and at the bottom
|
2017-06-11 11:36:42 +02:00
|
|
|
this->showingLatestMessages = this->scrollBar.isAtBottom() || !this->scrollBar.isVisible();
|
2017-06-06 17:18:23 +02:00
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
size_t start = this->scrollBar.getCurrentValue();
|
2017-12-27 21:29:56 +01:00
|
|
|
// int layoutWidth =
|
|
|
|
// (this->scrollBar.isVisible() ? width() - this->scrollBar.width() : width()) - 4;
|
2018-01-05 10:41:21 +01:00
|
|
|
int layoutWidth = LAYOUT_WIDTH;
|
2017-02-03 19:31:51 +01:00
|
|
|
|
2017-02-06 18:31:25 +01:00
|
|
|
// layout the visible messages in the view
|
2017-12-22 15:13:42 +01:00
|
|
|
if (messagesSnapshot.getLength() > start) {
|
|
|
|
int y =
|
|
|
|
-(messagesSnapshot[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
|
2017-02-06 11:38:26 +01:00
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) {
|
|
|
|
auto message = messagesSnapshot[i];
|
2017-01-16 03:15:07 +01:00
|
|
|
|
2017-12-17 00:06:24 +01:00
|
|
|
redrawRequired |= message->layout(layoutWidth, this->getDpiMultiplier());
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2017-02-06 18:31:25 +01:00
|
|
|
y += message->getHeight();
|
2017-02-03 19:31:51 +01:00
|
|
|
|
2017-02-06 18:31:25 +01:00
|
|
|
if (y >= height()) {
|
|
|
|
break;
|
|
|
|
}
|
2017-02-03 19:31:51 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-16 03:15:07 +01:00
|
|
|
|
2017-02-06 18:31:25 +01:00
|
|
|
// layout the messages at the bottom to determine the scrollbar thumb size
|
2017-04-14 17:47:28 +02:00
|
|
|
int h = height() - 8;
|
2017-01-26 04:26:40 +01:00
|
|
|
|
2018-01-05 03:41:31 +01:00
|
|
|
for (int i = (int)messagesSnapshot.getLength() - 1; i >= 0; i--) {
|
2017-12-22 15:13:42 +01:00
|
|
|
auto *message = messagesSnapshot[i].get();
|
2017-01-16 03:15:07 +01:00
|
|
|
|
2017-12-17 00:06:24 +01:00
|
|
|
message->layout(layoutWidth, this->getDpiMultiplier());
|
2017-01-26 04:26:40 +01:00
|
|
|
|
2017-01-26 07:10:46 +01:00
|
|
|
h -= message->getHeight();
|
|
|
|
|
|
|
|
if (h < 0) {
|
2017-12-22 15:13:42 +01:00
|
|
|
this->scrollBar.setLargeChange((messagesSnapshot.getLength() - i) +
|
2017-06-11 11:36:42 +02:00
|
|
|
(qreal)h / message->getHeight());
|
2017-12-18 20:18:20 +01:00
|
|
|
// this->scrollBar.setDesiredValue(this->scrollBar.getDesiredValue());
|
2017-01-26 07:10:46 +01:00
|
|
|
|
|
|
|
showScrollbar = true;
|
2017-02-01 16:28:28 +01:00
|
|
|
break;
|
2017-01-26 07:10:46 +01:00
|
|
|
}
|
2017-01-26 04:26:40 +01:00
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->scrollBar.setVisible(showScrollbar);
|
2017-01-26 07:10:46 +01:00
|
|
|
|
2017-02-07 00:03:15 +01:00
|
|
|
if (!showScrollbar) {
|
2017-06-11 11:36:42 +02:00
|
|
|
this->scrollBar.setDesiredValue(0);
|
2017-02-07 00:03:15 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
this->scrollBar.setMaximum(messagesSnapshot.getLength());
|
2017-01-26 07:10:46 +01:00
|
|
|
|
2018-01-05 03:14:46 +01:00
|
|
|
// If we were showing the latest messages and the scrollbar now wants to be rendered, scroll
|
|
|
|
// to bottom
|
|
|
|
// TODO: Do we want to check if the user is currently moving the scrollbar?
|
|
|
|
// Perhaps also if the user scrolled with the scrollwheel in this ChatWidget in the last 0.2
|
|
|
|
// seconds or something
|
2017-12-28 00:48:21 +01:00
|
|
|
if (this->enableScrollingToBottom && this->showingLatestMessages && showScrollbar) {
|
2018-01-05 03:14:46 +01:00
|
|
|
this->scrollBar.scrollToBottom(
|
|
|
|
this->messageWasAdded &&
|
|
|
|
singletons::SettingManager::getInstance().enableSmoothScrollingNewMessages.getValue());
|
2018-01-05 12:22:03 +01:00
|
|
|
this->messageWasAdded = false;
|
2017-06-06 17:18:23 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 22:05:08 +02:00
|
|
|
// MARK(timer);
|
2017-10-11 10:34:04 +02:00
|
|
|
|
|
|
|
if (redrawRequired) {
|
|
|
|
this->queueUpdate();
|
|
|
|
}
|
2017-01-26 04:26:40 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::clearMessages()
|
|
|
|
{
|
|
|
|
// Clear all stored messages in this chat widget
|
|
|
|
this->messages.clear();
|
|
|
|
|
|
|
|
// Layout chat widget messages, and force an update regardless if there are no messages
|
|
|
|
this->layoutMessages();
|
2017-10-11 10:34:04 +02:00
|
|
|
this->queueUpdate();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelView::updateGifEmotes()
|
2017-01-16 03:15:07 +01:00
|
|
|
{
|
2017-10-11 10:34:04 +02:00
|
|
|
if (!this->gifEmotes.empty()) {
|
|
|
|
this->onlyUpdateEmotes = true;
|
|
|
|
this->queueUpdate();
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
ScrollBar &ChannelView::getScrollBar()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
return this->scrollBar;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
QString ChannelView::getSelectedText()
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
2017-12-22 15:13:42 +01:00
|
|
|
auto messagesSnapshot = this->getMessagesSnapshot();
|
2017-09-12 19:06:16 +02:00
|
|
|
|
|
|
|
QString text;
|
|
|
|
bool isSingleMessage = this->selection.isSingleMessage();
|
|
|
|
|
|
|
|
size_t i = std::max(0, this->selection.min.messageIndex);
|
|
|
|
|
|
|
|
int charIndex = 0;
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
2017-09-21 01:26:53 +02:00
|
|
|
auto addPart = [&](const WordPart &part, int from = 0, int to = -1) {
|
|
|
|
if (part.getCopyText().isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part.getWord().isText()) {
|
|
|
|
text += part.getText().mid(from, to);
|
|
|
|
} else {
|
|
|
|
text += part.getCopyText();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// first line
|
2017-12-22 15:13:42 +01:00
|
|
|
for (const messages::WordPart &part : messagesSnapshot[i]->getWordParts()) {
|
2017-09-12 19:06:16 +02:00
|
|
|
int charLength = part.getCharacterLength();
|
|
|
|
|
|
|
|
if (charIndex + charLength < this->selection.min.charIndex) {
|
|
|
|
charIndex += charLength;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
2018-01-05 03:41:31 +01:00
|
|
|
bool isSingleWord =
|
|
|
|
isSingleMessage &&
|
|
|
|
this->selection.max.charIndex - charIndex < part.getCharacterLength();
|
2017-09-21 01:26:53 +02:00
|
|
|
|
|
|
|
if (isSingleWord) {
|
|
|
|
// return single word
|
|
|
|
addPart(part, this->selection.min.charIndex - charIndex,
|
|
|
|
this->selection.max.charIndex - this->selection.min.charIndex);
|
|
|
|
return text;
|
2017-09-12 19:06:16 +02:00
|
|
|
} else {
|
2017-09-21 01:26:53 +02:00
|
|
|
// add first word of the selection
|
|
|
|
addPart(part, this->selection.min.charIndex - charIndex);
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
2017-09-21 01:26:53 +02:00
|
|
|
} else if (isSingleMessage && charIndex + charLength >= selection.max.charIndex) {
|
|
|
|
addPart(part, 0, this->selection.max.charIndex - charIndex);
|
2017-09-12 19:06:16 +02:00
|
|
|
|
|
|
|
return text;
|
2017-09-21 01:26:53 +02:00
|
|
|
} else {
|
|
|
|
text += part.getCopyText() + (part.hasTrailingSpace() ? " " : "");
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
charIndex += charLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
text += "\n";
|
|
|
|
|
2017-09-21 01:26:53 +02:00
|
|
|
// middle lines
|
2017-12-23 23:24:35 +01:00
|
|
|
for (i++; (int)i < this->selection.max.messageIndex; i++) {
|
2017-12-22 15:13:42 +01:00
|
|
|
for (const messages::WordPart &part : messagesSnapshot[i]->getWordParts()) {
|
2017-09-21 01:26:53 +02:00
|
|
|
if (!part.getCopyText().isEmpty()) {
|
|
|
|
text += part.getCopyText();
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2017-09-21 01:26:53 +02:00
|
|
|
if (part.hasTrailingSpace()) {
|
|
|
|
text += " ";
|
|
|
|
}
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
text += "\n";
|
|
|
|
}
|
|
|
|
|
2017-09-21 01:26:53 +02:00
|
|
|
// last line
|
2017-09-12 19:06:16 +02:00
|
|
|
charIndex = 0;
|
|
|
|
|
|
|
|
for (const messages::WordPart &part :
|
2017-12-22 15:13:42 +01:00
|
|
|
messagesSnapshot[this->selection.max.messageIndex]->getWordParts()) {
|
2017-09-12 19:06:16 +02:00
|
|
|
int charLength = part.getCharacterLength();
|
|
|
|
|
|
|
|
if (charIndex + charLength >= this->selection.max.charIndex) {
|
2017-09-21 01:26:53 +02:00
|
|
|
addPart(part, 0, this->selection.max.charIndex - charIndex);
|
2017-09-12 19:06:16 +02:00
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
text += part.getCopyText();
|
|
|
|
|
|
|
|
if (part.hasTrailingSpace()) {
|
|
|
|
text += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
charIndex += charLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2017-09-21 02:20:02 +02:00
|
|
|
bool ChannelView::hasSelection()
|
|
|
|
{
|
|
|
|
return !this->selection.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelView::clearSelection()
|
|
|
|
{
|
|
|
|
this->selection = Selection();
|
|
|
|
layoutMessages();
|
|
|
|
}
|
|
|
|
|
2017-12-28 00:48:21 +01:00
|
|
|
void ChannelView::setEnableScrollingToBottom(bool value)
|
|
|
|
{
|
|
|
|
this->enableScrollingToBottom = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChannelView::getEnableScrollingToBottom() const
|
|
|
|
{
|
|
|
|
return this->enableScrollingToBottom;
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
messages::LimitedQueueSnapshot<SharedMessageRef> ChannelView::getMessagesSnapshot()
|
|
|
|
{
|
2018-01-05 11:22:51 +01:00
|
|
|
if (!this->paused) {
|
|
|
|
this->snapshot = this->messages.getSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->snapshot;
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
void ChannelView::setChannel(std::shared_ptr<Channel> newChannel)
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
|
|
|
if (this->channel) {
|
|
|
|
this->detachChannel();
|
|
|
|
}
|
|
|
|
this->messages.clear();
|
|
|
|
|
|
|
|
// on new message
|
|
|
|
this->messageAppendedConnection =
|
2017-12-22 15:13:42 +01:00
|
|
|
newChannel->messageAppended.connect([this](SharedMessage &message) {
|
2017-09-16 00:05:06 +02:00
|
|
|
SharedMessageRef deleted;
|
|
|
|
|
|
|
|
auto messageRef = new MessageRef(message);
|
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
if (this->messages.pushBack(SharedMessageRef(messageRef), deleted)) {
|
2017-12-18 22:13:46 +01:00
|
|
|
if (this->scrollBar.isAtBottom()) {
|
|
|
|
this->scrollBar.scrollToBottom();
|
|
|
|
} else {
|
|
|
|
this->scrollBar.offset(-1);
|
|
|
|
}
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-26 12:32:24 +01:00
|
|
|
if (message->containsHighlightedPhrase()) {
|
|
|
|
this->highlightedMessageReceived.invoke();
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:14:46 +01:00
|
|
|
this->messageWasAdded = true;
|
|
|
|
this->layoutMessages();
|
2018-01-01 22:29:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
this->messageAddedAtStartConnection =
|
|
|
|
newChannel->messagesAddedAtStart.connect([this](std::vector<SharedMessage> &messages) {
|
|
|
|
std::vector<SharedMessageRef> messageRefs;
|
|
|
|
messageRefs.resize(messages.size());
|
|
|
|
qDebug() << messages.size();
|
|
|
|
for (int i = 0; i < messages.size(); i++) {
|
|
|
|
messageRefs.at(i) = SharedMessageRef(new MessageRef(messages.at(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->messages.pushFront(messageRefs).size() > 0) {
|
|
|
|
if (this->scrollBar.isAtBottom()) {
|
|
|
|
this->scrollBar.scrollToBottom();
|
|
|
|
} else {
|
2018-01-05 02:56:18 +01:00
|
|
|
this->scrollBar.offset((qreal)messages.size());
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:14:46 +01:00
|
|
|
this->messageWasAdded = true;
|
|
|
|
this->layoutMessages();
|
2017-09-16 00:05:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// on message removed
|
|
|
|
this->messageRemovedConnection =
|
2017-12-22 15:13:42 +01:00
|
|
|
newChannel->messageRemovedFromStart.connect([this](SharedMessage &) {
|
2017-09-16 00:05:06 +02:00
|
|
|
this->selection.min.messageIndex--;
|
|
|
|
this->selection.max.messageIndex--;
|
|
|
|
this->selection.start.messageIndex--;
|
|
|
|
this->selection.end.messageIndex--;
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
this->layoutMessages();
|
2017-09-16 00:05:06 +02:00
|
|
|
});
|
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
auto snapshot = newChannel->getMessageSnapshot();
|
2017-09-16 00:05:06 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < snapshot.getLength(); i++) {
|
|
|
|
SharedMessageRef deleted;
|
|
|
|
|
|
|
|
auto messageRef = new MessageRef(snapshot[i]);
|
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
this->messages.pushBack(SharedMessageRef(messageRef), deleted);
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
this->channel = newChannel;
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
this->userPopupWidget.setChannel(newChannel);
|
2018-01-05 13:42:23 +01:00
|
|
|
this->layoutMessages();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelView::detachChannel()
|
|
|
|
{
|
|
|
|
// on message added
|
|
|
|
this->messageAppendedConnection.disconnect();
|
|
|
|
|
|
|
|
// on message removed
|
|
|
|
this->messageRemovedConnection.disconnect();
|
|
|
|
}
|
|
|
|
|
2018-01-05 11:22:51 +01:00
|
|
|
void ChannelView::pause(int msecTimeout)
|
|
|
|
{
|
|
|
|
this->paused = true;
|
|
|
|
|
|
|
|
this->pauseTimeout.start(msecTimeout);
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::resizeEvent(QResizeEvent *)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
this->scrollBar.resize(this->scrollBar.width(), height());
|
2018-01-05 10:41:21 +01:00
|
|
|
this->scrollBar.move(this->width() - this->scrollBar.width(), 0);
|
2017-01-16 03:15:07 +01:00
|
|
|
|
2017-09-21 17:34:41 +02:00
|
|
|
this->goToBottom->setGeometry(0, this->height() - 32, this->width(), 32);
|
|
|
|
|
|
|
|
this->scrollBar.raise();
|
|
|
|
|
2018-01-05 10:41:21 +01:00
|
|
|
this->layoutMessages();
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-06-06 17:18:23 +02:00
|
|
|
this->update();
|
2017-01-03 21:19:33 +01:00
|
|
|
}
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::setSelection(const SelectionItem &start, const SelectionItem &end)
|
2017-08-18 15:12:07 +02:00
|
|
|
{
|
|
|
|
// selections
|
2017-09-12 19:06:16 +02:00
|
|
|
this->selection = Selection(start, end);
|
2017-08-18 15:12:07 +02:00
|
|
|
|
2017-09-21 02:20:02 +02:00
|
|
|
this->selectionChanged();
|
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
// qDebug() << min.messageIndex << ":" << min.charIndex << " " << max.messageIndex << ":"
|
|
|
|
// << max.charIndex;
|
2017-08-18 15:12:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::paintEvent(QPaintEvent * /*event*/)
|
2017-01-05 16:07:20 +01:00
|
|
|
{
|
2017-10-11 10:34:04 +02:00
|
|
|
// BENCH(timer);
|
2017-08-18 15:12:07 +02:00
|
|
|
QPainter painter(this);
|
2017-01-15 16:38:30 +01:00
|
|
|
|
2017-08-18 15:12:07 +02:00
|
|
|
// only update gif emotes
|
2017-08-17 17:02:10 +02:00
|
|
|
#ifndef Q_OS_MAC
|
2017-09-15 17:23:49 +02:00
|
|
|
// if (this->onlyUpdateEmotes) {
|
|
|
|
// this->onlyUpdateEmotes = false;
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-09-15 17:23:49 +02:00
|
|
|
// for (const GifEmoteData &item : this->gifEmotes) {
|
2017-12-31 00:50:07 +01:00
|
|
|
// painter.fillRect(item.rect, this->themeManager.ChatBackground);
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-09-15 17:23:49 +02:00
|
|
|
// painter.drawPixmap(item.rect, *item.image->getPixmap());
|
|
|
|
// }
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-09-15 17:23:49 +02:00
|
|
|
// return;
|
|
|
|
// }
|
2017-08-17 17:02:10 +02:00
|
|
|
#endif
|
2017-02-07 00:03:15 +01:00
|
|
|
|
|
|
|
// update all messages
|
2017-06-11 11:36:42 +02:00
|
|
|
this->gifEmotes.clear();
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2018-01-02 02:15:11 +01:00
|
|
|
painter.fillRect(rect(), this->themeManager.splits.background);
|
2017-08-18 15:12:07 +02:00
|
|
|
|
|
|
|
// draw messages
|
2018-01-01 23:29:54 +01:00
|
|
|
this->drawMessages(painter, false);
|
2017-08-18 15:12:07 +02:00
|
|
|
|
2017-12-26 17:41:03 +01:00
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
|
2017-08-18 15:12:07 +02:00
|
|
|
// draw gif emotes
|
|
|
|
for (GifEmoteData &item : this->gifEmotes) {
|
|
|
|
painter.drawPixmap(item.rect, *item.image->getPixmap());
|
|
|
|
}
|
2018-01-01 23:29:54 +01:00
|
|
|
|
|
|
|
// draw the overlays of the messages (such as disabled message blur-out)
|
|
|
|
this->drawMessages(painter, true);
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// MARK(timer);
|
2017-08-18 15:12:07 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
// if overlays is false then it draws the message, if true then it draws things such as the grey
|
|
|
|
// overlay when a message is disabled
|
|
|
|
void ChannelView::drawMessages(QPainter &painter, bool overlays)
|
2017-08-18 15:12:07 +02:00
|
|
|
{
|
2017-12-22 15:13:42 +01:00
|
|
|
auto messagesSnapshot = this->getMessagesSnapshot();
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
size_t start = this->scrollBar.getCurrentValue();
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
if (start >= messagesSnapshot.getLength()) {
|
2017-01-18 04:33:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
int y = -(messagesSnapshot[start].get()->getHeight() *
|
|
|
|
(fmod(this->scrollBar.getCurrentValue(), 1)));
|
2017-01-18 04:33:30 +01:00
|
|
|
|
2017-12-26 18:24:02 +01:00
|
|
|
messages::MessageRef *end = nullptr;
|
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) {
|
|
|
|
messages::MessageRef *messageRef = messagesSnapshot[i].get();
|
2017-01-18 04:33:30 +01:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
if (overlays) {
|
|
|
|
if (messageRef->isDisabled()) {
|
|
|
|
painter.fillRect(0, y, this->width(), messageRef->getHeight(),
|
2018-01-02 02:15:11 +01:00
|
|
|
this->themeManager.messages.disabled);
|
2018-01-01 23:29:54 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::shared_ptr<QPixmap> buffer = messageRef->buffer;
|
2017-01-13 18:59:11 +01:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
// bool updateBuffer = messageRef->updateBuffer;
|
|
|
|
bool updateBuffer = false;
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
if (!buffer) {
|
2018-01-05 10:41:21 +01:00
|
|
|
buffer =
|
|
|
|
std::shared_ptr<QPixmap>(new QPixmap(this->width(), messageRef->getHeight()));
|
2018-01-01 23:29:54 +01:00
|
|
|
updateBuffer = true;
|
|
|
|
}
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
updateBuffer |= this->selecting;
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
// update messages that have been changed
|
|
|
|
if (updateBuffer) {
|
|
|
|
this->updateMessageBuffer(messageRef, buffer.get(), i);
|
|
|
|
// qDebug() << "updating buffer xD";
|
|
|
|
}
|
2017-01-07 20:43:55 +01:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
// get gif emotes
|
|
|
|
for (messages::WordPart const &wordPart : messageRef->getWordParts()) {
|
|
|
|
if (wordPart.getWord().isImage()) {
|
|
|
|
messages::LazyLoadedImage &lli = wordPart.getWord().getImage();
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
if (lli.getAnimated()) {
|
|
|
|
GifEmoteData gifEmoteData;
|
|
|
|
gifEmoteData.image = &lli;
|
|
|
|
QRect rect(wordPart.getX(), wordPart.getY() + y, wordPart.getWidth(),
|
|
|
|
wordPart.getHeight());
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
gifEmoteData.rect = rect;
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
this->gifEmotes.push_back(gifEmoteData);
|
|
|
|
}
|
2017-02-07 00:03:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
messageRef->buffer = buffer;
|
2017-02-03 19:31:51 +01:00
|
|
|
|
2018-01-01 23:29:54 +01:00
|
|
|
if (buffer) {
|
|
|
|
painter.drawPixmap(0, y, *buffer.get());
|
|
|
|
}
|
2017-10-11 10:34:04 +02:00
|
|
|
}
|
2017-02-03 19:31:51 +01:00
|
|
|
|
2017-02-02 22:15:09 +01:00
|
|
|
y += messageRef->getHeight();
|
2017-01-20 06:10:28 +01:00
|
|
|
|
2017-12-26 18:24:02 +01:00
|
|
|
end = messageRef;
|
2017-01-20 06:10:28 +01:00
|
|
|
if (y > height()) {
|
|
|
|
break;
|
|
|
|
}
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
2017-12-26 18:24:02 +01:00
|
|
|
|
|
|
|
if (end == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove messages that are on screen
|
|
|
|
// the messages that are left at the end get their buffers reset
|
|
|
|
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) {
|
2018-01-01 22:29:21 +01:00
|
|
|
auto it = this->messagesOnScreen.find(messagesSnapshot[i]);
|
2017-12-26 18:24:02 +01:00
|
|
|
if (it != this->messagesOnScreen.end()) {
|
|
|
|
this->messagesOnScreen.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the message buffers that aren't on screen
|
2018-01-01 22:29:21 +01:00
|
|
|
for (std::shared_ptr<messages::MessageRef> item : this->messagesOnScreen) {
|
2017-12-26 18:24:02 +01:00
|
|
|
item->buffer.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->messagesOnScreen.clear();
|
|
|
|
|
|
|
|
// add all messages on screen to the map
|
|
|
|
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) {
|
2018-01-01 22:29:21 +01:00
|
|
|
std::shared_ptr<messages::MessageRef> messageRef = messagesSnapshot[i];
|
2017-12-26 18:24:02 +01:00
|
|
|
|
|
|
|
this->messagesOnScreen.insert(messageRef);
|
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
if (messageRef.get() == end) {
|
2017-12-26 18:24:02 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 15:12:07 +02:00
|
|
|
}
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::updateMessageBuffer(messages::MessageRef *messageRef, QPixmap *buffer,
|
|
|
|
int messageIndex)
|
2017-08-18 15:12:07 +02:00
|
|
|
{
|
|
|
|
QPainter painter(buffer);
|
|
|
|
|
2017-12-26 17:41:03 +01:00
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
|
2017-08-18 15:12:07 +02:00
|
|
|
// draw background
|
|
|
|
// if (this->selectionMin.messageIndex <= messageIndex &&
|
|
|
|
// this->selectionMax.messageIndex >= messageIndex) {
|
|
|
|
// painter.fillRect(buffer->rect(), QColor(24, 55, 25));
|
|
|
|
//} else {
|
2018-01-05 03:41:31 +01:00
|
|
|
painter.fillRect(buffer->rect(),
|
|
|
|
(messageRef->getMessage()->containsHighlightedPhrase())
|
|
|
|
? this->themeManager.messages.backgrounds.highlighted
|
|
|
|
: this->themeManager.messages.backgrounds.regular);
|
2017-08-18 15:12:07 +02:00
|
|
|
//}
|
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
// draw selection
|
|
|
|
if (!selection.isEmpty()) {
|
|
|
|
drawMessageSelection(painter, messageRef, messageIndex, buffer->height());
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw message
|
2017-08-18 15:12:07 +02:00
|
|
|
for (messages::WordPart const &wordPart : messageRef->getWordParts()) {
|
|
|
|
// image
|
|
|
|
if (wordPart.getWord().isImage()) {
|
|
|
|
messages::LazyLoadedImage &lli = wordPart.getWord().getImage();
|
|
|
|
|
|
|
|
const QPixmap *image = lli.getPixmap();
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
if (image != nullptr && !lli.getAnimated()) {
|
2017-08-18 15:12:07 +02:00
|
|
|
painter.drawPixmap(QRect(wordPart.getX(), wordPart.getY(), wordPart.getWidth(),
|
|
|
|
wordPart.getHeight()),
|
|
|
|
*image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// text
|
|
|
|
else {
|
2017-12-31 00:50:07 +01:00
|
|
|
QColor color = wordPart.getWord().getTextColor().getColor(this->themeManager);
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
this->themeManager.normalizeColor(color);
|
2017-08-18 15:12:07 +02:00
|
|
|
|
|
|
|
painter.setPen(color);
|
2017-12-23 21:18:13 +01:00
|
|
|
painter.setFont(wordPart.getWord().getFont(this->getDpiMultiplier()));
|
|
|
|
|
2017-08-18 15:12:07 +02:00
|
|
|
painter.drawText(QRectF(wordPart.getX(), wordPart.getY(), 10000, 10000),
|
|
|
|
wordPart.getText(), QTextOption(Qt::AlignLeft | Qt::AlignTop));
|
|
|
|
}
|
2017-02-07 00:03:15 +01:00
|
|
|
}
|
2017-08-18 15:12:07 +02:00
|
|
|
|
|
|
|
messageRef->updateBuffer = false;
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
2017-01-26 04:26:40 +01:00
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::drawMessageSelection(QPainter &painter, messages::MessageRef *messageRef,
|
|
|
|
int messageIndex, int bufferHeight)
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
if (this->selection.min.messageIndex > messageIndex ||
|
|
|
|
this->selection.max.messageIndex < messageIndex) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-02 02:15:11 +01:00
|
|
|
QColor selectionColor = this->themeManager.messages.selection;
|
2017-09-12 19:06:16 +02:00
|
|
|
|
|
|
|
int charIndex = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
auto &parts = messageRef->getWordParts();
|
|
|
|
|
|
|
|
int currentLineNumber = 0;
|
|
|
|
QRect rect;
|
|
|
|
|
|
|
|
if (parts.size() > 0) {
|
|
|
|
if (selection.min.messageIndex == messageIndex) {
|
|
|
|
rect.setTop(parts.at(0).getY());
|
|
|
|
}
|
|
|
|
rect.setLeft(parts.at(0).getX());
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip until selection start
|
|
|
|
if (this->selection.min.messageIndex == messageIndex && this->selection.min.charIndex != 0) {
|
|
|
|
for (; i < parts.size(); i++) {
|
|
|
|
const messages::WordPart &part = parts.at(i);
|
|
|
|
auto characterLength = part.getCharacterLength();
|
|
|
|
|
|
|
|
if (characterLength + charIndex > selection.min.charIndex) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
charIndex += characterLength;
|
|
|
|
currentLineNumber = part.getLineNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= parts.size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle word that has a cut of selection
|
|
|
|
const messages::WordPart &part = parts.at(i);
|
|
|
|
|
|
|
|
// check if selection if single word
|
|
|
|
int characterLength = part.getCharacterLength();
|
|
|
|
bool isSingleWord = charIndex + characterLength > this->selection.max.charIndex &&
|
|
|
|
this->selection.max.messageIndex == messageIndex;
|
|
|
|
|
|
|
|
rect = part.getRect();
|
|
|
|
currentLineNumber = part.getLineNumber();
|
|
|
|
|
|
|
|
if (part.getWord().isText()) {
|
|
|
|
int offset = this->selection.min.charIndex - charIndex;
|
|
|
|
|
|
|
|
for (int j = 0; j < offset; j++) {
|
2017-12-23 21:18:13 +01:00
|
|
|
rect.setLeft(rect.left() + part.getCharWidth(j, this->getDpiMultiplier()));
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isSingleWord) {
|
|
|
|
int length = (this->selection.max.charIndex - charIndex) - offset;
|
|
|
|
|
|
|
|
rect.setRight(part.getX());
|
|
|
|
|
|
|
|
for (int j = 0; j < offset + length; j++) {
|
2017-12-23 21:18:13 +01:00
|
|
|
rect.setRight(rect.right() + part.getCharWidth(j, this->getDpiMultiplier()));
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
painter.fillRect(rect, selectionColor);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isSingleWord) {
|
|
|
|
if (charIndex + 1 != this->selection.max.charIndex) {
|
|
|
|
rect.setRight(part.getX() + part.getWord().getImage().getScaledWidth());
|
|
|
|
}
|
|
|
|
painter.fillRect(rect, selectionColor);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (charIndex != this->selection.min.charIndex) {
|
|
|
|
rect.setLeft(part.getX() + part.getWord().getImage().getScaledWidth());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
charIndex += characterLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// go through lines and draw selection
|
|
|
|
for (; i < parts.size(); i++) {
|
|
|
|
const messages::WordPart &part = parts.at(i);
|
|
|
|
|
|
|
|
int charLength = part.getCharacterLength();
|
|
|
|
|
|
|
|
bool isLastSelectedWord = this->selection.max.messageIndex == messageIndex &&
|
|
|
|
charIndex + charLength > this->selection.max.charIndex;
|
|
|
|
|
|
|
|
if (part.getLineNumber() == currentLineNumber) {
|
|
|
|
rect.setLeft(std::min(rect.left(), part.getX()));
|
|
|
|
rect.setTop(std::min(rect.top(), part.getY()));
|
|
|
|
rect.setRight(std::max(rect.right(), part.getRight()));
|
|
|
|
rect.setBottom(std::max(rect.bottom(), part.getBottom() - 1));
|
|
|
|
} else {
|
|
|
|
painter.fillRect(rect, selectionColor);
|
|
|
|
|
|
|
|
currentLineNumber = part.getLineNumber();
|
|
|
|
|
|
|
|
rect = part.getRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLastSelectedWord) {
|
|
|
|
if (part.getWord().isText()) {
|
|
|
|
int offset = this->selection.min.charIndex - charIndex;
|
|
|
|
|
|
|
|
int length = (this->selection.max.charIndex - charIndex) - offset;
|
|
|
|
|
|
|
|
rect.setRight(part.getX());
|
|
|
|
|
|
|
|
for (int j = 0; j < offset + length; j++) {
|
2017-12-23 21:18:13 +01:00
|
|
|
rect.setRight(rect.right() + part.getCharWidth(j, this->getDpiMultiplier()));
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this->selection.max.charIndex == charIndex) {
|
|
|
|
rect.setRight(part.getX());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
painter.fillRect(rect, selectionColor);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
charIndex += charLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->selection.max.messageIndex != messageIndex) {
|
|
|
|
rect.setBottom(bufferHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.fillRect(rect, selectionColor);
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::wheelEvent(QWheelEvent *event)
|
2017-01-26 04:26:40 +01:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
if (this->scrollBar.isVisible()) {
|
2017-12-31 22:58:35 +01:00
|
|
|
float mouseMultiplier = singletons::SettingManager::getInstance().mouseScrollMultiplier;
|
2017-04-14 17:47:28 +02:00
|
|
|
|
2018-01-05 02:55:24 +01:00
|
|
|
float desired = this->scrollBar.getDesiredValue();
|
|
|
|
float delta = event->delta() * 1.5 * mouseMultiplier;
|
|
|
|
|
|
|
|
auto snapshot = this->getMessagesSnapshot();
|
|
|
|
int i = std::min((int)desired, (int)snapshot.getLength());
|
|
|
|
|
|
|
|
if (delta > 0) {
|
|
|
|
float scrollFactor = fmod(desired, 1);
|
|
|
|
float currentScrollLeft = (int)(scrollFactor * snapshot[i]->getHeight());
|
|
|
|
|
|
|
|
for (; i >= 0; i--) {
|
|
|
|
if (delta < currentScrollLeft) {
|
|
|
|
desired -= scrollFactor * (delta / currentScrollLeft);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
delta -= currentScrollLeft;
|
|
|
|
desired -= scrollFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
desired = 0;
|
|
|
|
} else {
|
2018-01-05 10:41:21 +01:00
|
|
|
snapshot[i - 1]->layout(LAYOUT_WIDTH, this->getDpiMultiplier());
|
2018-01-05 02:55:24 +01:00
|
|
|
scrollFactor = 1;
|
|
|
|
currentScrollLeft = snapshot[i - 1]->getHeight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delta = -delta;
|
|
|
|
float scrollFactor = 1 - fmod(desired, 1);
|
|
|
|
float currentScrollLeft = (int)(scrollFactor * snapshot[i]->getHeight());
|
|
|
|
|
|
|
|
for (; i < snapshot.getLength(); i++) {
|
|
|
|
if (delta < currentScrollLeft) {
|
|
|
|
desired += scrollFactor * ((double)delta / currentScrollLeft);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
delta -= currentScrollLeft;
|
|
|
|
desired += scrollFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == snapshot.getLength() - 1) {
|
|
|
|
desired = snapshot.getLength();
|
|
|
|
} else {
|
2018-01-05 10:41:21 +01:00
|
|
|
snapshot[i + 1]->layout(LAYOUT_WIDTH, this->getDpiMultiplier());
|
2018-01-05 02:55:24 +01:00
|
|
|
|
|
|
|
scrollFactor = 1;
|
|
|
|
currentScrollLeft = snapshot[i + 1]->getHeight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->scrollBar.setDesiredValue(desired, true);
|
2017-02-06 18:31:25 +01:00
|
|
|
}
|
2017-01-26 04:26:40 +01:00
|
|
|
}
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2018-01-05 11:22:51 +01:00
|
|
|
void ChannelView::enterEvent(QEvent *)
|
|
|
|
{
|
|
|
|
// this->pause(PAUSE_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelView::leaveEvent(QEvent *)
|
|
|
|
{
|
|
|
|
this->paused = false;
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::mouseMoveEvent(QMouseEvent *event)
|
2017-02-17 23:51:35 +01:00
|
|
|
{
|
2018-01-05 11:22:51 +01:00
|
|
|
if (singletons::SettingManager::getInstance().pauseChatHover.getValue()) {
|
|
|
|
this->pause(300);
|
|
|
|
}
|
|
|
|
|
2017-12-23 22:17:38 +01:00
|
|
|
auto tooltipWidget = TooltipWidget::getInstance();
|
2017-02-17 23:51:35 +01:00
|
|
|
std::shared_ptr<messages::MessageRef> message;
|
|
|
|
QPoint relativePos;
|
2017-08-18 15:12:07 +02:00
|
|
|
int messageIndex;
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-12-19 00:09:38 +01:00
|
|
|
// no message under cursor
|
2017-08-18 15:12:07 +02:00
|
|
|
if (!tryGetMessageAt(event->pos(), message, relativePos, messageIndex)) {
|
2017-12-19 00:09:38 +01:00
|
|
|
this->setCursor(Qt::ArrowCursor);
|
2017-12-23 22:17:38 +01:00
|
|
|
tooltipWidget->hide();
|
2017-12-19 00:09:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is selecting
|
2017-08-18 15:12:07 +02:00
|
|
|
if (this->selecting) {
|
2018-01-05 11:22:51 +01:00
|
|
|
this->pause(500);
|
2017-08-18 15:12:07 +02:00
|
|
|
int index = message->getSelectionIndex(relativePos);
|
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
this->setSelection(this->selection.start, SelectionItem(messageIndex, index));
|
2017-08-18 15:12:07 +02:00
|
|
|
|
2018-01-05 11:22:51 +01:00
|
|
|
this->queueUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
// message under cursor is collapsed
|
|
|
|
if (message->isCollapsed()) {
|
|
|
|
this->setCursor(Qt::PointingHandCursor);
|
|
|
|
tooltipWidget->hide();
|
|
|
|
return;
|
2017-08-18 15:12:07 +02:00
|
|
|
}
|
2017-04-24 23:00:26 +02:00
|
|
|
|
2017-12-19 00:09:38 +01:00
|
|
|
// check if word underneath cursor
|
2017-09-12 19:06:16 +02:00
|
|
|
const messages::Word *hoverWord;
|
|
|
|
if ((hoverWord = message->tryGetWordPart(relativePos)) == nullptr) {
|
2017-12-19 00:09:38 +01:00
|
|
|
this->setCursor(Qt::ArrowCursor);
|
2017-12-23 22:17:38 +01:00
|
|
|
tooltipWidget->hide();
|
2017-02-17 23:51:35 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-12-19 03:36:05 +01:00
|
|
|
const auto &tooltip = hoverWord->getTooltip();
|
|
|
|
|
2017-12-23 22:17:38 +01:00
|
|
|
if (hoverWord->isImage()) {
|
|
|
|
tooltipWidget->moveTo(event->globalPos());
|
|
|
|
tooltipWidget->setText(tooltip);
|
|
|
|
tooltipWidget->show();
|
2017-12-19 03:36:05 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 00:09:38 +01:00
|
|
|
// check if word has a link
|
2017-09-12 19:06:16 +02:00
|
|
|
if (hoverWord->getLink().isValid()) {
|
2017-12-19 00:09:38 +01:00
|
|
|
this->setCursor(Qt::PointingHandCursor);
|
2017-02-17 23:51:35 +01:00
|
|
|
} else {
|
2017-12-19 00:09:38 +01:00
|
|
|
this->setCursor(Qt::ArrowCursor);
|
2017-02-17 23:51:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::mousePressEvent(QMouseEvent *event)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2018-01-05 11:22:51 +01:00
|
|
|
if (singletons::SettingManager::getInstance().linksDoubleClickOnly.getValue()) {
|
|
|
|
this->pause(200);
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->isMouseDown = true;
|
2017-08-18 15:12:07 +02:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->lastPressPosition = event->screenPos();
|
2017-06-11 20:53:43 +02:00
|
|
|
|
2017-08-18 15:12:07 +02:00
|
|
|
std::shared_ptr<messages::MessageRef> message;
|
|
|
|
QPoint relativePos;
|
|
|
|
int messageIndex;
|
|
|
|
|
2017-09-16 16:49:52 +02:00
|
|
|
this->mouseDown(event);
|
|
|
|
|
2017-08-18 15:12:07 +02:00
|
|
|
if (!tryGetMessageAt(event->pos(), message, relativePos, messageIndex)) {
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
auto messagesSnapshot = this->getMessagesSnapshot();
|
|
|
|
if (messagesSnapshot.getLength() == 0) {
|
2017-11-04 13:17:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start selection at the last message at its last index
|
2017-12-22 15:13:42 +01:00
|
|
|
auto lastMessageIndex = messagesSnapshot.getLength() - 1;
|
|
|
|
auto lastMessage = messagesSnapshot[lastMessageIndex];
|
2017-11-04 13:17:35 +01:00
|
|
|
auto lastCharacterIndex = lastMessage->getLastCharacterIndex();
|
|
|
|
|
|
|
|
SelectionItem selectionItem(lastMessageIndex, lastCharacterIndex);
|
|
|
|
this->setSelection(selectionItem, selectionItem);
|
|
|
|
this->selecting = true;
|
|
|
|
|
2017-08-18 15:12:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:09:38 +01:00
|
|
|
// check if message is collapsed
|
|
|
|
if (message->isCollapsed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-18 15:12:07 +02:00
|
|
|
int index = message->getSelectionIndex(relativePos);
|
|
|
|
|
|
|
|
auto selectionItem = SelectionItem(messageIndex, index);
|
|
|
|
this->setSelection(selectionItem, selectionItem);
|
|
|
|
this->selecting = true;
|
|
|
|
|
|
|
|
this->repaint();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void ChannelView::mouseReleaseEvent(QMouseEvent *event)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
if (!this->isMouseDown) {
|
2017-04-12 17:46:44 +02:00
|
|
|
// We didn't grab the mouse press, so we shouldn't be handling the mouse
|
|
|
|
// release
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-05 11:22:51 +01:00
|
|
|
if (this->selecting) {
|
|
|
|
this->paused = false;
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->isMouseDown = false;
|
2017-08-18 15:12:07 +02:00
|
|
|
this->selecting = false;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
float distance = util::distanceBetweenPoints(this->lastPressPosition, event->screenPos());
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-12-16 02:09:51 +01:00
|
|
|
// qDebug() << "Distance: " << distance;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
if (fabsf(distance) > 15.f) {
|
|
|
|
// It wasn't a proper click, so we don't care about that here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you clicked and released less than X pixels away, it counts
|
|
|
|
// as a click!
|
|
|
|
|
|
|
|
// show user thing pajaW
|
|
|
|
|
|
|
|
std::shared_ptr<messages::MessageRef> message;
|
|
|
|
QPoint relativePos;
|
2017-08-18 15:12:07 +02:00
|
|
|
int messageIndex;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-08-18 15:12:07 +02:00
|
|
|
if (!tryGetMessageAt(event->pos(), message, relativePos, messageIndex)) {
|
2017-04-12 17:46:44 +02:00
|
|
|
// No message at clicked position
|
2017-06-11 11:36:42 +02:00
|
|
|
this->userPopupWidget.hide();
|
2017-04-12 17:46:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:09:38 +01:00
|
|
|
// message under cursor is collapsed
|
|
|
|
if (message->isCollapsed()) {
|
|
|
|
message->setCollapsed(false);
|
|
|
|
this->layoutMessages();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
const messages::Word *hoverWord;
|
2017-04-24 23:00:26 +02:00
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
if ((hoverWord = message->tryGetWordPart(relativePos)) == nullptr) {
|
2017-04-24 23:00:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
auto &link = hoverWord->getLink();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-04-24 23:00:26 +02:00
|
|
|
switch (link.getType()) {
|
2017-07-31 00:57:42 +02:00
|
|
|
case messages::Link::UserInfo: {
|
2017-07-31 22:14:54 +02:00
|
|
|
auto user = link.getValue();
|
2017-06-11 11:36:42 +02:00
|
|
|
this->userPopupWidget.setName(user);
|
|
|
|
this->userPopupWidget.move(event->screenPos().toPoint());
|
2017-09-23 19:23:10 +02:00
|
|
|
this->userPopupWidget.updatePermissions();
|
2017-06-11 11:36:42 +02:00
|
|
|
this->userPopupWidget.show();
|
|
|
|
this->userPopupWidget.setFocus();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-04-24 23:00:26 +02:00
|
|
|
qDebug() << "Clicked " << user << "s message";
|
|
|
|
break;
|
2017-07-26 09:08:19 +02:00
|
|
|
}
|
2017-07-31 00:57:42 +02:00
|
|
|
case messages::Link::Url: {
|
2017-07-26 09:08:19 +02:00
|
|
|
QDesktopServices::openUrl(QUrl(link.getValue()));
|
|
|
|
break;
|
|
|
|
}
|
2017-04-24 23:00:26 +02:00
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
bool ChannelView::tryGetMessageAt(QPoint p, std::shared_ptr<messages::MessageRef> &_message,
|
|
|
|
QPoint &relativePos, int &index)
|
2017-02-17 23:51:35 +01:00
|
|
|
{
|
2017-12-22 15:13:42 +01:00
|
|
|
auto messagesSnapshot = this->getMessagesSnapshot();
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
size_t start = this->scrollBar.getCurrentValue();
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
if (start >= messagesSnapshot.getLength()) {
|
2017-02-17 23:51:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
int y = -(messagesSnapshot[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) {
|
|
|
|
auto message = messagesSnapshot[i];
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-04-24 23:00:26 +02:00
|
|
|
if (p.y() < y + message->getHeight()) {
|
|
|
|
relativePos = QPoint(p.x(), p.y() - y);
|
2017-02-17 23:51:35 +01:00
|
|
|
_message = message;
|
2017-08-18 15:12:07 +02:00
|
|
|
index = i;
|
2017-02-17 23:51:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-04-24 23:00:26 +02:00
|
|
|
|
|
|
|
y += message->getHeight();
|
2017-02-17 23:51:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-06 17:18:23 +02:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|