mirror-chatterino2/src/messages/lazyloadedimage.cpp

110 lines
3 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "messages/lazyloadedimage.hpp"
#include "asyncexec.hpp"
#include "emotemanager.hpp"
#include "ircmanager.hpp"
#include "util/urlfetch.hpp"
#include "windowmanager.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>
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace messages {
LazyLoadedImage::LazyLoadedImage(EmoteManager &_emoteManager, WindowManager &_windowManager,
const QString &url, qreal scale, const QString &name,
2017-04-12 17:46:44 +02:00
const QString &tooltip, const QMargins &margin, bool isHat)
: emoteManager(_emoteManager)
, windowManager(_windowManager)
, _currentPixmap(nullptr)
2017-04-14 14:52:31 +02:00
, _url(url)
, _name(name)
, _tooltip(tooltip)
, _margin(margin)
, _ishat(isHat)
, _scale(scale)
, _isLoading(false)
2017-01-05 16:07:20 +01:00
{
}
LazyLoadedImage::LazyLoadedImage(EmoteManager &_emoteManager, WindowManager &_windowManager,
QPixmap *image, qreal scale, const QString &name,
2017-04-12 17:46:44 +02:00
const QString &tooltip, const QMargins &margin, bool isHat)
: emoteManager(_emoteManager)
, windowManager(_windowManager)
, _currentPixmap(image)
2017-04-14 14:52:31 +02:00
, _name(name)
, _tooltip(tooltip)
, _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
this->emoteManager.getGifUpdateSignal().connect([this] {
gifUpdateTimout(); //
});
2017-01-13 18:59:11 +01:00
}
2017-01-04 15:12:31 +01:00
this->emoteManager.incGeneration();
this->windowManager.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-14 17:52:22 +02:00
} // namespace messages
} // namespace chatterino