mirror-chatterino2/lazyloadedimage.cpp

66 lines
1.6 KiB
C++
Raw Normal View History

2017-01-04 15:12:31 +01:00
#include "lazyloadedimage.h"
2017-01-11 18:52:09 +01:00
#include "asyncexec.h"
2017-01-15 16:38:30 +01:00
#include "emotes.h"
2017-01-11 18:52:09 +01:00
#include "ircmanager.h"
#include "windows.h"
2017-01-11 18:52:09 +01:00
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <functional>
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale,
const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
2017-01-18 04:33:30 +01:00
: pixmap(NULL)
, 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-01-13 18:59:11 +01:00
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale,
2017-01-11 18:52:09 +01:00
const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
2017-01-18 04:33:30 +01:00
: pixmap(image)
, url()
, name(name)
, tooltip(tooltip)
, animated(false)
, margin(margin)
, ishat(isHat)
, scale(scale)
, isLoading(true)
2017-01-11 18:52:09 +01:00
{
}
void
LazyLoadedImage::loadImage()
2017-01-04 15:12:31 +01:00
{
2017-01-13 18:59:11 +01:00
// QThreadPool::globalInstance()->start(new LambdaQRunnable([=] {
2017-01-18 04:33:30 +01:00
QUrl url(this->url);
2017-01-13 18:59:11 +01:00
QNetworkRequest request(url);
2017-01-18 04:33:30 +01:00
QNetworkReply *reply = IrcManager::getAccessManager().get(request);
2017-01-11 18:52:09 +01:00
2017-01-13 18:59:11 +01:00
QObject::connect(reply, &QNetworkReply::finished, [=] {
QPixmap *pixmap = new QPixmap();
pixmap->loadFromData(reply->readAll());
2017-01-11 18:52:09 +01:00
2017-01-13 18:59:11 +01:00
if (pixmap->isNull()) {
return;
}
2017-01-04 15:12:31 +01:00
2017-01-18 04:33:30 +01:00
this->pixmap = pixmap;
2017-01-15 16:38:30 +01:00
Emotes::incGeneration();
Windows::layoutVisibleChatWidgets();
2017-01-13 18:59:11 +01:00
});
//}));
2017-01-04 15:12:31 +01:00
}