mirror-chatterino2/lazyloadedimage.cpp

64 lines
1.5 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 <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-13 18:59:11 +01:00
: m_pixmap(NULL)
2017-01-11 01:08:20 +01:00
, m_url(url)
2017-01-06 23:28:48 +01:00
, m_name(name)
, m_tooltip(tooltip)
, m_animated(false)
, m_margin(margin)
, m_ishat(isHat)
, m_scale(scale)
2017-01-11 18:52:09 +01:00
, m_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-13 18:59:11 +01:00
: m_pixmap(image)
, m_url()
, m_name(name)
2017-01-06 23:28:48 +01:00
, m_tooltip(tooltip)
, m_animated(false)
, m_margin(margin)
, m_ishat(isHat)
, m_scale(scale)
2017-01-11 18:52:09 +01:00
, m_isLoading(true)
{
}
void
LazyLoadedImage::loadImage()
2017-01-04 15:12:31 +01:00
{
2017-01-13 18:59:11 +01:00
// QThreadPool::globalInstance()->start(new LambdaQRunnable([=] {
QUrl url(m_url);
QNetworkRequest request(url);
QNetworkReply *reply = IrcManager::accessManager().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-13 18:59:11 +01:00
m_pixmap = pixmap;
2017-01-15 16:38:30 +01:00
Emotes::incGeneration();
2017-01-13 18:59:11 +01:00
});
//}));
2017-01-04 15:12:31 +01:00
}