mirror-chatterino2/messages/lazyloadedimage.cpp

108 lines
2.8 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-04-12 17:46:44 +02:00
#include "emotemanager.h"
2017-01-11 18:52:09 +01:00
#include "ircmanager.h"
2017-04-14 14:52:31 +02:00
#include "util/urlfetch.h"
2017-04-12 17:46:44 +02:00
#include "windowmanager.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-04-12 17:46:44 +02:00
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale, const QString &name,
const QString &tooltip, const QMargins &margin, bool isHat)
2017-04-14 14:52:31 +02:00
: _currentPixmap(NULL)
, _currentFrame(0)
, _currentFrameOffset(0)
, _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-04-12 17:46:44 +02:00
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale, const QString &name,
const QString &tooltip, const QMargins &margin, bool isHat)
2017-04-14 14:52:31 +02:00
: _currentPixmap(image)
, _currentFrame(0)
, _currentFrameOffset(0)
, _name(name)
, _tooltip(tooltip)
, _animated(false)
, _margin(margin)
, _ishat(isHat)
, _scale(scale)
, _isLoading(true)
2017-01-11 18:52:09 +01:00
{
}
2017-04-12 17:46:44 +02:00
void LazyLoadedImage::loadImage()
2017-01-04 15:12:31 +01:00
{
2017-04-14 14:52:31 +02:00
util::urlFetch(_url, [=](QNetworkReply &reply) {
QByteArray array = reply.readAll();
2017-02-06 17:42:28 +01:00
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;
2017-04-14 14:52:31 +02:00
_currentPixmap = pixmap;
2017-02-06 17:42:28 +01:00
}
FrameData data;
data.duration = std::max(20, reader.nextImageDelay());
data.image = pixmap;
2017-04-14 14:52:31 +02:00
_allFrames.push_back(data);
2017-02-06 17:42:28 +01:00
}
}
2017-04-14 14:52:31 +02:00
if (_allFrames.size() > 1) {
_animated = true;
2017-02-07 00:03:15 +01:00
2017-04-12 17:46:44 +02:00
EmoteManager::getInstance().getGifUpdateSignal().connect([this] { gifUpdateTimout(); });
2017-01-13 18:59:11 +01:00
}
2017-01-04 15:12:31 +01:00
2017-04-12 17:46:44 +02:00
EmoteManager::getInstance().incGeneration();
2017-04-13 19:25:33 +02:00
WindowManager::getInstance().layoutVisibleChatWidgets();
2017-01-13 18:59:11 +01:00
});
2017-02-06 17:42:28 +01:00
}
2017-04-12 17:46:44 +02:00
void LazyLoadedImage::gifUpdateTimout()
2017-02-06 17:42:28 +01:00
{
2017-04-14 14:52:31 +02:00
_currentFrameOffset += GIF_FRAME_LENGTH;
while (true) {
2017-04-14 14:52:31 +02:00
if (_currentFrameOffset > _allFrames.at(_currentFrame).duration) {
_currentFrameOffset -= _allFrames.at(_currentFrame).duration;
_currentFrame = (_currentFrame + 1) % _allFrames.size();
} else {
break;
}
2017-02-06 17:42:28 +01:00
}
2017-04-14 14:52:31 +02:00
_currentPixmap = _allFrames[_currentFrame].image;
2017-01-04 15:12:31 +01:00
}
2017-04-13 19:25:33 +02:00
} // namespace messages
} // namespace chatterino