2018-01-11 20:16:25 +01:00
|
|
|
#include "messages/layouts/messagelayoutelement.hpp"
|
|
|
|
#include "messages/messageelement.hpp"
|
|
|
|
|
2018-01-16 00:26:04 +01:00
|
|
|
#include <QDebug>
|
2018-01-11 20:16:25 +01:00
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace messages {
|
|
|
|
namespace layouts {
|
|
|
|
|
|
|
|
const QRect &MessageLayoutElement::getRect() const
|
|
|
|
{
|
|
|
|
return this->rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageLayoutElement::MessageLayoutElement(MessageElement &_creator, const QSize &size)
|
|
|
|
: creator(_creator)
|
|
|
|
{
|
|
|
|
this->rect.setSize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageElement &MessageLayoutElement::getCreator() const
|
|
|
|
{
|
|
|
|
return this->creator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageLayoutElement::setPosition(QPoint point)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
this->link = _link;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Link &MessageLayoutElement::getLink() const
|
|
|
|
{
|
|
|
|
return this->link;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
//
|
|
|
|
// IMAGE
|
|
|
|
//
|
|
|
|
|
2018-01-17 14:14:31 +01:00
|
|
|
ImageLayoutElement::ImageLayoutElement(MessageElement &_creator, Image *_image, const QSize &_size)
|
2018-01-11 20:16:25 +01:00
|
|
|
: MessageLayoutElement(_creator, _size)
|
|
|
|
, image(_image)
|
|
|
|
{
|
|
|
|
this->trailingSpace = _creator.hasTrailingSpace();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageLayoutElement::addCopyTextToString(QString &str, int from, int to) const
|
|
|
|
{
|
2018-01-17 14:14:31 +01:00
|
|
|
str += this->image->getName();
|
2018-01-16 02:39:31 +01:00
|
|
|
|
|
|
|
if (this->hasTrailingSpace()) {
|
|
|
|
str += " ";
|
|
|
|
}
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ImageLayoutElement::getSelectionIndexCount()
|
|
|
|
{
|
|
|
|
return this->trailingSpace ? 2 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageLayoutElement::paint(QPainter &painter)
|
|
|
|
{
|
2018-01-19 22:45:33 +01:00
|
|
|
if (this->image == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:14:31 +01:00
|
|
|
const QPixmap *pixmap = this->image->getPixmap();
|
2018-01-11 20:16:25 +01:00
|
|
|
|
2018-01-17 14:14:31 +01:00
|
|
|
if (pixmap != nullptr && !this->image->isAnimated()) {
|
2018-01-11 20:16:25 +01:00
|
|
|
// 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-01-11 20:16:25 +01:00
|
|
|
{
|
2018-01-19 22:45:33 +01:00
|
|
|
if (this->image == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:14:31 +01:00
|
|
|
if (this->image->isAnimated()) {
|
2018-01-19 22:45:33 +01:00
|
|
|
auto pixmap = this->image->getPixmap();
|
|
|
|
|
|
|
|
if (pixmap != nullptr) {
|
2018-01-11 20:16:25 +01:00
|
|
|
// fourtf: make it use qreal values
|
2018-04-03 02:55:32 +02:00
|
|
|
QRect _rect = this->getRect();
|
|
|
|
_rect.moveTop(_rect.y() + yOffset);
|
|
|
|
painter.drawPixmap(QRectF(_rect), *pixmap, QRectF());
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ImageLayoutElement::getMouseOverIndex(const QPoint &abs)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
//
|
|
|
|
// TEXT
|
|
|
|
//
|
|
|
|
|
2018-01-17 14:14:31 +01:00
|
|
|
TextLayoutElement::TextLayoutElement(MessageElement &_creator, QString &_text, const QSize &_size,
|
2018-01-11 20:16:25 +01:00
|
|
|
QColor _color, FontStyle _style, float _scale)
|
|
|
|
: MessageLayoutElement(_creator, _size)
|
|
|
|
, text(_text)
|
|
|
|
, color(_color)
|
|
|
|
, style(_style)
|
|
|
|
, scale(_scale)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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 += " ";
|
|
|
|
}
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int TextLayoutElement::getSelectionIndexCount()
|
|
|
|
{
|
|
|
|
return this->text.length() + (this->trailingSpace ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextLayoutElement::paint(QPainter &painter)
|
|
|
|
{
|
|
|
|
painter.setPen(this->color);
|
|
|
|
|
|
|
|
painter.setFont(singletons::FontManager::getInstance().getFont(this->style, this->scale));
|
|
|
|
|
|
|
|
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)
|
2018-01-11 20:16:25 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int TextLayoutElement::getMouseOverIndex(const QPoint &abs)
|
|
|
|
{
|
2018-01-16 00:26:04 +01:00
|
|
|
if (abs.x() < this->getRect().left()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFontMetrics &metrics =
|
|
|
|
singletons::FontManager::getInstance().getFontMetrics(this->style, this->scale);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
QFontMetrics &metrics =
|
|
|
|
singletons::FontManager::getInstance().getFontMetrics(this->style, this->scale);
|
|
|
|
|
|
|
|
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-11 20:16:25 +01:00
|
|
|
}
|
2018-01-17 14:14:31 +01:00
|
|
|
|
|
|
|
// TEXT ICON
|
|
|
|
TextIconLayoutElement::TextIconLayoutElement(MessageElement &creator, const QString &_line1,
|
|
|
|
const QString &_line2, float _scale, const QSize &size)
|
|
|
|
: MessageLayoutElement(creator, size)
|
|
|
|
, scale(_scale)
|
|
|
|
, line1(_line1)
|
|
|
|
, line2(_line2)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextIconLayoutElement::addCopyTextToString(QString &str, int from, int to) const
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int TextIconLayoutElement::getSelectionIndexCount()
|
|
|
|
{
|
|
|
|
return this->trailingSpace ? 2 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextIconLayoutElement::paint(QPainter &painter)
|
|
|
|
{
|
|
|
|
QFont font = singletons::FontManager::getInstance().getFont(FontStyle::Tiny, this->scale);
|
|
|
|
|
|
|
|
painter.setBrush(singletons::ThemeManager::getInstance().messages.textColors.regular);
|
|
|
|
painter.setFont(font);
|
|
|
|
|
|
|
|
QTextOption option;
|
|
|
|
option.setAlignment(Qt::AlignHCenter);
|
|
|
|
|
|
|
|
if (this->line2.isEmpty()) {
|
2018-04-03 02:55:32 +02:00
|
|
|
QRect _rect(this->getRect());
|
|
|
|
painter.drawText(_rect, this->line1, option);
|
2018-01-17 14:14:31 +01:00
|
|
|
} else {
|
|
|
|
painter.drawText(
|
|
|
|
QPoint(this->getRect().x(), this->getRect().y() + this->getRect().height() / 2),
|
|
|
|
this->line1);
|
|
|
|
painter.drawText(
|
|
|
|
QPoint(this->getRect().x(), this->getRect().y() + this->getRect().height()),
|
|
|
|
this->line2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextIconLayoutElement::paintAnimated(QPainter &painter, int yOffset)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int TextIconLayoutElement::getMouseOverIndex(const QPoint &abs)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
} // namespace layouts
|
|
|
|
} // namespace messages
|
|
|
|
} // namespace chatterino
|