mirror-chatterino2/src/widgets/chatwidgetview.cpp

405 lines
12 KiB
C++
Raw Normal View History

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"
#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 {
ChatWidgetView::ChatWidgetView(ChatWidget *_chatWidget)
: BaseWidget(_chatWidget)
, chatWidget(_chatWidget)
, scrollBar(this)
, userPopupWidget(_chatWidget->getChannelRef())
2017-01-01 02:30:42 +01:00
{
#ifndef Q_OS_MAC
this->setAttribute(Qt::WA_OpaquePaintEvent);
#endif
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
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-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
{
auto messages = this->chatWidget->getMessagesSnapshot();
2017-01-11 01:08:20 +01:00
if (messages.getLength() == 0) {
this->scrollBar.setVisible(false);
return false;
}
bool showScrollbar = false;
bool redraw = 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
int start = this->scrollBar.getCurrentValue();
int layoutWidth = this->scrollBar.isVisible() ? width() - this->scrollBar.width() : width();
// layout the visible messages in the view
if (messages.getLength() > start) {
int y = -(messages[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
for (int i = start; i < messages.getLength(); ++i) {
2017-04-14 17:47:28 +02:00
auto message = messages[i];
2017-04-24 23:00:26 +02:00
redraw |= message->layout(layoutWidth, true);
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
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-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) {
this->scrollBar.setLargeChange((messages.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(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
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()
{
this->onlyUpdateEmotes = true;
2017-06-06 17:18:23 +02:00
this->update();
2017-04-12 17:46:44 +02:00
}
ScrollBar &ChatWidgetView::getScrollBar()
2017-04-12 17:46:44 +02:00
{
return this->scrollBar;
2017-04-12 17:46:44 +02:00
}
void ChatWidgetView::resizeEvent(QResizeEvent *)
{
this->scrollBar.resize(this->scrollBar.width(), height());
this->scrollBar.move(width() - this->scrollBar.width(), 0);
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 ChatWidgetView::paintEvent(QPaintEvent * /*event*/)
2017-01-05 16:07:20 +01:00
{
QPainter _painter(this);
2017-01-15 16:38:30 +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
#ifndef Q_OS_MAC
if (this->onlyUpdateEmotes) {
this->onlyUpdateEmotes = false;
2017-02-07 00:03:15 +01:00
for (const GifEmoteData &item : this->gifEmotes) {
_painter.fillRect(item.rect, this->colorScheme.ChatBackground);
2017-02-07 00:03:15 +01:00
_painter.drawPixmap(item.rect, *item.image->getPixmap());
}
return;
}
#endif
2017-02-07 00:03:15 +01:00
// update all messages
this->gifEmotes.clear();
2017-02-07 00:03:15 +01:00
_painter.fillRect(rect(), this->colorScheme.ChatBackground);
2017-02-07 00:03:15 +01:00
auto messages = this->chatWidget->getMessagesSnapshot();
2017-01-11 01:08:20 +01:00
int start = this->scrollBar.getCurrentValue();
2017-01-11 01:08:20 +01:00
if (start >= messages.getLength()) {
2017-01-18 04:33:30 +01:00
return;
}
int y = -(messages[start].get()->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
2017-01-18 04:33:30 +01:00
for (int i = start; i < messages.getLength(); ++i) {
messages::MessageRef *messageRef = messages[i].get();
2017-01-18 04:33:30 +01:00
std::shared_ptr<QPixmap> bufferPtr = messageRef->buffer;
QPixmap *buffer = bufferPtr.get();
2017-01-13 18:59:11 +01:00
bool updateBuffer = messageRef->updateBuffer;
2017-01-11 01:08:20 +01:00
if (buffer == nullptr) {
2017-04-14 17:47:28 +02:00
buffer = new QPixmap(width(), messageRef->getHeight());
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
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()) {
// image
if (wordPart.getWord().isImage()) {
2017-04-12 17:46:44 +02:00
messages::LazyLoadedImage &lli = wordPart.getWord().getImage();
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-01-11 01:08:20 +01:00
}
// text
else {
QColor color = wordPart.getWord().getColor();
2017-01-18 01:04:54 +01:00
this->colorScheme.normalizeColor(color);
2017-01-26 07:10:46 +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-01-11 01:08:20 +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()) {
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
gifEmoteData.rect = rect;
2017-02-07 00:03:15 +01:00
this->gifEmotes.push_back(gifEmoteData);
2017-02-07 00:03:15 +01:00
}
}
}
messageRef->buffer = bufferPtr;
_painter.drawPixmap(0, y, *buffer);
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
for (GifEmoteData &item : this->gifEmotes) {
_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
{
if (this->scrollBar.isVisible()) {
2017-04-14 17:47:28 +02:00
auto mouseMultiplier = SettingsManager::getInstance().mouseScrollMultiplier.get();
this->scrollBar.setDesiredValue(
this->scrollBar.getDesiredValue() - event->delta() / 10.0 * mouseMultiplier, true);
}
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)
{
this->isMouseDown = true;
this->lastPressPosition = event->screenPos();
this->chatWidget->giveFocus(Qt::MouseFocusReason);
2017-04-12 17:46:44 +02:00
}
void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event)
{
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;
}
this->isMouseDown = 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
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
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: {
auto user = link.getValue();
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
{
auto messages = this->chatWidget->getMessagesSnapshot();
2017-02-17 23:51:35 +01:00
int start = this->scrollBar.getCurrentValue();
2017-02-17 23:51:35 +01:00
if (start >= messages.getLength()) {
2017-02-17 23:51:35 +01:00
return false;
}
int y = -(messages[start]->getHeight() * (fmod(this->scrollBar.getCurrentValue(), 1)));
2017-02-17 23:51:35 +01: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