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

313 lines
7.2 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "messages/layouts/MessageLayoutElement.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
2018-06-26 14:09:39 +02:00
#include "messages/MessageElement.hpp"
#include "singletons/Theme.hpp"
2018-06-26 14:09:39 +02:00
#include "util/DebugCount.hpp"
2018-01-16 00:26:04 +01:00
#include <QDebug>
#include <QPainter>
namespace chatterino {
const QRect &MessageLayoutElement::getRect() const
{
2018-07-06 19:23:47 +02:00
return this->rect_;
}
2018-08-06 21:17:03 +02:00
MessageLayoutElement::MessageLayoutElement(MessageElement &creator,
const QSize &size)
2018-07-06 19:23:47 +02:00
: creator_(creator)
{
2018-07-06 19:23:47 +02:00
this->rect_.setSize(size);
2018-06-26 17:06:17 +02:00
DebugCount::increase("message layout elements");
2018-04-06 16:37:30 +02:00
}
MessageLayoutElement::~MessageLayoutElement()
{
2018-06-26 17:06:17 +02:00
DebugCount::decrease("message layout elements");
}
MessageElement &MessageLayoutElement::getCreator() const
{
2018-07-06 19:23:47 +02:00
return this->creator_;
}
void MessageLayoutElement::setPosition(QPoint point)
{
2018-07-06 19:23:47 +02:00
this->rect_.moveTopLeft(point);
}
bool MessageLayoutElement::hasTrailingSpace() const
{
return this->trailingSpace;
}
MessageLayoutElement *MessageLayoutElement::setTrailingSpace(bool value)
{
this->trailingSpace = value;
return this;
}
2018-01-17 14:14:31 +01:00
MessageLayoutElement *MessageLayoutElement::setLink(const Link &_link)
{
2018-07-06 19:23:47 +02:00
this->link_ = _link;
2018-01-17 14:14:31 +01:00
return this;
}
MessageLayoutElement *MessageLayoutElement::setText(const QString &_text)
{
this->text_ = _text;
return this;
}
2018-01-17 14:14:31 +01:00
const Link &MessageLayoutElement::getLink() const
{
2018-07-06 19:23:47 +02:00
return this->link_;
2018-01-17 14:14:31 +01:00
}
const QString &MessageLayoutElement::getText() const
{
return this->text_;
}
//
// IMAGE
//
2018-08-06 21:17:03 +02:00
ImageLayoutElement::ImageLayoutElement(MessageElement &creator, ImagePtr image,
const QSize &size)
2018-08-02 14:23:27 +02:00
: MessageLayoutElement(creator, size)
, image_(image)
{
2018-08-02 14:23:27 +02:00
this->trailingSpace = creator.hasTrailingSpace();
}
2018-08-06 21:17:03 +02:00
void ImageLayoutElement::addCopyTextToString(QString &str, int from,
int to) const
{
2018-09-30 18:55:41 +02:00
const auto *emoteElement =
dynamic_cast<EmoteElement *>(&this->getCreator());
if (emoteElement) {
str += emoteElement->getEmote()->getCopyString();
if (this->hasTrailingSpace()) {
str += " ";
}
2018-01-16 02:39:31 +01:00
}
}
int ImageLayoutElement::getSelectionIndexCount() const
{
return this->trailingSpace ? 2 : 1;
}
void ImageLayoutElement::paint(QPainter &painter)
{
2018-08-02 14:23:27 +02:00
if (this->image_ == nullptr) {
2018-01-19 22:45:33 +01:00
return;
}
2018-08-06 18:25:47 +02:00
auto pixmap = this->image_->pixmap();
if (pixmap && !this->image_->animated()) {
// fourtf: make it use qreal values
painter.drawPixmap(QRectF(this->getRect()), *pixmap, QRectF());
}
}
2018-01-13 02:13:59 +01:00
void ImageLayoutElement::paintAnimated(QPainter &painter, int yOffset)
{
2018-08-02 14:23:27 +02:00
if (this->image_ == nullptr) {
2018-01-19 22:45:33 +01:00
return;
}
2018-08-06 18:25:47 +02:00
if (this->image_->animated()) {
if (auto pixmap = this->image_->pixmap()) {
2018-08-02 14:23:27 +02:00
auto rect = this->getRect();
rect.moveTop(rect.y() + yOffset);
painter.drawPixmap(QRectF(rect), *pixmap, QRectF());
}
}
}
int ImageLayoutElement::getMouseOverIndex(const QPoint &abs) const
{
return 0;
}
2018-01-16 00:26:04 +01:00
int ImageLayoutElement::getXFromIndex(int index)
{
if (index <= 0) {
return this->getRect().left();
} else if (index == 1) {
// fourtf: remove space width
return this->getRect().right();
} else {
return this->getRect().right();
}
}
//
// TEXT
//
2018-08-06 21:17:03 +02:00
TextLayoutElement::TextLayoutElement(MessageElement &_creator, QString &_text,
const QSize &_size, QColor _color,
FontStyle _style, float _scale)
: MessageLayoutElement(_creator, _size)
, text(_text)
, color(_color)
, style(_style)
, scale(_scale)
{
}
2018-08-06 21:17:03 +02:00
void TextLayoutElement::addCopyTextToString(QString &str, int from,
int to) const
{
2018-01-16 02:39:31 +01:00
str += this->text.mid(from, to - from);
if (this->hasTrailingSpace()) {
str += " ";
}
}
int TextLayoutElement::getSelectionIndexCount() const
{
return this->text.length() + (this->trailingSpace ? 1 : 0);
}
void TextLayoutElement::paint(QPainter &painter)
{
auto app = getApp();
painter.setPen(this->color);
painter.setFont(app->fonts->getFont(this->style, this->scale));
2018-08-06 21:17:03 +02:00
painter.drawText(
QRectF(this->getRect().x(), this->getRect().y(), 10000, 10000),
this->text, QTextOption(Qt::AlignLeft | Qt::AlignTop));
}
2018-01-13 02:13:59 +01:00
void TextLayoutElement::paintAnimated(QPainter &, int)
{
}
int TextLayoutElement::getMouseOverIndex(const QPoint &abs) const
{
2018-01-16 00:26:04 +01:00
if (abs.x() < this->getRect().left()) {
return 0;
}
auto app = getApp();
2018-05-23 04:22:17 +02:00
QFontMetrics metrics = app->fonts->getFontMetrics(this->style, this->scale);
2018-01-16 00:26:04 +01:00
int x = this->getRect().left();
for (int i = 0; i < this->text.size(); i++) {
int w = metrics.width(this->text[i]);
if (x + w > abs.x()) {
return i;
}
x += w;
}
return this->getSelectionIndexCount();
}
int TextLayoutElement::getXFromIndex(int index)
{
auto app = getApp();
2018-05-23 04:22:17 +02:00
QFontMetrics metrics = app->fonts->getFontMetrics(this->style, this->scale);
2018-01-16 00:26:04 +01:00
if (index <= 0) {
return this->getRect().left();
} else if (index < this->text.size()) {
int x = 0;
for (int i = 0; i < index; i++) {
x += metrics.width(this->text[i]);
}
return x + this->getRect().left();
} else {
return this->getRect().right();
}
}
2018-01-17 14:14:31 +01:00
// TEXT ICON
2018-08-06 21:17:03 +02:00
TextIconLayoutElement::TextIconLayoutElement(MessageElement &creator,
const QString &_line1,
const QString &_line2,
float _scale, const QSize &size)
2018-01-17 14:14:31 +01:00
: MessageLayoutElement(creator, size)
, scale(_scale)
, line1(_line1)
, line2(_line2)
{
}
2018-08-06 21:17:03 +02:00
void TextIconLayoutElement::addCopyTextToString(QString &str, int from,
int to) const
2018-01-17 14:14:31 +01:00
{
}
int TextIconLayoutElement::getSelectionIndexCount() const
2018-01-17 14:14:31 +01:00
{
return this->trailingSpace ? 2 : 1;
}
void TextIconLayoutElement::paint(QPainter &painter)
{
auto app = getApp();
QFont font = app->fonts->getFont(FontStyle::Tiny, this->scale);
2018-01-17 14:14:31 +01:00
painter.setPen(app->themes->messages.textColors.system);
2018-01-17 14:14:31 +01:00
painter.setFont(font);
QTextOption option;
option.setAlignment(Qt::AlignHCenter);
if (this->line2.isEmpty()) {
QRect _rect(this->getRect());
painter.drawText(_rect, this->line1, option);
2018-01-17 14:14:31 +01:00
} else {
painter.drawText(
2018-08-06 21:17:03 +02:00
QPoint(this->getRect().x(),
this->getRect().y() + this->getRect().height() / 2),
2018-01-17 14:14:31 +01:00
this->line1);
2018-08-06 21:17:03 +02:00
painter.drawText(QPoint(this->getRect().x(),
this->getRect().y() + this->getRect().height()),
this->line2);
2018-01-17 14:14:31 +01:00
}
}
void TextIconLayoutElement::paintAnimated(QPainter &painter, int yOffset)
{
}
int TextIconLayoutElement::getMouseOverIndex(const QPoint &abs) const
2018-01-17 14:14:31 +01:00
{
return 0;
}
int TextIconLayoutElement::getXFromIndex(int index)
{
if (index <= 0) {
return this->getRect().left();
} else if (index == 1) {
// fourtf: remove space width
return this->getRect().right();
} else {
return this->getRect().right();
}
}
2018-04-01 16:43:30 +02:00
} // namespace chatterino