added namespaces
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#include "messages/lazyloadedimage.h"
|
||||
|
||||
#include "asyncexec.h"
|
||||
#include "emotes.h"
|
||||
#include "ircmanager.h"
|
||||
#include "windows.h"
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <functional>
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale,
|
||||
const QString &name, const QString &tooltip,
|
||||
const QMargins &margin, bool isHat)
|
||||
: pixmap(NULL)
|
||||
, url(url)
|
||||
, name(name)
|
||||
, tooltip(tooltip)
|
||||
, animated(false)
|
||||
, margin(margin)
|
||||
, ishat(isHat)
|
||||
, scale(scale)
|
||||
, isLoading(false)
|
||||
{
|
||||
}
|
||||
|
||||
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale,
|
||||
const QString &name, const QString &tooltip,
|
||||
const QMargins &margin, bool isHat)
|
||||
: pixmap(image)
|
||||
, url()
|
||||
, name(name)
|
||||
, tooltip(tooltip)
|
||||
, animated(false)
|
||||
, margin(margin)
|
||||
, ishat(isHat)
|
||||
, scale(scale)
|
||||
, isLoading(true)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
LazyLoadedImage::loadImage()
|
||||
{
|
||||
// QThreadPool::globalInstance()->start(new LambdaQRunnable([=] {
|
||||
QUrl url(this->url);
|
||||
QNetworkRequest request(url);
|
||||
|
||||
QNetworkReply *reply = IrcManager::getAccessManager().get(request);
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||
QPixmap *pixmap = new QPixmap();
|
||||
pixmap->loadFromData(reply->readAll());
|
||||
|
||||
if (pixmap->isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this->pixmap = pixmap;
|
||||
Emotes::incGeneration();
|
||||
Windows::layoutVisibleChatWidgets();
|
||||
});
|
||||
//}));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
#ifndef LAZYLOADEDIMAGE_H
|
||||
#define LAZYLOADEDIMAGE_H
|
||||
|
||||
#include <QPixmap>
|
||||
#include <QString>
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
class LazyLoadedImage
|
||||
{
|
||||
public:
|
||||
explicit LazyLoadedImage(const QString &url, qreal scale = 1,
|
||||
const QString &getName = "",
|
||||
const QString &getTooltip = "",
|
||||
const QMargins &getMargin = QMargins(),
|
||||
bool getIsHat = false);
|
||||
explicit LazyLoadedImage(QPixmap *pixmap, qreal scale = 1,
|
||||
const QString &getName = "",
|
||||
const QString &getTooltip = "",
|
||||
const QMargins &getMargin = QMargins(),
|
||||
bool getIsHat = false);
|
||||
|
||||
const QPixmap *
|
||||
getPixmap()
|
||||
{
|
||||
if (!isLoading) {
|
||||
isLoading = true;
|
||||
|
||||
loadImage();
|
||||
}
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
qreal
|
||||
getScale() const
|
||||
{
|
||||
return scale;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getUrl() const
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getTooltip() const
|
||||
{
|
||||
return tooltip;
|
||||
}
|
||||
|
||||
const QMargins &
|
||||
getMargin() const
|
||||
{
|
||||
return margin;
|
||||
}
|
||||
|
||||
bool
|
||||
getAnimated() const
|
||||
{
|
||||
return animated;
|
||||
}
|
||||
|
||||
bool
|
||||
getIsHat() const
|
||||
{
|
||||
return ishat;
|
||||
}
|
||||
|
||||
int
|
||||
getWidth() const
|
||||
{
|
||||
if (pixmap == NULL) {
|
||||
return 16;
|
||||
}
|
||||
return pixmap->width();
|
||||
}
|
||||
|
||||
int
|
||||
getHeight() const
|
||||
{
|
||||
if (pixmap == NULL) {
|
||||
return 16;
|
||||
}
|
||||
return pixmap->height();
|
||||
}
|
||||
|
||||
private:
|
||||
QPixmap *pixmap;
|
||||
|
||||
QString url;
|
||||
QString name;
|
||||
QString tooltip;
|
||||
bool animated;
|
||||
QMargins margin;
|
||||
bool ishat;
|
||||
qreal scale;
|
||||
|
||||
bool isLoading;
|
||||
|
||||
void loadImage();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // LAZYLOADEDIMAGE_H
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "messages/link.h"
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
Link::Link()
|
||||
: type(None)
|
||||
, value(QString())
|
||||
{
|
||||
}
|
||||
|
||||
Link::Link(Type type, const QString &value)
|
||||
: type(type)
|
||||
, value(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef LINK_H
|
||||
#define LINK_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
class Link
|
||||
{
|
||||
public:
|
||||
enum Type {
|
||||
None,
|
||||
Url,
|
||||
CloseCurrentSplit,
|
||||
UserInfo,
|
||||
UserTimeout,
|
||||
UserBan,
|
||||
InsertText,
|
||||
ShowMessage,
|
||||
};
|
||||
|
||||
Link();
|
||||
Link(Type getType, const QString &getValue);
|
||||
|
||||
bool
|
||||
getIsValid()
|
||||
{
|
||||
return type == None;
|
||||
}
|
||||
|
||||
Type
|
||||
getType()
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
private:
|
||||
Type type;
|
||||
QString value;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // LINK_H
|
||||
@@ -0,0 +1,605 @@
|
||||
#include "messages/message.h"
|
||||
#include "colorscheme.h"
|
||||
#include "emojis.h"
|
||||
#include "emotes.h"
|
||||
#include "fonts.h"
|
||||
#include "ircmanager.h"
|
||||
#include "messages/link.h"
|
||||
#include "qcolor.h"
|
||||
#include "resources.h"
|
||||
#include "settings/settings.h"
|
||||
|
||||
#include <QStringList>
|
||||
#include <ctime>
|
||||
#include <list>
|
||||
#include <tuple>
|
||||
|
||||
#define MARGIN_LEFT 8
|
||||
#define MARGIN_RIGHT 8
|
||||
#define MARGIN_TOP 8
|
||||
#define MARGIN_BOTTOM 8
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
QRegularExpression *Message::cheerRegex =
|
||||
new QRegularExpression("cheer[1-9][0-9]*");
|
||||
|
||||
Message::Message(const QString &text)
|
||||
: wordParts(new std::vector<WordPart>())
|
||||
{
|
||||
}
|
||||
|
||||
Message::Message(const IrcPrivateMessage &ircMessage, const Channel &channel,
|
||||
bool enablePingSound, bool isReceivedWhisper,
|
||||
bool isSentWhisper, bool includeChannel)
|
||||
: wordParts(new std::vector<WordPart>())
|
||||
{
|
||||
this->parseTime = std::chrono::system_clock::now();
|
||||
|
||||
auto words = std::vector<Word>();
|
||||
|
||||
auto tags = ircMessage.tags();
|
||||
|
||||
auto iterator = tags.find("id");
|
||||
|
||||
if (iterator != tags.end()) {
|
||||
this->id = iterator.value().toString();
|
||||
}
|
||||
|
||||
// timestamps
|
||||
iterator = tags.find("tmi-sent-ts");
|
||||
|
||||
std::time_t time = std::time(NULL);
|
||||
|
||||
// if (iterator != tags.end()) {
|
||||
// time = strtoll(iterator.value().toString().toStdString().c_str(),
|
||||
// NULL,
|
||||
// 10);
|
||||
// }
|
||||
|
||||
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);
|
||||
|
||||
words.push_back(Word(timestamp, Word::TimestampNoSeconds,
|
||||
ColorScheme::instance().SystemMessageColor, QString(),
|
||||
QString()));
|
||||
words.push_back(Word(timestampWithSeconds, Word::TimestampWithSeconds,
|
||||
ColorScheme::instance().SystemMessageColor, QString(),
|
||||
QString()));
|
||||
|
||||
// mod buttons
|
||||
static QString buttonBanTooltip("Ban user");
|
||||
static QString buttonTimeoutTooltip("Timeout user");
|
||||
|
||||
words.push_back(Word(Resources::getButtonBan(), Word::ButtonBan, QString(),
|
||||
buttonBanTooltip,
|
||||
Link(Link::UserBan, ircMessage.account())));
|
||||
words.push_back(Word(Resources::getButtonTimeout(), Word::ButtonTimeout,
|
||||
QString(), buttonTimeoutTooltip,
|
||||
Link(Link::UserTimeout, ircMessage.account())));
|
||||
|
||||
// badges
|
||||
iterator = tags.find("badges");
|
||||
|
||||
if (iterator != tags.end()) {
|
||||
auto badges = iterator.value().toString().split(',');
|
||||
|
||||
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") {
|
||||
words.push_back(Word(Resources::getBadgeStaff(),
|
||||
Word::BadgeStaff, QString(),
|
||||
QString("Twitch Staff")));
|
||||
} else if (badge == "admin/1") {
|
||||
words.push_back(Word(Resources::getBadgeAdmin(),
|
||||
Word::BadgeAdmin, QString(),
|
||||
QString("Twitch Admin")));
|
||||
} else if (badge == "global_mod/1") {
|
||||
words.push_back(Word(Resources::getBadgeGlobalmod(),
|
||||
Word::BadgeGlobalMod, QString(),
|
||||
QString("Global Moderator")));
|
||||
} else if (badge == "moderator/1") {
|
||||
// TODO: implement this xD
|
||||
words.push_back(Word(
|
||||
Resources::getBadgeTurbo(), Word::BadgeModerator, QString(),
|
||||
QString("Channel Moderator"))); // custom badge
|
||||
} else if (badge == "turbo/1") {
|
||||
words.push_back(Word(Resources::getBadgeStaff(),
|
||||
Word::BadgeTurbo, QString(),
|
||||
QString("Turbo Subscriber")));
|
||||
} else if (badge == "broadcaster/1") {
|
||||
words.push_back(Word(Resources::getBadgeBroadcaster(),
|
||||
Word::BadgeBroadcaster, QString(),
|
||||
QString("Channel Broadcaster")));
|
||||
} else if (badge == "premium/1") {
|
||||
words.push_back(Word(Resources::getBadgePremium(),
|
||||
Word::BadgePremium, QString(),
|
||||
QString("Twitch Prime")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// color
|
||||
QColor usernameColor = ColorScheme::instance().SystemMessageColor;
|
||||
|
||||
iterator = tags.find("color");
|
||||
if (iterator != tags.end()) {
|
||||
usernameColor = QColor(iterator.value().toString());
|
||||
}
|
||||
|
||||
// channel name
|
||||
if (includeChannel) {
|
||||
QString channelName("#" + channel.getName());
|
||||
words.push_back(Word(
|
||||
channelName, Word::Misc, ColorScheme::instance().SystemMessageColor,
|
||||
QString(channelName), QString(),
|
||||
Link(Link::Url, channel.getName() + "\n" + this->id)));
|
||||
}
|
||||
|
||||
// username
|
||||
this->userName = ircMessage.account();
|
||||
|
||||
if (this->userName.isEmpty()) {
|
||||
auto iterator = tags.find("login");
|
||||
|
||||
if (iterator != tags.end()) {
|
||||
this->userName = iterator.value().toString();
|
||||
}
|
||||
}
|
||||
|
||||
QString displayName;
|
||||
|
||||
iterator = tags.find("display-name");
|
||||
if (iterator == tags.end()) {
|
||||
displayName = ircMessage.account();
|
||||
} else {
|
||||
displayName = iterator.value().toString();
|
||||
}
|
||||
|
||||
bool hasLocalizedName =
|
||||
QString::compare(displayName, ircMessage.account()) == 0;
|
||||
QString userDisplayString =
|
||||
displayName +
|
||||
(hasLocalizedName ? (" (" + ircMessage.account() + ")") : QString());
|
||||
|
||||
if (isSentWhisper) {
|
||||
userDisplayString = IrcManager::account->getUsername() + " -> ";
|
||||
}
|
||||
|
||||
if (isReceivedWhisper) {
|
||||
userDisplayString += " -> " + IrcManager::account->getUsername();
|
||||
}
|
||||
|
||||
if (!ircMessage.isAction()) {
|
||||
userDisplayString += ": ";
|
||||
}
|
||||
|
||||
words.push_back(Word(userDisplayString, Word::Username, usernameColor,
|
||||
userDisplayString, QString()));
|
||||
|
||||
// highlights
|
||||
// TODO: implement this xD
|
||||
|
||||
// bits
|
||||
QString bits = "";
|
||||
|
||||
iterator = tags.find("bits");
|
||||
if (iterator != tags.end()) {
|
||||
bits = iterator.value().toString();
|
||||
}
|
||||
|
||||
// twitch emotes
|
||||
std::vector<std::pair<long int, LazyLoadedImage *>> twitchEmotes;
|
||||
|
||||
iterator = tags.find("emotes");
|
||||
|
||||
if (iterator != tags.end()) {
|
||||
auto emotes = iterator.value().toString().split('/');
|
||||
|
||||
for (QString emote : emotes) {
|
||||
if (!emote.contains(':'))
|
||||
continue;
|
||||
|
||||
QStringList parameters = emote.split(':');
|
||||
|
||||
if (parameters.length() < 2)
|
||||
continue;
|
||||
|
||||
long int id = std::stol(parameters.at(0).toStdString(), NULL, 10);
|
||||
|
||||
QStringList occurences = parameters.at(1).split(',');
|
||||
|
||||
for (QString occurence : occurences) {
|
||||
QStringList coords = occurence.split('-');
|
||||
|
||||
if (coords.length() < 2)
|
||||
continue;
|
||||
|
||||
long int start =
|
||||
std::stol(coords.at(0).toStdString(), NULL, 10);
|
||||
long int end = std::stol(coords.at(1).toStdString(), NULL, 10);
|
||||
|
||||
if (start >= end || start < 0 ||
|
||||
end > ircMessage.content().length())
|
||||
continue;
|
||||
|
||||
QString name = ircMessage.content().mid(start, end - start);
|
||||
|
||||
twitchEmotes.push_back(std::pair<long int, LazyLoadedImage *>(
|
||||
start, Emotes::getTwitchEmoteById(name, id)));
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(twitchEmotes.begin(), twitchEmotes.end(), sortTwitchEmotes);
|
||||
}
|
||||
|
||||
auto currentTwitchEmote = twitchEmotes.begin();
|
||||
|
||||
// words
|
||||
QColor textColor =
|
||||
ircMessage.isAction() ? usernameColor : ColorScheme::instance().Text;
|
||||
|
||||
QStringList splits = ircMessage.content().split(' ');
|
||||
|
||||
long int i = 0;
|
||||
|
||||
for (QString split : splits) {
|
||||
// twitch emote
|
||||
if (currentTwitchEmote != twitchEmotes.end() &&
|
||||
currentTwitchEmote->first == i) {
|
||||
words.push_back(Word(currentTwitchEmote->second,
|
||||
Word::TwitchEmoteImage,
|
||||
currentTwitchEmote->second->getName(),
|
||||
currentTwitchEmote->second->getName() +
|
||||
QString("\nTwitch Emote")));
|
||||
words.push_back(Word(currentTwitchEmote->second->getName(),
|
||||
Word::TwitchEmoteText, textColor,
|
||||
currentTwitchEmote->second->getName(),
|
||||
currentTwitchEmote->second->getName() +
|
||||
QString("\nTwitch Emote")));
|
||||
|
||||
i += split.length() + 1;
|
||||
currentTwitchEmote = std::next(currentTwitchEmote);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// split words
|
||||
std::vector<std::tuple<LazyLoadedImage *, QString>> parsed;
|
||||
|
||||
Emojis::parseEmojis(parsed, split);
|
||||
for (const std::tuple<LazyLoadedImage *, QString> &tuple : parsed) {
|
||||
LazyLoadedImage *image = std::get<0>(tuple);
|
||||
|
||||
if (image == NULL) { // is text
|
||||
QString string = std::get<1>(tuple);
|
||||
|
||||
// cheers
|
||||
if (!bits.isEmpty() && string.length() >= 6 &&
|
||||
cheerRegex->match(string).isValid()) {
|
||||
auto cheer = string.mid(5).toInt();
|
||||
|
||||
QString color;
|
||||
|
||||
QColor bitsColor;
|
||||
|
||||
if (cheer >= 10000) {
|
||||
color = "red";
|
||||
bitsColor = QColor::fromHslF(0, 1, 0.5);
|
||||
} else if (cheer >= 5000) {
|
||||
color = "blue";
|
||||
bitsColor = QColor::fromHslF(0.61, 1, 0.4);
|
||||
} else if (cheer >= 1000) {
|
||||
color = "green";
|
||||
bitsColor = QColor::fromHslF(0.5, 1, 0.5);
|
||||
} else if (cheer >= 100) {
|
||||
color = "purple";
|
||||
bitsColor = QColor::fromHslF(0.8, 1, 0.5);
|
||||
} else {
|
||||
color = "gray";
|
||||
bitsColor = QColor::fromHslF(0.5f, 0.5f, 0.5f);
|
||||
}
|
||||
|
||||
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::getMiscImageFromCache().getOrAdd(
|
||||
bitsLinkAnimated, [&bitsLinkAnimated] {
|
||||
return new LazyLoadedImage(bitsLinkAnimated);
|
||||
});
|
||||
LazyLoadedImage *image =
|
||||
Emotes::getMiscImageFromCache().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"))));
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// bttv / ffz emotes
|
||||
LazyLoadedImage *bttvEmote;
|
||||
|
||||
// TODO: Implement this (ignored emotes)
|
||||
if (Emotes::getBttvEmotes().tryGet(string, bttvEmote) ||
|
||||
channel.getBttvChannelEmotes().tryGet(string, bttvEmote) ||
|
||||
Emotes::getFfzEmotes().tryGet(string, bttvEmote) ||
|
||||
channel.getFfzChannelEmotes().tryGet(string, bttvEmote) ||
|
||||
Emotes::getChatterinoEmotes().tryGet(string, bttvEmote)) {
|
||||
words.push_back(Word(bttvEmote, Word::BttvEmoteImage,
|
||||
bttvEmote->getName(),
|
||||
bttvEmote->getTooltip(),
|
||||
Link(Link::Url, bttvEmote->getUrl())));
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// actually just a word
|
||||
QString link = matchLink(string);
|
||||
|
||||
words.push_back(
|
||||
Word(string, Word::Text, textColor, string, QString(),
|
||||
link.isEmpty() ? Link() : Link(Link::Url, link)));
|
||||
} else { // is emoji
|
||||
static QString emojiTooltip("Emoji");
|
||||
|
||||
words.push_back(Word(image, Word::EmojiImage, image->getName(),
|
||||
emojiTooltip));
|
||||
Word(image->getName(), Word::EmojiText, textColor,
|
||||
image->getName(), emojiTooltip);
|
||||
}
|
||||
}
|
||||
|
||||
i += split.length() + 1;
|
||||
}
|
||||
|
||||
this->words = words;
|
||||
|
||||
// TODO: Implement this xD
|
||||
// if (!isReceivedWhisper &&
|
||||
// AppSettings.HighlightIgnoredUsers.ContainsKey(Username))
|
||||
// {
|
||||
// HighlightTab = false;
|
||||
// }
|
||||
}
|
||||
|
||||
bool
|
||||
Message::layout(int width, bool enableEmoteMargins)
|
||||
{
|
||||
width = width - (width % 2);
|
||||
|
||||
int mediumTextLineHeight = Fonts::getFontMetrics(Fonts::Medium).height();
|
||||
int spaceWidth = 4;
|
||||
|
||||
bool redraw = width != this->currentLayoutWidth || this->relayoutRequested;
|
||||
|
||||
bool recalculateImages = this->emoteGeneration != Emotes::getGeneration();
|
||||
bool recalculateText = this->fontGeneration != Fonts::getGeneration();
|
||||
|
||||
if (recalculateImages || recalculateText) {
|
||||
this->emoteGeneration = Emotes::getGeneration();
|
||||
this->fontGeneration = Fonts::getGeneration();
|
||||
|
||||
redraw = true;
|
||||
|
||||
for (auto &word : this->words) {
|
||||
if (word.isImage()) {
|
||||
if (recalculateImages) {
|
||||
auto &image = word.getImage();
|
||||
|
||||
qreal w = image.getWidth();
|
||||
qreal h = image.getHeight();
|
||||
|
||||
if (settings::Settings::getScaleEmotesByLineHeight()) {
|
||||
word.setSize(w * mediumTextLineHeight / h *
|
||||
settings::Settings::getEmoteScale(),
|
||||
mediumTextLineHeight *
|
||||
settings::Settings::getEmoteScale());
|
||||
} else {
|
||||
word.setSize(w * image.getScale() *
|
||||
settings::Settings::getEmoteScale(),
|
||||
h * image.getScale() *
|
||||
settings::Settings::getEmoteScale());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (recalculateText) {
|
||||
QFontMetrics &metrics = word.getFontMetrics();
|
||||
word.setSize(metrics.width(word.getText()),
|
||||
metrics.height());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!redraw) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int x = MARGIN_LEFT;
|
||||
int y = MARGIN_TOP;
|
||||
|
||||
int right = width - MARGIN_RIGHT - MARGIN_LEFT;
|
||||
|
||||
std::vector<WordPart> *parts = new std::vector<WordPart>();
|
||||
|
||||
int lineStart = 0;
|
||||
int lineHeight = 0;
|
||||
bool first = true;
|
||||
|
||||
auto alignParts = [&lineStart, &lineHeight, &parts, this] {
|
||||
for (size_t i = lineStart; i < parts->size(); i++) {
|
||||
WordPart &wordPart2 = parts->at(i);
|
||||
|
||||
wordPart2.setY(wordPart2.getY() + lineHeight);
|
||||
}
|
||||
};
|
||||
|
||||
int flags = settings::Settings::getWordTypeMask();
|
||||
|
||||
for (auto it = this->words.begin(); it != this->words.end(); ++it) {
|
||||
Word &word = *it;
|
||||
|
||||
if ((word.getType() & flags) == Word::None) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int xOffset = 0, yOffset = 0;
|
||||
|
||||
if (enableEmoteMargins) {
|
||||
if (word.isImage() && word.getImage().getIsHat()) {
|
||||
xOffset = -word.getWidth() + 2;
|
||||
} else {
|
||||
xOffset = word.getXOffset();
|
||||
yOffset = word.getYOffset();
|
||||
}
|
||||
}
|
||||
|
||||
// word wrapping
|
||||
if (word.isText() && word.getWidth() + 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.getCharacterWidthCache();
|
||||
|
||||
if (charWidths.size() == 0) {
|
||||
charWidths.reserve(text.length());
|
||||
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
charWidths.push_back(metrics.width(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.getHeight(), 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.getHeight(),
|
||||
width, word.getHeight(), mid, mid));
|
||||
x = width + MARGIN_LEFT + spaceWidth;
|
||||
|
||||
lineHeight = word.getHeight();
|
||||
|
||||
lineStart = parts->size() - 1;
|
||||
|
||||
first = false;
|
||||
}
|
||||
|
||||
// fits in the line
|
||||
else if (first || x + word.getWidth() + xOffset <= right) {
|
||||
parts->push_back(
|
||||
WordPart(word, x, y - word.getHeight(), word.getCopyText()));
|
||||
|
||||
x += word.getWidth() + xOffset;
|
||||
x += spaceWidth;
|
||||
|
||||
lineHeight = std::max(word.getHeight(), lineHeight);
|
||||
|
||||
first = false;
|
||||
}
|
||||
|
||||
// doesn't fit in the line
|
||||
else {
|
||||
alignParts();
|
||||
|
||||
y += lineHeight;
|
||||
|
||||
parts->push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(),
|
||||
word.getCopyText()));
|
||||
|
||||
lineStart = parts->size() - 1;
|
||||
|
||||
lineHeight = word.getHeight();
|
||||
|
||||
x = word.getWidth() + MARGIN_LEFT;
|
||||
x += spaceWidth;
|
||||
}
|
||||
}
|
||||
|
||||
alignParts();
|
||||
|
||||
auto tmp = this->wordParts;
|
||||
this->wordParts = parts;
|
||||
delete tmp;
|
||||
|
||||
this->height = y + lineHeight;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
Message::sortTwitchEmotes(const std::pair<long int, LazyLoadedImage *> &a,
|
||||
const std::pair<long int, LazyLoadedImage *> &b)
|
||||
{
|
||||
return a.first < b.first;
|
||||
}
|
||||
|
||||
QString
|
||||
Message::matchLink(const QString &string)
|
||||
{
|
||||
// TODO: Implement this xD
|
||||
return QString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
#ifndef MESSAGE_H
|
||||
#define MESSAGE_H
|
||||
|
||||
#include "channel.h"
|
||||
#include "messages/word.h"
|
||||
#include "messages/wordpart.h"
|
||||
|
||||
#include <IrcMessage>
|
||||
#include <QVector>
|
||||
#include <chrono>
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
class Message
|
||||
{
|
||||
public:
|
||||
Message(const QString &text);
|
||||
Message(const IrcPrivateMessage &ircMessage, const Channel &Channel,
|
||||
bool enablePingSound = true, bool isReceivedWhisper = false,
|
||||
bool isSentWhisper = false, bool includeChannel = false);
|
||||
|
||||
~Message()
|
||||
{
|
||||
if (wordParts != NULL) {
|
||||
delete wordParts;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
getCanHighlightTab() const
|
||||
{
|
||||
return highlightTab;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getTimeoutUser() const
|
||||
{
|
||||
return timeoutUser;
|
||||
}
|
||||
|
||||
int
|
||||
getTimeoutCount() const
|
||||
{
|
||||
return timeoutCount;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getUserName() const
|
||||
{
|
||||
return userName;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getDisplayName() const
|
||||
{
|
||||
return displayName;
|
||||
}
|
||||
|
||||
const std::vector<Word>
|
||||
getWords() const
|
||||
{
|
||||
return words;
|
||||
}
|
||||
|
||||
const std::vector<WordPart>
|
||||
getWordParts() const
|
||||
{
|
||||
return *wordParts;
|
||||
}
|
||||
|
||||
bool
|
||||
getDisabled() const
|
||||
{
|
||||
return disabled;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getId() const
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
int
|
||||
getHeight() const
|
||||
{
|
||||
return height;
|
||||
}
|
||||
|
||||
bool layout(int width, bool enableEmoteMargins = true);
|
||||
|
||||
void
|
||||
requestRelayout()
|
||||
{
|
||||
relayoutRequested = true;
|
||||
}
|
||||
|
||||
private:
|
||||
static LazyLoadedImage *badgeStaff;
|
||||
static LazyLoadedImage *badgeAdmin;
|
||||
static LazyLoadedImage *badgeGlobalmod;
|
||||
static LazyLoadedImage *badgeModerator;
|
||||
static LazyLoadedImage *badgeTurbo;
|
||||
static LazyLoadedImage *badgeBroadcaster;
|
||||
static LazyLoadedImage *badgePremium;
|
||||
|
||||
static QRegularExpression *cheerRegex;
|
||||
|
||||
bool highlightTab = false;
|
||||
QString timeoutUser = "";
|
||||
int timeoutCount = 0;
|
||||
bool disabled = false;
|
||||
std::chrono::time_point<std::chrono::system_clock> parseTime;
|
||||
|
||||
QString userName = "";
|
||||
QString displayName = "";
|
||||
QString id = "";
|
||||
|
||||
int height = 0;
|
||||
|
||||
std::vector<Word> words;
|
||||
std::vector<WordPart> *wordParts;
|
||||
|
||||
long currentLayoutWidth = -1;
|
||||
bool relayoutRequested = true;
|
||||
int fontGeneration = -1;
|
||||
int emoteGeneration = -1;
|
||||
|
||||
static QString matchLink(const QString &string);
|
||||
|
||||
static bool sortTwitchEmotes(
|
||||
const std::pair<long int, LazyLoadedImage *> &a,
|
||||
const std::pair<long int, LazyLoadedImage *> &b);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // MESSAGE_H
|
||||
@@ -0,0 +1,37 @@
|
||||
#include "messages/word.h"
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
// Image word
|
||||
Word::Word(LazyLoadedImage *image, Type type, const QString ©text,
|
||||
const QString &tooltip, const Link &link)
|
||||
: image(image)
|
||||
, text()
|
||||
, color()
|
||||
, _isImage(true)
|
||||
, type(type)
|
||||
, copyText(copytext)
|
||||
, tooltip(tooltip)
|
||||
, link(link)
|
||||
, characterWidthCache()
|
||||
{
|
||||
image->getWidth(); // professional segfault test
|
||||
}
|
||||
|
||||
// Text word
|
||||
Word::Word(const QString &text, Type type, const QColor &color,
|
||||
const QString ©text, const QString &tooltip, const Link &link)
|
||||
: image(NULL)
|
||||
, text(text)
|
||||
, color(color)
|
||||
, _isImage(false)
|
||||
, type(type)
|
||||
, copyText(copytext)
|
||||
, tooltip(tooltip)
|
||||
, link(link)
|
||||
, characterWidthCache()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
+214
@@ -0,0 +1,214 @@
|
||||
#ifndef WORD_H
|
||||
#define WORD_H
|
||||
|
||||
#include "fonts.h"
|
||||
#include "messages/lazyloadedimage.h"
|
||||
#include "messages/link.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <QRect>
|
||||
#include <QString>
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
class Word
|
||||
{
|
||||
public:
|
||||
enum Type : uint32_t {
|
||||
None = 0,
|
||||
Misc = (1 << 0),
|
||||
Text = (1 << 1),
|
||||
|
||||
TimestampNoSeconds = (1 << 2),
|
||||
TimestampWithSeconds = (1 << 3),
|
||||
|
||||
TwitchEmoteImage = (1 << 4),
|
||||
TwitchEmoteText = (1 << 5),
|
||||
BttvEmoteImage = (1 << 6),
|
||||
BttvEmoteText = (1 << 7),
|
||||
BttvGifEmoteImage = (1 << 8),
|
||||
BttvGifEmoteText = (1 << 9),
|
||||
FfzEmoteImage = (1 << 10),
|
||||
FfzEmoteText = (1 << 11),
|
||||
EmoteImages = TwitchEmoteImage | BttvEmoteImage | BttvGifEmoteImage |
|
||||
FfzEmoteImage,
|
||||
|
||||
Bits = (1 << 12),
|
||||
BitsAnimated = (1 << 13),
|
||||
|
||||
BadgeStaff = (1 << 14),
|
||||
BadgeAdmin = (1 << 15),
|
||||
BadgeGlobalMod = (1 << 16),
|
||||
BadgeModerator = (1 << 17),
|
||||
BadgeTurbo = (1 << 18),
|
||||
BadgeBroadcaster = (1 << 19),
|
||||
BadgePremium = (1 << 20),
|
||||
BadgeChatterino = (1 << 21),
|
||||
BadgeCheer = (1 << 22),
|
||||
Badges = BadgeStaff | BadgeAdmin | BadgeGlobalMod | BadgeModerator |
|
||||
BadgeTurbo | BadgeBroadcaster | BadgePremium |
|
||||
BadgeChatterino | BadgeCheer,
|
||||
|
||||
Username = (1 << 23),
|
||||
BitsAmount = (1 << 24),
|
||||
|
||||
ButtonBan = (1 << 25),
|
||||
ButtonTimeout = (1 << 26),
|
||||
|
||||
EmojiImage = (1 << 27),
|
||||
EmojiText = (1 << 28),
|
||||
|
||||
Default = TimestampNoSeconds | Badges | Username | Bits |
|
||||
FfzEmoteImage | BttvEmoteImage | BttvGifEmoteImage |
|
||||
TwitchEmoteImage | BitsAmount | Text | ButtonBan |
|
||||
ButtonTimeout
|
||||
};
|
||||
|
||||
explicit Word(LazyLoadedImage *image, Type getType, const QString ©text,
|
||||
const QString &getTooltip, const Link &getLink = Link());
|
||||
explicit Word(const QString &text, Type getType, const QColor &getColor,
|
||||
const QString ©text, const QString &getTooltip,
|
||||
const Link &getLink = Link());
|
||||
|
||||
LazyLoadedImage &
|
||||
getImage() const
|
||||
{
|
||||
return *image;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getText() const
|
||||
{
|
||||
return this->text;
|
||||
}
|
||||
|
||||
int
|
||||
getWidth() const
|
||||
{
|
||||
return this->width;
|
||||
}
|
||||
|
||||
int
|
||||
getHeight() const
|
||||
{
|
||||
return this->height;
|
||||
}
|
||||
|
||||
void
|
||||
setSize(int width, int height)
|
||||
{
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
}
|
||||
|
||||
bool
|
||||
isImage() const
|
||||
{
|
||||
return this->_isImage;
|
||||
}
|
||||
|
||||
bool
|
||||
isText() const
|
||||
{
|
||||
return !this->_isImage;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getCopyText() const
|
||||
{
|
||||
return this->copyText;
|
||||
}
|
||||
|
||||
bool
|
||||
hasTrailingSpace() const
|
||||
{
|
||||
return this->_hasTrailingSpace;
|
||||
}
|
||||
|
||||
QFont &
|
||||
getFont() const
|
||||
{
|
||||
return Fonts::getFont(this->font);
|
||||
}
|
||||
|
||||
QFontMetrics &
|
||||
getFontMetrics() const
|
||||
{
|
||||
return Fonts::getFontMetrics(this->font);
|
||||
}
|
||||
|
||||
Type
|
||||
getType() const
|
||||
{
|
||||
return this->type;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getTooltip() const
|
||||
{
|
||||
return this->tooltip;
|
||||
}
|
||||
|
||||
const QColor &
|
||||
getColor() const
|
||||
{
|
||||
return this->color;
|
||||
}
|
||||
|
||||
const Link &
|
||||
getLink() const
|
||||
{
|
||||
return this->link;
|
||||
}
|
||||
|
||||
int
|
||||
getXOffset() const
|
||||
{
|
||||
return this->xOffset;
|
||||
}
|
||||
|
||||
int
|
||||
getYOffset() const
|
||||
{
|
||||
return this->yOffset;
|
||||
}
|
||||
|
||||
void
|
||||
setOffset(int xOffset, int yOffset)
|
||||
{
|
||||
this->xOffset = std::max(0, xOffset);
|
||||
this->yOffset = std::max(0, yOffset);
|
||||
}
|
||||
|
||||
std::vector<short> &
|
||||
getCharacterWidthCache()
|
||||
{
|
||||
return this->characterWidthCache;
|
||||
}
|
||||
|
||||
private:
|
||||
LazyLoadedImage *image;
|
||||
QString text;
|
||||
QColor color;
|
||||
bool _isImage;
|
||||
|
||||
Type type;
|
||||
QString copyText;
|
||||
QString tooltip;
|
||||
|
||||
int width = 16;
|
||||
int height = 16;
|
||||
int xOffset = 0;
|
||||
int yOffset = 0;
|
||||
|
||||
bool _hasTrailingSpace;
|
||||
Fonts::Type font = Fonts::Medium;
|
||||
Link link;
|
||||
|
||||
std::vector<short> characterWidthCache;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // WORD_H
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "messages/wordpart.h"
|
||||
#include "messages/word.h"
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
WordPart::WordPart(Word &word, int x, int y, const QString ©Text,
|
||||
bool allowTrailingSpace)
|
||||
: m_word(word)
|
||||
, copyText(copyText)
|
||||
, text(word.isText() ? m_word.getText() : QString())
|
||||
, x(x)
|
||||
, y(y)
|
||||
, width(word.getWidth())
|
||||
, height(word.getHeight())
|
||||
, _trailingSpace(word.hasTrailingSpace() & allowTrailingSpace)
|
||||
{
|
||||
}
|
||||
|
||||
WordPart::WordPart(Word &word, int x, int y, int width, int height,
|
||||
const QString ©Text, const QString &customText,
|
||||
bool allowTrailingSpace)
|
||||
: m_word(word)
|
||||
, copyText(copyText)
|
||||
, text(customText)
|
||||
, x(x)
|
||||
, y(y)
|
||||
, width(width)
|
||||
, height(height)
|
||||
, _trailingSpace(word.hasTrailingSpace() & allowTrailingSpace)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
#ifndef WORDPART_H
|
||||
#define WORDPART_H
|
||||
|
||||
#include <QRect>
|
||||
#include <QString>
|
||||
|
||||
namespace chatterino {
|
||||
namespace messages {
|
||||
|
||||
class Word;
|
||||
|
||||
class WordPart
|
||||
{
|
||||
public:
|
||||
WordPart(Word &getWord, int getX, int getY, const QString &getCopyText,
|
||||
bool allowTrailingSpace = true);
|
||||
|
||||
WordPart(Word &getWord, int getX, int getY, int getWidth, int getHeight,
|
||||
const QString &getCopyText, const QString &customText,
|
||||
bool allowTrailingSpace = true);
|
||||
|
||||
const Word &
|
||||
getWord() const
|
||||
{
|
||||
return this->m_word;
|
||||
}
|
||||
|
||||
int
|
||||
getWidth() const
|
||||
{
|
||||
return this->width;
|
||||
}
|
||||
|
||||
int
|
||||
getHeight() const
|
||||
{
|
||||
return this->height;
|
||||
}
|
||||
|
||||
int
|
||||
getX() const
|
||||
{
|
||||
return this->x;
|
||||
}
|
||||
|
||||
int
|
||||
getY() const
|
||||
{
|
||||
return this->y;
|
||||
}
|
||||
|
||||
void
|
||||
setPosition(int x, int y)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
void
|
||||
setY(int y)
|
||||
{
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
int
|
||||
getRight() const
|
||||
{
|
||||
return this->x + this->width;
|
||||
}
|
||||
|
||||
int
|
||||
getBottom() const
|
||||
{
|
||||
return this->y + this->height;
|
||||
}
|
||||
|
||||
QRect
|
||||
getRect() const
|
||||
{
|
||||
return QRect(this->x, this->y, this->width, this->height);
|
||||
}
|
||||
|
||||
const QString
|
||||
getCopyText() const
|
||||
{
|
||||
return this->copyText;
|
||||
}
|
||||
|
||||
int
|
||||
hasTrailingSpace() const
|
||||
{
|
||||
return this->_trailingSpace;
|
||||
}
|
||||
|
||||
const QString &
|
||||
getText() const
|
||||
{
|
||||
return this->text;
|
||||
}
|
||||
|
||||
private:
|
||||
Word &m_word;
|
||||
|
||||
QString copyText;
|
||||
QString text;
|
||||
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
bool _trailingSpace;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // WORDPART_H
|
||||
Reference in New Issue
Block a user