mirror-chatterino2/src/widgets/helper/channelview.cpp

936 lines
27 KiB
C++
Raw Normal View History

2017-12-31 00:50:07 +01:00
#include "channelview.hpp"
#include "debug/log.hpp"
#include "messages/layouts/messagelayout.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"
2018-02-05 15:11:50 +01:00
#include "providers/twitch/twitchserver.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"
#include "ui_accountpopupform.h"
#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"
#include "widgets/tooltipwidget.hpp"
2017-01-11 01:08:20 +01:00
2018-01-24 21:16:00 +01:00
#include <QClipboard>
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
2017-09-12 19:06:16 +02:00
#include <algorithm>
2017-02-07 00:03:15 +01:00
#include <chrono>
#include <cmath>
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-25 20:49:49 +01:00
#define LAYOUT_WIDTH (this->width() - (this->scrollBar.isVisible() ? 16 : 4) * this->getScale())
using namespace chatterino::messages;
2018-02-05 15:11:50 +01:00
using namespace chatterino::providers::twitch;
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace widgets {
2017-12-17 02:18:13 +01:00
ChannelView::ChannelView(BaseWidget *parent)
: BaseWidget(parent)
, scrollBar(this)
2018-02-05 15:11:50 +01:00
, userPopupWidget(std::shared_ptr<TwitchChannel>())
2017-01-01 02:30:42 +01:00
{
#ifndef Q_OS_MAC
2017-09-15 17:23:49 +02:00
// this->setAttribute(Qt::WA_OpaquePaintEvent);
#endif
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::wordFlagsChanged, this,
&ChannelView::wordFlagsChanged);
2017-01-26 04:26:40 +01: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();
this->goToBottom->setVisible(this->enableScrollingToBottom && this->scrollBar.isVisible() &&
!this->scrollBar.isAtBottom());
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
this->repaintGifsConnection = windowManager.repaintGifs.connect([&] { this->queueUpdate(); });
this->layoutConnection = windowManager.layout.connect([&](Channel *channel) {
if (channel == nullptr || this->channel.get() == channel) {
this->layoutMessages();
}
});
this->goToBottom = new RippleEffectLabel(this, 0);
this->goToBottom->setStyleSheet("background-color: rgba(0,0,0,0.66); color: #FFF;");
this->goToBottom->getLabel().setText("More messages below");
this->goToBottom->setVisible(false);
2017-12-31 22:58:35 +01:00
this->managedConnections.emplace_back(
singletons::FontManager::getInstance().fontChanged.connect([this] {
this->layoutMessages(); //
}));
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());
});
});
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();
}
});
this->pauseTimeout.setSingleShot(true);
2018-01-16 01:40:52 +01:00
auto e = new QResizeEvent(this->size(), this->size());
this->resizeEvent(e);
delete e;
2018-01-23 22:48:33 +01:00
singletons::SettingManager::getInstance().showLastMessageIndicator.connect(
[this](auto, auto) { this->update(); }, this->managedConnections);
2017-01-22 12:46:35 +01: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::wordFlagsChanged, this,
&ChannelView::wordFlagsChanged);
this->messageAppendedConnection.disconnect();
this->messageRemovedConnection.disconnect();
this->repaintGifsConnection.disconnect();
this->layoutConnection.disconnect();
2018-01-05 23:14:55 +01:00
this->messageAddedAtStartConnection.disconnect();
this->messageReplacedConnection.disconnect();
2017-01-01 02:30:42 +01:00
}
2017-01-03 21:19:33 +01:00
2018-01-25 20:49:49 +01:00
void ChannelView::themeRefreshEvent()
2018-01-24 20:35:26 +01:00
{
2018-01-25 20:49:49 +01:00
BaseWidget::themeRefreshEvent();
2018-01-24 20:35:26 +01:00
this->layoutMessages();
}
void ChannelView::queueUpdate()
2017-01-03 21:19:33 +01:00
{
2018-01-19 23:41:02 +01:00
if (this->updateTimer.isActive()) {
this->updateQueued = true;
return;
}
2018-01-19 23:41:02 +01:00
// this->repaint();
this->update();
2018-01-19 23:41:02 +01:00
this->updateTimer.start();
}
void ChannelView::layoutMessages()
{
this->actuallyLayoutMessages();
}
void ChannelView::actuallyLayoutMessages()
{
2017-10-27 22:05:08 +02:00
// BENCH(timer)
auto messagesSnapshot = this->getMessagesSnapshot();
2017-01-11 01:08:20 +01:00
if (messagesSnapshot.getLength() == 0) {
this->scrollBar.setVisible(false);
return;
}
bool redrawRequired = false;
bool showScrollbar = false;
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
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;
int layoutWidth = LAYOUT_WIDTH;
2018-01-17 16:52:51 +01:00
MessageElement::Flags flags = this->getFlags();
// layout the visible messages in the view
if (messagesSnapshot.getLength() > start) {
int y =
-(messagesSnapshot[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) {
auto message = messagesSnapshot[i];
2018-01-25 20:49:49 +01:00
redrawRequired |= message->layout(layoutWidth, this->getScale(), flags);
2017-02-02 20:35:12 +01:00
y += message->getHeight();
if (y >= height()) {
break;
}
}
}
// 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--) {
auto *message = messagesSnapshot[i].get();
2018-01-25 20:49:49 +01:00
message->layout(layoutWidth, this->getScale(), flags);
2017-01-26 04:26:40 +01:00
2017-01-26 07:10:46 +01:00
h -= message->getHeight();
if (h < 0) {
this->scrollBar.setLargeChange((messagesSnapshot.getLength() - i) +
(qreal)h / message->getHeight());
// 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
}
this->scrollBar.setVisible(showScrollbar);
2017-01-26 07:10:46 +01:00
2017-02-07 00:03:15 +01:00
if (!showScrollbar) {
this->scrollBar.setDesiredValue(0);
2017-02-07 00:03:15 +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
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);
if (redrawRequired) {
this->queueUpdate();
}
2017-01-26 04:26:40 +01: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();
this->queueUpdate();
}
2018-01-06 03:48:56 +01:00
Scrollbar &ChannelView::getScrollBar()
2017-04-12 17:46:44 +02:00
{
return this->scrollBar;
2017-04-12 17:46:44 +02:00
}
QString ChannelView::getSelectedText()
2017-09-12 19:06:16 +02:00
{
2018-01-16 02:39:31 +01:00
QString result = "";
2017-09-12 19:06:16 +02:00
2018-01-16 02:39:31 +01:00
messages::LimitedQueueSnapshot<MessageLayoutPtr> messagesSnapshot = this->getMessagesSnapshot();
2017-09-12 19:06:16 +02:00
2018-01-16 02:39:31 +01:00
Selection selection = this->selection;
2017-09-12 19:06:16 +02:00
2018-01-16 02:39:31 +01:00
if (selection.isEmpty()) {
return result;
}
2017-09-12 19:06:16 +02:00
2018-01-16 02:39:31 +01:00
for (int msg = selection.min.messageIndex; msg <= selection.min.messageIndex; msg++) {
MessageLayoutPtr layout = messagesSnapshot[msg];
int from = msg == selection.min.messageIndex ? selection.min.charIndex : 0;
int to = msg == selection.max.messageIndex ? selection.max.charIndex
: layout->getLastCharacterIndex();
2017-09-12 19:06:16 +02:00
2018-01-16 02:39:31 +01:00
layout->addSelectionText(result, from, to);
}
2017-09-12 19:06:16 +02:00
2018-01-16 02:39:31 +01:00
return result;
2017-09-12 19:06:16 +02:00
}
2017-09-21 02:20:02 +02:00
bool ChannelView::hasSelection()
{
return !this->selection.isEmpty();
}
void ChannelView::clearSelection()
{
this->selection = Selection();
layoutMessages();
}
void ChannelView::setEnableScrollingToBottom(bool value)
{
this->enableScrollingToBottom = value;
}
bool ChannelView::getEnableScrollingToBottom() const
{
return this->enableScrollingToBottom;
}
2018-01-27 21:13:22 +01:00
void ChannelView::setOverrideFlags(boost::optional<messages::MessageElement::Flags> value)
{
this->overrideFlags = value;
}
const boost::optional<messages::MessageElement::Flags> &ChannelView::getOverrideFlags() const
{
return this->overrideFlags;
}
messages::LimitedQueueSnapshot<MessageLayoutPtr> ChannelView::getMessagesSnapshot()
{
if (!this->paused) {
this->snapshot = this->messages.getSnapshot();
}
return this->snapshot;
}
void ChannelView::setChannel(ChannelPtr newChannel)
{
if (this->channel) {
this->detachChannel();
}
this->messages.clear();
// on new message
this->messageAppendedConnection =
newChannel->messageAppended.connect([this](MessagePtr &message) {
MessageLayoutPtr deleted;
auto messageRef = new MessageLayout(message);
if (this->messages.pushBack(MessageLayoutPtr(messageRef), deleted)) {
2018-01-11 21:03:40 +01:00
if (!this->paused) {
if (this->scrollBar.isAtBottom()) {
this->scrollBar.scrollToBottom();
} else {
this->scrollBar.offset(-1);
}
2017-12-18 22:13:46 +01:00
}
}
2018-01-28 03:29:42 +01:00
if (message->flags & ~Message::DoNotTriggerNotification) {
this->highlightedMessageReceived.invoke();
}
2018-01-06 03:48:56 +01:00
this->scrollBar.addHighlight(message->getScrollBarHighlight());
2018-01-05 03:14:46 +01:00
this->messageWasAdded = true;
this->layoutMessages();
});
this->messageAddedAtStartConnection =
newChannel->messagesAddedAtStart.connect([this](std::vector<MessagePtr> &messages) {
std::vector<MessageLayoutPtr> messageRefs;
messageRefs.resize(messages.size());
for (size_t i = 0; i < messages.size(); i++) {
messageRefs.at(i) = MessageLayoutPtr(new MessageLayout(messages.at(i)));
}
2018-01-11 21:03:40 +01:00
if (!this->paused) {
if (this->messages.pushFront(messageRefs).size() > 0) {
if (this->scrollBar.isAtBottom()) {
this->scrollBar.scrollToBottom();
} else {
this->scrollBar.offset((qreal)messages.size());
}
}
}
2018-01-06 03:48:56 +01:00
std::vector<ScrollbarHighlight> highlights;
highlights.reserve(messages.size());
for (size_t i = 0; i < messages.size(); i++) {
2018-01-06 03:48:56 +01:00
highlights.push_back(messages.at(i)->getScrollBarHighlight());
}
this->scrollBar.addHighlightsAtStart(highlights);
2018-01-05 03:14:46 +01:00
this->messageWasAdded = true;
this->layoutMessages();
});
// on message removed
this->messageRemovedConnection =
newChannel->messageRemovedFromStart.connect([this](MessagePtr &) {
this->selection.min.messageIndex--;
this->selection.max.messageIndex--;
this->selection.start.messageIndex--;
this->selection.end.messageIndex--;
this->layoutMessages();
});
2018-01-05 23:14:55 +01:00
// on message replaced
this->messageReplacedConnection =
newChannel->messageReplaced.connect([this](size_t index, MessagePtr replacement) {
MessageLayoutPtr newItem(new MessageLayout(replacement));
2018-01-05 23:14:55 +01:00
2018-01-06 03:48:56 +01:00
this->scrollBar.replaceHighlight(index, replacement->getScrollBarHighlight());
2018-01-05 23:14:55 +01:00
this->messages.replaceItem(this->messages.getSnapshot()[index], newItem);
this->layoutMessages();
});
auto snapshot = newChannel->getMessageSnapshot();
for (size_t i = 0; i < snapshot.getLength(); i++) {
MessageLayoutPtr deleted;
auto messageRef = new MessageLayout(snapshot[i]);
this->messages.pushBack(MessageLayoutPtr(messageRef), deleted);
}
this->channel = newChannel;
this->userPopupWidget.setChannel(newChannel);
2018-01-05 13:42:23 +01:00
this->layoutMessages();
2018-01-07 00:05:32 +01:00
this->queueUpdate();
}
void ChannelView::detachChannel()
{
// on message added
this->messageAppendedConnection.disconnect();
// on message removed
this->messageRemovedConnection.disconnect();
}
void ChannelView::pause(int msecTimeout)
{
this->paused = true;
this->pauseTimeout.start(msecTimeout);
}
2018-01-23 22:48:33 +01:00
void ChannelView::updateLastReadMessage()
{
auto _snapshot = this->getMessagesSnapshot();
if (_snapshot.getLength() > 0) {
this->lastReadMessage = _snapshot[_snapshot.getLength() - 1];
}
this->update();
}
void ChannelView::resizeEvent(QResizeEvent *)
2017-04-12 17:46:44 +02:00
{
this->scrollBar.resize(this->scrollBar.width(), height());
this->scrollBar.move(this->width() - this->scrollBar.width(), 0);
this->goToBottom->setGeometry(0, this->height() - 32, this->width(), 32);
this->scrollBar.raise();
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
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
this->selectionChanged.invoke();
2017-08-18 15:12:07 +02:00
}
2018-01-17 16:52:51 +01:00
messages::MessageElement::Flags ChannelView::getFlags() const
{
2018-01-27 21:13:22 +01:00
if (this->overrideFlags) {
return this->overrideFlags.get();
}
MessageElement::Flags flags = singletons::SettingManager::getInstance().getWordFlags();
2018-01-17 16:52:51 +01:00
Split *split = dynamic_cast<Split *>(this->parentWidget());
if (split != nullptr) {
if (split->getModerationMode()) {
flags = (MessageElement::Flags)(flags | MessageElement::ModeratorTools);
}
2018-02-05 15:11:50 +01:00
if (this->channel == TwitchServer::getInstance().mentionsChannel) {
2018-01-23 23:28:06 +01:00
flags = (MessageElement::Flags)(flags | MessageElement::ChannelName);
}
2018-01-17 16:52:51 +01:00
}
return flags;
}
void ChannelView::paintEvent(QPaintEvent * /*event*/)
2017-01-05 16:07:20 +01:00
{
// BENCH(timer);
2017-02-07 00:03:15 +01:00
QPainter painter(this);
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
this->drawMessages(painter);
// MARK(timer);
2017-08-18 15:12:07 +02: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)
2017-08-18 15:12:07 +02: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
if (start >= messagesSnapshot.getLength()) {
2017-01-18 04:33:30 +01:00
return;
}
int y = -(messagesSnapshot[start].get()->getHeight() *
(fmod(this->scrollBar.getCurrentValue(), 1)));
2017-01-18 04:33:30 +01:00
messages::MessageLayout *end = nullptr;
2018-01-23 22:48:33 +01:00
bool windowFocused = this->window() == QApplication::activeWindow();
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) {
messages::MessageLayout *layout = messagesSnapshot[i].get();
2017-02-07 00:03:15 +01:00
2018-01-23 22:48:33 +01:00
bool isLastMessage = false;
if (singletons::SettingManager::getInstance().showLastMessageIndicator) {
isLastMessage = this->lastReadMessage.get() == layout;
}
layout->paint(painter, y, i, this->selection, isLastMessage, windowFocused);
y += layout->getHeight();
2017-01-20 06:10:28 +01:00
end = layout;
2017-01-20 06:10:28 +01:00
if (y > height()) {
break;
}
2017-01-11 01:08:20 +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) {
auto it = this->messagesOnScreen.find(messagesSnapshot[i]);
if (it != this->messagesOnScreen.end()) {
this->messagesOnScreen.erase(it);
}
}
// delete the message buffers that aren't on screen
for (const std::shared_ptr<messages::MessageLayout> &item : this->messagesOnScreen) {
item->deleteBuffer();
}
this->messagesOnScreen.clear();
// add all messages on screen to the map
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) {
std::shared_ptr<messages::MessageLayout> layout = messagesSnapshot[i];
this->messagesOnScreen.insert(layout);
if (layout.get() == end) {
break;
}
}
2017-08-18 15:12:07 +02:00
}
2017-02-07 00:03:15 +01:00
void ChannelView::wheelEvent(QWheelEvent *event)
2017-01-26 04:26:40 +01: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 snapshotLength = (int)snapshot.getLength();
int i = std::min((int)desired, snapshotLength);
2018-01-05 02:55:24 +01:00
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-25 20:49:49 +01:00
snapshot[i - 1]->layout(LAYOUT_WIDTH, this->getScale(), this->getFlags());
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 < snapshotLength; i++) {
2018-01-05 02:55:24 +01:00
if (delta < currentScrollLeft) {
desired += scrollFactor * ((double)delta / currentScrollLeft);
break;
} else {
delta -= currentScrollLeft;
desired += scrollFactor;
}
if (i == snapshotLength - 1) {
2018-01-05 02:55:24 +01:00
desired = snapshot.getLength();
} else {
2018-01-25 20:49:49 +01:00
snapshot[i + 1]->layout(LAYOUT_WIDTH, this->getScale(), this->getFlags());
2018-01-05 02:55:24 +01:00
scrollFactor = 1;
currentScrollLeft = snapshot[i + 1]->getHeight();
}
}
}
this->scrollBar.setDesiredValue(desired, true);
}
2017-01-26 04:26:40 +01:00
}
2017-02-17 23:51:35 +01:00
void ChannelView::enterEvent(QEvent *)
{
// this->pause(PAUSE_TIME);
}
void ChannelView::leaveEvent(QEvent *)
{
this->paused = false;
}
void ChannelView::mouseMoveEvent(QMouseEvent *event)
2017-02-17 23:51:35 +01:00
{
if (event->modifiers() & (Qt::AltModifier | Qt::ControlModifier)) {
this->unsetCursor();
event->ignore();
return;
}
if (singletons::SettingManager::getInstance().pauseChatHover.getValue()) {
this->pause(300);
}
auto tooltipWidget = TooltipWidget::getInstance();
std::shared_ptr<messages::MessageLayout> layout;
2017-02-17 23:51:35 +01:00
QPoint relativePos;
2017-08-18 15:12:07 +02:00
int messageIndex;
2017-02-17 23:51:35 +01:00
// no message under cursor
if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex)) {
this->setCursor(Qt::ArrowCursor);
tooltipWidget->hide();
return;
}
// is selecting
2017-08-18 15:12:07 +02:00
if (this->selecting) {
this->pause(500);
int index = layout->getSelectionIndex(relativePos);
2017-08-18 15:12:07 +02:00
2017-09-12 19:06:16 +02:00
this->setSelection(this->selection.start, SelectionItem(messageIndex, index));
2017-08-18 15:12:07 +02:00
this->queueUpdate();
}
// message under cursor is collapsed
2018-01-28 04:07:45 +01:00
if (layout->flags & MessageLayout::Collapsed) {
this->setCursor(Qt::PointingHandCursor);
tooltipWidget->hide();
return;
2017-08-18 15:12:07 +02:00
}
2017-04-24 23:00:26 +02:00
// check if word underneath cursor
const messages::MessageLayoutElement *hoverLayoutElement = layout->getElementAt(relativePos);
if (hoverLayoutElement == nullptr) {
this->setCursor(Qt::ArrowCursor);
tooltipWidget->hide();
2017-02-17 23:51:35 +01:00
return;
}
const auto &tooltip = hoverLayoutElement->getCreator().getTooltip();
2017-12-19 03:36:05 +01:00
2018-01-17 03:26:32 +01:00
if (tooltip.isEmpty()) {
tooltipWidget->hide();
} else {
tooltipWidget->moveTo(this, event->globalPos());
2018-01-17 03:26:32 +01:00
tooltipWidget->setText(tooltip);
tooltipWidget->show();
}
2017-12-19 03:36:05 +01:00
// check if word has a link
2018-01-17 14:14:31 +01:00
if (hoverLayoutElement->getLink().isValid()) {
this->setCursor(Qt::PointingHandCursor);
2017-02-17 23:51:35 +01:00
} else {
this->setCursor(Qt::ArrowCursor);
2017-02-17 23:51:35 +01:00
}
}
void ChannelView::mousePressEvent(QMouseEvent *event)
2017-04-12 17:46:44 +02:00
{
if (event->modifiers() & (Qt::AltModifier | Qt::ControlModifier)) {
this->unsetCursor();
event->ignore();
return;
}
if (singletons::SettingManager::getInstance().linksDoubleClickOnly.getValue()) {
this->pause(200);
}
this->isMouseDown = true;
2017-08-18 15:12:07 +02:00
this->lastPressPosition = event->screenPos();
std::shared_ptr<messages::MessageLayout> layout;
2017-08-18 15:12:07 +02:00
QPoint relativePos;
int messageIndex;
this->mouseDown.invoke(event);
2017-09-16 16:49:52 +02:00
if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex)) {
2017-08-18 15:12:07 +02:00
setCursor(Qt::ArrowCursor);
auto messagesSnapshot = this->getMessagesSnapshot();
if (messagesSnapshot.getLength() == 0) {
return;
}
// Start selection at the last message at its last index
auto lastMessageIndex = messagesSnapshot.getLength() - 1;
auto lastMessage = messagesSnapshot[lastMessageIndex];
auto lastCharacterIndex = lastMessage->getLastCharacterIndex();
SelectionItem selectionItem(lastMessageIndex, lastCharacterIndex);
this->setSelection(selectionItem, selectionItem);
this->selecting = true;
2017-08-18 15:12:07 +02:00
return;
}
// check if message is collapsed
2018-01-28 04:07:45 +01:00
if (layout->flags & MessageLayout::Collapsed) {
return;
}
int index = layout->getSelectionIndex(relativePos);
2017-08-18 15:12:07 +02:00
auto selectionItem = SelectionItem(messageIndex, index);
this->setSelection(selectionItem, selectionItem);
this->selecting = true;
this->repaint();
2017-04-12 17:46:44 +02:00
}
void ChannelView::mouseReleaseEvent(QMouseEvent *event)
2017-04-12 17:46:44 +02:00
{
if (event->modifiers() & (Qt::AltModifier | Qt::ControlModifier)) {
this->unsetCursor();
event->ignore();
return;
}
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;
}
if (this->selecting) {
this->paused = false;
}
this->isMouseDown = false;
2017-08-18 15:12:07 +02:00
this->selecting = false;
2017-04-12 17:46:44 +02:00
float distance = util::distanceBetweenPoints(this->lastPressPosition, event->screenPos());
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!
2018-02-06 00:10:30 +01:00
this->layoutMessages();
2017-04-12 17:46:44 +02:00
std::shared_ptr<messages::MessageLayout> layout;
2017-04-12 17:46:44 +02:00
QPoint relativePos;
2017-08-18 15:12:07 +02:00
int messageIndex;
2017-04-12 17:46:44 +02:00
if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex)) {
2017-04-12 17:46:44 +02:00
// No message at clicked position
this->userPopupWidget.hide();
2017-04-12 17:46:44 +02:00
return;
}
// message under cursor is collapsed
2018-01-28 04:07:45 +01:00
if (layout->flags & MessageLayout::Collapsed) {
layout->flags &= MessageLayout::Collapsed;
2018-02-06 00:10:30 +01:00
// this->layoutMessages();
return;
}
const messages::MessageLayoutElement *hoverLayoutElement = layout->getElementAt(relativePos);
2017-04-24 23:00:26 +02:00
if (hoverLayoutElement == nullptr) {
2017-04-24 23:00:26 +02:00
return;
}
2018-01-17 14:14:31 +01:00
auto &link = hoverLayoutElement->getLink();
2018-01-24 22:09:26 +01:00
if (event->button() != Qt::LeftButton ||
!singletons::SettingManager::getInstance().linksDoubleClickOnly) {
2018-01-24 21:44:31 +01:00
this->handleLinkClick(event, link, layout.get());
}
this->linkClicked.invoke(link);
}
void ChannelView::mouseDoubleClickEvent(QMouseEvent *event)
{
if (singletons::SettingManager::getInstance().linksDoubleClickOnly) {
std::shared_ptr<messages::MessageLayout> layout;
QPoint relativePos;
int messageIndex;
if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex)) {
return;
}
// message under cursor is collapsed
2018-01-28 04:07:45 +01:00
if (layout->flags & MessageLayout::Collapsed) {
2018-01-24 21:44:31 +01:00
return;
}
2017-04-12 17:46:44 +02:00
2018-01-24 21:44:31 +01:00
const messages::MessageLayoutElement *hoverLayoutElement =
layout->getElementAt(relativePos);
if (hoverLayoutElement == nullptr) {
return;
}
2017-04-12 17:46:44 +02:00
2018-01-24 21:44:31 +01:00
auto &link = hoverLayoutElement->getLink();
this->handleLinkClick(event, link, layout.get());
}
}
void ChannelView::handleLinkClick(QMouseEvent *event, const messages::Link &link,
messages::MessageLayout *layout)
{
2018-01-28 03:52:52 +01:00
switch (link.type) {
2017-07-31 00:57:42 +02:00
case messages::Link::UserInfo: {
2018-01-28 03:52:52 +01:00
auto user = link.value;
this->userPopupWidget.setName(user);
this->userPopupWidget.moveTo(this, event->screenPos().toPoint());
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: {
2018-01-24 21:16:00 +01:00
if (event->button() == Qt::RightButton) {
static QMenu *menu = nullptr;
static QString url;
if (menu == nullptr) {
menu = new QMenu;
menu->addAction("Open in browser",
[] { QDesktopServices::openUrl(QUrl(url)); });
menu->addAction("Copy to clipboard",
[] { QApplication::clipboard()->setText(url); });
}
2018-01-28 03:52:52 +01:00
url = link.value;
2018-01-24 21:16:00 +01:00
menu->move(QCursor::pos());
menu->show();
} else {
2018-01-28 03:52:52 +01:00
QDesktopServices::openUrl(QUrl(link.value));
2018-01-24 21:16:00 +01:00
}
2017-07-26 09:08:19 +02:00
break;
}
2018-01-17 14:14:31 +01:00
case messages::Link::UserAction: {
2018-01-28 03:52:52 +01:00
QString value = link.value;
2018-01-17 14:14:31 +01:00
value.replace("{user}", layout->getMessage()->loginName);
this->channel->sendMessage(value);
}
2017-04-24 23:00:26 +02:00
}
2017-04-12 17:46:44 +02:00
}
bool ChannelView::tryGetMessageAt(QPoint p, std::shared_ptr<messages::MessageLayout> &_message,
QPoint &relativePos, int &index)
2017-02-17 23:51:35 +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
if (start >= messagesSnapshot.getLength()) {
2017-02-17 23:51:35 +01:00
return false;
}
int y = -(messagesSnapshot[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
2017-02-17 23:51:35 +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