2017-01-05 16:07:20 +01:00
|
|
|
#include "message.h"
|
2017-01-11 18:52:09 +01:00
|
|
|
#include "appsettings.h"
|
2017-01-05 16:07:20 +01:00
|
|
|
#include "colorscheme.h"
|
2017-01-07 20:43:55 +01:00
|
|
|
#include "emojis.h"
|
2017-01-11 18:52:09 +01:00
|
|
|
#include "emotes.h"
|
2017-01-11 01:08:20 +01:00
|
|
|
#include "fonts.h"
|
2017-01-11 18:52:09 +01:00
|
|
|
#include "ircmanager.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "qcolor.h"
|
2017-01-13 18:59:11 +01:00
|
|
|
#include "resources.h"
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
#include <QStringList>
|
2017-01-05 16:43:01 +01:00
|
|
|
#include <ctime>
|
2017-01-11 01:08:20 +01:00
|
|
|
#include <list>
|
2017-01-11 18:52:09 +01:00
|
|
|
#include <tuple>
|
2017-01-05 16:43:01 +01:00
|
|
|
|
2017-01-11 01:08:20 +01:00
|
|
|
#define MARGIN_LEFT 8
|
|
|
|
#define MARGIN_RIGHT 8
|
|
|
|
#define MARGIN_TOP 8
|
|
|
|
#define MARGIN_BOTTOM 8
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
QRegularExpression *Message::cheerRegex =
|
|
|
|
new QRegularExpression("cheer[1-9][0-9]*");
|
2017-01-07 20:43:55 +01:00
|
|
|
|
2017-01-05 16:07:20 +01:00
|
|
|
Message::Message(const QString &text)
|
2017-01-15 16:38:30 +01:00
|
|
|
: m_wordParts(new std::vector<WordPart>())
|
2017-01-05 16:07:20 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
Message::Message(const IrcPrivateMessage &ircMessage, const Channel &channel,
|
|
|
|
bool enablePingSound, bool isReceivedWhisper,
|
|
|
|
bool isSentWhisper, bool includeChannel)
|
2017-01-15 16:38:30 +01:00
|
|
|
: m_wordParts(new std::vector<WordPart>())
|
2017-01-05 16:07:20 +01:00
|
|
|
{
|
|
|
|
m_parseTime = std::chrono::system_clock::now();
|
|
|
|
|
2017-01-11 01:08:20 +01:00
|
|
|
auto words = std::vector<Word>();
|
|
|
|
|
|
|
|
auto tags = ircMessage.tags();
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2017-01-11 01:08:20 +01:00
|
|
|
auto iterator = tags.find("id");
|
2017-01-05 20:49:33 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (iterator != tags.end()) {
|
2017-01-05 20:49:33 +01:00
|
|
|
m_id = iterator.value().toString();
|
|
|
|
}
|
|
|
|
|
2017-01-05 16:07:20 +01:00
|
|
|
// timestamps
|
2017-01-11 01:08:20 +01:00
|
|
|
iterator = tags.find("tmi-sent-ts");
|
2017-01-13 18:59:11 +01:00
|
|
|
|
2017-01-05 16:43:01 +01:00
|
|
|
std::time_t time = std::time(NULL);
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
// if (iterator != tags.end()) {
|
|
|
|
// time = strtoll(iterator.value().toString().toStdString().c_str(),
|
|
|
|
// NULL,
|
|
|
|
// 10);
|
|
|
|
// }
|
2017-01-05 16:07:20 +01:00
|
|
|
|
|
|
|
char timeStampBuffer[69];
|
|
|
|
|
|
|
|
strftime(timeStampBuffer, 69, "%H:%M", localtime(&time));
|
|
|
|
QString timestamp = QString(timeStampBuffer);
|
|
|
|
|
|
|
|
strftime(timeStampBuffer, 69, "%H:%M:%S", localtime(&time));
|
|
|
|
QString timestampWithSeconds = QString(timeStampBuffer);
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
words.push_back(Word(timestamp, Word::TimestampNoSeconds,
|
|
|
|
ColorScheme::instance().SystemMessageColor, QString(),
|
|
|
|
QString()));
|
|
|
|
words.push_back(Word(timestampWithSeconds, Word::TimestampWithSeconds,
|
|
|
|
ColorScheme::instance().SystemMessageColor, QString(),
|
|
|
|
QString()));
|
2017-01-06 23:28:48 +01:00
|
|
|
|
2017-01-15 16:38:30 +01:00
|
|
|
// mod buttons
|
|
|
|
static QString buttonBanTooltip("Ban user");
|
|
|
|
static QString buttonTimeoutTooltip("Timeout user");
|
|
|
|
|
|
|
|
words.push_back(Word(Resources::buttonBan(), Word::ButtonBan, QString(),
|
|
|
|
buttonBanTooltip,
|
|
|
|
Link(Link::UserBan, ircMessage.account())));
|
|
|
|
words.push_back(Word(Resources::buttonTimeout(), Word::ButtonTimeout,
|
|
|
|
QString(), buttonTimeoutTooltip,
|
|
|
|
Link(Link::UserTimeout, ircMessage.account())));
|
|
|
|
|
2017-01-06 23:28:48 +01:00
|
|
|
// badges
|
2017-01-11 01:08:20 +01:00
|
|
|
iterator = tags.find("badges");
|
2017-01-06 23:28:48 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (iterator != tags.end()) {
|
2017-01-06 23:28:48 +01:00
|
|
|
auto badges = iterator.value().toString().split(',');
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
for (QString badge : badges) {
|
|
|
|
if (badge.startsWith("bits/")) {
|
|
|
|
long long int cheer =
|
|
|
|
strtoll(badge.mid(5).toStdString().c_str(), NULL, 10);
|
|
|
|
words.push_back(Word(
|
|
|
|
Emotes::getCheerBadge(cheer), Word::BadgeCheer, QString(),
|
|
|
|
QString("Twitch Cheer" + QString::number(cheer))));
|
|
|
|
} else if (badge == "staff/1") {
|
2017-01-13 18:59:11 +01:00
|
|
|
words.push_back(Word(Resources::badgeStaff(), Word::BadgeStaff,
|
|
|
|
QString(), QString("Twitch Staff")));
|
2017-01-11 18:52:09 +01:00
|
|
|
} else if (badge == "admin/1") {
|
2017-01-13 18:59:11 +01:00
|
|
|
words.push_back(Word(Resources::badgeAdmin(), Word::BadgeAdmin,
|
|
|
|
QString(), QString("Twitch Admin")));
|
2017-01-11 18:52:09 +01:00
|
|
|
} else if (badge == "global_mod/1") {
|
2017-01-13 18:59:11 +01:00
|
|
|
words.push_back(Word(Resources::badgeGlobalmod(),
|
|
|
|
Word::BadgeGlobalMod, QString(),
|
|
|
|
QString("Global Moderator")));
|
2017-01-11 18:52:09 +01:00
|
|
|
} else if (badge == "moderator/1") {
|
2017-01-15 17:21:28 +01:00
|
|
|
// TODO: implement this xD
|
2017-01-13 18:59:11 +01:00
|
|
|
words.push_back(Word(
|
|
|
|
Resources::badgeTurbo(), Word::BadgeModerator, QString(),
|
|
|
|
QString("Channel Moderator"))); // custom badge
|
2017-01-11 18:52:09 +01:00
|
|
|
} else if (badge == "turbo/1") {
|
2017-01-13 18:59:11 +01:00
|
|
|
words.push_back(Word(Resources::badgeStaff(), Word::BadgeTurbo,
|
|
|
|
QString(), QString("Turbo Subscriber")));
|
2017-01-11 18:52:09 +01:00
|
|
|
} else if (badge == "broadcaster/1") {
|
2017-01-13 18:59:11 +01:00
|
|
|
words.push_back(Word(Resources::badgeBroadcaster(),
|
|
|
|
Word::BadgeBroadcaster, QString(),
|
2017-01-11 18:52:09 +01:00
|
|
|
QString("Channel Broadcaster")));
|
|
|
|
} else if (badge == "premium/1") {
|
2017-01-13 18:59:11 +01:00
|
|
|
words.push_back(Word(Resources::badgePremium(),
|
|
|
|
Word::BadgePremium, QString(),
|
|
|
|
QString("Twitch Prime")));
|
2017-01-06 23:28:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-05 20:49:33 +01:00
|
|
|
|
|
|
|
// color
|
2017-01-06 23:28:48 +01:00
|
|
|
QColor usernameColor = ColorScheme::instance().SystemMessageColor;
|
2017-01-05 20:49:33 +01:00
|
|
|
|
2017-01-11 01:08:20 +01:00
|
|
|
iterator = tags.find("color");
|
2017-01-11 18:52:09 +01:00
|
|
|
if (iterator != tags.end()) {
|
|
|
|
usernameColor = QColor(iterator.value().toString());
|
2017-01-05 20:49:33 +01:00
|
|
|
}
|
2017-01-05 16:22:55 +01:00
|
|
|
|
2017-01-05 20:49:33 +01:00
|
|
|
// channel name
|
2017-01-11 18:52:09 +01:00
|
|
|
if (includeChannel) {
|
2017-01-05 20:49:33 +01:00
|
|
|
QString channelName("#" + channel.name());
|
2017-01-11 18:52:09 +01:00
|
|
|
words.push_back(Word(channelName, Word::Misc,
|
|
|
|
ColorScheme::instance().SystemMessageColor,
|
|
|
|
QString(channelName), QString(),
|
|
|
|
Link(Link::Url, channel.name() + "\n" + m_id)));
|
2017-01-05 20:49:33 +01:00
|
|
|
}
|
2017-01-05 16:07:20 +01:00
|
|
|
|
|
|
|
// username
|
|
|
|
m_userName = ircMessage.account();
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (m_userName.isEmpty()) {
|
2017-01-11 01:08:20 +01:00
|
|
|
auto iterator = tags.find("login");
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (iterator != tags.end()) {
|
2017-01-05 16:07:20 +01:00
|
|
|
m_userName = iterator.value().toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString displayName;
|
|
|
|
|
2017-01-11 01:08:20 +01:00
|
|
|
iterator = tags.find("display-name");
|
|
|
|
if (iterator == tags.end()) {
|
2017-01-05 20:49:33 +01:00
|
|
|
displayName = ircMessage.account();
|
2017-01-11 18:52:09 +01:00
|
|
|
} else {
|
2017-01-05 16:07:20 +01:00
|
|
|
displayName = iterator.value().toString();
|
|
|
|
}
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
bool hasLocalizedName =
|
|
|
|
QString::compare(displayName, ircMessage.account()) == 0;
|
|
|
|
QString userDisplayString =
|
|
|
|
displayName +
|
|
|
|
(hasLocalizedName ? (" (" + ircMessage.account() + ")") : QString());
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (isSentWhisper) {
|
2017-01-07 20:43:55 +01:00
|
|
|
userDisplayString = IrcManager::account->username() + " -> ";
|
|
|
|
}
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (isReceivedWhisper) {
|
2017-01-07 20:43:55 +01:00
|
|
|
userDisplayString += " -> " + IrcManager::account->username();
|
|
|
|
}
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (!ircMessage.isAction()) {
|
2017-01-07 20:43:55 +01:00
|
|
|
userDisplayString += ": ";
|
|
|
|
}
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
words.push_back(Word(userDisplayString, Word::Username, usernameColor,
|
|
|
|
userDisplayString, QString()));
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
// highlights
|
2017-01-15 17:21:28 +01:00
|
|
|
// TODO: implement this xD
|
2017-01-05 16:07:20 +01:00
|
|
|
|
|
|
|
// bits
|
|
|
|
QString bits = "";
|
|
|
|
|
2017-01-11 01:08:20 +01:00
|
|
|
iterator = tags.find("bits");
|
2017-01-11 18:52:09 +01:00
|
|
|
if (iterator != tags.end()) {
|
|
|
|
bits = iterator.value().toString();
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 23:28:48 +01:00
|
|
|
// twitch emotes
|
2017-01-11 18:52:09 +01:00
|
|
|
std::vector<std::pair<long int, LazyLoadedImage *>> twitchEmotes;
|
2017-01-06 23:28:48 +01:00
|
|
|
|
2017-01-11 01:08:20 +01:00
|
|
|
iterator = tags.find("emotes");
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (iterator != tags.end()) {
|
2017-01-06 23:28:48 +01:00
|
|
|
auto emotes = iterator.value().toString().split('/');
|
2017-01-05 16:07:20 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
for (QString emote : emotes) {
|
|
|
|
if (!emote.contains(':'))
|
|
|
|
continue;
|
2017-01-06 23:28:48 +01:00
|
|
|
|
|
|
|
QStringList parameters = emote.split(':');
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (parameters.length() < 2)
|
|
|
|
continue;
|
2017-01-06 23:28:48 +01:00
|
|
|
|
|
|
|
long int id = std::stol(parameters.at(0).toStdString(), NULL, 10);
|
|
|
|
|
|
|
|
QStringList occurences = parameters.at(1).split(',');
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
for (QString occurence : occurences) {
|
2017-01-06 23:28:48 +01:00
|
|
|
QStringList coords = occurence.split('-');
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (coords.length() < 2)
|
|
|
|
continue;
|
2017-01-06 23:28:48 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
long int start =
|
|
|
|
std::stol(coords.at(0).toStdString(), NULL, 10);
|
2017-01-06 23:28:48 +01:00
|
|
|
long int end = std::stol(coords.at(1).toStdString(), NULL, 10);
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (start >= end || start < 0 ||
|
|
|
|
end > ircMessage.content().length())
|
|
|
|
continue;
|
2017-01-06 23:28:48 +01:00
|
|
|
|
|
|
|
QString name = ircMessage.content().mid(start, end - start);
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
twitchEmotes.push_back(std::pair<long int, LazyLoadedImage *>(
|
|
|
|
start, Emotes::getTwitchEmoteById(name, id)));
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-06 23:28:48 +01:00
|
|
|
|
|
|
|
std::sort(twitchEmotes.begin(), twitchEmotes.end(), sortTwitchEmotes);
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
2017-01-05 20:49:33 +01:00
|
|
|
|
2017-01-06 23:28:48 +01:00
|
|
|
auto currentTwitchEmote = twitchEmotes.begin();
|
|
|
|
|
|
|
|
// words
|
2017-01-11 18:52:09 +01:00
|
|
|
QColor textColor =
|
|
|
|
ircMessage.isAction() ? usernameColor : ColorScheme::instance().Text;
|
2017-01-05 20:49:33 +01:00
|
|
|
|
2017-01-06 23:28:48 +01:00
|
|
|
QStringList splits = ircMessage.content().split(' ');
|
|
|
|
|
|
|
|
long int i = 0;
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
for (QString split : splits) {
|
2017-01-06 23:28:48 +01:00
|
|
|
// twitch emote
|
2017-01-15 16:38:30 +01:00
|
|
|
if (currentTwitchEmote != twitchEmotes.end() &&
|
|
|
|
currentTwitchEmote->first == i) {
|
2017-01-11 18:52:09 +01:00
|
|
|
words.push_back(Word(currentTwitchEmote->second,
|
|
|
|
Word::TwitchEmoteImage,
|
|
|
|
currentTwitchEmote->second->name(),
|
|
|
|
currentTwitchEmote->second->name() +
|
|
|
|
QString("\nTwitch Emote")));
|
|
|
|
words.push_back(Word(currentTwitchEmote->second->name(),
|
|
|
|
Word::TwitchEmoteText, textColor,
|
|
|
|
currentTwitchEmote->second->name(),
|
|
|
|
currentTwitchEmote->second->name() +
|
|
|
|
QString("\nTwitch Emote")));
|
2017-01-06 23:28:48 +01:00
|
|
|
|
|
|
|
i += split.length() + 1;
|
|
|
|
currentTwitchEmote = std::next(currentTwitchEmote);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// split words
|
2017-01-11 18:52:09 +01:00
|
|
|
std::vector<std::tuple<LazyLoadedImage *, QString>> parsed;
|
2017-01-07 20:43:55 +01:00
|
|
|
|
|
|
|
Emojis::parseEmojis(parsed, split);
|
2017-01-11 18:52:09 +01:00
|
|
|
for (const std::tuple<LazyLoadedImage *, QString> &tuple : parsed) {
|
|
|
|
LazyLoadedImage *image = std::get<0>(tuple);
|
2017-01-15 16:38:30 +01:00
|
|
|
|
|
|
|
if (image == NULL) { // is text
|
2017-01-07 20:43:55 +01:00
|
|
|
QString string = std::get<1>(tuple);
|
|
|
|
|
|
|
|
// cheers
|
2017-01-11 18:52:09 +01:00
|
|
|
if (!bits.isEmpty() && string.length() >= 6 &&
|
|
|
|
cheerRegex->match(string).isValid()) {
|
2017-01-07 20:43:55 +01:00
|
|
|
auto cheer = string.mid(5).toInt();
|
|
|
|
|
|
|
|
QString color;
|
|
|
|
|
|
|
|
QColor bitsColor;
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (cheer >= 10000) {
|
2017-01-07 20:43:55 +01:00
|
|
|
color = "red";
|
|
|
|
bitsColor = QColor::fromHslF(0, 1, 0.5);
|
2017-01-11 18:52:09 +01:00
|
|
|
} else if (cheer >= 5000) {
|
2017-01-07 20:43:55 +01:00
|
|
|
color = "blue";
|
|
|
|
bitsColor = QColor::fromHslF(0.61, 1, 0.4);
|
2017-01-11 18:52:09 +01:00
|
|
|
} else if (cheer >= 1000) {
|
2017-01-07 20:43:55 +01:00
|
|
|
color = "green";
|
|
|
|
bitsColor = QColor::fromHslF(0.5, 1, 0.5);
|
2017-01-11 18:52:09 +01:00
|
|
|
} else if (cheer >= 100) {
|
2017-01-07 20:43:55 +01:00
|
|
|
color = "purple";
|
|
|
|
bitsColor = QColor::fromHslF(0.8, 1, 0.5);
|
2017-01-11 18:52:09 +01:00
|
|
|
} else {
|
2017-01-07 20:43:55 +01:00
|
|
|
color = "gray";
|
|
|
|
bitsColor = QColor::fromHslF(0.5f, 0.5f, 0.5f);
|
|
|
|
}
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
QString bitsLinkAnimated = QString(
|
|
|
|
"http://static-cdn.jtvnw.net/bits/dark/animated/" +
|
|
|
|
color + "/1");
|
|
|
|
QString bitsLink = QString(
|
|
|
|
"http://static-cdn.jtvnw.net/bits/dark/static/" +
|
|
|
|
color + "/1");
|
|
|
|
|
|
|
|
LazyLoadedImage *imageAnimated =
|
|
|
|
Emotes::miscImageFromCache().getOrAdd(
|
|
|
|
bitsLinkAnimated, [&bitsLinkAnimated] {
|
|
|
|
return new LazyLoadedImage(bitsLinkAnimated);
|
|
|
|
});
|
|
|
|
LazyLoadedImage *image =
|
|
|
|
Emotes::miscImageFromCache().getOrAdd(
|
|
|
|
bitsLink, [&bitsLink] {
|
|
|
|
return new LazyLoadedImage(bitsLink);
|
|
|
|
});
|
|
|
|
|
|
|
|
words.push_back(
|
|
|
|
Word(imageAnimated, Word::BitsAnimated,
|
|
|
|
QString("cheer"), QString("Twitch Cheer"),
|
|
|
|
Link(Link::Url,
|
|
|
|
QString("https://blog.twitch.tv/"
|
|
|
|
"introducing-cheering-celebrate-"
|
|
|
|
"together-da62af41fac6"))));
|
|
|
|
words.push_back(
|
|
|
|
Word(image, Word::Bits, QString("cheer"),
|
|
|
|
QString("Twitch Cheer"),
|
|
|
|
Link(Link::Url,
|
|
|
|
QString("https://blog.twitch.tv/"
|
|
|
|
"introducing-cheering-celebrate-"
|
|
|
|
"together-da62af41fac6"))));
|
|
|
|
|
|
|
|
words.push_back(
|
|
|
|
Word(QString("x" + string.mid(5)), Word::BitsAmount,
|
|
|
|
bitsColor, QString(string.mid(5)),
|
|
|
|
QString("Twitch Cheer"),
|
|
|
|
Link(Link::Url,
|
|
|
|
QString("https://blog.twitch.tv/"
|
|
|
|
"introducing-cheering-celebrate-"
|
|
|
|
"together-da62af41fac6"))));
|
2017-01-07 20:43:55 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
continue;
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
2017-01-07 20:43:55 +01:00
|
|
|
|
|
|
|
// bttv / ffz emotes
|
2017-01-11 18:52:09 +01:00
|
|
|
LazyLoadedImage *bttvEmote;
|
|
|
|
|
2017-01-15 17:21:28 +01:00
|
|
|
// TODO: Implement this (ignored emotes)
|
2017-01-11 18:52:09 +01:00
|
|
|
if (Emotes::bttvEmotes().tryGet(string, bttvEmote) ||
|
|
|
|
channel.bttvChannelEmotes().tryGet(string, bttvEmote) ||
|
|
|
|
Emotes::ffzEmotes().tryGet(string, bttvEmote) ||
|
|
|
|
channel.ffzChannelEmotes().tryGet(string, bttvEmote) ||
|
|
|
|
Emotes::chatterinoEmotes().tryGet(string, bttvEmote)) {
|
|
|
|
words.push_back(Word(bttvEmote, Word::BttvEmoteImage,
|
|
|
|
bttvEmote->name(),
|
|
|
|
bttvEmote->tooltip(),
|
|
|
|
Link(Link::Url, bttvEmote->url())));
|
2017-01-07 20:43:55 +01:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// actually just a word
|
|
|
|
QString link = matchLink(string);
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
words.push_back(
|
|
|
|
Word(string, Word::Text, textColor, string, QString(),
|
|
|
|
link.isEmpty() ? Link() : Link(Link::Url, link)));
|
2017-01-15 16:38:30 +01:00
|
|
|
} else { // is emoji
|
|
|
|
static QString emojiTooltip("Emoji");
|
|
|
|
|
|
|
|
words.push_back(
|
|
|
|
Word(image, Word::EmojiImage, image->name(), emojiTooltip));
|
|
|
|
Word(image->name(), Word::EmojiText, textColor, image->name(),
|
|
|
|
emojiTooltip);
|
2017-01-07 20:43:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i += split.length() + 1;
|
2017-01-06 23:28:48 +01:00
|
|
|
}
|
2017-01-07 20:43:55 +01:00
|
|
|
|
2017-01-11 01:08:20 +01:00
|
|
|
this->m_words = words;
|
2017-01-07 20:43:55 +01:00
|
|
|
|
2017-01-15 17:21:28 +01:00
|
|
|
// TODO: Implement this xD
|
2017-01-11 18:52:09 +01:00
|
|
|
// if (!isReceivedWhisper &&
|
|
|
|
// AppSettings.HighlightIgnoredUsers.ContainsKey(Username))
|
|
|
|
// {
|
|
|
|
// HighlightTab = false;
|
|
|
|
// }
|
2017-01-06 23:28:48 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
bool
|
|
|
|
Message::layout(int width, bool enableEmoteMargins)
|
2017-01-11 01:08:20 +01:00
|
|
|
{
|
|
|
|
width = width - (width % 2);
|
|
|
|
|
|
|
|
int mediumTextLineHeight = Fonts::getFontMetrics(Fonts::Medium).height();
|
2017-01-13 18:59:11 +01:00
|
|
|
int spaceWidth = 4;
|
2017-01-11 01:08:20 +01:00
|
|
|
|
|
|
|
bool redraw = width != m_currentLayoutWidth || m_relayoutRequested;
|
|
|
|
|
2017-01-15 16:38:30 +01:00
|
|
|
bool recalculateImages = m_emoteGeneration != Emotes::generation();
|
|
|
|
bool recalculateText = m_fontGeneration != Fonts::generation();
|
|
|
|
|
|
|
|
if (recalculateImages || recalculateText) {
|
|
|
|
m_emoteGeneration = Emotes::generation();
|
|
|
|
m_fontGeneration = Fonts::generation();
|
|
|
|
|
2017-01-11 01:08:20 +01:00
|
|
|
redraw = true;
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
for (auto &word : m_words) {
|
|
|
|
if (word.isImage()) {
|
2017-01-15 16:38:30 +01:00
|
|
|
if (recalculateImages) {
|
2017-01-11 18:52:09 +01:00
|
|
|
auto &image = word.getImage();
|
2017-01-11 01:08:20 +01:00
|
|
|
|
|
|
|
qreal w = image.width();
|
|
|
|
qreal h = image.height();
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
if (AppSettings::scaleEmotesByLineHeight()) {
|
|
|
|
word.setSize(
|
|
|
|
w * mediumTextLineHeight / h *
|
|
|
|
AppSettings::emoteScale(),
|
|
|
|
mediumTextLineHeight * AppSettings::emoteScale());
|
|
|
|
} else {
|
|
|
|
word.setSize(
|
|
|
|
w * image.scale() * AppSettings::emoteScale(),
|
|
|
|
h * image.scale() * AppSettings::emoteScale());
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-11 18:52:09 +01:00
|
|
|
} else {
|
2017-01-15 16:38:30 +01:00
|
|
|
if (recalculateText) {
|
2017-01-11 18:52:09 +01:00
|
|
|
QFontMetrics &metrics = word.getFontMetrics();
|
|
|
|
word.setSize(metrics.width(word.getText()),
|
|
|
|
metrics.height());
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
if (!redraw) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
int x = MARGIN_LEFT;
|
|
|
|
int y = MARGIN_TOP;
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
int right = width - MARGIN_RIGHT - MARGIN_LEFT;
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-15 16:38:30 +01:00
|
|
|
std::vector<WordPart> *parts = new std::vector<WordPart>();
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-15 16:38:30 +01:00
|
|
|
int lineStart = 0;
|
2017-01-13 18:59:11 +01:00
|
|
|
int lineHeight = 0;
|
2017-01-15 16:38:30 +01:00
|
|
|
bool first = true;
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
auto alignParts = [&lineStart, &lineHeight, &parts, this] {
|
2017-01-15 16:38:30 +01:00
|
|
|
for (int i = lineStart; i < parts->size(); i++) {
|
|
|
|
WordPart &wordPart2 = parts->at(i);
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
wordPart2.setY(wordPart2.y() + lineHeight);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int flags = AppSettings::wordTypeMask();
|
|
|
|
|
|
|
|
for (auto it = m_words.begin(); it != m_words.end(); ++it) {
|
|
|
|
Word &word = *it;
|
|
|
|
|
|
|
|
if ((word.type() & flags) == Word::None) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xOffset = 0, yOffset = 0;
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
if (enableEmoteMargins) {
|
|
|
|
if (word.isImage() && word.getImage().isHat()) {
|
|
|
|
xOffset = -word.width() + 2;
|
|
|
|
} else {
|
|
|
|
xOffset = word.xOffset();
|
|
|
|
yOffset = word.yOffset();
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
2017-01-13 18:59:11 +01:00
|
|
|
}
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-15 16:38:30 +01:00
|
|
|
// word wrapping
|
|
|
|
if (word.isText() && word.width() + MARGIN_LEFT > right) {
|
|
|
|
alignParts();
|
|
|
|
|
|
|
|
y += lineHeight;
|
|
|
|
|
|
|
|
const QString &text = word.getText();
|
|
|
|
|
|
|
|
int start = 0;
|
|
|
|
QFontMetrics &metrics = word.getFontMetrics();
|
|
|
|
|
|
|
|
int width = 0;
|
|
|
|
|
|
|
|
std::vector<short> &charWidths = word.characterWidthCache();
|
|
|
|
|
|
|
|
if (charWidths.size() == 0) {
|
|
|
|
charWidths.reserve(text.length());
|
|
|
|
|
|
|
|
for (int i = 0; i < text.length(); i++) {
|
|
|
|
charWidths.push_back(metrics.charWidth(text, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
parts->push_back(WordPart(word, MARGIN_LEFT, y, width,
|
|
|
|
word.height(), mid, mid));
|
|
|
|
|
|
|
|
y += metrics.height();
|
|
|
|
|
|
|
|
start = i - 1;
|
|
|
|
|
|
|
|
width = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString mid(text.mid(start));
|
|
|
|
width = metrics.width(mid);
|
|
|
|
|
|
|
|
parts->push_back(WordPart(word, MARGIN_LEFT, y - word.height(),
|
|
|
|
width, word.height(), mid, mid));
|
|
|
|
x = width + MARGIN_LEFT + spaceWidth;
|
|
|
|
|
|
|
|
lineHeight = word.height();
|
|
|
|
|
|
|
|
lineStart = parts->size() - 1;
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
// fits in the line
|
2017-01-15 16:38:30 +01:00
|
|
|
else if (first || x + word.width() + xOffset <= right) {
|
2017-01-13 18:59:11 +01:00
|
|
|
parts->push_back(
|
|
|
|
WordPart(word, x, y - word.height(), word.copyText()));
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
x += word.width() + xOffset;
|
2017-01-15 16:38:30 +01:00
|
|
|
x += spaceWidth;
|
2017-01-13 18:59:11 +01:00
|
|
|
|
|
|
|
lineHeight = std::max(word.height(), lineHeight);
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-15 16:38:30 +01:00
|
|
|
first = false;
|
|
|
|
}
|
2017-01-13 18:59:11 +01:00
|
|
|
|
|
|
|
// doesn't fit in the line
|
|
|
|
else {
|
|
|
|
alignParts();
|
|
|
|
|
|
|
|
y += lineHeight;
|
|
|
|
|
|
|
|
parts->push_back(WordPart(word, MARGIN_LEFT, y - word.height(),
|
|
|
|
word.copyText()));
|
|
|
|
|
2017-01-15 16:38:30 +01:00
|
|
|
lineStart = parts->size() - 1;
|
2017-01-11 01:08:20 +01:00
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
lineHeight = word.height();
|
|
|
|
|
2017-01-15 16:38:30 +01:00
|
|
|
x = word.width() + MARGIN_LEFT;
|
|
|
|
x += spaceWidth;
|
2017-01-13 18:59:11 +01:00
|
|
|
}
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 18:59:11 +01:00
|
|
|
alignParts();
|
|
|
|
|
|
|
|
auto tmp = m_wordParts;
|
|
|
|
m_wordParts = parts;
|
|
|
|
delete tmp;
|
|
|
|
|
|
|
|
m_height = y + lineHeight;
|
|
|
|
|
|
|
|
return true;
|
2017-01-11 01:08:20 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
bool
|
|
|
|
Message::sortTwitchEmotes(const std::pair<long int, LazyLoadedImage *> &a,
|
|
|
|
const std::pair<long int, LazyLoadedImage *> &b)
|
2017-01-06 23:28:48 +01:00
|
|
|
{
|
|
|
|
return a.first < b.first;
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
2017-01-07 20:43:55 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
QString
|
|
|
|
Message::matchLink(const QString &string)
|
2017-01-07 20:43:55 +01:00
|
|
|
{
|
2017-01-15 17:21:28 +01:00
|
|
|
// TODO: Implement this xD
|
2017-01-07 20:43:55 +01:00
|
|
|
return QString();
|
|
|
|
}
|