2018-01-11 20:16:25 +01:00
|
|
|
#include "messages/layouts/messagelayout.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
|
|
|
#include "application.hpp"
|
2018-01-11 20:16:25 +01:00
|
|
|
#include "singletons/emotemanager.hpp"
|
|
|
|
#include "singletons/settingsmanager.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 {
|
|
|
|
namespace messages {
|
|
|
|
namespace layouts {
|
|
|
|
|
|
|
|
MessageLayout::MessageLayout(MessagePtr _message)
|
|
|
|
: message(_message)
|
|
|
|
, buffer(nullptr)
|
|
|
|
{
|
2018-04-06 16:37:30 +02:00
|
|
|
util::DebugCount::increase("message layout");
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageLayout::~MessageLayout()
|
|
|
|
{
|
|
|
|
util::DebugCount::decrease("message layout");
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-01-11 20:16:25 +01:00
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-01-11 20:16:25 +01:00
|
|
|
|
|
|
|
bool layoutRequired = false;
|
|
|
|
|
|
|
|
// check if width changed
|
|
|
|
bool widthChanged = width != this->currentLayoutWidth;
|
|
|
|
layoutRequired |= widthChanged;
|
|
|
|
this->currentLayoutWidth = width;
|
|
|
|
|
|
|
|
// check if emotes changed
|
2018-04-27 22:11:19 +02:00
|
|
|
bool imagesChanged = this->emoteGeneration != app->emotes->getGeneration();
|
2018-01-11 20:16:25 +01:00
|
|
|
layoutRequired |= imagesChanged;
|
2018-04-27 22:11:19 +02:00
|
|
|
this->emoteGeneration = app->emotes->getGeneration();
|
2018-01-11 20:16:25 +01:00
|
|
|
|
|
|
|
// check if text changed
|
|
|
|
bool textChanged =
|
|
|
|
this->fontGeneration != singletons::FontManager::getInstance().getGeneration();
|
|
|
|
layoutRequired |= textChanged;
|
|
|
|
this->fontGeneration = singletons::FontManager::getInstance().getGeneration();
|
|
|
|
|
|
|
|
// check if work mask changed
|
2018-04-27 22:11:19 +02:00
|
|
|
bool wordMaskChanged = this->currentWordFlags != flags; // app->settings->getWordTypeMask();
|
2018-01-11 20:16:25 +01:00
|
|
|
layoutRequired |= wordMaskChanged;
|
2018-04-27 22:11:19 +02:00
|
|
|
this->currentWordFlags = flags; // app->settings->getWordTypeMask();
|
2018-01-11 20:16:25 +01:00
|
|
|
|
2018-01-23 21:56:25 +01:00
|
|
|
// check if timestamp format changed
|
2018-04-27 22:11:19 +02:00
|
|
|
bool timestampFormatChanged = this->timestampFormat != app->settings->timestampFormat;
|
2018-01-23 21:56:25 +01:00
|
|
|
|
|
|
|
layoutRequired |= timestampFormatChanged;
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
// check if dpi changed
|
|
|
|
bool scaleChanged = this->scale != scale;
|
|
|
|
layoutRequired |= scaleChanged;
|
|
|
|
this->scale = scale;
|
|
|
|
imagesChanged |= scaleChanged;
|
|
|
|
textChanged |= scaleChanged;
|
|
|
|
|
|
|
|
// update word sizes if needed
|
|
|
|
if (imagesChanged) {
|
2018-01-23 22:00:58 +01:00
|
|
|
// this->container.updateImages();
|
2018-04-18 09:12:29 +02:00
|
|
|
this->flags |= MessageLayout::RequiresBufferUpdate;
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
if (textChanged) {
|
2018-01-23 22:00:58 +01:00
|
|
|
// this->container.updateText();
|
2018-04-18 09:12:29 +02:00
|
|
|
this->flags |= MessageLayout::RequiresBufferUpdate;
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
if (widthChanged || wordMaskChanged) {
|
|
|
|
this->deleteBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// return if no layout is required
|
|
|
|
if (!layoutRequired) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:52:51 +01:00
|
|
|
this->actuallyLayout(width, flags);
|
2018-01-11 20:16:25 +01:00
|
|
|
this->invalidateBuffer();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:52:51 +01:00
|
|
|
void MessageLayout::actuallyLayout(int width, MessageElement::Flags flags)
|
2018-01-11 20:16:25 +01:00
|
|
|
{
|
2018-01-28 16:29:47 +01:00
|
|
|
this->container.begin(width, this->scale, this->message->flags.value);
|
2018-01-11 20:16:25 +01:00
|
|
|
|
|
|
|
for (const std::unique_ptr<MessageElement> &element : this->message->getElements()) {
|
2018-01-17 16:52:51 +01:00
|
|
|
element->addToContainer(this->container, flags);
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this->height != this->container.getHeight()) {
|
|
|
|
this->deleteBuffer();
|
|
|
|
}
|
|
|
|
|
2018-04-01 16:41:32 +02:00
|
|
|
this->container.end();
|
2018-01-11 20:16:25 +01:00
|
|
|
this->height = this->container.getHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Painting
|
2018-01-23 22:48:33 +01:00
|
|
|
void MessageLayout::paint(QPainter &painter, int y, int messageIndex, Selection &selection,
|
|
|
|
bool isLastReadMessage, bool isWindowFocused)
|
2018-01-11 20:16:25 +01:00
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-01-11 20:16:25 +01:00
|
|
|
QPixmap *pixmap = this->buffer.get();
|
|
|
|
|
|
|
|
// create new buffer if required
|
|
|
|
if (!pixmap) {
|
|
|
|
#ifdef Q_OS_MACOS
|
|
|
|
pixmap =
|
2018-02-05 23:56:16 +01:00
|
|
|
new QPixmap((int)(this->container.getWidth() * painter.device()->devicePixelRatioF()),
|
2018-01-11 20:16:25 +01:00
|
|
|
(int)(this->container.getHeight() * painter.device()->devicePixelRatioF()));
|
|
|
|
pixmap->setDevicePixelRatio(painter.device()->devicePixelRatioF());
|
|
|
|
#else
|
2018-01-28 16:29:47 +01:00
|
|
|
pixmap = new QPixmap(this->container.getWidth(), std::max(16, this->container.getHeight()));
|
2018-01-11 20:16:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
this->buffer = std::shared_ptr<QPixmap>(pixmap);
|
|
|
|
this->bufferValid = false;
|
2018-04-06 17:46:12 +02:00
|
|
|
util::DebugCount::increase("message drawing buffers");
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 00:26:04 +01:00
|
|
|
if (!this->bufferValid || !selection.isEmpty()) {
|
2018-01-11 20:16:25 +01:00
|
|
|
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);
|
2018-01-11 20:16:25 +01:00
|
|
|
|
2018-04-01 11:43:26 +02:00
|
|
|
// draw gif emotes
|
|
|
|
this->container.paintAnimatedElements(painter, y);
|
|
|
|
|
2018-01-13 02:03:53 +01:00
|
|
|
// draw disabled
|
2018-03-31 11:32:29 +02:00
|
|
|
if (this->message->flags.HasFlag(Message::Disabled)) {
|
2018-04-27 22:11:19 +02:00
|
|
|
painter.fillRect(0, y, pixmap->width(), pixmap->height(),
|
|
|
|
app->themes->messages.disabled);
|
2018-01-13 02:03:53 +01:00
|
|
|
}
|
|
|
|
|
2018-04-10 03:29:00 +02:00
|
|
|
// draw selection
|
|
|
|
if (!selection.isEmpty()) {
|
|
|
|
this->container.paintSelection(painter, messageIndex, selection, y);
|
|
|
|
}
|
|
|
|
|
2018-01-23 22:48:33 +01:00
|
|
|
// draw last read message line
|
|
|
|
if (isLastReadMessage) {
|
2018-04-27 22:11:19 +02:00
|
|
|
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-01-28 16:29:47 +01:00
|
|
|
painter.fillRect(0, y + this->container.getHeight() - 1, this->container.getWidth(), 1,
|
|
|
|
brush);
|
2018-01-23 22:48:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
this->bufferValid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageLayout::updateBuffer(QPixmap *buffer, int messageIndex, Selection &selection)
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-01-11 20:16:25 +01:00
|
|
|
|
|
|
|
QPainter painter(buffer);
|
|
|
|
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
|
|
|
|
// draw background
|
2018-01-28 03:29:42 +01:00
|
|
|
painter.fillRect(buffer->rect(), this->message->flags & Message::Highlighted
|
2018-04-27 22:11:19 +02:00
|
|
|
? app->themes->messages.backgrounds.highlighted
|
|
|
|
: app->themes->messages.backgrounds.regular);
|
2018-01-11 20:16:25 +01:00
|
|
|
|
|
|
|
// draw message
|
|
|
|
this->container.paintElements(painter);
|
|
|
|
|
2018-04-25 14:49:30 +02:00
|
|
|
#ifdef FOURTF
|
2018-01-11 20:16:25 +01:00
|
|
|
// 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),
|
2018-01-11 20:16:25 +01:00
|
|
|
QString::number(++this->bufferUpdatedCount), option);
|
2018-01-11 20:26:32 +01:00
|
|
|
#endif
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageLayout::invalidateBuffer()
|
|
|
|
{
|
|
|
|
this->bufferValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageLayout::deleteBuffer()
|
|
|
|
{
|
2018-04-06 17:46:12 +02:00
|
|
|
if (this->buffer != nullptr) {
|
|
|
|
util::DebugCount::decrease("message drawing buffers");
|
|
|
|
|
|
|
|
this->buffer = nullptr;
|
|
|
|
}
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 09:12:29 +02:00
|
|
|
void MessageLayout::deleteCache()
|
|
|
|
{
|
|
|
|
this->deleteBuffer();
|
|
|
|
|
|
|
|
#ifdef XD
|
|
|
|
this->container.clear();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
// 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
|
|
|
|
{
|
2018-01-16 02:39:31 +01:00
|
|
|
return this->container.getLastCharacterIndex();
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int MessageLayout::getSelectionIndex(QPoint position)
|
|
|
|
{
|
2018-01-15 04:08:48 +01:00
|
|
|
return this->container.getSelectionIndex(position);
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
2018-01-16 02:39:31 +01:00
|
|
|
|
|
|
|
void MessageLayout::addSelectionText(QString &str, int from, int to)
|
|
|
|
{
|
|
|
|
this->container.addSelectionText(str, from, to);
|
|
|
|
}
|
2018-04-01 16:43:30 +02:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
} // namespace layouts
|
|
|
|
} // namespace messages
|
|
|
|
} // namespace chatterino
|