mirror-chatterino2/messages/lazyloadedimage.cpp

126 lines
3.1 KiB
C++
Raw Normal View History

2017-01-18 21:30:23 +01:00
#include "messages/lazyloadedimage.h"
2017-01-04 15:12:31 +01:00
2017-01-11 18:52:09 +01:00
#include "asyncexec.h"
2017-01-15 16:38:30 +01:00
#include "emotes.h"
2017-01-11 18:52:09 +01:00
#include "ircmanager.h"
#include "windows.h"
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-01-11 18:52:09 +01:00
#include <functional>
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace messages {
2017-01-11 18:52:09 +01:00
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale,
const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
2017-02-06 17:42:28 +01:00
: currentPixmap(NULL)
, allFrames()
, currentFrame(0)
, currentFrameOffset(0)
2017-01-18 04:33:30 +01:00
, url(url)
, name(name)
, tooltip(tooltip)
, animated(false)
, margin(margin)
, ishat(isHat)
, scale(scale)
, isLoading(false)
2017-01-05 16:07:20 +01:00
{
}
2017-01-13 18:59:11 +01:00
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale,
2017-01-11 18:52:09 +01:00
const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
2017-02-06 17:42:28 +01:00
: currentPixmap(image)
, allFrames()
, currentFrame(0)
, currentFrameOffset(0)
2017-01-18 04:33:30 +01:00
, url()
, name(name)
, tooltip(tooltip)
, animated(false)
, margin(margin)
, ishat(isHat)
, scale(scale)
, isLoading(true)
2017-01-11 18:52:09 +01:00
{
}
void
LazyLoadedImage::loadImage()
2017-01-04 15:12:31 +01:00
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
2017-01-18 04:33:30 +01:00
QUrl url(this->url);
2017-01-13 18:59:11 +01:00
QNetworkRequest request(url);
QNetworkReply *reply = manager->get(request);
2017-01-11 18:52:09 +01:00
2017-01-13 18:59:11 +01:00
QObject::connect(reply, &QNetworkReply::finished, [=] {
2017-02-06 17:42:28 +01:00
QByteArray array = reply->readAll();
QBuffer buffer(&array);
buffer.open(QIODevice::ReadOnly);
2017-01-11 18:52:09 +01:00
2017-02-06 17:42:28 +01:00
QImage image;
QImageReader reader(&buffer);
bool first = true;
for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) {
auto pixmap = new QPixmap(QPixmap::fromImage(image));
if (first) {
first = false;
this->currentPixmap = pixmap;
}
FrameData data;
data.duration = std::max(20, reader.nextImageDelay());
data.image = pixmap;
allFrames.push_back(data);
}
}
if (allFrames.size() > 1) {
QObject::connect(&Emotes::getGifUpdateTimer(), &QTimer::timeout,
[this] { gifUpdateTimout(); });
2017-01-13 18:59:11 +01:00
}
2017-01-04 15:12:31 +01:00
2017-01-15 16:38:30 +01:00
Emotes::incGeneration();
Windows::layoutVisibleChatWidgets();
2017-01-26 17:26:20 +01:00
reply->deleteLater();
manager->deleteLater();
2017-01-13 18:59:11 +01:00
});
2017-02-06 17:42:28 +01:00
}
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();
} else {
break;
}
2017-02-06 17:42:28 +01:00
}
this->currentPixmap = this->allFrames[this->currentFrame].image;
2017-01-04 15:12:31 +01:00
}
2017-01-18 21:30:23 +01:00
}
}