clean up wordParts system in Message

This commit is contained in:
Rasmus Karlsson 2017-01-28 20:29:02 +01:00
parent a38b3c1626
commit fbb6868b70
2 changed files with 45 additions and 50 deletions

View file

@ -26,7 +26,6 @@ QRegularExpression *Message::cheerRegex =
new QRegularExpression("cheer[1-9][0-9]*"); new QRegularExpression("cheer[1-9][0-9]*");
Message::Message(const QString &text) Message::Message(const QString &text)
: wordParts()
{ {
words.push_back(Word(text, Word::Text, words.push_back(Word(text, Word::Text,
ColorScheme::getInstance().SystemMessageColor, text, ColorScheme::getInstance().SystemMessageColor, text,
@ -36,7 +35,6 @@ Message::Message(const QString &text)
Message::Message(const IrcPrivateMessage &ircMessage, Channel &channel, Message::Message(const IrcPrivateMessage &ircMessage, Channel &channel,
bool enablePingSound, bool isReceivedWhisper, bool enablePingSound, bool isReceivedWhisper,
bool isSentWhisper, bool includeChannel) bool isSentWhisper, bool includeChannel)
: wordParts()
{ {
this->parseTime = std::chrono::system_clock::now(); this->parseTime = std::chrono::system_clock::now();
@ -464,19 +462,11 @@ Message::layout(int width, bool enableEmoteMargins)
int right = width - MARGIN_RIGHT - MARGIN_LEFT; int right = width - MARGIN_RIGHT - MARGIN_LEFT;
std::vector<WordPart> *parts = new std::vector<WordPart>();
int lineStart = 0; int lineStart = 0;
int lineHeight = 0; int lineHeight = 0;
bool first = true; bool first = true;
auto alignParts = [&lineStart, &lineHeight, &parts, this] { this->wordParts.clear();
for (size_t i = lineStart; i < parts->size(); i++) {
WordPart &wordPart2 = parts->at(i);
wordPart2.setY(wordPart2.getY() + lineHeight);
}
};
uint32_t flags = Settings::getInstance().getWordTypeMask(); uint32_t flags = Settings::getInstance().getWordTypeMask();
@ -500,7 +490,7 @@ Message::layout(int width, bool enableEmoteMargins)
// word wrapping // word wrapping
if (word.isText() && word.getWidth() + MARGIN_LEFT > right) { if (word.isText() && word.getWidth() + MARGIN_LEFT > right) {
alignParts(); this->alignWordParts(lineStart, lineHeight);
y += lineHeight; y += lineHeight;
@ -525,8 +515,9 @@ Message::layout(int width, bool enableEmoteMargins)
if ((width = width + charWidths[i - 1]) + MARGIN_LEFT > right) { if ((width = width + charWidths[i - 1]) + MARGIN_LEFT > right) {
QString mid = text.mid(start, i - start - 1); QString mid = text.mid(start, i - start - 1);
parts->push_back(WordPart(word, MARGIN_LEFT, y, width, this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y,
word.getHeight(), mid, mid)); width, word.getHeight(),
mid, mid));
y += metrics.height(); y += metrics.height();
@ -539,20 +530,19 @@ Message::layout(int width, bool enableEmoteMargins)
QString mid(text.mid(start)); QString mid(text.mid(start));
width = metrics.width(mid); width = metrics.width(mid);
parts->push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(), this->wordParts.push_back(WordPart(word, MARGIN_LEFT,
width, word.getHeight(), mid, mid)); y - word.getHeight(), width,
word.getHeight(), mid, mid));
x = width + MARGIN_LEFT + spaceWidth; x = width + MARGIN_LEFT + spaceWidth;
lineHeight = word.getHeight(); lineHeight = word.getHeight();
lineStart = parts->size() - 1; lineStart = this->wordParts.size() - 1;
first = false; first = false;
} } else if (first || x + word.getWidth() + xOffset <= right) {
// fits in the line // fits in the line
else if (first || x + word.getWidth() + xOffset <= right) { this->wordParts.push_back(
parts->push_back(
WordPart(word, x, y - word.getHeight(), word.getCopyText())); WordPart(word, x, y - word.getHeight(), word.getCopyText()));
x += word.getWidth() + xOffset; x += word.getWidth() + xOffset;
@ -561,18 +551,16 @@ Message::layout(int width, bool enableEmoteMargins)
lineHeight = std::max(word.getHeight(), lineHeight); lineHeight = std::max(word.getHeight(), lineHeight);
first = false; first = false;
} } else {
// doesn't fit in the line // doesn't fit in the line
else { this->alignWordParts(lineStart, lineHeight);
alignParts();
y += lineHeight; y += lineHeight;
parts->push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(), this->wordParts.push_back(WordPart(
word.getCopyText())); word, MARGIN_LEFT, y - word.getHeight(), word.getCopyText()));
lineStart = parts->size() - 1; lineStart = this->wordParts.size() - 1;
lineHeight = word.getHeight(); lineHeight = word.getHeight();
@ -581,17 +569,30 @@ Message::layout(int width, bool enableEmoteMargins)
} }
} }
alignParts(); this->alignWordParts(lineStart, lineHeight);
auto tmp = this->wordParts;
this->wordParts = parts;
delete tmp;
this->height = y + lineHeight; this->height = y + lineHeight;
return true; return true;
} }
void
Message::alignWordParts(int lineStart, int lineHeight)
{
for (size_t i = lineStart; i < this->wordParts.size(); i++) {
WordPart &wordPart2 = this->wordParts.at(i);
wordPart2.setY(wordPart2.getY() + lineHeight);
}
}
QString
Message::matchLink(const QString &string)
{
// TODO: Implement this xD
return QString();
}
bool bool
Message::sortTwitchEmotes(const std::pair<long int, LazyLoadedImage *> &a, Message::sortTwitchEmotes(const std::pair<long int, LazyLoadedImage *> &a,
const std::pair<long int, LazyLoadedImage *> &b) const std::pair<long int, LazyLoadedImage *> &b)
@ -599,11 +600,5 @@ Message::sortTwitchEmotes(const std::pair<long int, LazyLoadedImage *> &a,
return a.first < b.first; return a.first < b.first;
} }
QString } // namespace messages
Message::matchLink(const QString &string) } // namespace chatterino
{
// TODO: Implement this xD
return QString();
}
}
}

View file

@ -22,9 +22,6 @@ public:
~Message() ~Message()
{ {
if (wordParts != NULL) {
delete wordParts;
}
} }
bool bool
@ -66,7 +63,7 @@ public:
const std::vector<WordPart> const std::vector<WordPart>
getWordParts() const getWordParts() const
{ {
return *wordParts; return wordParts;
} }
bool bool
@ -119,20 +116,23 @@ private:
int height = 0; int height = 0;
std::vector<Word> words; std::vector<Word> words;
std::vector<WordPart> *wordParts; std::vector<WordPart> wordParts;
long currentLayoutWidth = -1; long currentLayoutWidth = -1;
bool relayoutRequested = true; bool relayoutRequested = true;
int fontGeneration = -1; int fontGeneration = -1;
int emoteGeneration = -1; int emoteGeneration = -1;
void alignWordParts(int lineStart, int lineHeight);
static QString matchLink(const QString &string); static QString matchLink(const QString &string);
static bool sortTwitchEmotes( static bool sortTwitchEmotes(
const std::pair<long int, LazyLoadedImage *> &a, const std::pair<long int, LazyLoadedImage *> &a,
const std::pair<long int, LazyLoadedImage *> &b); const std::pair<long int, LazyLoadedImage *> &b);
}; };
}
} } // namespace messages
} // namespace chatterino
#endif // MESSAGE_H #endif // MESSAGE_H