mirror-chatterino2/src/messages/image.cpp

261 lines
6.2 KiB
C++
Raw Normal View History

#include "messages/image.hpp"
#include "application.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/emotemanager.hpp"
#include "singletons/ircmanager.hpp"
#include "singletons/windowmanager.hpp"
#include "util/networkmanager.hpp"
2018-01-19 23:41:02 +01:00
#include "util/posttothread.hpp"
2017-06-11 09:31:45 +02:00
#include "util/urlfetch.hpp"
2017-01-11 18:52:09 +01:00
2017-02-06 17:42:28 +01:00
#include <QBuffer>
#include <QImageReader>
2017-01-11 18:52:09 +01:00
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
2017-02-06 17:42:28 +01:00
#include <QTimer>
2017-06-06 21:18:05 +02:00
2017-01-11 18:52:09 +01:00
#include <functional>
#include <thread>
2017-01-11 18:52:09 +01:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace messages {
bool Image::loadedEventQueued = false;
Image::Image(const QString &url, qreal scale, const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
: url(url)
2017-09-12 19:06:16 +02:00
, name(name)
, tooltip(tooltip)
, margin(margin)
, ishat(isHat)
, scale(scale)
2017-01-05 16:07:20 +01:00
{
2018-04-06 16:37:30 +02:00
util::DebugCount::increase("images");
2017-01-05 16:07:20 +01:00
}
Image::Image(QPixmap *image, qreal scale, const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
2017-12-17 02:18:13 +01:00
: currentPixmap(image)
2017-09-12 19:06:16 +02:00
, name(name)
, tooltip(tooltip)
, margin(margin)
, ishat(isHat)
, scale(scale)
, isLoading(true)
2018-01-19 23:41:02 +01:00
, isLoaded(true)
2017-01-11 18:52:09 +01:00
{
2018-04-06 16:37:30 +02:00
util::DebugCount::increase("images");
}
Image::~Image()
{
util::DebugCount::decrease("images");
if (this->isAnimated()) {
util::DebugCount::decrease("animated images");
}
if (this->isLoaded) {
util::DebugCount::decrease("loaded images");
}
2017-01-11 18:52:09 +01:00
}
void Image::loadImage()
2017-01-04 15:12:31 +01:00
{
util::NetworkRequest req(this->getUrl());
req.setCaller(this);
2018-01-19 22:45:33 +01:00
req.setUseQuickLoadCache(true);
req.get([this](QByteArray bytes) -> bool {
2018-01-19 22:45:33 +01:00
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()) {
util::DebugCount::decrease("animated images");
}
if (this->isLoaded) {
util::DebugCount::decrease("loaded images");
}
if (reader.imageCount() == -1) {
// An error occured in the reader
debug::Log("An error occured reading the image: '{}'", reader.errorString());
debug::Log("Image url: {}", this->url);
return false;
}
if (reader.imageCount() == 0) {
debug::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;
}
chatterino::messages::Image::FrameData data;
data.duration = std::max(20, reader.nextImageDelay());
data.image = pixmap;
this->allFrames.push_back(data);
}
}
if (this->allFrames.size() != reader.imageCount()) {
// debug::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) {
util::postToThread([this] {
getApp()->emotes->getGifUpdateSignal().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;
2018-04-06 16:37:30 +02:00
util::DebugCount::increase("animated images");
}
this->currentPixmap = this->loadedPixmap;
2018-01-19 22:45:33 +01:00
this->isLoaded = true;
2018-04-06 16:37:30 +02:00
util::DebugCount::increase("loaded images");
2018-01-19 22:45:33 +01:00
if (!loadedEventQueued) {
loadedEventQueued = true;
2018-04-18 17:10:17 +02:00
QTimer::singleShot(500, [] {
getApp()->emotes->incGeneration();
2018-04-18 17:10:17 +02:00
auto app = getApp();
app->windows->layoutVisibleChatWidgets();
loadedEventQueued = false;
});
}
return true;
});
2017-02-06 17:42:28 +01:00
}
void Image::gifUpdateTimout()
2017-02-06 17:42:28 +01:00
{
2018-01-19 22:45:33 +01:00
if (this->animated) {
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();
} else {
break;
}
}
this->currentPixmap = this->allFrames[this->currentFrame].image;
}
2017-01-04 15:12:31 +01:00
}
2017-09-12 19:06:16 +02:00
const QPixmap *Image::getPixmap()
2017-09-12 19:06:16 +02:00
{
if (!this->isLoading) {
this->isLoading = true;
2018-01-19 22:45:33 +01:00
this->loadImage();
return nullptr;
}
if (this->isLoaded) {
return this->currentPixmap;
} else {
return nullptr;
2017-09-12 19:06:16 +02:00
}
}
qreal Image::getScale() const
2017-09-12 19:06:16 +02:00
{
return this->scale;
}
const QString &Image::getUrl() const
2017-09-12 19:06:16 +02:00
{
return this->url;
}
const QString &Image::getName() const
2017-09-12 19:06:16 +02:00
{
return this->name;
}
const QString &Image::getTooltip() const
2017-09-12 19:06:16 +02:00
{
return this->tooltip;
}
const QMargins &Image::getMargin() const
2017-09-12 19:06:16 +02:00
{
return this->margin;
}
bool Image::isAnimated() const
2017-09-12 19:06:16 +02:00
{
return this->animated;
}
bool Image::isHat() const
2017-09-12 19:06:16 +02:00
{
return this->ishat;
}
int Image::getWidth() const
2017-09-12 19:06:16 +02:00
{
if (this->currentPixmap == nullptr) {
return 16;
}
2018-01-19 22:45:33 +01:00
2017-09-12 19:06:16 +02:00
return this->currentPixmap->width();
}
int Image::getScaledWidth() const
2017-09-12 19:06:16 +02:00
{
return static_cast<int>(this->getWidth() * this->scale);
2017-09-12 19:06:16 +02:00
}
int Image::getHeight() const
2017-09-12 19:06:16 +02:00
{
if (this->currentPixmap == nullptr) {
return 16;
}
return this->currentPixmap->height();
}
int Image::getScaledHeight() const
2017-09-12 19:06:16 +02:00
{
return static_cast<int>(this->getHeight() * this->scale);
2017-09-12 19:06:16 +02:00
}
2017-04-14 17:52:22 +02:00
} // namespace messages
} // namespace chatterino