Merge branch '4tf'

This commit is contained in:
fourtf
2018-08-06 18:41:30 +02:00
197 changed files with 3807 additions and 2801 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
+249 -220
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"
@@ -18,259 +19,287 @@
#include <thread>
namespace chatterino {
namespace {
// Frame
Frame::Frame(const QPixmap *nonOwning, int duration)
: nonOwning_(nonOwning)
, duration_(duration)
{
}
bool Image::loadedEventQueued = false;
Frame::Frame(std::unique_ptr<QPixmap> owning, int duration)
: owning_(std::move(owning))
, duration_(duration)
{
}
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)
int Frame::duration() const
{
return this->duration_;
}
const QPixmap *Frame::pixmap() const
{
if (this->nonOwning_) return this->nonOwning_;
return this->owning_.get();
}
// Frames
Frames::Frames()
{
DebugCount::increase("images");
}
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)
Frames::Frames(std::vector<Frame> &&frames)
: items_(std::move(frames))
{
DebugCount::increase("images");
if (this->animated()) DebugCount::increase("animated images");
}
Image::~Image()
Frames::~Frames()
{
DebugCount::decrease("images");
if (this->animated()) DebugCount::decrease("animated images");
}
if (this->isAnimated()) {
DebugCount::decrease("animated images");
}
void Frames::advance()
{
this->timeOffset_ += GIF_FRAME_LENGTH;
if (this->isLoaded) {
DebugCount::decrease("loaded images");
while (true) {
this->index_ %= this->items_.size();
if (this->timeOffset_ > this->items_[this->index_].duration()) {
this->timeOffset_ -= this->items_[this->index_].duration();
this->index_ = (this->index_ + 1) % this->items_.size();
} else {
break;
}
}
}
void Image::loadImage()
bool Frames::animated() const
{
NetworkRequest req(this->getUrl());
req.setCaller(this);
req.setUseQuickLoadCache(true);
req.onSuccess([this](auto result) -> bool {
auto bytes = result.getData();
QByteArray copy = QByteArray::fromRawData(bytes.constData(), bytes.length());
QBuffer buffer(&copy);
buffer.open(QIODevice::ReadOnly);
return this->items_.size() > 1;
}
QImage image;
const QPixmap *Frames::current() const
{
if (this->items_.size() == 0) return nullptr;
return this->items_[this->index_].pixmap();
}
const QPixmap *Frames::first() const
{
if (this->items_.size() == 0) return nullptr;
return this->items_.front().pixmap();
}
// functions
std::vector<Frame> readFrames(QImageReader &reader, const Url &url)
{
std::vector<Frame> frames;
if (reader.imageCount() <= 0) {
Log("Error while reading image {}: '{}'", url.string, reader.errorString());
return frames;
}
QImage image;
for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) {
auto pixmap = std::make_unique<QPixmap>(QPixmap::fromImage(image));
int duration = std::max(20, reader.nextImageDelay());
frames.push_back(Frame(std::move(pixmap), duration));
}
}
if (frames.size() != 0) {
Log("Error while reading image {}: '{}'", url.string, reader.errorString());
}
return frames;
}
void queueLoadedEvent()
{
static auto eventQueued = false;
if (!eventQueued) {
eventQueued = true;
QTimer::singleShot(250, [] {
getApp()->windows->incGeneration();
getApp()->windows->layoutChannelViews();
eventQueued = false;
});
}
}
} // namespace
// IMAGE2
std::atomic<bool> Image::loadedEventQueued{false};
ImagePtr Image::fromUrl(const Url &url, qreal scale)
{
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;
}
ImagePtr Image::fromOwningPixmap(std::unique_ptr<QPixmap> pixmap, qreal scale)
{
return ImagePtr(new Image(std::move(pixmap), scale));
}
ImagePtr Image::fromNonOwningPixmap(QPixmap *pixmap, qreal scale)
{
return ImagePtr(new Image(pixmap, scale));
}
ImagePtr Image::getEmpty()
{
static auto empty = ImagePtr(new Image);
return empty;
}
Image::Image()
: empty_(true)
{
}
Image::Image(const Url &url, qreal scale)
: url_(url)
, scale_(scale)
, shouldLoad_(true)
{
}
Image::Image(std::unique_ptr<QPixmap> owning, qreal scale)
: scale_(scale)
{
std::vector<Frame> vec;
vec.push_back(Frame(std::move(owning)));
this->frames_ = std::move(vec);
}
Image::Image(QPixmap *nonOwning, qreal scale)
: scale_(scale)
{
std::vector<Frame> vec;
vec.push_back(Frame(nonOwning));
this->frames_ = std::move(vec);
}
const Url &Image::url() const
{
return this->url_;
}
const QPixmap *Image::pixmap() const
{
assertInGuiThread();
if (this->shouldLoad_) {
const_cast<Image *>(this)->shouldLoad_ = false;
const_cast<Image *>(this)->load();
}
return this->frames_.current();
}
qreal Image::scale() const
{
return this->scale_;
}
bool Image::empty() const
{
return this->empty_;
}
bool Image::animated() const
{
assertInGuiThread();
return this->frames_.animated();
}
int Image::width() const
{
assertInGuiThread();
if (auto pixmap = this->frames_.first())
return pixmap->width() * this->scale_;
else
return 16;
}
int Image::height() const
{
assertInGuiThread();
if (auto pixmap = this->frames_.first())
return pixmap->height() * this->scale_;
else
return 16;
}
void Image::load()
{
NetworkRequest req(this->url().string);
req.setCaller(&this->object_);
req.setUseQuickLoadCache(true);
req.onSuccess([this, weak = weakOf(this)](auto result) -> Outcome {
assertInGuiThread();
auto shared = weak.lock();
if (!shared) return Failure;
// const cast since we are only reading from it
QBuffer buffer(const_cast<QByteArray *>(&result.getData()));
buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer);
bool first = true;
this->frames_ = readFrames(reader, this->url());
return Success;
});
req.onError([this, weak = weakOf(this)](int) {
auto shared = weak.lock();
if (!shared) return false;
// clear stuff before loading the image again
this->allFrames.clear();
if (this->isAnimated()) {
DebugCount::decrease("animated images");
}
if (this->isLoaded) {
DebugCount::decrease("loaded images");
}
this->frames_ = std::vector<Frame>();
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 false;
});
req.execute();
}
void Image::gifUpdateTimout()
bool Image::operator==(const Image &other) const
{
if (this->animated) {
this->currentFrameOffset += GIF_FRAME_LENGTH;
if (this->empty() && other.empty()) return true;
if (!this->url_.string.isEmpty() && this->url_ == other.url_) return true;
if (this->frames_.first() == other.frames_.first()) return true;
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();
} else {
break;
}
}
this->currentPixmap = this->allFrames[this->currentFrame].image;
}
return false;
}
const QPixmap *Image::getPixmap()
bool Image::operator!=(const Image &other) const
{
if (!this->isLoading) {
this->isLoading = true;
this->loadImage();
return nullptr;
}
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;
}
return this->copyString;
}
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;
}
return this->currentPixmap->width();
}
int Image::getScaledWidth() const
{
return static_cast<int>((float)this->getWidth() * this->scale *
getApp()->settings->emoteScale.getValue());
}
int Image::getHeight() const
{
if (this->currentPixmap == nullptr) {
return 16;
}
return this->currentPixmap->height();
}
int Image::getScaledHeight() const
{
return static_cast<int>((float)this->getHeight() * this->scale *
getApp()->settings->emoteScale.getValue());
}
void Image::setCopyString(const QString &newCopyString)
{
this->copyString = newCopyString;
return !this->operator==(other);
}
} // namespace chatterino
+75 -54
View File
@@ -1,69 +1,90 @@
#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
namespace {
class Frame
{
public:
explicit Image(const QString &_url, qreal _scale = 1, const QString &_name = "",
const QString &_tooltip = "", const QMargins &_margin = QMargins(),
bool isHat = false);
explicit Frame(const QPixmap *nonOwning, int duration = 1);
explicit Frame(std::unique_ptr<QPixmap> owning, int duration = 1);
explicit Image(QPixmap *_currentPixmap, qreal _scale = 1, const QString &_name = "",
const QString &_tooltip = "", const QMargins &_margin = QMargins(),
bool isHat = false);
~Image();
const QPixmap *getPixmap();
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;
void setCopyString(const QString &newCopyString);
const QPixmap *pixmap() const;
int duration() const;
private:
struct FrameData {
QPixmap *image;
int duration;
};
static bool loadedEventQueued;
QPixmap *currentPixmap = nullptr;
QPixmap *loadedPixmap = nullptr;
std::vector<FrameData> allFrames;
int currentFrame = 0;
int currentFrameOffset = 0;
QString url;
QString name;
QString copyString;
QString tooltip;
bool animated = false;
QMargins margin;
bool ishat;
qreal scale;
bool isLoading = false;
std::atomic<bool> isLoaded{false};
void loadImage();
void gifUpdateTimout();
const QPixmap *nonOwning_{nullptr};
std::unique_ptr<QPixmap> owning_{};
int duration_{};
};
class Frames
{
public:
Frames();
Frames(std::vector<Frame> &&frames);
~Frames();
Frames(Frames &&other) = default;
Frames &operator=(Frames &&other) = default;
bool animated() const;
void advance();
const QPixmap *current() const;
const QPixmap *first() const;
private:
std::vector<Frame> items_;
int index_{0};
int timeOffset_{0};
};
} // namespace
class Image;
using ImagePtr = std::shared_ptr<Image>;
class Image : public std::enable_shared_from_this<Image>, boost::noncopyable
{
public:
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();
const Url &url() const;
const QPixmap *pixmap() const;
qreal scale() const;
bool empty() const;
int width() const;
int height() const;
bool animated() const;
bool operator==(const Image &image) const;
bool operator!=(const Image &image) const;
private:
Image();
Image(const Url &url, qreal scale);
Image(std::unique_ptr<QPixmap> owning, qreal scale);
Image(QPixmap *nonOwning, qreal scale);
void load();
Url url_{};
qreal scale_{1};
bool empty_{false};
bool shouldLoad_{false};
Frames frames_{};
QObject object_{};
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_->empty() && quality == 3) {
return this->imageX3_;
}
if (!this->imageX2_->empty() && 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
+1
View File
@@ -1,6 +1,7 @@
#pragma once
#include <QString>
#include <common/Common.hpp>
namespace chatterino {
+27 -30
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_->width() * container.getScale(),
this->image_->height() * container.getScale());
container.addElement(
(new ImageLayoutElement(*this, this->image_, size))->setLink(this->getLink()));
@@ -79,29 +79,29 @@ 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));
}
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->empty()) return;
Image *image = this->data.getImage(container.getScale());
QSize size(int(container.getScale() * image->getScaledWidth()),
int(container.getScale() * image->getScaledHeight()));
auto size = QSize(int(container.getScale() * image->width()),
int(container.getScale() * image->height()));
container.addElement(
(new ImageLayoutElement(*this, image, size))->setLink(this->getLink()));
@@ -120,7 +120,7 @@ TextElement::TextElement(const QString &text, MessageElement::Flags flags,
, color_(color)
, style_(style)
{
for (QString word : text.split(' ')) {
for (const auto &word : text.split(' ')) {
this->words_.push_back({word, -1});
// fourtf: add logic to store multiple spaces after message
}
@@ -173,7 +173,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 +183,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 +194,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 +245,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
@@ -137,12 +137,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
@@ -173,15 +173,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_->pixmap();
if (pixmap && !this->image_->animated()) {
// 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_->animated()) {
if (auto pixmap = this->image_->pixmap()) {
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