added gif frame updating
This commit is contained in:
@@ -5,9 +5,12 @@
|
||||
#include "ircmanager.h"
|
||||
#include "windows.h"
|
||||
|
||||
#include <QBuffer>
|
||||
#include <QImageReader>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QTimer>
|
||||
#include <functional>
|
||||
|
||||
namespace chatterino {
|
||||
@@ -16,7 +19,9 @@ namespace messages {
|
||||
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale,
|
||||
const QString &name, const QString &tooltip,
|
||||
const QMargins &margin, bool isHat)
|
||||
: pixmap(NULL)
|
||||
: currentPixmap(NULL)
|
||||
, allFrames()
|
||||
, currentFrame(0)
|
||||
, url(url)
|
||||
, name(name)
|
||||
, tooltip(tooltip)
|
||||
@@ -31,7 +36,9 @@ LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale,
|
||||
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale,
|
||||
const QString &name, const QString &tooltip,
|
||||
const QMargins &margin, bool isHat)
|
||||
: pixmap(image)
|
||||
: currentPixmap(image)
|
||||
, allFrames()
|
||||
, currentFrame(0)
|
||||
, url()
|
||||
, name(name)
|
||||
, tooltip(tooltip)
|
||||
@@ -46,7 +53,6 @@ LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale,
|
||||
void
|
||||
LazyLoadedImage::loadImage()
|
||||
{
|
||||
// QThreadPool::globalInstance()->start(new LambdaQRunnable([=] {
|
||||
QNetworkAccessManager *manager = new QNetworkAccessManager();
|
||||
|
||||
QUrl url(this->url);
|
||||
@@ -55,21 +61,54 @@ LazyLoadedImage::loadImage()
|
||||
QNetworkReply *reply = manager->get(request);
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||
QPixmap *pixmap = new QPixmap();
|
||||
pixmap->loadFromData(reply->readAll());
|
||||
QByteArray array = reply->readAll();
|
||||
QBuffer buffer(&array);
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
|
||||
if (pixmap->isNull()) {
|
||||
return;
|
||||
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(); });
|
||||
}
|
||||
|
||||
this->pixmap = pixmap;
|
||||
Emotes::incGeneration();
|
||||
Windows::layoutVisibleChatWidgets();
|
||||
|
||||
reply->deleteLater();
|
||||
manager->deleteLater();
|
||||
});
|
||||
// }));
|
||||
}
|
||||
|
||||
void
|
||||
LazyLoadedImage::gifUpdateTimout()
|
||||
{
|
||||
if (this->currentFrame >= this->allFrames.size() - 1) {
|
||||
this->currentFrame = 0;
|
||||
this->currentPixmap = this->allFrames.at(0).image;
|
||||
} else {
|
||||
this->currentPixmap = this->allFrames.at(++this->currentFrame).image;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user