mirror-chatterino2/src/messages/layouts/MessageLayout.cpp

276 lines
7.8 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "messages/layouts/MessageLayout.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
2018-06-26 17:20:03 +02:00
#include "debug/Benchmark.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Emotes.hpp"
#include "singletons/Settings.hpp"
2018-06-26 14:09:39 +02:00
#include "singletons/WindowManager.hpp"
#include <QApplication>
#include <QDebug>
#include <QPainter>
#include <QThread>
#include <QtGlobal>
#define MARGIN_LEFT (int)(8 * this->scale)
#define MARGIN_RIGHT (int)(8 * this->scale)
#define MARGIN_TOP (int)(4 * this->scale)
#define MARGIN_BOTTOM (int)(4 * this->scale)
#define COMPACT_EMOTES_OFFSET 6
namespace chatterino {
2018-05-25 13:02:14 +02:00
MessageLayout::MessageLayout(MessagePtr message)
: message_(message)
, buffer_(nullptr)
{
2018-06-26 17:06:17 +02:00
DebugCount::increase("message layout");
2018-04-06 16:37:30 +02:00
}
MessageLayout::~MessageLayout()
{
2018-06-26 17:06:17 +02:00
DebugCount::decrease("message layout");
}
Message *MessageLayout::getMessage()
{
return this->message_.get();
}
// Height
int MessageLayout::getHeight() const
{
return container_.getHeight();
}
// Layout
// return true if redraw is required
2018-01-17 16:52:51 +01:00
bool MessageLayout::layout(int width, float scale, MessageElement::Flags flags)
{
// BenchmarkGuard benchmark("MessageLayout::layout()");
auto app = getApp();
bool layoutRequired = false;
// check if width changed
bool widthChanged = width != this->currentLayoutWidth_;
layoutRequired |= widthChanged;
this->currentLayoutWidth_ = width;
// check if layout state changed
if (this->layoutState_ != app->windows->getGeneration()) {
layoutRequired = true;
this->flags |= RequiresBufferUpdate;
this->layoutState_ = app->windows->getGeneration();
}
// check if work mask changed
layoutRequired |= this->currentWordFlags_ != flags;
this->currentWordFlags_ = flags; // app->settings->getWordTypeMask();
2018-05-25 12:45:18 +02:00
// check if layout was requested manually
layoutRequired |= bool(this->flags & RequiresLayout);
this->flags &= decltype(RequiresLayout)(~RequiresLayout);
2018-05-25 12:45:18 +02:00
// check if dpi changed
layoutRequired |= this->scale_ != scale;
this->scale_ = scale;
2018-05-24 23:12:50 +02:00
if (!layoutRequired) {
return false;
}
int oldHeight = this->container_.getHeight();
2018-01-17 16:52:51 +01:00
this->actuallyLayout(width, flags);
if (widthChanged || this->container_.getHeight() != oldHeight) {
this->deleteBuffer();
}
this->invalidateBuffer();
return true;
}
void MessageLayout::actuallyLayout(int width, MessageElement::Flags _flags)
{
auto messageFlags = this->message_->flags.value;
if (this->flags & MessageLayout::Expanded ||
(_flags & MessageElement::ModeratorTools &&
!(this->message_->flags & Message::MessageFlags::Disabled))) {
2018-05-25 12:45:18 +02:00
messageFlags = Message::MessageFlags(messageFlags & ~Message::MessageFlags::Collapsed);
}
this->container_.begin(width, this->scale_, messageFlags);
for (const std::unique_ptr<MessageElement> &element : this->message_->getElements()) {
element->addToContainer(this->container_, _flags);
}
if (this->height_ != this->container_.getHeight()) {
this->deleteBuffer();
}
this->container_.end();
this->height_ = this->container_.getHeight();
// collapsed state
this->flags &= ~Flags::Collapsed;
if (this->container_.isCollapsed()) {
this->flags |= Flags::Collapsed;
}
}
// Painting
void MessageLayout::paint(QPainter &painter, int width, int y, int messageIndex,
Selection &selection, bool isLastReadMessage, bool isWindowFocused)
{
auto app = getApp();
QPixmap *pixmap = this->buffer_.get();
// create new buffer if required
if (!pixmap) {
#ifdef Q_OS_MACOS
pixmap = new QPixmap(int(width * painter.device()->devicePixelRatioF()),
int(container_.getHeight() * painter.device()->devicePixelRatioF()));
pixmap->setDevicePixelRatio(painter.device()->devicePixelRatioF());
#else
pixmap = new QPixmap(width, std::max(16, this->container_.getHeight()));
#endif
this->buffer_ = std::shared_ptr<QPixmap>(pixmap);
this->bufferValid_ = false;
2018-06-26 17:06:17 +02:00
DebugCount::increase("message drawing buffers");
}
if (!this->bufferValid_ || !selection.isEmpty()) {
this->updateBuffer(pixmap, messageIndex, selection);
}
// draw on buffer
2018-01-23 22:51:15 +01:00
painter.drawPixmap(0, y, *pixmap);
// painter.drawPixmap(0, y, this->container.width, this->container.getHeight(), *pixmap);
// draw gif emotes
this->container_.paintAnimatedElements(painter, y);
// draw disabled
if (this->message_->flags.HasFlag(Message::Disabled)) {
painter.fillRect(0, y, pixmap->width(), pixmap->height(), app->themes->messages.disabled);
}
2018-04-10 03:29:00 +02:00
// draw selection
if (!selection.isEmpty()) {
this->container_.paintSelection(painter, messageIndex, selection, y);
2018-04-10 03:29:00 +02:00
}
// draw message seperation line
2018-06-24 18:32:00 +02:00
if (app->settings->separateMessages.getValue()) {
painter.fillRect(0, y, this->container_.getWidth(), 1,
app->themes->splits.messageSeperator);
}
2018-01-23 22:48:33 +01:00
// draw last read message line
if (isLastReadMessage) {
QColor color = isWindowFocused ? app->themes->tabs.selected.backgrounds.regular.color()
: app->themes->tabs.selected.backgrounds.unfocused.color();
2018-01-23 22:48:33 +01:00
2018-03-24 16:55:28 +01:00
QBrush brush(color, Qt::VerPattern);
2018-01-23 22:48:33 +01:00
2018-06-05 00:14:20 +02:00
painter.fillRect(0, y + this->container_.getHeight() - 1, pixmap->width(), 1, brush);
2018-01-23 22:48:33 +01:00
}
this->bufferValid_ = true;
}
2018-05-25 12:45:18 +02:00
void MessageLayout::updateBuffer(QPixmap *buffer, int /*messageIndex*/, Selection & /*selection*/)
{
auto app = getApp();
QPainter painter(buffer);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
// draw background
QColor backgroundColor;
if (this->message_->flags & Message::Highlighted) {
backgroundColor = app->themes->messages.backgrounds.highlighted;
2018-06-04 12:23:23 +02:00
} else if (this->message_->flags & Message::Subscription) {
backgroundColor = app->themes->messages.backgrounds.subscription;
} else if (app->settings->alternateMessageBackground.getValue() &&
this->flags & MessageLayout::AlternateBackground) {
backgroundColor = app->themes->messages.backgrounds.alternate;
} else {
backgroundColor = app->themes->messages.backgrounds.regular;
}
painter.fillRect(buffer->rect(), backgroundColor);
// draw message
this->container_.paintElements(painter);
#ifdef FOURTF
// debug
painter.setPen(QColor(255, 0, 0));
painter.drawRect(buffer->rect().x(), buffer->rect().y(), buffer->rect().width() - 1,
buffer->rect().height() - 1);
QTextOption option;
option.setAlignment(Qt::AlignRight | Qt::AlignTop);
2018-04-10 02:42:41 +02:00
painter.drawText(QRectF(1, 1, this->container.getWidth() - 3, 1000),
QString::number(++this->bufferUpdatedCount), option);
2018-01-11 20:26:32 +01:00
#endif
}
void MessageLayout::invalidateBuffer()
{
this->bufferValid_ = false;
}
void MessageLayout::deleteBuffer()
{
if (this->buffer_ != nullptr) {
2018-06-26 17:06:17 +02:00
DebugCount::decrease("message drawing buffers");
this->buffer_ = nullptr;
}
}
2018-04-18 09:12:29 +02:00
void MessageLayout::deleteCache()
{
this->deleteBuffer();
#ifdef XD
this->container_.clear();
2018-04-18 09:12:29 +02:00
#endif
}
// Elements
// assert(QThread::currentThread() == QApplication::instance()->thread());
// returns nullptr if none was found
// fourtf: this should return a MessageLayoutItem
const MessageLayoutElement *MessageLayout::getElementAt(QPoint point)
{
// go through all words and return the first one that contains the point.
return this->container_.getElementAt(point);
}
int MessageLayout::getLastCharacterIndex() const
{
return this->container_.getLastCharacterIndex();
}
int MessageLayout::getSelectionIndex(QPoint position)
{
return this->container_.getSelectionIndex(position);
}
2018-01-16 02:39:31 +01:00
void MessageLayout::addSelectionText(QString &str, int from, int to)
{
this->container_.addSelectionText(str, from, to);
2018-01-16 02:39:31 +01:00
}
2018-04-01 16:43:30 +02:00
} // namespace chatterino