mirror-chatterino2/src/messages/word.cpp

147 lines
2.6 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "messages/word.hpp"
2017-01-18 21:30:23 +01:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace messages {
2017-01-05 16:07:20 +01:00
// Image word
2017-04-12 17:46:44 +02:00
Word::Word(LazyLoadedImage *image, Type type, const QString &copytext, const QString &tooltip,
const Link &link)
2017-09-12 19:06:16 +02:00
: image(image)
2017-01-18 04:33:30 +01:00
, _isImage(true)
2017-09-12 19:06:16 +02:00
, type(type)
, copyText(copytext)
, tooltip(tooltip)
, link(link)
2017-01-05 16:07:20 +01:00
{
2017-01-18 04:33:30 +01:00
image->getWidth(); // professional segfault test
2017-01-05 16:07:20 +01:00
}
// Text word
2017-04-12 17:46:44 +02:00
Word::Word(const QString &text, Type type, const QColor &color, const QString &copytext,
const QString &tooltip, const Link &link)
2017-09-12 19:06:16 +02:00
: image(nullptr)
, text(text)
, color(color)
2017-01-18 04:33:30 +01:00
, _isImage(false)
2017-09-12 19:06:16 +02:00
, type(type)
, copyText(copytext)
, tooltip(tooltip)
, link(link)
2017-04-12 17:46:44 +02:00
{
}
LazyLoadedImage &Word::getImage() const
{
2017-09-12 19:06:16 +02:00
return *this->image;
2017-04-12 17:46:44 +02:00
}
const QString &Word::getText() const
{
2017-09-12 19:06:16 +02:00
return this->text;
2017-04-12 17:46:44 +02:00
}
int Word::getWidth() const
{
2017-09-12 19:06:16 +02:00
return this->width;
2017-04-12 17:46:44 +02:00
}
int Word::getHeight() const
{
2017-09-12 19:06:16 +02:00
return this->height;
2017-04-12 17:46:44 +02:00
}
void Word::setSize(int width, int height)
{
2017-09-12 19:06:16 +02:00
this->width = width;
this->height = height;
2017-04-12 17:46:44 +02:00
}
bool Word::isImage() const
{
2017-09-12 19:06:16 +02:00
return this->_isImage;
2017-04-12 17:46:44 +02:00
}
bool Word::isText() const
{
2017-09-12 19:06:16 +02:00
return !this->_isImage;
2017-04-12 17:46:44 +02:00
}
const QString &Word::getCopyText() const
{
2017-09-12 19:06:16 +02:00
return this->copyText;
2017-04-12 17:46:44 +02:00
}
bool Word::hasTrailingSpace() const
2017-01-05 16:07:20 +01:00
{
2017-09-12 19:06:16 +02:00
return this->_hasTrailingSpace;
2017-01-05 16:07:20 +01:00
}
2017-04-12 17:46:44 +02:00
QFont &Word::getFont() const
{
2017-09-12 19:06:16 +02:00
return FontManager::getInstance().getFont(this->font);
2017-04-12 17:46:44 +02:00
}
QFontMetrics &Word::getFontMetrics() const
{
2017-09-12 19:06:16 +02:00
return FontManager::getInstance().getFontMetrics(this->font);
2017-04-12 17:46:44 +02:00
}
Word::Type Word::getType() const
{
2017-09-12 19:06:16 +02:00
return this->type;
2017-04-12 17:46:44 +02:00
}
const QString &Word::getTooltip() const
{
2017-09-12 19:06:16 +02:00
return this->tooltip;
2017-04-12 17:46:44 +02:00
}
const QColor &Word::getColor() const
{
2017-09-12 19:06:16 +02:00
return this->color;
2017-04-12 17:46:44 +02:00
}
const Link &Word::getLink() const
{
2017-09-12 19:06:16 +02:00
return this->link;
2017-04-12 17:46:44 +02:00
}
int Word::getXOffset() const
{
2017-09-12 19:06:16 +02:00
return this->xOffset;
2017-04-12 17:46:44 +02:00
}
int Word::getYOffset() const
{
2017-09-12 19:06:16 +02:00
return this->yOffset;
2017-04-12 17:46:44 +02:00
}
void Word::setOffset(int xOffset, int yOffset)
{
2017-09-12 19:06:16 +02:00
this->xOffset = std::max(0, xOffset);
this->yOffset = std::max(0, yOffset);
}
int Word::getCharacterLength() const
{
return this->isImage() ? 2 : this->getText().length() + 1;
2017-04-12 17:46:44 +02:00
}
std::vector<short> &Word::getCharacterWidthCache() const
{
2017-09-12 19:06:16 +02:00
// lock not required because there is only one gui thread
// std::lock_guard<std::mutex> lock(this->charWidthCacheMutex);
if (this->charWidthCache.size() == 0 && this->isText()) {
for (int i = 0; i < this->getText().length(); i++) {
this->charWidthCache.push_back(this->getFontMetrics().charWidth(this->getText(), i));
}
}
// TODO: on font change
return this->charWidthCache;
2017-04-12 17:46:44 +02:00
}
2017-04-14 17:52:22 +02:00
} // namespace messages
} // namespace chatterino