refactoring

This commit is contained in:
fourtf
2017-04-12 17:46:44 +02:00
parent 8ef492d7ae
commit 96db82e867
114 changed files with 5554 additions and 3703 deletions
+14 -21
View File
@@ -1,9 +1,9 @@
#include "messages/lazyloadedimage.h"
#include "asyncexec.h"
#include "emotes.h"
#include "emotemanager.h"
#include "ircmanager.h"
#include "windows.h"
#include "windowmanager.h"
#include <QBuffer>
#include <QImageReader>
@@ -16,9 +16,8 @@
namespace chatterino {
namespace messages {
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale,
const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale, const QString &name,
const QString &tooltip, const QMargins &margin, bool isHat)
: currentPixmap(NULL)
, allFrames()
, currentFrame(0)
@@ -34,9 +33,8 @@ LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale,
{
}
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale,
const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale, const QString &name,
const QString &tooltip, const QMargins &margin, bool isHat)
: currentPixmap(image)
, allFrames()
, currentFrame(0)
@@ -52,8 +50,7 @@ LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale,
{
}
void
LazyLoadedImage::loadImage()
void LazyLoadedImage::loadImage()
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
@@ -92,29 +89,25 @@ LazyLoadedImage::loadImage()
if (allFrames.size() > 1) {
this->animated = true;
Emotes::getGifUpdateSignal().connect([this] { gifUpdateTimout(); });
EmoteManager::getInstance().getGifUpdateSignal().connect([this] { gifUpdateTimout(); });
}
Emotes::incGeneration();
Windows::layoutVisibleChatWidgets();
EmoteManager::getInstance().incGeneration();
WindowManager::layoutVisibleChatWidgets();
reply->deleteLater();
manager->deleteLater();
});
}
void
LazyLoadedImage::gifUpdateTimout()
void LazyLoadedImage::gifUpdateTimout()
{
this->currentFrameOffset += GIF_FRAME_LENGTH;
while (true) {
if (this->currentFrameOffset >
this->allFrames.at(this->currentFrame).duration) {
this->currentFrameOffset -=
this->allFrames.at(this->currentFrame).duration;
this->currentFrame =
(this->currentFrame + 1) % this->allFrames.size();
if (this->currentFrameOffset > this->allFrames.at(this->currentFrame).duration) {
this->currentFrameOffset -= this->allFrames.at(this->currentFrame).duration;
this->currentFrame = (this->currentFrame + 1) % this->allFrames.size();
} else {
break;
}
+14 -28
View File
@@ -10,19 +10,14 @@ namespace messages {
class LazyLoadedImage : QObject
{
public:
explicit LazyLoadedImage(const QString &url, qreal scale = 1,
const QString &name = "",
const QString &tooltip = "",
const QMargins &margin = QMargins(),
explicit LazyLoadedImage(const QString &url, qreal scale = 1, const QString &name = "",
const QString &tooltip = "", const QMargins &margin = QMargins(),
bool isHat = false);
explicit LazyLoadedImage(QPixmap *currentPixmap, qreal scale = 1,
const QString &name = "",
const QString &tooltip = "",
const QMargins &margin = QMargins(),
explicit LazyLoadedImage(QPixmap *currentPixmap, qreal scale = 1, const QString &name = "",
const QString &tooltip = "", const QMargins &margin = QMargins(),
bool isHat = false);
const QPixmap *
getPixmap()
const QPixmap *getPixmap()
{
if (!isLoading) {
isLoading = true;
@@ -32,50 +27,42 @@ public:
return currentPixmap;
}
qreal
getScale() const
qreal getScale() const
{
return scale;
}
const QString &
getUrl() const
const QString &getUrl() const
{
return url;
}
const QString &
getName() const
const QString &getName() const
{
return name;
}
const QString &
getTooltip() const
const QString &getTooltip() const
{
return tooltip;
}
const QMargins &
getMargin() const
const QMargins &getMargin() const
{
return margin;
}
bool
getAnimated() const
bool getAnimated() const
{
return animated;
}
bool
getIsHat() const
bool getIsHat() const
{
return ishat;
}
int
getWidth() const
int getWidth() const
{
if (currentPixmap == NULL) {
return 16;
@@ -83,8 +70,7 @@ public:
return currentPixmap->width();
}
int
getHeight() const
int getHeight() const
{
if (currentPixmap == NULL) {
return 16;
+5 -8
View File
@@ -24,8 +24,7 @@ public:
vector->reserve(this->limit + this->buffer);
}
void
clear()
void clear()
{
std::lock_guard<std::mutex> lock(this->mutex);
@@ -37,8 +36,7 @@ public:
// return true if an item was deleted
// deleted will be set if the item was deleted
bool
appendItem(const T &item, T &deleted)
bool appendItem(const T &item, T &deleted)
{
std::lock_guard<std::mutex> lock(this->mutex);
@@ -63,7 +61,7 @@ public:
} else {
deleted = this->vector->at(this->offset);
//append item and increment offset("deleting" first element)
// append item and increment offset("deleting" first element)
this->vector->push_back(item);
this->offset++;
@@ -77,12 +75,11 @@ public:
}
}
messages::LimitedQueueSnapshot<T>
getSnapshot()
messages::LimitedQueueSnapshot<T> getSnapshot()
{
std::lock_guard<std::mutex> lock(this->mutex);
if(vector->size() < limit) {
if (vector->size() < limit) {
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->vector->size());
} else {
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->limit);
+2 -4
View File
@@ -11,16 +11,14 @@ template <typename T>
class LimitedQueueSnapshot
{
public:
LimitedQueueSnapshot(std::shared_ptr<std::vector<T>> ptr, int offset,
int length)
LimitedQueueSnapshot(std::shared_ptr<std::vector<T>> ptr, int offset, int length)
: vector(ptr)
, offset(offset)
, length(length)
{
}
int
getLength()
int getLength()
{
return this->length;
}
+3 -6
View File
@@ -23,20 +23,17 @@ public:
Link();
Link(Type getType, const QString &getValue);
bool
getIsValid() const
bool getIsValid() const
{
return type == None;
}
Type
getType() const
Type getType() const
{
return type;
}
const QString &
getValue() const
const QString &getValue() const
{
return value;
}
+52 -382
View File
@@ -2,13 +2,13 @@
#include "channel.h"
#include "colorscheme.h"
#include "emojis.h"
#include "emotes.h"
#include "fonts.h"
#include "emotemanager.h"
#include "fontmanager.h"
#include "ircmanager.h"
#include "messages/link.h"
#include "qcolor.h"
#include "resources.h"
#include "settings.h"
#include "settingsmanager.h"
#include <QObjectUserData>
#include <QStringList>
@@ -19,396 +19,66 @@
namespace chatterino {
namespace messages {
QRegularExpression *Message::cheerRegex =
new QRegularExpression("cheer[1-9][0-9]*");
Message::Message(const QString &text)
: _words()
{
words.push_back(Word(text, Word::Text,
ColorScheme::getInstance().SystemMessageColor, text,
QString()));
_words.push_back(
Word(text, Word::Text, ColorScheme::getInstance().SystemMessageColor, text, QString()));
}
Message::Message(const IrcPrivateMessage &ircMessage, Channel &channel,
bool enablePingSound, bool isReceivedWhisper,
bool isSentWhisper, bool includeChannel)
: content(ircMessage.content())
Message::Message(const std::vector<Word> &words)
: _words(words)
{
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::getInstance().SystemMessageColor,
QString(), QString()));
words.push_back(Word(timestampWithSeconds, Word::TimestampWithSeconds,
ColorScheme::getInstance().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::getInstance().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::getInstance().SystemMessageColor, QString(channelName),
QString(), Link(Link::Url, channel.getName() + "\n" + this->id)));
}
// username
this->userName = ircMessage.nick();
if (this->userName.isEmpty()) {
this->userName = tags.value(QLatin1String("login")).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 + 1);
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::getInstance().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::BitsStatic, 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;
// }
}
QString
Message::matchLink(const QString &string)
bool Message::getCanHighlightTab() const
{
// TODO: Implement this xD
return QString();
return _highlightTab;
}
bool
Message::sortTwitchEmotes(const std::pair<long int, LazyLoadedImage *> &a,
const std::pair<long int, LazyLoadedImage *> &b)
const QString &Message::getTimeoutUser() const
{
return a.first < b.first;
return _timeoutUser;
}
int Message::getTimeoutCount() const
{
return _timeoutCount;
}
const QString &Message::getUserName() const
{
return _userName;
}
const QString &Message::getDisplayName() const
{
return _displayName;
}
const QString &Message::getContent() const
{
return _content;
}
const std::chrono::time_point<std::chrono::system_clock> &Message::getParseTime() const
{
return _parseTime;
}
std::vector<Word> &Message::getWords()
{
return _words;
}
bool Message::isDisabled() const
{
return _isDisabled;
}
const QString &Message::getId() const
{
return _id;
}
} // namespace messages
+27 -82
View File
@@ -1,6 +1,8 @@
#ifndef MESSAGE_H
#define MESSAGE_H
#include "messages/message.h"
#include "messages/messageparseargs.h"
#include "messages/word.h"
#include "messages/wordpart.h"
@@ -8,84 +10,33 @@
#include <QVector>
#include <chrono>
#include <memory>
namespace chatterino {
class Channel;
namespace messages {
class Message;
typedef std::shared_ptr<Message> SharedMessage;
class Message
{
public:
Message(const QString &text);
Message(const IrcPrivateMessage &ircMessage, Channel &channel,
bool enablePingSound = true, bool isReceivedWhisper = false,
bool isSentWhisper = false, bool includeChannel = false);
Message(const std::vector<messages::Word> &words);
~Message()
{
}
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;
}
inline const QString &
getContent() const
{
return this->content;
}
inline const std::chrono::time_point<std::chrono::system_clock> &
getParseTime() const
{
return this->parseTime;
}
std::vector<Word> &
getWords()
{
return words;
}
bool
getDisabled() const
{
return disabled;
}
const QString &
getId() const
{
return id;
}
bool getCanHighlightTab() const;
const QString &getTimeoutUser() const;
int getTimeoutCount() const;
const QString &getUserName() const;
const QString &getDisplayName() const;
const QString &getContent() const;
const std::chrono::time_point<std::chrono::system_clock> &getParseTime() const;
std::vector<Word> &getWords();
bool isDisabled() const;
const QString &getId() const;
private:
static LazyLoadedImage *badgeStaff;
@@ -98,24 +49,18 @@ private:
static QRegularExpression *cheerRegex;
bool highlightTab = false;
QString timeoutUser = "";
int timeoutCount = 0;
bool disabled = false;
std::chrono::time_point<std::chrono::system_clock> parseTime;
bool _highlightTab = false;
QString _timeoutUser = "";
int _timeoutCount = 0;
bool _isDisabled = false;
std::chrono::time_point<std::chrono::system_clock> _parseTime;
QString userName = "";
QString displayName = "";
QString content;
QString id = "";
QString _userName = "";
QString _displayName = "";
QString _content;
QString _id = "";
std::vector<Word> words;
static QString matchLink(const QString &string);
static bool sortTwitchEmotes(
const std::pair<long int, LazyLoadedImage *> &a,
const std::pair<long int, LazyLoadedImage *> &b);
std::vector<Word> _words;
};
} // namespace messages
+54
View File
@@ -0,0 +1,54 @@
#include "messagebuilder.h"
#include "colorscheme.h"
#include "emotemanager.h"
#include "resources.h"
namespace chatterino {
namespace messages {
MessageBuilder::MessageBuilder()
: _words()
{
_parseTime = std::chrono::system_clock::now();
}
SharedMessage MessageBuilder::build()
{
return SharedMessage(new Message(_words));
}
void MessageBuilder::appendWord(const Word &word)
{
_words.push_back(word);
}
void MessageBuilder::appendTimestamp()
{
time_t t;
time(&t);
appendTimestamp(t);
}
void MessageBuilder::appendTimestamp(time_t time)
{
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);
appendWord(Word(timestamp, Word::TimestampNoSeconds,
ColorScheme::getInstance().SystemMessageColor, QString(), QString()));
appendWord(Word(timestampWithSeconds, Word::TimestampWithSeconds,
ColorScheme::getInstance().SystemMessageColor, QString(), QString()));
}
QString MessageBuilder::matchLink(const QString &string)
{
// TODO: Implement this xD
return QString();
}
}
}
+29
View File
@@ -0,0 +1,29 @@
#ifndef MESSAGEBUILDER_H
#define MESSAGEBUILDER_H
#include "messages/message.h"
namespace chatterino {
namespace messages {
class MessageBuilder
{
public:
MessageBuilder();
SharedMessage build();
void appendWord(const Word &word);
void appendTimestamp();
void appendTimestamp(std::time_t time);
QString matchLink(const QString &string);
private:
std::vector<Word> _words;
std::chrono::time_point<std::chrono::system_clock> _parseTime;
};
}
}
#endif // MESSAGEBUILDER_H
+17
View File
@@ -0,0 +1,17 @@
#ifndef MESSAGEPARSEARGS_H
#define MESSAGEPARSEARGS_H
namespace chatterino {
namespace messages {
struct MessageParseArgs {
public:
bool disablePingSoungs = false;
bool isReceivedWhisper = false;
bool isSentWhisper = false;
bool includeChannelName = false;
};
}
}
#endif // MESSAGEPARSEARGS_H
+106 -103
View File
@@ -1,6 +1,6 @@
#include "messageref.h"
#include "emotes.h"
#include "settings.h"
#include "emotemanager.h"
#include "settingsmanager.h"
#include <QDebug>
@@ -9,82 +9,87 @@
#define MARGIN_TOP 8
#define MARGIN_BOTTOM 8
using namespace chatterino::messages;
namespace chatterino {
namespace messages {
MessageRef::MessageRef(std::shared_ptr<Message> message)
: message(message.get())
, messagePtr(message)
, wordParts()
MessageRef::MessageRef(SharedMessage message)
: _message(message)
, _wordParts()
, buffer()
{
}
bool
MessageRef::layout(int width, bool enableEmoteMargins)
Message *MessageRef::getMessage()
{
auto &settings = Settings::getInstance();
return _message.get();
}
bool sizeChanged = width != this->currentLayoutWidth;
bool redraw = width != this->currentLayoutWidth;
int MessageRef::getHeight() const
{
return _height;
}
bool MessageRef::layout(int width, bool enableEmoteMargins)
{
auto &settings = SettingsManager::getInstance();
bool sizeChanged = width != _currentLayoutWidth;
bool redraw = width != _currentLayoutWidth;
int spaceWidth = 4;
{
int mediumTextLineHeight =
Fonts::getFontMetrics(Fonts::Medium).height();
int mediumTextLineHeight =
FontManager::getInstance().getFontMetrics(FontManager::Medium).height();
bool recalculateImages =
this->emoteGeneration != Emotes::getGeneration();
bool recalculateText = this->fontGeneration != Fonts::getGeneration();
bool newWordTypes =
this->currentWordTypes != Settings::getInstance().getWordTypeMask();
bool recalculateImages = _emoteGeneration != EmoteManager::getInstance().getGeneration();
bool recalculateText = _fontGeneration != FontManager::getInstance().getGeneration();
bool newWordTypes = _currentWordTypes != SettingsManager::getInstance().getWordTypeMask();
qreal emoteScale = settings.emoteScale.get();
bool scaleEmotesByLineHeight = settings.scaleEmotesByLineHeight.get();
qreal emoteScale = settings.emoteScale.get();
bool scaleEmotesByLineHeight = settings.scaleEmotesByLineHeight.get();
if (recalculateImages || recalculateText || newWordTypes) {
this->emoteGeneration = Emotes::getGeneration();
this->fontGeneration = Fonts::getGeneration();
redraw = true;
for (auto &word : this->message->getWords()) {
if (word.isImage()) {
if (recalculateImages) {
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) {
QFontMetrics &metrics = word.getFontMetrics();
word.setSize(metrics.width(word.getText()),
metrics.height());
}
}
}
}
if (newWordTypes) {
this->currentWordTypes = Settings::getInstance().getWordTypeMask();
}
}
if (!redraw) {
// calculate word sizes
if (!redraw && !recalculateImages && !recalculateText && !newWordTypes) {
return false;
}
this->currentLayoutWidth = width;
_emoteGeneration = EmoteManager::getInstance().getGeneration();
_fontGeneration = FontManager::getInstance().getGeneration();
for (auto &word : _message->getWords()) {
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;
}
QFontMetrics &metrics = word.getFontMetrics();
word.setSize(metrics.width(word.getText()), metrics.height());
}
}
if (newWordTypes) {
_currentWordTypes = SettingsManager::getInstance().getWordTypeMask();
}
// layout
_currentLayoutWidth = width;
int x = MARGIN_LEFT;
int y = MARGIN_TOP;
@@ -96,12 +101,11 @@ MessageRef::layout(int width, bool enableEmoteMargins)
int lineHeight = 0;
bool first = true;
this->wordParts.clear();
_wordParts.clear();
uint32_t flags = Settings::getInstance().getWordTypeMask();
uint32_t flags = SettingsManager::getInstance().getWordTypeMask();
for (auto it = this->message->getWords().begin();
it != this->message->getWords().end(); ++it) {
for (auto it = _message->getWords().begin(); it != _message->getWords().end(); ++it) {
Word &word = *it;
if ((word.getType() & flags) == Word::None) {
@@ -121,7 +125,7 @@ MessageRef::layout(int width, bool enableEmoteMargins)
// word wrapping
if (word.isText() && word.getWidth() + MARGIN_LEFT > right) {
this->alignWordParts(lineStart, lineHeight);
alignWordParts(lineStart, lineHeight);
y += lineHeight;
@@ -144,9 +148,8 @@ MessageRef::layout(int width, bool enableEmoteMargins)
if ((width = width + charWidths[i - 1]) + MARGIN_LEFT > right) {
QString mid = text.mid(start, i - start - 1);
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y,
width, word.getHeight(),
lineNumber, mid, mid));
_wordParts.push_back(WordPart(word, MARGIN_LEFT, y, width, word.getHeight(),
lineNumber, mid, mid));
y += metrics.height();
@@ -160,20 +163,19 @@ MessageRef::layout(int width, bool enableEmoteMargins)
QString mid(text.mid(start));
width = metrics.width(mid);
this->wordParts.push_back(
WordPart(word, MARGIN_LEFT, y - word.getHeight(), width,
word.getHeight(), lineNumber, mid, mid));
_wordParts.push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(), width,
word.getHeight(), lineNumber, mid, mid));
x = width + MARGIN_LEFT + spaceWidth;
lineHeight = word.getHeight();
lineStart = this->wordParts.size() - 1;
lineStart = _wordParts.size() - 1;
first = false;
} else if (first || x + word.getWidth() + xOffset <= right) {
// fits in the line
this->wordParts.push_back(WordPart(word, x, y - word.getHeight(),
lineNumber, word.getCopyText()));
_wordParts.push_back(
WordPart(word, x, y - word.getHeight(), lineNumber, word.getCopyText()));
x += word.getWidth() + xOffset;
x += spaceWidth;
@@ -183,15 +185,14 @@ MessageRef::layout(int width, bool enableEmoteMargins)
first = false;
} else {
// doesn't fit in the line
this->alignWordParts(lineStart, lineHeight);
alignWordParts(lineStart, lineHeight);
y += lineHeight;
this->wordParts.push_back(WordPart(word, MARGIN_LEFT,
y - word.getHeight(), lineNumber,
word.getCopyText()));
_wordParts.push_back(
WordPart(word, MARGIN_LEFT, y - word.getHeight(), lineNumber, word.getCopyText()));
lineStart = this->wordParts.size() - 1;
lineStart = _wordParts.size() - 1;
lineHeight = word.getHeight();
@@ -202,36 +203,40 @@ MessageRef::layout(int width, bool enableEmoteMargins)
}
}
this->alignWordParts(lineStart, lineHeight);
alignWordParts(lineStart, lineHeight);
if (this->height != y + lineHeight) {
if (_height != y + lineHeight) {
sizeChanged = true;
this->height = y + lineHeight;
_height = y + lineHeight;
}
if (sizeChanged) {
this->buffer = nullptr;
buffer = nullptr;
}
this->updateBuffer = true;
updateBuffer = true;
return true;
}
void
MessageRef::alignWordParts(int lineStart, int lineHeight)
const std::vector<WordPart> &MessageRef::getWordParts() const
{
for (size_t i = lineStart; i < this->wordParts.size(); i++) {
WordPart &wordPart2 = this->wordParts.at(i);
return _wordParts;
}
void MessageRef::alignWordParts(int lineStart, int lineHeight)
{
for (size_t i = lineStart; i < _wordParts.size(); i++) {
WordPart &wordPart2 = _wordParts.at(i);
wordPart2.setY(wordPart2.getY() + lineHeight);
}
}
bool
MessageRef::tryGetWordPart(QPoint point, messages::Word &word)
bool MessageRef::tryGetWordPart(QPoint point, Word &word)
{
for (messages::WordPart &wordPart : this->wordParts) {
// go through all words and return the first one that contains the point.
for (WordPart &wordPart : _wordParts) {
if (wordPart.getRect().contains(point)) {
word = wordPart.getWord();
return true;
@@ -241,18 +246,17 @@ MessageRef::tryGetWordPart(QPoint point, messages::Word &word)
return false;
}
int
MessageRef::getSelectionIndex(QPoint position)
int MessageRef::getSelectionIndex(QPoint position)
{
if (this->wordParts.size() == 0) {
if (_wordParts.size() == 0) {
return 0;
}
// find out in which line the cursor is
int lineNumber = 0, lineStart = 0, lineEnd = 0;
for (int i = 0; i < this->wordParts.size(); i++) {
WordPart &part = this->wordParts[i];
for (int i = 0; i < _wordParts.size(); i++) {
WordPart &part = _wordParts[i];
// return if curser under the word
if (position.y() >= part.getBottom()) {
@@ -271,13 +275,13 @@ MessageRef::getSelectionIndex(QPoint position)
int index = 0;
for (int i = 0; i < lineStart; i++) {
WordPart &part = this->wordParts[i];
WordPart &part = _wordParts[i];
index += part.getWord().isImage() ? 2 : part.getText().length() + 1;
}
for (int i = lineStart; i < lineEnd; i++) {
WordPart &part = this->wordParts[i];
WordPart &part = _wordParts[i];
// curser is left of the word part
if (position.x() < part.getX()) {
@@ -304,8 +308,7 @@ MessageRef::getSelectionIndex(QPoint position)
}
index++;
x = part.getX() +
part.getWord().getFontMetrics().width(text, j + 1);
x = part.getX() + part.getWord().getFontMetrics().width(text, j + 1);
}
}
@@ -315,9 +318,9 @@ MessageRef::getSelectionIndex(QPoint position)
return index;
// go through all the wordparts
// for (int i = 0; i < this->wordParts; i < this->wordParts.size()) {
// for (int i = 0; i < wordParts; i < wordParts.size()) {
// WordPart &part = this->wordParts[i];
// WordPart &part = wordParts[i];
// // return if curser under the word
// if (position.y() >= part.getBottom()) {
+17 -26
View File
@@ -9,30 +9,21 @@
namespace chatterino {
namespace messages {
class MessageRef;
typedef std::shared_ptr<MessageRef> SharedMessageRef;
class MessageRef
{
public:
MessageRef(std::shared_ptr<Message> message);
MessageRef(SharedMessage message);
Message *
getMessage()
{
return this->message;
}
int
getHeight() const
{
return height;
}
Message *getMessage();
int getHeight() const;
bool layout(int width, bool enableEmoteMargins = true);
const std::vector<WordPart> &
getWordParts() const
{
return wordParts;
}
const std::vector<WordPart> &getWordParts() const;
std::shared_ptr<QPixmap> buffer = nullptr;
bool updateBuffer = false;
@@ -42,18 +33,18 @@ public:
int getSelectionIndex(QPoint position);
private:
Message *message;
std::shared_ptr<Message> messagePtr;
// variables
SharedMessage _message;
std::vector<messages::WordPart> _wordParts;
std::vector<messages::WordPart> wordParts;
int _height = 0;
int height = 0;
int currentLayoutWidth = -1;
int fontGeneration = -1;
int emoteGeneration = -1;
Word::Type currentWordTypes = Word::None;
int _currentLayoutWidth = -1;
int _fontGeneration = -1;
int _emoteGeneration = -1;
Word::Type _currentWordTypes = Word::None;
// methods
void alignWordParts(int lineStart, int lineHeight);
};
}
+116 -20
View File
@@ -4,35 +4,131 @@ namespace chatterino {
namespace messages {
// Image word
Word::Word(LazyLoadedImage *image, Type type, const QString &copytext,
const QString &tooltip, const Link &link)
: image(image)
, text()
, color()
Word::Word(LazyLoadedImage *image, Type type, const QString &copytext, const QString &tooltip,
const Link &link)
: _image(image)
, _text()
, _color()
, _isImage(true)
, type(type)
, copyText(copytext)
, tooltip(tooltip)
, link(link)
, characterWidthCache()
, _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 &copytext, const QString &tooltip, const Link &link)
: image(NULL)
, text(text)
, color(color)
Word::Word(const QString &text, Type type, const QColor &color, const QString &copytext,
const QString &tooltip, const Link &link)
: _image(NULL)
, _text(text)
, _color(color)
, _isImage(false)
, type(type)
, copyText(copytext)
, tooltip(tooltip)
, link(link)
, characterWidthCache()
, _type(type)
, _copyText(copytext)
, _tooltip(tooltip)
, _link(link)
, _characterWidthCache()
{
}
LazyLoadedImage &Word::getImage() const
{
return *_image;
}
const QString &Word::getText() const
{
return _text;
}
int Word::getWidth() const
{
return _width;
}
int Word::getHeight() const
{
return _height;
}
void Word::setSize(int width, int height)
{
_width = width;
_height = height;
}
bool Word::isImage() const
{
return _isImage;
}
bool Word::isText() const
{
return !_isImage;
}
const QString &Word::getCopyText() const
{
return _copyText;
}
bool Word::hasTrailingSpace() const
{
return _hasTrailingSpace;
}
QFont &Word::getFont() const
{
return FontManager::getInstance().getFont(_font);
}
QFontMetrics &Word::getFontMetrics() const
{
return FontManager::getInstance().getFontMetrics(_font);
}
Word::Type Word::getType() const
{
return _type;
}
const QString &Word::getTooltip() const
{
return _tooltip;
}
const QColor &Word::getColor() const
{
return _color;
}
const Link &Word::getLink() const
{
return _link;
}
int Word::getXOffset() const
{
return _xOffset;
}
int Word::getYOffset() const
{
return _yOffset;
}
void Word::setOffset(int xOffset, int yOffset)
{
_xOffset = std::max(0, xOffset);
_yOffset = std::max(0, yOffset);
}
std::vector<short> &Word::getCharacterWidthCache() const
{
return _characterWidthCache;
}
} // namespace messages
} // namespace chatterino
+42 -140
View File
@@ -1,7 +1,7 @@
#ifndef WORD_H
#define WORD_H
#include "fonts.h"
#include "fontmanager.h"
#include "messages/lazyloadedimage.h"
#include "messages/link.h"
@@ -31,8 +31,7 @@ public:
BttvGifEmoteText = (1 << 9),
FfzEmoteImage = (1 << 10),
FfzEmoteText = (1 << 11),
EmoteImages = TwitchEmoteImage | BttvEmoteImage | BttvGifEmoteImage |
FfzEmoteImage,
EmoteImages = TwitchEmoteImage | BttvEmoteImage | BttvGifEmoteImage | FfzEmoteImage,
BitsStatic = (1 << 12),
BitsAnimated = (1 << 13),
@@ -46,9 +45,8 @@ public:
BadgePremium = (1 << 20),
BadgeChatterino = (1 << 21),
BadgeCheer = (1 << 22),
Badges = BadgeStaff | BadgeAdmin | BadgeGlobalMod | BadgeModerator |
BadgeTurbo | BadgeBroadcaster | BadgePremium |
BadgeChatterino | BadgeCheer,
Badges = BadgeStaff | BadgeAdmin | BadgeGlobalMod | BadgeModerator | BadgeTurbo |
BadgeBroadcaster | BadgePremium | BadgeChatterino | BadgeCheer,
Username = (1 << 23),
BitsAmount = (1 << 24),
@@ -59,157 +57,61 @@ public:
EmojiImage = (1 << 27),
EmojiText = (1 << 28),
Default = TimestampNoSeconds | Badges | Username | BitsStatic |
FfzEmoteImage | BttvEmoteImage | BttvGifEmoteImage |
TwitchEmoteImage | BitsAmount | Text | ButtonBan |
ButtonTimeout
Default = TimestampNoSeconds | Badges | Username | BitsStatic | FfzEmoteImage |
BttvEmoteImage | BttvGifEmoteImage | TwitchEmoteImage | BitsAmount | Text |
ButtonBan | ButtonTimeout
};
Word()
{
}
explicit Word(LazyLoadedImage *image, Type getType, const QString &copytext,
explicit Word(LazyLoadedImage *_image, Type getType, const QString &copytext,
const QString &getTooltip, const Link &getLink = Link());
explicit Word(const QString &text, Type getType, const QColor &getColor,
const QString &copytext, const QString &getTooltip,
const Link &getLink = Link());
explicit Word(const QString &_text, Type getType, const QColor &getColor,
const QString &copytext, const QString &getTooltip, const Link &getLink = Link());
LazyLoadedImage &
getImage() const
{
return *image;
}
LazyLoadedImage &getImage() const;
const QString &getText() const;
int getWidth() const;
int getHeight() const;
void setSize(int _width, int _height);
const QString &
getText() const
{
return this->text;
}
bool isImage() const;
bool isText() const;
const QString &getCopyText() const;
bool hasTrailingSpace() const;
QFont &getFont() const;
QFontMetrics &getFontMetrics() const;
Type getType() const;
const QString &getTooltip() const;
const QColor &getColor() const;
const Link &getLink() const;
int getXOffset() const;
int getYOffset() const;
void setOffset(int _xOffset, int _yOffset);
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() const
{
return this->characterWidthCache;
}
std::vector<short> &getCharacterWidthCache() const;
private:
LazyLoadedImage *image;
QString text;
QColor color;
LazyLoadedImage *_image;
QString _text;
QColor _color;
bool _isImage;
Type type;
QString copyText;
QString tooltip;
Type _type;
QString _copyText;
QString _tooltip;
int width = 16;
int height = 16;
int xOffset = 0;
int yOffset = 0;
int _width = 16;
int _height = 16;
int _xOffset = 0;
int _yOffset = 0;
bool _hasTrailingSpace;
Fonts::Type font = Fonts::Medium;
Link link;
FontManager::Type _font = FontManager::Medium;
Link _link;
mutable std::vector<short> characterWidthCache;
mutable std::vector<short> _characterWidthCache;
};
} // namespace messages
+91 -21
View File
@@ -4,33 +4,103 @@
namespace chatterino {
namespace messages {
WordPart::WordPart(Word &word, int x, int y, int lineNumber,
const QString &copyText, bool allowTrailingSpace)
: m_word(word)
, copyText(copyText)
, text(word.isText() ? m_word.getText() : QString())
, x(x)
, y(y)
, width(word.getWidth())
, height(word.getHeight())
, lineNumber(lineNumber)
WordPart::WordPart(Word &word, int x, int y, int lineNumber, const QString &copyText,
bool allowTrailingSpace)
: _word(word)
, _copyText(copyText)
, _text(word.isText() ? _word.getText() : QString())
, _x(x)
, _y(y)
, _width(word.getWidth())
, _height(word.getHeight())
, _lineNumber(lineNumber)
, _trailingSpace(word.hasTrailingSpace() & allowTrailingSpace)
{
}
WordPart::WordPart(Word &word, int x, int y, int width, int height,
int lineNumber, const QString &copyText,
const QString &customText, bool allowTrailingSpace)
: m_word(word)
, copyText(copyText)
, text(customText)
, x(x)
, y(y)
, width(width)
, height(height)
, lineNumber(lineNumber)
WordPart::WordPart(Word &word, int x, int y, int width, int height, int lineNumber,
const QString &copyText, const QString &customText, bool allowTrailingSpace)
: _word(word)
, _copyText(copyText)
, _text(customText)
, _x(x)
, _y(y)
, _width(width)
, _height(height)
, _lineNumber(lineNumber)
, _trailingSpace(word.hasTrailingSpace() & allowTrailingSpace)
{
}
const Word &WordPart::getWord() const
{
return _word;
}
int WordPart::getWidth() const
{
return _width;
}
int WordPart::getHeight() const
{
return _height;
}
int WordPart::getX() const
{
return _x;
}
int WordPart::getY() const
{
return _y;
}
void WordPart::setPosition(int x, int y)
{
_x = x;
_y = y;
}
void WordPart::setY(int y)
{
_y = y;
}
int WordPart::getRight() const
{
return _x + _width;
}
int WordPart::getBottom() const
{
return _y + _height;
}
QRect WordPart::getRect() const
{
return QRect(_x, _y, _width, _height);
}
const QString WordPart::getCopyText() const
{
return _copyText;
}
int WordPart::hasTrailingSpace() const
{
return _trailingSpace;
}
const QString &WordPart::getText() const
{
return _text;
}
int WordPart::getLineNumber()
{
return _lineNumber;
}
}
}
+26 -97
View File
@@ -12,110 +12,39 @@ class Word;
class WordPart
{
public:
WordPart(Word &getWord, int getX, int getY, int lineNumber,
const QString &getCopyText, bool allowTrailingSpace = true);
WordPart(Word &getWord, int getX, int getY, int _lineNumber, const QString &getCopyText,
bool allowTrailingSpace = true);
WordPart(Word &getWord, int getX, int getY, int getWidth, int getHeight,
int lineNumber, const QString &getCopyText,
const QString &customText, bool allowTrailingSpace = true);
WordPart(Word &getWord, int getX, int getY, int getWidth, int getHeight, int _lineNumber,
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;
}
int
getLineNumber()
{
return this->lineNumber;
}
const Word &getWord() const;
int getWidth() const;
int getHeight() const;
int getX() const;
int getY() const;
void setPosition(int _x, int _y);
void setY(int _y);
int getRight() const;
int getBottom() const;
QRect getRect() const;
const QString getCopyText() const;
int hasTrailingSpace() const;
const QString &getText() const;
int getLineNumber();
private:
Word &m_word;
Word &_word;
QString copyText;
QString text;
QString _copyText;
QString _text;
int x;
int y;
int width;
int height;
int _x;
int _y;
int _width;
int _height;
int lineNumber;
int _lineNumber;
bool _trailingSpace;
};