2017-06-11 09:31:45 +02:00
|
|
|
#include "widgets/chatwidgetview.hpp"
|
|
|
|
#include "channelmanager.hpp"
|
|
|
|
#include "colorscheme.hpp"
|
|
|
|
#include "messages/message.hpp"
|
|
|
|
#include "messages/wordpart.hpp"
|
|
|
|
#include "settingsmanager.hpp"
|
2017-04-19 15:25:05 +02:00
|
|
|
#include "ui_accountpopupform.h"
|
2017-06-11 09:31:45 +02:00
|
|
|
#include "util/distancebetweenpoints.hpp"
|
|
|
|
#include "widgets/chatwidget.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-02-07 00:03:15 +01:00
|
|
|
#include <chrono>
|
2017-01-18 01:04:54 +01:00
|
|
|
#include <functional>
|
2017-01-01 02:30:42 +01:00
|
|
|
|
2017-01-18 21:30:23 +01:00
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
ChatWidgetView::ChatWidgetView(ChatWidget *_chatWidget)
|
2017-06-26 16:41:20 +02:00
|
|
|
: BaseWidget(_chatWidget)
|
2017-06-11 11:36:42 +02:00
|
|
|
, chatWidget(_chatWidget)
|
|
|
|
, scrollBar(this)
|
|
|
|
, userPopupWidget(_chatWidget->getChannelRef())
|
2017-01-01 02:30:42 +01:00
|
|
|
{
|
2017-08-17 17:02:10 +02:00
|
|
|
#ifndef Q_OS_MAC
|
2017-06-11 11:36:42 +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-04-12 17:46:44 +02:00
|
|
|
QObject::connect(&SettingsManager::getInstance(), &SettingsManager::wordTypeMaskChanged, this,
|
|
|
|
&ChatWidgetView::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
|
|
|
|
this->update();
|
2017-08-05 22:26:37 +02:00
|
|
|
|
2017-08-12 12:09:26 +02:00
|
|
|
this->layoutMessages();
|
2017-06-06 17:18:23 +02:00
|
|
|
});
|
2017-01-22 12:46:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ChatWidgetView::~ChatWidgetView()
|
|
|
|
{
|
2017-04-12 17:46:44 +02:00
|
|
|
QObject::disconnect(&SettingsManager::getInstance(), &SettingsManager::wordTypeMaskChanged,
|
|
|
|
this, &ChatWidgetView::wordTypeMaskChanged);
|
2017-01-01 02:30:42 +01:00
|
|
|
}
|
2017-01-03 21:19:33 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
bool ChatWidgetView::layoutMessages()
|
2017-01-03 21:19:33 +01:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
auto messages = this->chatWidget->getMessagesSnapshot();
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
if (messages.getLength() == 0) {
|
2017-06-11 11:36:42 +02:00
|
|
|
this->scrollBar.setVisible(false);
|
2017-02-03 19:31:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
bool showScrollbar = false;
|
|
|
|
bool redraw = 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-06-11 11:36:42 +02:00
|
|
|
int start = this->scrollBar.getCurrentValue();
|
|
|
|
int layoutWidth = this->scrollBar.isVisible() ? width() - this->scrollBar.width() : 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-06-26 16:41:20 +02:00
|
|
|
if (messages.getLength() > start) {
|
2017-06-11 11:36:42 +02:00
|
|
|
int y = -(messages[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
|
2017-02-06 11:38:26 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
for (int i = start; i < messages.getLength(); ++i) {
|
2017-04-14 17:47:28 +02:00
|
|
|
auto message = messages[i];
|
2017-01-16 03:15:07 +01:00
|
|
|
|
2017-04-24 23:00:26 +02:00
|
|
|
redraw |= message->layout(layoutWidth, true);
|
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
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
for (std::size_t i = messages.getLength() - 1; i > 0; i--) {
|
2017-01-26 07:10:46 +01:00
|
|
|
auto *message = messages[i].get();
|
2017-01-16 03:15:07 +01:00
|
|
|
|
2017-04-24 23:00:26 +02:00
|
|
|
message->layout(layoutWidth, true);
|
2017-01-26 04:26:40 +01:00
|
|
|
|
2017-01-26 07:10:46 +01:00
|
|
|
h -= message->getHeight();
|
|
|
|
|
|
|
|
if (h < 0) {
|
2017-06-26 16:41:20 +02:00
|
|
|
this->scrollBar.setLargeChange((messages.getLength() - i) +
|
2017-06-11 11:36:42 +02:00
|
|
|
(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
|
|
|
}
|
|
|
|
|
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-06-26 16:41:20 +02:00
|
|
|
this->scrollBar.setMaximum(messages.getLength());
|
2017-01-26 07:10:46 +01:00
|
|
|
|
2017-06-06 21:18:05 +02:00
|
|
|
if (this->showingLatestMessages && showScrollbar) {
|
2017-06-06 17:18:23 +02: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-06-11 11:36:42 +02:00
|
|
|
this->scrollBar.scrollToBottom();
|
2017-06-06 17:18:23 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 07:10:46 +01:00
|
|
|
return redraw;
|
2017-01-26 04:26:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void ChatWidgetView::updateGifEmotes()
|
2017-01-16 03:15:07 +01:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
this->onlyUpdateEmotes = true;
|
2017-06-06 17:18:23 +02:00
|
|
|
this->update();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
ScrollBar &ChatWidgetView::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
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWidgetView::resizeEvent(QResizeEvent *)
|
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
this->scrollBar.resize(this->scrollBar.width(), height());
|
|
|
|
this->scrollBar.move(width() - this->scrollBar.width(), 0);
|
2017-01-16 03:15:07 +01:00
|
|
|
|
|
|
|
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-06-11 11:36:42 +02:00
|
|
|
void ChatWidgetView::paintEvent(QPaintEvent * /*event*/)
|
2017-01-05 16:07:20 +01:00
|
|
|
{
|
2017-02-03 19:31:51 +01:00
|
|
|
QPainter _painter(this);
|
2017-01-15 16:38:30 +01:00
|
|
|
|
2017-02-03 19:31:51 +01:00
|
|
|
_painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
2017-01-18 01:04:54 +01:00
|
|
|
|
2017-02-07 00:03:15 +01:00
|
|
|
// only update gif emotes
|
2017-08-17 17:02:10 +02:00
|
|
|
#ifndef Q_OS_MAC
|
2017-06-11 11:36:42 +02:00
|
|
|
if (this->onlyUpdateEmotes) {
|
|
|
|
this->onlyUpdateEmotes = false;
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
for (const GifEmoteData &item : this->gifEmotes) {
|
2017-06-26 16:41:20 +02:00
|
|
|
_painter.fillRect(item.rect, this->colorScheme.ChatBackground);
|
2017-02-07 00:03:15 +01:00
|
|
|
|
|
|
|
_painter.drawPixmap(item.rect, *item.image->getPixmap());
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
_painter.fillRect(rect(), this->colorScheme.ChatBackground);
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-01-18 01:04:54 +01:00
|
|
|
// code for tesing colors
|
|
|
|
/*
|
2017-02-03 19:31:51 +01:00
|
|
|
QColor color;
|
2017-01-18 01:04:54 +01:00
|
|
|
static ConcurrentMap<qreal, QImage *> imgCache;
|
|
|
|
|
|
|
|
std::function<QImage *(qreal)> getImg = [&scheme](qreal light) {
|
|
|
|
return imgCache.getOrAdd(light, [&scheme, &light] {
|
|
|
|
QImage *img = new QImage(150, 50, QImage::Format_RGB32);
|
|
|
|
|
|
|
|
QColor color;
|
|
|
|
|
|
|
|
for (int j = 0; j < 50; j++) {
|
|
|
|
for (qreal i = 0; i < 150; i++) {
|
|
|
|
color = QColor::fromHslF(i / 150.0, light, j / 50.0);
|
|
|
|
|
|
|
|
scheme.normalizeColor(color);
|
|
|
|
|
|
|
|
img->setPixelColor(i, j, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return img;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
for (qreal k = 0; k < 4.8; k++) {
|
|
|
|
auto img = getImg(k / 5);
|
|
|
|
|
|
|
|
painter.drawImage(QRect(k * 150, 0, 150, 150), *img);
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.fillRect(QRect(0, 9, 500, 2), QColor(0, 0, 0));*/
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
auto messages = this->chatWidget->getMessagesSnapshot();
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
int start = this->scrollBar.getCurrentValue();
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
if (start >= messages.getLength()) {
|
2017-01-18 04:33:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
int y = -(messages[start].get()->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
|
2017-01-18 04:33:30 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
for (int i = start; i < messages.getLength(); ++i) {
|
2017-02-02 22:15:09 +01:00
|
|
|
messages::MessageRef *messageRef = messages[i].get();
|
2017-01-18 04:33:30 +01:00
|
|
|
|
2017-02-03 19:31:51 +01:00
|
|
|
std::shared_ptr<QPixmap> bufferPtr = messageRef->buffer;
|
|
|
|
QPixmap *buffer = bufferPtr.get();
|
2017-01-13 18:59:11 +01:00
|
|
|
|
2017-02-03 19:31:51 +01:00
|
|
|
bool updateBuffer = messageRef->updateBuffer;
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-02-03 19:31:51 +01:00
|
|
|
if (buffer == nullptr) {
|
2017-04-14 17:47:28 +02:00
|
|
|
buffer = new QPixmap(width(), messageRef->getHeight());
|
2017-02-03 19:31:51 +01:00
|
|
|
bufferPtr = std::shared_ptr<QPixmap>(buffer);
|
|
|
|
updateBuffer = true;
|
|
|
|
}
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-02-07 00:03:15 +01:00
|
|
|
// update messages that have been changed
|
2017-02-03 19:31:51 +01:00
|
|
|
if (updateBuffer) {
|
|
|
|
QPainter painter(buffer);
|
2017-07-31 00:57:42 +02:00
|
|
|
painter.fillRect(buffer->rect(), (messageRef->getMessage()->getCanHighlightTab())
|
|
|
|
? this->colorScheme.ChatBackgroundHighlighted
|
|
|
|
: this->colorScheme.ChatBackground);
|
2017-04-12 17:46:44 +02:00
|
|
|
for (messages::WordPart const &wordPart : messageRef->getWordParts()) {
|
2017-02-03 19:31:51 +01:00
|
|
|
// image
|
|
|
|
if (wordPart.getWord().isImage()) {
|
2017-04-12 17:46:44 +02:00
|
|
|
messages::LazyLoadedImage &lli = wordPart.getWord().getImage();
|
2017-02-03 19:31:51 +01:00
|
|
|
|
|
|
|
const QPixmap *image = lli.getPixmap();
|
|
|
|
|
2017-04-20 21:34:51 +02:00
|
|
|
if (image != nullptr) {
|
2017-04-12 17:46:44 +02:00
|
|
|
painter.drawPixmap(QRect(wordPart.getX(), wordPart.getY(),
|
|
|
|
wordPart.getWidth(), wordPart.getHeight()),
|
|
|
|
*image);
|
2017-02-03 19:31:51 +01:00
|
|
|
}
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
2017-02-03 19:31:51 +01:00
|
|
|
// text
|
|
|
|
else {
|
|
|
|
QColor color = wordPart.getWord().getColor();
|
2017-01-18 01:04:54 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
this->colorScheme.normalizeColor(color);
|
2017-01-26 07:10:46 +01:00
|
|
|
|
2017-02-03 19:31:51 +01:00
|
|
|
painter.setPen(color);
|
|
|
|
painter.setFont(wordPart.getWord().getFont());
|
2017-01-11 18:52:09 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
painter.drawText(QRectF(wordPart.getX(), wordPart.getY(), 10000, 10000),
|
|
|
|
wordPart.getText(), QTextOption(Qt::AlignLeft | Qt::AlignTop));
|
2017-02-03 19:31:51 +01:00
|
|
|
}
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
2017-02-03 19:31:51 +01:00
|
|
|
|
|
|
|
messageRef->updateBuffer = false;
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
2017-01-07 20:43:55 +01:00
|
|
|
|
2017-02-07 00:03:15 +01:00
|
|
|
// get gif emotes
|
|
|
|
for (messages::WordPart const &wordPart : messageRef->getWordParts()) {
|
|
|
|
if (wordPart.getWord().isImage()) {
|
|
|
|
messages::LazyLoadedImage &lli = wordPart.getWord().getImage();
|
|
|
|
|
|
|
|
if (lli.getAnimated()) {
|
2017-06-26 16:41:20 +02:00
|
|
|
GifEmoteData gifEmoteData;
|
|
|
|
gifEmoteData.image = &lli;
|
2017-04-12 17:46:44 +02:00
|
|
|
QRect rect(wordPart.getX(), wordPart.getY() + y, wordPart.getWidth(),
|
|
|
|
wordPart.getHeight());
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
gifEmoteData.rect = rect;
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
this->gifEmotes.push_back(gifEmoteData);
|
2017-02-07 00:03:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 19:31:51 +01:00
|
|
|
messageRef->buffer = bufferPtr;
|
|
|
|
|
|
|
|
_painter.drawPixmap(0, y, *buffer);
|
|
|
|
|
2017-02-02 22:15:09 +01:00
|
|
|
y += messageRef->getHeight();
|
2017-01-20 06:10:28 +01:00
|
|
|
|
|
|
|
if (y > height()) {
|
|
|
|
break;
|
|
|
|
}
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
2017-02-07 00:03:15 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
for (GifEmoteData &item : this->gifEmotes) {
|
2017-06-26 16:41:20 +02:00
|
|
|
_painter.fillRect(item.rect, this->colorScheme.ChatBackground);
|
2017-02-07 00:03:15 +01:00
|
|
|
|
|
|
|
_painter.drawPixmap(item.rect, *item.image->getPixmap());
|
|
|
|
}
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
2017-01-26 04:26:40 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void ChatWidgetView::wheelEvent(QWheelEvent *event)
|
2017-01-26 04:26:40 +01:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
if (this->scrollBar.isVisible()) {
|
2017-04-14 17:47:28 +02:00
|
|
|
auto mouseMultiplier = SettingsManager::getInstance().mouseScrollMultiplier.get();
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->scrollBar.setDesiredValue(
|
|
|
|
this->scrollBar.getDesiredValue() - event->delta() / 10.0 * mouseMultiplier, 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
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void ChatWidgetView::mouseMoveEvent(QMouseEvent *event)
|
2017-02-17 23:51:35 +01:00
|
|
|
{
|
|
|
|
std::shared_ptr<messages::MessageRef> message;
|
|
|
|
QPoint relativePos;
|
|
|
|
|
|
|
|
if (!tryGetMessageAt(event->pos(), message, relativePos)) {
|
2017-04-14 17:47:28 +02:00
|
|
|
setCursor(Qt::ArrowCursor);
|
2017-02-17 23:51:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-29 21:02:10 +02:00
|
|
|
// int index = message->getSelectionIndex(relativePos);
|
|
|
|
// qDebug() << index;
|
2017-04-24 23:00:26 +02:00
|
|
|
|
2017-02-17 23:51:35 +01:00
|
|
|
messages::Word hoverWord;
|
|
|
|
if (!message->tryGetWordPart(relativePos, hoverWord)) {
|
2017-04-14 17:47:28 +02:00
|
|
|
setCursor(Qt::ArrowCursor);
|
2017-02-17 23:51:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:00:26 +02:00
|
|
|
if (hoverWord.getLink().isValid()) {
|
2017-04-14 17:47:28 +02:00
|
|
|
setCursor(Qt::PointingHandCursor);
|
2017-02-17 23:51:35 +01:00
|
|
|
} else {
|
2017-04-14 17:47:28 +02:00
|
|
|
setCursor(Qt::ArrowCursor);
|
2017-02-17 23:51:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void ChatWidgetView::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
this->isMouseDown = true;
|
|
|
|
this->lastPressPosition = event->screenPos();
|
2017-06-11 20:53:43 +02:00
|
|
|
|
2017-08-12 15:41:14 +02:00
|
|
|
this->chatWidget->giveFocus(Qt::MouseFocusReason);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
this->isMouseDown = 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
|
|
|
|
|
|
|
qDebug() << "Distance: " << distance;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!tryGetMessageAt(event->pos(), message, relativePos)) {
|
|
|
|
// 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-04-24 23:00:26 +02:00
|
|
|
messages::Word hoverWord;
|
|
|
|
|
|
|
|
if (!message->tryGetWordPart(relativePos, hoverWord)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatWidgetView::tryGetMessageAt(QPoint p, std::shared_ptr<messages::MessageRef> &_message,
|
|
|
|
QPoint &relativePos)
|
2017-02-17 23:51:35 +01:00
|
|
|
{
|
2017-06-11 11:36:42 +02:00
|
|
|
auto messages = this->chatWidget->getMessagesSnapshot();
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
int start = this->scrollBar.getCurrentValue();
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
if (start >= messages.getLength()) {
|
2017-02-17 23:51:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
int y = -(messages[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
for (int i = start; i < messages.getLength(); ++i) {
|
2017-02-17 23:51:35 +01:00
|
|
|
auto message = messages[i];
|
|
|
|
|
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;
|
|
|
|
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
|