this commit is too big

This commit is contained in:
fourtf
2018-08-02 14:23:27 +02:00
parent 3b3c5d8d75
commit c2e2dfb577
186 changed files with 3626 additions and 2656 deletions
+75
View File
@@ -0,0 +1,75 @@
#include "Emote.hpp"
#include <unordered_map>
namespace chatterino {
bool operator==(const Emote &a, const Emote &b)
{
return std::tie(a.homePage, a.name, a.tooltip, a.images) ==
std::tie(b.homePage, b.name, b.tooltip, b.images);
}
bool operator!=(const Emote &a, const Emote &b)
{
return !(a == b);
}
// EmotePtr Emote::create(const EmoteData2 &data)
//{
//}
// EmotePtr Emote::create(EmoteData2 &&data)
//{
//}
// Emote::Emote(EmoteData2 &&data)
// : data_(data)
//{
//}
//
// Emote::Emote(const EmoteData2 &data)
// : data_(data)
//{
//}
//
// const Url &Emote::getHomePage() const
//{
// return this->data_.homePage;
//}
//
// const EmoteName &Emote::getName() const
//{
// return this->data_.name;
//}
//
// const Tooltip &Emote::getTooltip() const
//{
// return this->data_.tooltip;
//}
//
// const ImageSet &Emote::getImages() const
//{
// return this->data_.images;
//}
//
// const QString &Emote::getCopyString() const
//{
// return this->data_.name.string;
//}
//
// bool Emote::operator==(const Emote &other) const
//{
// auto &a = this->data_;
// auto &b = other.data_;
//
// return std::tie(a.homePage, a.name, a.tooltip, a.images) ==
// std::tie(b.homePage, b.name, b.tooltip, b.images);
//}
//
// bool Emote::operator!=(const Emote &other) const
//{
// return !this->operator==(other);
//}
} // namespace chatterino
+65
View File
@@ -0,0 +1,65 @@
#pragma once
#include "messages/Image.hpp"
#include "messages/ImageSet.hpp"
#include <functional>
#include <memory>
#include <unordered_map>
QStringAlias(EmoteId);
QStringAlias(EmoteName);
namespace chatterino {
struct Emote {
EmoteName name;
ImageSet images;
Tooltip tooltip;
Url homePage;
// FOURTF: no solution yet, to be refactored later
const QString &getCopyString() const
{
return name.string;
}
};
bool operator==(const Emote &a, const Emote &b);
bool operator!=(const Emote &a, const Emote &b);
using EmotePtr = std::shared_ptr<const Emote>;
class EmoteMap : public std::unordered_map<EmoteName, EmotePtr>
{
};
using EmoteIdMap = std::unordered_map<EmoteId, EmotePtr>;
using WeakEmoteMap = std::unordered_map<EmoteName, std::weak_ptr<const Emote>>;
using WeakEmoteIdMap = std::unordered_map<EmoteId, std::weak_ptr<const Emote>>;
// struct EmoteData2 {
// EmoteName name;
// ImageSet images;
// Tooltip tooltip;
// Url homePage;
//};
//
// class Emote
//{
// public:
// Emote(EmoteData2 &&data);
// Emote(const EmoteData2 &data);
//
// const Url &getHomePage() const;
// const EmoteName &getName() const;
// const Tooltip &getTooltip() const;
// const ImageSet &getImages() const;
// const QString &getCopyString() const;
// bool operator==(const Emote &other) const;
// bool operator!=(const Emote &other) const;
//
// private:
// EmoteData2 data_;
//};
} // namespace chatterino
+93
View File
@@ -0,0 +1,93 @@
#pragma once
#include <QString>
#include <boost/optional.hpp>
#include <unordered_map>
#include <util/QStringHash.hpp>
#include "common/UniqueAccess.hpp"
#include "messages/Emote.hpp"
namespace chatterino {
template <typename TKey>
class MapReplacement
{
public:
MapReplacement(std::unordered_map<TKey, EmotePtr> &items)
: oldItems_(items)
{
}
void add(const TKey &key, const Emote &data)
{
this->add(key, Emote(data));
}
void add(const TKey &key, Emote &&data)
{
auto it = this->oldItems_.find(key);
if (it != this->oldItems_.end() && *it->second == data) {
this->newItems_[key] = it->second;
} else {
this->newItems_[key] = std::make_shared<Emote>(std::move(data));
}
}
void apply()
{
this->oldItems_ = std::move(this->newItems_);
}
private:
std::unordered_map<TKey, EmotePtr> &oldItems_;
std::unordered_map<TKey, EmotePtr> newItems_;
};
template <typename TKey>
class EmoteCache
{
public:
using Iterator = typename std::unordered_map<TKey, EmotePtr>::iterator;
using ConstIterator = typename std::unordered_map<TKey, EmotePtr>::iterator;
Iterator begin()
{
return this->items_.begin();
}
ConstIterator begin() const
{
return this->items_.begin();
}
Iterator end()
{
return this->items_.end();
}
ConstIterator end() const
{
return this->items_.end();
}
boost::optional<EmotePtr> get(const TKey &key) const
{
auto it = this->items_.find(key);
if (it == this->items_.end())
return boost::none;
else
return it->second;
}
MapReplacement<TKey> makeReplacment()
{
return MapReplacement<TKey>(this->items_);
}
private:
std::unordered_map<TKey, EmotePtr> items_;
};
} // namespace chatterino
+44
View File
@@ -0,0 +1,44 @@
#include "EmoteMap.hpp"
#include "Application.hpp"
#include "singletons/Settings.hpp"
namespace chatterino {
// EmoteData::EmoteData(Image *image)
// : image1x(image)
//{
//}
//// Emotes must have a 1x image to be valid
// bool EmoteData::isValid() const
//{
// return this->image1x != nullptr;
//}
// Image *EmoteData::getImage(float scale) const
//{
// int quality = getApp()->settings->preferredEmoteQuality;
// if (quality == 0) {
// scale *= getApp()->settings->emoteScale.getValue();
// quality = [&] {
// if (scale <= 1) return 1;
// if (scale <= 2) return 2;
// return 3;
// }();
// }
// Image *_image;
// if (quality == 3 && this->image3x != nullptr) {
// _image = this->image3x;
// } else if (quality >= 2 && this->image2x != nullptr) {
// _image = this->image2x;
// } else {
// _image = this->image1x;
// }
// return _image;
//}
} // namespace chatterino
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#include "boost/optional.hpp"
#include "messages/Emote.hpp"
namespace chatterino {
// class EmoteMap
//{
// public:
// void add(Emote emote);
// void remove(const Emote &emote);
// void remove(const QString &name);
// private:
//};
// using EmoteMap = ConcurrentMap<QString, EmoteData>;
} // namespace chatterino
+250 -199
View File
@@ -2,6 +2,7 @@
#include "Application.hpp"
#include "common/NetworkRequest.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "debug/Log.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/WindowManager.hpp"
@@ -19,258 +20,308 @@
namespace chatterino {
bool Image::loadedEventQueued = false;
// IMAGE2
std::atomic<bool> Image::loadedEventQueued{false};
Image::Image(const QString &url, qreal scale, const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
: url(url)
, name(name)
, tooltip(tooltip)
, margin(margin)
, ishat(isHat)
, scale(scale)
ImagePtr Image::fromUrl(const Url &url, qreal scale)
{
DebugCount::increase("images");
// herb sutter cache
static std::unordered_map<Url, std::weak_ptr<Image>> cache;
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
auto shared = cache[url].lock();
if (!shared) {
cache[url] = shared = ImagePtr(new Image(url, scale));
} else {
Warn("same image loaded multiple times: {}", url.string);
}
return shared;
}
Image::Image(QPixmap *image, qreal scale, const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
: currentPixmap(image)
, name(name)
, tooltip(tooltip)
, margin(margin)
, ishat(isHat)
, scale(scale)
, isLoading(true)
, isLoaded(true)
ImagePtr Image::fromOwningPixmap(std::unique_ptr<QPixmap> pixmap, qreal scale)
{
DebugCount::increase("images");
return ImagePtr(new Image(std::move(pixmap), scale));
}
Image::~Image()
ImagePtr Image::fromNonOwningPixmap(QPixmap *pixmap, qreal scale)
{
DebugCount::decrease("images");
return ImagePtr(new Image(pixmap, scale));
}
if (this->isAnimated()) {
ImagePtr Image::getEmpty()
{
static auto empty = ImagePtr(new Image);
return empty;
}
Image::Image()
{
this->isLoaded_ = true;
this->isNull_ = true;
}
Image::Image(const Url &url, qreal scale)
{
this->url_ = url;
this->scale_ = scale;
if (url.string.isEmpty()) {
this->isLoaded_ = true;
this->isNull_ = true;
}
}
Image::Image(std::unique_ptr<QPixmap> owning, qreal scale)
{
this->frames_.push_back(Frame(std::move(owning)));
this->scale_ = scale;
this->isLoaded_ = true;
this->currentFramePixmap_ = this->frames_.front().getPixmap();
}
Image::Image(QPixmap *nonOwning, qreal scale)
{
this->frames_.push_back(Frame(nonOwning));
this->scale_ = scale;
this->isLoaded_ = true;
this->currentFramePixmap_ = this->frames_.front().getPixmap();
}
const Url &Image::getUrl() const
{
return this->url_;
}
NullablePtr<const QPixmap> Image::getPixmap() const
{
assertInGuiThread();
if (!this->isLoaded_) {
const_cast<Image *>(this)->load();
}
return this->currentFramePixmap_;
}
qreal Image::getScale() const
{
return this->scale_;
}
bool Image::isAnimated() const
{
return this->isAnimated_;
}
int Image::getWidth() const
{
if (!this->isLoaded_) return 16;
return this->frames_.front().getPixmap()->width() * this->scale_;
}
int Image::getHeight() const
{
if (!this->isLoaded_) return 16;
return this->frames_.front().getPixmap()->height() * this->scale_;
}
bool Image::isLoaded() const
{
return this->isLoaded_;
}
bool Image::isValid() const
{
return !this->isNull_;
}
bool Image::isNull() const
{
return this->isNull_;
}
void Image::load()
{
// decrease debug count
if (this->isAnimated_) {
DebugCount::decrease("animated images");
}
if (this->isLoaded) {
if (this->isLoaded_) {
DebugCount::decrease("loaded images");
}
}
void Image::loadImage()
{
NetworkRequest req(this->getUrl());
req.setCaller(this);
this->isLoaded_ = false;
this->isLoading_ = true;
this->frames_.clear();
NetworkRequest req(this->getUrl().string);
req.setCaller(&this->object_);
req.setUseQuickLoadCache(true);
req.onSuccess([this](auto result) -> bool {
auto bytes = result.getData();
req.onSuccess([this, weak = weakOf(this)](auto result) -> Outcome {
auto shared = weak.lock();
if (!shared) return Failure;
auto &bytes = result.getData();
QByteArray copy = QByteArray::fromRawData(bytes.constData(), bytes.length());
QBuffer buffer(&copy);
buffer.open(QIODevice::ReadOnly);
QImage image;
QImageReader reader(&buffer);
bool first = true;
// clear stuff before loading the image again
this->allFrames.clear();
if (this->isAnimated()) {
DebugCount::decrease("animated images");
}
if (this->isLoaded) {
DebugCount::decrease("loaded images");
}
if (reader.imageCount() == -1) {
// An error occured in the reader
Log("An error occured reading the image: '{}'", reader.errorString());
Log("Image url: {}", this->url);
return false;
}
if (reader.imageCount() == 0) {
Log("Error: No images read in the buffer");
// No images read in the buffer. maybe a cache error?
return false;
}
for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) {
auto pixmap = new QPixmap(QPixmap::fromImage(image));
if (first) {
first = false;
this->loadedPixmap = pixmap;
}
Image::FrameData data;
data.duration = std::max(20, reader.nextImageDelay());
data.image = pixmap;
this->allFrames.push_back(data);
}
}
if (this->allFrames.size() != reader.imageCount()) {
// Log("Error: Wrong amount of images read");
// One or more images failed to load from the buffer
// return false;
}
if (this->allFrames.size() > 1) {
if (!this->animated) {
postToThread([this] {
getApp()->emotes->gifTimer.signal.connect([=]() {
this->gifUpdateTimout();
}); // For some reason when Boost signal is in
// thread scope and thread deletes the signal
// doesn't work, so this is the fix.
});
}
this->animated = true;
DebugCount::increase("animated images");
}
this->currentPixmap = this->loadedPixmap;
this->isLoaded = true;
DebugCount::increase("loaded images");
if (!loadedEventQueued) {
loadedEventQueued = true;
QTimer::singleShot(500, [] {
getApp()->windows->incGeneration();
auto app = getApp();
app->windows->layoutChannelViews();
loadedEventQueued = false;
});
}
return true;
return this->parse(result.getData());
});
req.execute();
}
void Image::gifUpdateTimout()
Outcome Image::parse(const QByteArray &data)
{
if (this->animated) {
this->currentFrameOffset += GIF_FRAME_LENGTH;
// const cast since we are only reading from it
QBuffer buffer(const_cast<QByteArray *>(&data));
buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer);
return this->setFrames(this->readFrames(reader));
}
std::vector<Image::Frame> Image::readFrames(QImageReader &reader)
{
std::vector<Frame> frames;
if (reader.imageCount() <= 0) {
Log("Error while reading image {}: '{}'", this->url_.string, reader.errorString());
return frames;
}
QImage image;
for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) {
auto pixmap = new QPixmap(QPixmap::fromImage(image));
int duration = std::max(20, reader.nextImageDelay());
frames.push_back(Image::Frame(pixmap, duration));
}
}
if (frames.size() != 0) {
Log("Error while reading image {}: '{}'", this->url_.string, reader.errorString());
}
return frames;
}
Outcome Image::setFrames(std::vector<Frame> frames)
{
std::lock_guard<std::mutex> lock(this->framesMutex_);
if (frames.size() > 0) {
this->currentFramePixmap_ = frames.front().getPixmap();
if (frames.size() > 1) {
if (!this->isAnimated_) {
getApp()->emotes->gifTimer.signal.connect([=]() { this->updateAnimation(); });
}
this->isAnimated_ = true;
DebugCount::increase("animated images");
}
this->isLoaded_ = true;
DebugCount::increase("loaded images");
return Success;
}
this->frames_ = std::move(frames);
this->queueLoadedEvent();
return Failure;
}
void Image::queueLoadedEvent()
{
if (!loadedEventQueued) {
loadedEventQueued = true;
QTimer::singleShot(250, [] {
getApp()->windows->incGeneration();
getApp()->windows->layoutChannelViews();
loadedEventQueued = false;
});
}
}
void Image::updateAnimation()
{
if (this->isAnimated_) {
std::lock_guard<std::mutex> lock(this->framesMutex_);
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();
this->currentFrameIndex_ %= this->frames_.size();
if (this->currentFrameOffset_ > this->frames_[this->currentFrameIndex_].getDuration()) {
this->currentFrameOffset_ -= this->frames_[this->currentFrameIndex_].getDuration();
this->currentFrameIndex_ = (this->currentFrameIndex_ + 1) % this->frames_.size();
} else {
break;
}
}
this->currentPixmap = this->allFrames[this->currentFrame].image;
this->currentFramePixmap_ = this->frames_[this->currentFrameIndex_].getPixmap();
}
}
const QPixmap *Image::getPixmap()
bool Image::operator==(const Image &other) const
{
if (!this->isLoading) {
this->isLoading = true;
this->loadImage();
return nullptr;
if (this->isNull() && other.isNull()) {
return true;
}
if (this->isLoaded) {
return this->currentPixmap;
} else {
return nullptr;
}
}
qreal Image::getScale() const
{
return this->scale;
}
const QString &Image::getUrl() const
{
return this->url;
}
const QString &Image::getName() const
{
return this->name;
}
const QString &Image::getCopyString() const
{
if (this->copyString.isEmpty()) {
return this->name;
if (!this->url_.string.isEmpty() && this->url_ == other.url_) {
return true;
}
return this->copyString;
}
assert(this->frames_.size() == 1);
assert(other.frames_.size() == 1);
const QString &Image::getTooltip() const
{
return this->tooltip;
}
const QMargins &Image::getMargin() const
{
return this->margin;
}
bool Image::isAnimated() const
{
return this->animated;
}
bool Image::isHat() const
{
return this->ishat;
}
int Image::getWidth() const
{
if (this->currentPixmap == nullptr) {
return 16;
if (this->currentFramePixmap_ == other.currentFramePixmap_) {
return true;
}
return this->currentPixmap->width();
return false;
}
int Image::getScaledWidth() const
bool Image::operator!=(const Image &other) const
{
return static_cast<int>((float)this->getWidth() * this->scale *
getApp()->settings->emoteScale.getValue());
return !this->operator==(other);
}
int Image::getHeight() const
// FRAME
Image::Frame::Frame(QPixmap *nonOwning, int duration)
: nonOwning_(nonOwning)
, duration_(duration)
{
if (this->currentPixmap == nullptr) {
return 16;
}
return this->currentPixmap->height();
}
int Image::getScaledHeight() const
Image::Frame::Frame(std::unique_ptr<QPixmap> nonOwning, int duration)
: owning_(std::move(nonOwning))
, duration_(duration)
{
return static_cast<int>((float)this->getHeight() * this->scale *
getApp()->settings->emoteScale.getValue());
}
void Image::setCopyString(const QString &newCopyString)
int Image::Frame::getDuration() const
{
this->copyString = newCopyString;
return this->duration_;
}
QPixmap *Image::Frame::getPixmap() const
{
if (this->nonOwning_) return this->nonOwning_;
return this->owning_.get();
}
} // namespace chatterino
+60 -43
View File
@@ -1,69 +1,86 @@
#pragma once
#include "common/Common.hpp"
#include <QPixmap>
#include <QString>
#include <boost/noncopyable.hpp>
#include <atomic>
#include <boost/noncopyable.hpp>
#include <memory>
#include <mutex>
#include "common/NullablePtr.hpp"
namespace chatterino {
class Image : public QObject, boost::noncopyable
class Image;
using ImagePtr = std::shared_ptr<Image>;
class Image : public std::enable_shared_from_this<Image>, boost::noncopyable
{
public:
explicit Image(const QString &_url, qreal _scale = 1, const QString &_name = "",
const QString &_tooltip = "", const QMargins &_margin = QMargins(),
bool isHat = false);
static ImagePtr fromUrl(const Url &url, qreal scale = 1);
static ImagePtr fromOwningPixmap(std::unique_ptr<QPixmap> pixmap, qreal scale = 1);
static ImagePtr fromNonOwningPixmap(QPixmap *pixmap, qreal scale = 1);
static ImagePtr getEmpty();
explicit Image(QPixmap *_currentPixmap, qreal _scale = 1, const QString &_name = "",
const QString &_tooltip = "", const QMargins &_margin = QMargins(),
bool isHat = false);
~Image();
const QPixmap *getPixmap();
const Url &getUrl() const;
NullablePtr<const QPixmap> getPixmap() const;
qreal getScale() const;
const QString &getUrl() const;
const QString &getName() const;
const QString &getCopyString() const;
const QString &getTooltip() const;
const QMargins &getMargin() const;
bool isAnimated() const;
bool isHat() const;
int getWidth() const;
int getScaledWidth() const;
int getHeight() const;
int getScaledHeight() const;
bool isLoaded() const;
bool isError() const;
bool isValid() const;
bool isNull() const;
void setCopyString(const QString &newCopyString);
bool operator==(const Image &image) const;
bool operator!=(const Image &image) const;
private:
struct FrameData {
QPixmap *image;
int duration;
class Frame
{
public:
QPixmap *getPixmap() const;
int getDuration() const;
Frame(QPixmap *nonOwning, int duration = 1);
Frame(std::unique_ptr<QPixmap> nonOwning, int duration = 1);
private:
QPixmap *nonOwning_;
std::unique_ptr<QPixmap> owning_;
int duration_;
};
static bool loadedEventQueued;
Image();
Image(const Url &url, qreal scale);
Image(std::unique_ptr<QPixmap> owning, qreal scale);
Image(QPixmap *nonOwning, qreal scale);
QPixmap *currentPixmap = nullptr;
QPixmap *loadedPixmap = nullptr;
std::vector<FrameData> allFrames;
int currentFrame = 0;
int currentFrameOffset = 0;
void load();
Outcome parse(const QByteArray &data);
std::vector<Frame> readFrames(QImageReader &reader);
Outcome setFrames(std::vector<Frame> frames);
void updateAnimation();
void queueLoadedEvent();
QString url;
QString name;
QString copyString;
QString tooltip;
bool animated = false;
QMargins margin;
bool ishat;
qreal scale;
Url url_;
bool isLoaded_{false};
bool isLoading_{false};
bool isAnimated_{false};
bool isError_{false};
bool isNull_ = false;
qreal scale_ = 1;
QObject object_;
bool isLoading = false;
std::atomic<bool> isLoaded{false};
std::vector<Frame> frames_;
std::mutex framesMutex_;
NullablePtr<QPixmap> currentFramePixmap_;
int currentFrameIndex_ = 0;
int currentFrameOffset_ = 0;
void loadImage();
void gifUpdateTimout();
static std::atomic<bool> loadedEventQueued;
};
} // namespace chatterino
+95
View File
@@ -0,0 +1,95 @@
#include "ImageSet.hpp"
#include "Application.hpp"
#include "singletons/Resources.hpp"
#include "singletons/Settings.hpp"
namespace chatterino {
ImageSet::ImageSet()
: imageX1_(Image::getEmpty())
, imageX2_(Image::getEmpty())
, imageX3_(Image::getEmpty())
{
}
ImageSet::ImageSet(const ImagePtr &image1, const ImagePtr &image2, const ImagePtr &image3)
: imageX1_(image1)
, imageX2_(image2)
, imageX3_(image3)
{
}
ImageSet::ImageSet(const Url &image1, const Url &image2, const Url &image3)
: imageX1_(Image::fromUrl(image1, 1))
, imageX2_(Image::fromUrl(image2, 0.5))
, imageX3_(Image::fromUrl(image3, 0.25))
{
}
void ImageSet::setImage1(const ImagePtr &image)
{
this->imageX1_ = image;
}
void ImageSet::setImage2(const ImagePtr &image)
{
this->imageX2_ = image;
}
void ImageSet::setImage3(const ImagePtr &image)
{
this->imageX3_ = image;
}
const ImagePtr &ImageSet::getImage1() const
{
return this->imageX1_;
}
const ImagePtr &ImageSet::getImage2() const
{
return this->imageX2_;
}
const ImagePtr &ImageSet::getImage3() const
{
return this->imageX3_;
}
const ImagePtr &ImageSet::getImage(float scale) const
{
int quality = getSettings()->preferredEmoteQuality;
if (!quality) {
if (scale > 3.999)
quality = 3;
else if (scale > 1.999)
quality = 2;
else
scale = 1;
}
if (this->imageX3_->isValid() && quality == 3) {
return this->imageX3_;
}
if (this->imageX2_->isValid() && quality == 2) {
return this->imageX3_;
}
return this->imageX1_;
}
bool ImageSet::operator==(const ImageSet &other) const
{
return std::tie(this->imageX1_, this->imageX2_, this->imageX3_) ==
std::tie(other.imageX1_, other.imageX2_, other.imageX3_);
}
bool ImageSet::operator!=(const ImageSet &other) const
{
return !this->operator==(other);
}
} // namespace chatterino
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include "messages/Image.hpp"
namespace chatterino {
class ImageSet
{
public:
ImageSet();
ImageSet(const ImagePtr &image1, const ImagePtr &image2 = Image::getEmpty(),
const ImagePtr &image3 = Image::getEmpty());
ImageSet(const Url &image1, const Url &image2 = {}, const Url &image3 = {});
void setImage1(const ImagePtr &image);
void setImage2(const ImagePtr &image);
void setImage3(const ImagePtr &image);
const ImagePtr &getImage1() const;
const ImagePtr &getImage2() const;
const ImagePtr &getImage3() const;
const ImagePtr &getImage(float scale) const;
ImagePtr getImage(float scale);
bool operator==(const ImageSet &other) const;
bool operator!=(const ImageSet &other) const;
private:
ImagePtr imageX1_;
ImagePtr imageX2_;
ImagePtr imageX3_;
};
} // namespace chatterino
+28 -28
View File
@@ -60,18 +60,18 @@ MessageElement::Flags MessageElement::getFlags() const
}
// IMAGE
ImageElement::ImageElement(Image *image, MessageElement::Flags flags)
ImageElement::ImageElement(ImagePtr image, MessageElement::Flags flags)
: MessageElement(flags)
, image_(image)
{
this->setTooltip(image->getTooltip());
// this->setTooltip(image->getTooltip());
}
void ImageElement::addToContainer(MessageLayoutContainer &container, MessageElement::Flags flags)
{
if (flags & this->getFlags()) {
QSize size(this->image_->getScaledWidth() * container.getScale(),
this->image_->getScaledHeight() * container.getScale());
auto size = QSize(this->image_->getWidth() * container.getScale(),
this->image_->getHeight() * container.getScale());
container.addElement(
(new ImageLayoutElement(*this, this->image_, size))->setLink(this->getLink()));
@@ -79,29 +79,32 @@ void ImageElement::addToContainer(MessageLayoutContainer &container, MessageElem
}
// EMOTE
EmoteElement::EmoteElement(const EmoteData &data, MessageElement::Flags flags)
EmoteElement::EmoteElement(const EmotePtr &emote, MessageElement::Flags flags)
: MessageElement(flags)
, data(data)
, emote_(emote)
{
if (data.isValid()) {
this->setTooltip(data.image1x->getTooltip());
this->textElement_.reset(
new TextElement(data.image1x->getCopyString(), MessageElement::Misc));
auto image = emote->images.getImage1();
if (image->isValid()) {
this->textElement_.reset(new TextElement(emote->getCopyString(), MessageElement::Misc));
}
this->setTooltip(emote->tooltip.string);
}
EmotePtr EmoteElement::getEmote() const
{
return this->emote_;
}
void EmoteElement::addToContainer(MessageLayoutContainer &container, MessageElement::Flags flags)
{
if (flags & this->getFlags()) {
if (flags & MessageElement::EmoteImages) {
if (!this->data.isValid()) {
return;
}
auto image = this->emote_->images.getImage(container.getScale());
if (!image->isValid()) return;
Image *image = this->data.getImage(container.getScale());
QSize size(int(container.getScale() * image->getScaledWidth()),
int(container.getScale() * image->getScaledHeight()));
QSize size(int(container.getScale() * image->getWidth()),
int(container.getScale() * image->getHeight()));
container.addElement(
(new ImageLayoutElement(*this, image, size))->setLink(this->getLink()));
@@ -173,7 +176,6 @@ void TextElement::addToContainer(MessageLayoutContainer &container, MessageEleme
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]);
@@ -184,7 +186,6 @@ void TextElement::addToContainer(MessageLayoutContainer &container, MessageEleme
container.breakLine();
wordStart = i;
lastWidth = width;
width = 0;
if (textLength > i + 2) {
width += metrics.width(text[i]);
@@ -196,8 +197,6 @@ void TextElement::addToContainer(MessageLayoutContainer &container, MessageEleme
width += charWidth;
}
UNUSED(lastWidth); // XXX: What should this be used for (if anything)? KKona
container.addElement(
getTextLayoutElement(text.mid(wordStart), width, this->hasTrailingSpace()));
container.breakLine();
@@ -249,14 +248,15 @@ void TwitchModerationElement::addToContainer(MessageLayoutContainer &container,
if (flags & MessageElement::ModeratorTools) {
QSize size(int(container.getScale() * 16), int(container.getScale() * 16));
for (const ModerationAction &m : getApp()->moderationActions->items.getVector()) {
if (m.isImage()) {
container.addElement((new ImageLayoutElement(*this, m.getImage(), size))
->setLink(Link(Link::UserAction, m.getAction())));
for (const auto &action : getApp()->moderationActions->items.getVector()) {
if (auto image = action.getImage()) {
container.addElement((new ImageLayoutElement(*this, image.get(), size))
->setLink(Link(Link::UserAction, action.getAction())));
} else {
container.addElement((new TextIconLayoutElement(*this, m.getLine1(), m.getLine2(),
container.getScale(), size))
->setLink(Link(Link::UserAction, m.getAction())));
container.addElement(
(new TextIconLayoutElement(*this, action.getLine1(), action.getLine2(),
container.getScale(), size))
->setLink(Link(Link::UserAction, action.getAction())));
}
}
}
+6 -7
View File
@@ -1,6 +1,7 @@
#pragma once
#include "common/Emotemap.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
#include "messages/Link.hpp"
#include "messages/MessageColor.hpp"
@@ -16,7 +17,6 @@
namespace chatterino {
class Channel;
struct EmoteData;
struct MessageLayoutContainer;
class MessageElement : boost::noncopyable
@@ -133,12 +133,12 @@ private:
class ImageElement : public MessageElement
{
public:
ImageElement(Image *image, MessageElement::Flags flags);
ImageElement(ImagePtr image, MessageElement::Flags flags);
void addToContainer(MessageLayoutContainer &container, MessageElement::Flags flags) override;
private:
Image *image_;
ImagePtr image_;
};
// contains a text, it will split it into words
@@ -169,15 +169,14 @@ private:
class EmoteElement : public MessageElement
{
public:
EmoteElement(const EmoteData &data, MessageElement::Flags flags_);
~EmoteElement() override = default;
EmoteElement(const EmotePtr &data, MessageElement::Flags flags_);
void addToContainer(MessageLayoutContainer &container, MessageElement::Flags flags_) override;
const EmoteData data;
EmotePtr getEmote() const;
private:
std::unique_ptr<TextElement> textElement_;
EmotePtr emote_;
};
// contains a text, formated depending on the preferences
+15 -19
View File
@@ -63,16 +63,17 @@ const Link &MessageLayoutElement::getLink() const
// IMAGE
//
ImageLayoutElement::ImageLayoutElement(MessageElement &_creator, Image *_image, const QSize &_size)
: MessageLayoutElement(_creator, _size)
, image(_image)
ImageLayoutElement::ImageLayoutElement(MessageElement &creator, ImagePtr image, const QSize &size)
: MessageLayoutElement(creator, size)
, image_(image)
{
this->trailingSpace = _creator.hasTrailingSpace();
this->trailingSpace = creator.hasTrailingSpace();
}
void ImageLayoutElement::addCopyTextToString(QString &str, int from, int to) const
{
str += this->image->getCopyString();
// str += this->image_->getCopyString();
str += "not implemented";
if (this->hasTrailingSpace()) {
str += " ";
@@ -86,13 +87,12 @@ int ImageLayoutElement::getSelectionIndexCount()
void ImageLayoutElement::paint(QPainter &painter)
{
if (this->image == nullptr) {
if (this->image_ == nullptr) {
return;
}
const QPixmap *pixmap = this->image->getPixmap();
if (pixmap != nullptr && !this->image->isAnimated()) {
auto pixmap = this->image_->getPixmap();
if (pixmap && !this->image_->isAnimated()) {
// fourtf: make it use qreal values
painter.drawPixmap(QRectF(this->getRect()), *pixmap, QRectF());
}
@@ -100,19 +100,15 @@ void ImageLayoutElement::paint(QPainter &painter)
void ImageLayoutElement::paintAnimated(QPainter &painter, int yOffset)
{
if (this->image == nullptr) {
if (this->image_ == nullptr) {
return;
}
if (this->image->isAnimated()) {
// qDebug() << this->image->getUrl();
auto pixmap = this->image->getPixmap();
if (pixmap != nullptr) {
// fourtf: make it use qreal values
QRect _rect = this->getRect();
_rect.moveTop(_rect.y() + yOffset);
painter.drawPixmap(QRectF(_rect), *pixmap, QRectF());
if (this->image_->isAnimated()) {
if (auto pixmap = this->image_->getPixmap()) {
auto rect = this->getRect();
rect.moveTop(rect.y() + yOffset);
painter.drawPixmap(QRectF(rect), *pixmap, QRectF());
}
}
}
@@ -7,6 +7,7 @@
#include <boost/noncopyable.hpp>
#include <climits>
#include "messages/Image.hpp"
#include "messages/Link.hpp"
#include "messages/MessageColor.hpp"
#include "singletons/Fonts.hpp"
@@ -15,7 +16,6 @@ class QPainter;
namespace chatterino {
class MessageElement;
class Image;
class MessageLayoutElement : boost::noncopyable
{
@@ -52,7 +52,7 @@ private:
class ImageLayoutElement : public MessageLayoutElement
{
public:
ImageLayoutElement(MessageElement &creator_, Image *image, const QSize &size);
ImageLayoutElement(MessageElement &creator, ImagePtr image, const QSize &size);
protected:
void addCopyTextToString(QString &str, int from = 0, int to = INT_MAX) const override;
@@ -63,7 +63,7 @@ protected:
int getXFromIndex(int index) override;
private:
Image *image;
ImagePtr image_;
};
// TEXT