fixed emotes settings a little bit

This commit is contained in:
fourtf 2018-01-22 22:38:44 +01:00
parent cb6af11b5a
commit 91d45214d9
4 changed files with 134 additions and 120 deletions

View file

@ -61,45 +61,65 @@ ImageElement::ImageElement(Image *_image, MessageElement::Flags flags)
void ImageElement::addToContainer(MessageLayoutContainer &container, MessageElement::Flags _flags)
{
QSize size(this->image->getWidth() * this->image->getScale() * container.scale,
this->image->getHeight() * this->image->getScale() * container.scale);
if (_flags & this->getFlags()) {
QSize size(this->image->getWidth() * this->image->getScale() * container.scale,
this->image->getHeight() * this->image->getScale() * container.scale);
container.addElement(
(new ImageLayoutElement(*this, this->image, size))->setLink(this->getLink()));
container.addElement(
(new ImageLayoutElement(*this, this->image, size))->setLink(this->getLink()));
}
}
// EMOTE
EmoteElement::EmoteElement(const util::EmoteData &_data, MessageElement::Flags flags)
: MessageElement(flags)
, data(_data)
, textElement(nullptr)
{
if (_data.isValid()) {
this->setTooltip(data.image1x->getTooltip());
qDebug() << "valid xDDDDDDDDD" << _data.image1x->getName();
this->textElement = new TextElement(_data.image1x->getName(), MessageElement::Misc);
}
}
EmoteElement::~EmoteElement()
{
if (this->textElement != nullptr) {
delete this->textElement;
}
}
void EmoteElement::addToContainer(MessageLayoutContainer &container, MessageElement::Flags _flags)
{
if (!this->data.isValid()) {
qDebug() << "EmoteElement::data is invalid xD";
return;
if (_flags & this->getFlags()) {
if (_flags & this->getFlags() & MessageElement::EmoteImages) {
if (!this->data.isValid()) {
return;
}
int quality = singletons::SettingManager::getInstance().preferredEmoteQuality;
Image *_image;
if (quality == 3 && this->data.image3x != nullptr) {
_image = this->data.image3x;
} else if (quality >= 2 && this->data.image2x != nullptr) {
_image = this->data.image2x;
} else {
_image = this->data.image1x;
}
QSize size((int)(container.scale * _image->getScaledWidth()),
(int)(container.scale * _image->getScaledHeight()));
container.addElement(
(new ImageLayoutElement(*this, _image, size))->setLink(this->getLink()));
} else {
if (this->textElement != nullptr) {
this->textElement->addToContainer(container, MessageElement::Misc);
}
}
}
int quality = singletons::SettingManager::getInstance().preferredEmoteQuality;
Image *_image;
if (quality == 3 && this->data.image3x != nullptr) {
_image = this->data.image3x;
} else if (quality >= 2 && this->data.image2x != nullptr) {
_image = this->data.image2x;
} else {
_image = this->data.image1x;
}
QSize size((int)(container.scale * _image->getScaledWidth()),
(int)(container.scale * _image->getScaledHeight()));
container.addElement((new ImageLayoutElement(*this, _image, size))->setLink(this->getLink()));
}
// TEXT
@ -117,75 +137,78 @@ TextElement::TextElement(const QString &text, MessageElement::Flags flags,
void TextElement::addToContainer(MessageLayoutContainer &container, MessageElement::Flags _flags)
{
QFontMetrics &metrics =
singletons::FontManager::getInstance().getFontMetrics(this->style, container.scale);
singletons::ThemeManager &themeManager = singletons::ThemeManager::ThemeManager::getInstance();
if (_flags & this->getFlags()) {
QFontMetrics &metrics =
singletons::FontManager::getInstance().getFontMetrics(this->style, container.scale);
singletons::ThemeManager &themeManager =
singletons::ThemeManager::ThemeManager::getInstance();
for (Word &word : this->words) {
auto getTextLayoutElement = [&](QString text, int width, bool trailingSpace) {
QColor color = this->color.getColor(themeManager);
themeManager.normalizeColor(color);
for (Word &word : this->words) {
auto getTextLayoutElement = [&](QString text, int width, bool trailingSpace) {
QColor color = this->color.getColor(themeManager);
themeManager.normalizeColor(color);
auto e = (new TextLayoutElement(*this, text, QSize(width, metrics.height()), color,
this->style, container.scale))
->setLink(this->getLink());
e->setTrailingSpace(trailingSpace);
return e;
};
auto e = (new TextLayoutElement(*this, text, QSize(width, metrics.height()), color,
this->style, container.scale))
->setLink(this->getLink());
e->setTrailingSpace(trailingSpace);
return e;
};
if (word.width == -1) {
word.width = metrics.width(word.text);
}
// see if the text fits in the current line
if (container.fitsInLine(word.width)) {
container.addElementNoLineBreak(
getTextLayoutElement(word.text, word.width, this->hasTrailingSpace()));
continue;
}
// see if the text fits in the next line
if (!container.atStartOfLine()) {
container.breakLine();
if (word.width == -1) {
word.width = metrics.width(word.text);
}
// see if the text fits in the current line
if (container.fitsInLine(word.width)) {
container.addElementNoLineBreak(
getTextLayoutElement(word.text, word.width, this->hasTrailingSpace()));
continue;
}
}
// we done goofed, we need to wrap the text
QString text = word.text;
int textLength = text.length();
int wordStart = 0;
int width = metrics.width(text[0]);
int lastWidth = 0;
for (int i = 1; i < textLength; i++) {
int charWidth = metrics.width(text[i]);
if (!container.fitsInLine(width + charWidth)) {
container.addElementNoLineBreak(getTextLayoutElement(
text.mid(wordStart, i - wordStart), width - lastWidth, false));
// see if the text fits in the next line
if (!container.atStartOfLine()) {
container.breakLine();
wordStart = i;
lastWidth = width;
width = 0;
if (textLength > i + 2) {
width += metrics.width(text[i]);
width += metrics.width(text[i + 1]);
i += 1;
if (container.fitsInLine(word.width)) {
container.addElementNoLineBreak(
getTextLayoutElement(word.text, word.width, this->hasTrailingSpace()));
continue;
}
continue;
}
width += charWidth;
}
container.addElement(getTextLayoutElement(text.mid(wordStart), word.width - lastWidth,
this->hasTrailingSpace()));
container.breakLine();
// we done goofed, we need to wrap the text
QString text = word.text;
int textLength = text.length();
int wordStart = 0;
int width = metrics.width(text[0]);
int lastWidth = 0;
for (int i = 1; i < textLength; i++) {
int charWidth = metrics.width(text[i]);
if (!container.fitsInLine(width + charWidth)) {
container.addElementNoLineBreak(getTextLayoutElement(
text.mid(wordStart, i - wordStart), width - lastWidth, false));
container.breakLine();
wordStart = i;
lastWidth = width;
width = 0;
if (textLength > i + 2) {
width += metrics.width(text[i]);
width += metrics.width(text[i + 1]);
i += 1;
}
continue;
}
width += charWidth;
}
container.addElement(getTextLayoutElement(text.mid(wordStart), word.width - lastWidth,
this->hasTrailingSpace()));
container.breakLine();
}
}
}
@ -211,13 +234,15 @@ TimestampElement::~TimestampElement()
void TimestampElement::addToContainer(MessageLayoutContainer &container,
MessageElement::Flags _flags)
{
if (singletons::SettingManager::getInstance().timestampFormat != this->format) {
this->format = singletons::SettingManager::getInstance().timestampFormat.getValue();
delete this->element;
this->element = TimestampElement::formatTime(this->time);
}
if (_flags & this->getFlags()) {
if (singletons::SettingManager::getInstance().timestampFormat != this->format) {
this->format = singletons::SettingManager::getInstance().timestampFormat.getValue();
delete this->element;
this->element = TimestampElement::formatTime(this->time);
}
this->element->addToContainer(container, _flags);
this->element->addToContainer(container, _flags);
}
}
TextElement *TimestampElement::formatTime(const QTime &time)
@ -239,8 +264,6 @@ TwitchModerationElement::TwitchModerationElement()
void TwitchModerationElement::addToContainer(MessageLayoutContainer &container,
MessageElement::Flags _flags)
{
// qDebug() << _flags;
if (_flags & MessageElement::ModeratorTools) {
QSize size((int)(container.scale * 16), (int)(container.scale * 16));

View file

@ -145,20 +145,6 @@ public:
MessageElement::Flags flags) override;
};
// contains emote data and will pick the emote based on :
// a) are images for the emote type enabled
// b) which size it wants
class EmoteElement : public MessageElement
{
const util::EmoteData data;
public:
EmoteElement(const util::EmoteData &data, MessageElement::Flags flags);
virtual void addToContainer(MessageLayoutContainer &container,
MessageElement::Flags flags) override;
};
// contains a text, it will split it into words
class TextElement : public MessageElement
{
@ -180,6 +166,22 @@ public:
MessageElement::Flags flags) override;
};
// contains emote data and will pick the emote based on :
// a) are images for the emote type enabled
// b) which size it wants
class EmoteElement : public MessageElement
{
const util::EmoteData data;
TextElement *textElement;
public:
EmoteElement(const util::EmoteData &data, MessageElement::Flags flags);
~EmoteElement();
virtual void addToContainer(MessageLayoutContainer &container,
MessageElement::Flags flags) override;
};
// contains a text, formated depending on the preferences
class TimestampElement : public MessageElement
{
@ -208,12 +210,5 @@ public:
virtual void addToContainer(MessageLayoutContainer &container,
MessageElement::Flags flags) override;
};
// adds bits as text, static image or animated image
// class BitsElement : public MessageElement
//{
// public:
// virtual void addToContainer(LayoutContainer &container) override;
//};
} // namespace messages
} // namespace chatterino

View file

@ -450,36 +450,33 @@ bool TwitchMessageBuilder::tryAppendEmote(QString &emoteString)
singletons::EmoteManager &emoteManager = singletons::EmoteManager::getInstance();
util::EmoteData emoteData;
auto appendEmote = [=](MessageElement::Flags flags) {
this->emplace<EmoteElement>(emoteData, flags);
return true;
};
if (emoteManager.bttvGlobalEmotes.tryGet(emoteString, emoteData)) {
// BTTV Global Emote
return this->appendEmote(emoteData);
return appendEmote(MessageElement::BttvEmote);
} else if (this->twitchChannel != nullptr &&
this->twitchChannel->bttvChannelEmotes->tryGet(emoteString, emoteData)) {
// BTTV Channel Emote
return this->appendEmote(emoteData);
return appendEmote(MessageElement::BttvEmote);
} else if (emoteManager.ffzGlobalEmotes.tryGet(emoteString, emoteData)) {
// FFZ Global Emote
return this->appendEmote(emoteData);
return appendEmote(MessageElement::FfzEmote);
} else if (this->twitchChannel != nullptr &&
this->twitchChannel->ffzChannelEmotes->tryGet(emoteString, emoteData)) {
// FFZ Channel Emote
return this->appendEmote(emoteData);
return appendEmote(MessageElement::FfzEmote);
} else if (emoteManager.getChatterinoEmotes().tryGet(emoteString, emoteData)) {
// Chatterino Emote
return this->appendEmote(emoteData);
return appendEmote(MessageElement::Misc);
}
return false;
}
bool TwitchMessageBuilder::appendEmote(const util::EmoteData &emoteData)
{
this->emplace<EmoteElement>(emoteData, MessageElement::BttvEmote);
// Perhaps check for ignored emotes here?
return true;
}
// fourtf: this is ugly
// maybe put the individual badges into a map instead of this mess
void TwitchMessageBuilder::parseTwitchBadges()

View file

@ -55,7 +55,6 @@ private:
void appendTwitchEmote(const Communi::IrcPrivateMessage *ircMessage, const QString &emote,
std::vector<std::pair<long, util::EmoteData>> &vec);
bool tryAppendEmote(QString &emoteString);
bool appendEmote(const util::EmoteData &emoteData);
void parseTwitchBadges();
void addChatterinoBadges();