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,8 +51,17 @@ 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;
request.setUrl(QUrl(this->url));
QNetworkAccessManager NaM;
QEventLoop eventLoop;
QNetworkReply *reply = NaM.get(request);
QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
eventLoop.exec(); // Wait until response is read.
qDebug() << "Received emote " << this->url;
QByteArray array = reply->readAll();
QBuffer buffer(&array); QBuffer buffer(&array);
buffer.open(QIODevice::ReadOnly); buffer.open(QIODevice::ReadOnly);
@ -79,19 +89,19 @@ void LazyLoadedImage::loadImage()
if (this->allFrames.size() > 1) { if (this->allFrames.size() > 1) {
this->animated = true; this->animated = true;
this->emoteManager.getGifUpdateSignal().connect([this] {
gifUpdateTimout(); //
});
} }
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()
{ {
if (animated) {
this->currentFrameOffset += GIF_FRAME_LENGTH; this->currentFrameOffset += GIF_FRAME_LENGTH;
while (true) { while (true) {
@ -105,6 +115,7 @@ void LazyLoadedImage::gifUpdateTimout()
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();
}; };