2017-06-11 09:31:45 +02:00
|
|
|
#include "messageref.hpp"
|
|
|
|
#include "emotemanager.hpp"
|
|
|
|
#include "settingsmanager.hpp"
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-02-03 19:31:51 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
2017-02-02 22:15:09 +01:00
|
|
|
#define MARGIN_LEFT 8
|
|
|
|
#define MARGIN_RIGHT 8
|
2017-09-12 19:06:16 +02:00
|
|
|
#define MARGIN_TOP 4
|
|
|
|
#define MARGIN_BOTTOM 4
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
using namespace chatterino::messages;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
|
|
|
namespace messages {
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
MessageRef::MessageRef(SharedMessage _message)
|
|
|
|
: message(_message)
|
|
|
|
, wordParts()
|
2017-02-02 22:15:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
Message *MessageRef::getMessage()
|
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
return this->message.get();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int MessageRef::getHeight() const
|
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
return this->height;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageRef::layout(int width, bool enableEmoteMargins)
|
2017-02-02 22:15:09 +01:00
|
|
|
{
|
2017-04-12 17:46:44 +02:00
|
|
|
auto &settings = SettingsManager::getInstance();
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
bool sizeChanged = width != this->currentLayoutWidth;
|
|
|
|
bool redraw = width != this->currentLayoutWidth;
|
2017-02-02 22:15:09 +01:00
|
|
|
int spaceWidth = 4;
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
int mediumTextLineHeight =
|
|
|
|
FontManager::getInstance().getFontMetrics(FontManager::Medium).height();
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
/* TODO(pajlada): Re-implement
|
2017-09-21 12:15:01 +02:00
|
|
|
bool recalculateImages = this->emoteGeneration != EmoteManager::getInstance().getGeneration();
|
2017-06-13 21:13:58 +02:00
|
|
|
*/
|
|
|
|
bool recalculateImages = true;
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
bool recalculateText = this->fontGeneration != FontManager::getInstance().getGeneration();
|
|
|
|
bool newWordTypes = this->currentWordTypes != SettingsManager::getInstance().getWordTypeMask();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
qreal emoteScale = settings.emoteScale.get();
|
|
|
|
bool scaleEmotesByLineHeight = settings.scaleEmotesByLineHeight.get();
|
|
|
|
|
|
|
|
// calculate word sizes
|
|
|
|
if (!redraw && !recalculateImages && !recalculateText && !newWordTypes) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
// this->emoteGeneration = EmoteManager::getInstance().getGeneration();
|
|
|
|
this->fontGeneration = FontManager::getInstance().getGeneration();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
for (auto &word : this->message->getWords()) {
|
2017-04-12 17:46:44 +02:00
|
|
|
if (word.isImage()) {
|
|
|
|
if (!recalculateImages) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &image = word.getImage();
|
|
|
|
|
|
|
|
qreal w = image.getWidth();
|
|
|
|
qreal h = image.getHeight();
|
|
|
|
|
|
|
|
if (scaleEmotesByLineHeight) {
|
|
|
|
word.setSize(w * mediumTextLineHeight / h * emoteScale,
|
|
|
|
mediumTextLineHeight * emoteScale);
|
|
|
|
} else {
|
|
|
|
word.setSize(w * image.getScale() * emoteScale, h * image.getScale() * emoteScale);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!recalculateText) {
|
|
|
|
continue;
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
QFontMetrics &metrics = word.getFontMetrics();
|
|
|
|
word.setSize(metrics.width(word.getText()), metrics.height());
|
2017-02-17 23:51:35 +01:00
|
|
|
}
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
if (newWordTypes) {
|
2017-09-21 12:15:01 +02:00
|
|
|
this->currentWordTypes = settings.getWordTypeMask();
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// layout
|
2017-09-21 12:15:01 +02:00
|
|
|
this->currentLayoutWidth = width;
|
2017-02-03 19:31:51 +01:00
|
|
|
|
2017-02-02 22:15:09 +01:00
|
|
|
int x = MARGIN_LEFT;
|
|
|
|
int y = MARGIN_TOP;
|
|
|
|
|
2017-04-24 23:00:26 +02:00
|
|
|
int right = width - MARGIN_RIGHT;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-02-17 23:51:35 +01:00
|
|
|
int lineNumber = 0;
|
2017-02-02 22:15:09 +01:00
|
|
|
int lineStart = 0;
|
|
|
|
int lineHeight = 0;
|
|
|
|
bool first = true;
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
this->wordParts.clear();
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-06-11 20:53:43 +02:00
|
|
|
uint32_t flags = settings.getWordTypeMask();
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
for (auto it = this->message->getWords().begin(); it != this->message->getWords().end(); ++it) {
|
2017-02-02 22:15:09 +01:00
|
|
|
Word &word = *it;
|
|
|
|
|
2017-06-11 20:53:43 +02:00
|
|
|
// Check if given word is supposed to be rendered by comparing it to the current setting
|
2017-02-02 22:15:09 +01:00
|
|
|
if ((word.getType() & flags) == Word::None) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xOffset = 0, yOffset = 0;
|
|
|
|
|
|
|
|
if (enableEmoteMargins) {
|
2017-04-14 14:52:31 +02:00
|
|
|
if (word.isImage() && word.getImage().isHat()) {
|
2017-02-02 22:15:09 +01:00
|
|
|
xOffset = -word.getWidth() + 2;
|
|
|
|
} else {
|
|
|
|
xOffset = word.getXOffset();
|
|
|
|
yOffset = word.getYOffset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// word wrapping
|
|
|
|
if (word.isText() && word.getWidth() + MARGIN_LEFT > right) {
|
2017-09-16 00:05:06 +02:00
|
|
|
alignWordParts(lineStart, lineHeight, width);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
y += lineHeight;
|
|
|
|
|
|
|
|
const QString &text = word.getText();
|
|
|
|
|
|
|
|
int start = 0;
|
|
|
|
QFontMetrics &metrics = word.getFontMetrics();
|
|
|
|
|
|
|
|
int width = 0;
|
|
|
|
|
|
|
|
std::vector<short> &charWidths = word.getCharacterWidthCache();
|
2017-09-21 02:20:02 +02:00
|
|
|
int charOffset = 0;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
for (int i = 2; i <= text.length(); i++) {
|
|
|
|
if ((width = width + charWidths[i - 1]) + MARGIN_LEFT > right) {
|
|
|
|
QString mid = text.mid(start, i - start - 1);
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y, width,
|
|
|
|
word.getHeight(), lineNumber, mid, mid,
|
|
|
|
false, charOffset));
|
2017-09-21 02:20:02 +02:00
|
|
|
|
|
|
|
charOffset = i;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
y += metrics.height();
|
|
|
|
|
|
|
|
start = i - 1;
|
|
|
|
|
|
|
|
width = 0;
|
2017-02-17 23:51:35 +01:00
|
|
|
lineNumber++;
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString mid(text.mid(start));
|
|
|
|
width = metrics.width(mid);
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(), width,
|
|
|
|
word.getHeight(), lineNumber, mid, mid, charOffset));
|
2017-02-02 22:15:09 +01:00
|
|
|
x = width + MARGIN_LEFT + spaceWidth;
|
|
|
|
|
|
|
|
lineHeight = word.getHeight();
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
lineStart = this->wordParts.size() - 1;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
first = false;
|
|
|
|
} else if (first || x + word.getWidth() + xOffset <= right) {
|
|
|
|
// fits in the line
|
2017-09-21 12:15:01 +02:00
|
|
|
this->wordParts.push_back(
|
2017-04-12 17:46:44 +02:00
|
|
|
WordPart(word, x, y - word.getHeight(), lineNumber, word.getCopyText()));
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
x += word.getWidth() + xOffset;
|
|
|
|
x += spaceWidth;
|
|
|
|
|
|
|
|
lineHeight = std::max(word.getHeight(), lineHeight);
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
// doesn't fit in the line
|
2017-09-16 00:05:06 +02:00
|
|
|
alignWordParts(lineStart, lineHeight, width);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
y += lineHeight;
|
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
lineNumber++;
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
this->wordParts.push_back(
|
2017-04-12 17:46:44 +02:00
|
|
|
WordPart(word, MARGIN_LEFT, y - word.getHeight(), lineNumber, word.getCopyText()));
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
lineStart = this->wordParts.size() - 1;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
lineHeight = word.getHeight();
|
|
|
|
|
|
|
|
x = word.getWidth() + MARGIN_LEFT;
|
|
|
|
x += spaceWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
alignWordParts(lineStart, lineHeight, width);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
if (this->height != y + lineHeight) {
|
2017-02-03 19:31:51 +01:00
|
|
|
sizeChanged = true;
|
2017-09-21 12:15:01 +02:00
|
|
|
this->height = y + lineHeight;
|
2017-02-03 19:31:51 +01:00
|
|
|
}
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
this->height += MARGIN_BOTTOM;
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2017-02-03 19:31:51 +01:00
|
|
|
if (sizeChanged) {
|
2017-04-12 17:46:44 +02:00
|
|
|
buffer = nullptr;
|
2017-02-03 19:31:51 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
updateBuffer = true;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
const std::vector<WordPart> &MessageRef::getWordParts() const
|
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
return this->wordParts;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void MessageRef::alignWordParts(int lineStart, int lineHeight, int width)
|
2017-02-02 22:15:09 +01:00
|
|
|
{
|
2017-09-16 00:05:06 +02:00
|
|
|
int xOffset = 0;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
if (this->message->centered && this->wordParts.size() > 0) {
|
|
|
|
xOffset = (width - this->wordParts.at(this->wordParts.size() - 1).getRight()) / 2;
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
for (size_t i = lineStart; i < this->wordParts.size(); i++) {
|
|
|
|
WordPart &wordPart2 = this->wordParts.at(i);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
|
|
|
wordPart2.setPosition(wordPart2.getX() + xOffset, wordPart2.getY() + lineHeight);
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
const Word *MessageRef::tryGetWordPart(QPoint point)
|
2017-02-17 23:51:35 +01:00
|
|
|
{
|
2017-04-12 17:46:44 +02:00
|
|
|
// go through all words and return the first one that contains the point.
|
2017-09-21 12:15:01 +02:00
|
|
|
for (WordPart &wordPart : this->wordParts) {
|
2017-02-17 23:51:35 +01:00
|
|
|
if (wordPart.getRect().contains(point)) {
|
2017-09-12 19:06:16 +02:00
|
|
|
return &wordPart.getWord();
|
2017-02-17 23:51:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
return nullptr;
|
2017-02-17 23:51:35 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
int MessageRef::getSelectionIndex(QPoint position)
|
2017-02-17 23:51:35 +01:00
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
if (this->wordParts.size() == 0) {
|
2017-02-17 23:51:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find out in which line the cursor is
|
|
|
|
int lineNumber = 0, lineStart = 0, lineEnd = 0;
|
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
for (size_t i = 0; i < this->wordParts.size(); i++) {
|
|
|
|
WordPart &part = this->wordParts[i];
|
2017-02-17 23:51:35 +01:00
|
|
|
|
2017-04-24 23:00:26 +02:00
|
|
|
if (part.getLineNumber() != 0 && position.y() < part.getY()) {
|
2017-02-17 23:51:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part.getLineNumber() != lineNumber) {
|
2017-09-12 19:06:16 +02:00
|
|
|
lineStart = i;
|
2017-02-17 23:51:35 +01:00
|
|
|
lineNumber = part.getLineNumber();
|
|
|
|
}
|
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
lineEnd = i + 1;
|
2017-02-17 23:51:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// count up to the cursor
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < lineStart; i++) {
|
2017-09-21 12:15:01 +02:00
|
|
|
WordPart &part = this->wordParts[i];
|
2017-02-17 23:51:35 +01:00
|
|
|
|
|
|
|
index += part.getWord().isImage() ? 2 : part.getText().length() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = lineStart; i < lineEnd; i++) {
|
2017-09-21 12:15:01 +02:00
|
|
|
WordPart &part = this->wordParts[i];
|
2017-02-17 23:51:35 +01:00
|
|
|
|
|
|
|
// curser is left of the word part
|
|
|
|
if (position.x() < part.getX()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cursor is right of the word part
|
2017-04-24 23:00:26 +02:00
|
|
|
if (position.x() > part.getX() + part.getWidth()) {
|
2017-09-12 19:06:16 +02:00
|
|
|
index += part.getCharacterLength();
|
2017-02-17 23:51:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cursor is over the word part
|
|
|
|
if (part.getWord().isImage()) {
|
2017-09-12 19:06:16 +02:00
|
|
|
if (position.x() - part.getX() > part.getWidth() / 2) {
|
|
|
|
index++;
|
|
|
|
}
|
2017-02-17 23:51:35 +01:00
|
|
|
} else {
|
2017-09-12 19:06:16 +02:00
|
|
|
// TODO: use word.getCharacterWidthCache();
|
|
|
|
|
|
|
|
auto text = part.getText();
|
2017-02-17 23:51:35 +01:00
|
|
|
|
|
|
|
int x = part.getX();
|
|
|
|
|
|
|
|
for (int j = 0; j < text.length(); j++) {
|
|
|
|
if (x > position.x()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
2017-04-12 17:46:44 +02:00
|
|
|
x = part.getX() + part.getWord().getFontMetrics().width(text, j + 1);
|
2017-02-17 23:51:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
2017-05-27 16:16:39 +02:00
|
|
|
|
|
|
|
} // namespace messages
|
|
|
|
} // namespace chatterino
|