Threaded image downloading (#118)

This commit is contained in:
Matija 2017-09-28 22:24:03 +02:00 committed by fourtf
parent e6ee009698
commit cb8846c0ae
2 changed files with 50 additions and 40 deletions

View file

@ -5,6 +5,7 @@
#include "util/urlfetch.hpp" #include "util/urlfetch.hpp"
#include "windowmanager.hpp" #include "windowmanager.hpp"
#include <thread>
#include <QBuffer> #include <QBuffer>
#include <QImageReader> #include <QImageReader>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
@ -50,60 +51,70 @@ LazyLoadedImage::LazyLoadedImage(EmoteManager &_emoteManager, WindowManager &_wi
void LazyLoadedImage::loadImage() void LazyLoadedImage::loadImage()
{ {
util::urlFetch(this->url, [=](QNetworkReply &reply) { std::thread([=] () {
QByteArray array = reply.readAll(); QNetworkRequest request;
QBuffer buffer(&array); request.setUrl(QUrl(this->url));
buffer.open(QIODevice::ReadOnly); QNetworkAccessManager NaM;
QEventLoop eventLoop;
QNetworkReply *reply = NaM.get(request);
QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
eventLoop.exec(); // Wait until response is read.
QImage image; qDebug() << "Received emote " << this->url;
QImageReader reader(&buffer); QByteArray array = reply->readAll();
QBuffer buffer(&array);
buffer.open(QIODevice::ReadOnly);
bool first = true; QImage image;
QImageReader reader(&buffer);
for (int index = 0; index < reader.imageCount(); ++index) { bool first = true;
if (reader.read(&image)) {
auto pixmap = new QPixmap(QPixmap::fromImage(image));
if (first) { for (int index = 0; index < reader.imageCount(); ++index) {
first = false; if (reader.read(&image)) {
this->currentPixmap = pixmap; auto pixmap = new QPixmap(QPixmap::fromImage(image));
}
FrameData data; if (first) {
data.duration = std::max(20, reader.nextImageDelay()); first = false;
data.image = pixmap; this->currentPixmap = pixmap;
}
this->allFrames.push_back(data); FrameData data;
} data.duration = std::max(20, reader.nextImageDelay());
} data.image = pixmap;
if (this->allFrames.size() > 1) { this->allFrames.push_back(data);
this->animated = true; }
}
this->emoteManager.getGifUpdateSignal().connect([this] { if (this->allFrames.size() > 1) {
gifUpdateTimout(); // this->animated = true;
}); }
}
this->emoteManager.incGeneration(); this->emoteManager.incGeneration();
this->windowManager.layoutVisibleChatWidgets(); this->windowManager.layoutVisibleChatWidgets();
});
delete reply;
}).detach();
this->emoteManager.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.
} }
void LazyLoadedImage::gifUpdateTimout() void LazyLoadedImage::gifUpdateTimout()
{ {
this->currentFrameOffset += GIF_FRAME_LENGTH; if (animated) {
this->currentFrameOffset += GIF_FRAME_LENGTH;
while (true) { while (true) {
if (this->currentFrameOffset > this->allFrames.at(this->currentFrame).duration) { if (this->currentFrameOffset > this->allFrames.at(this->currentFrame).duration) {
this->currentFrameOffset -= this->allFrames.at(this->currentFrame).duration; this->currentFrameOffset -= this->allFrames.at(this->currentFrame).duration;
this->currentFrame = (this->currentFrame + 1) % this->allFrames.size(); this->currentFrame = (this->currentFrame + 1) % this->allFrames.size();
} else { } else {
break; break;
} }
} }
this->currentPixmap = this->allFrames[this->currentFrame].image; this->currentPixmap = this->allFrames[this->currentFrame].image;
}
} }
const QPixmap *LazyLoadedImage::getPixmap() const QPixmap *LazyLoadedImage::getPixmap()

View file

@ -63,7 +63,6 @@ private:
bool isLoading; bool isLoading;
void loadImage(); void loadImage();
void gifUpdateTimout(); void gifUpdateTimout();
}; };