added gif frame updating

This commit is contained in:
fourtf 2017-02-06 17:42:28 +01:00
parent bdd03ba3fa
commit 33ba35471f
4 changed files with 85 additions and 17 deletions

View file

@ -27,6 +27,9 @@ ConcurrentMap<int, messages::LazyLoadedImage *>
ConcurrentMap<long, messages::LazyLoadedImage *> Emotes::twitchEmoteFromCache; ConcurrentMap<long, messages::LazyLoadedImage *> Emotes::twitchEmoteFromCache;
ConcurrentMap<QString, messages::LazyLoadedImage *> Emotes::miscImageFromCache; ConcurrentMap<QString, messages::LazyLoadedImage *> Emotes::miscImageFromCache;
QTimer Emotes::gifUpdateTimer;
bool Emotes::gifUpdateTimerInitiated(false);
int Emotes::generation = 0; int Emotes::generation = 0;
Emotes::Emotes() Emotes::Emotes()

View file

@ -7,6 +7,7 @@
#include <QMap> #include <QMap>
#include <QMutex> #include <QMutex>
#include <QTimer>
namespace chatterino { namespace chatterino {
@ -82,6 +83,19 @@ public:
generation++; generation++;
} }
static QTimer &
getGifUpdateTimer()
{
if (!gifUpdateTimerInitiated) {
gifUpdateTimerInitiated = true;
gifUpdateTimer.setInterval(33);
gifUpdateTimer.start();
}
return gifUpdateTimer;
}
private: private:
Emotes(); Emotes();
@ -102,6 +116,9 @@ private:
static QString getTwitchEmoteLink(long id, qreal &scale); static QString getTwitchEmoteLink(long id, qreal &scale);
static QTimer gifUpdateTimer;
static bool gifUpdateTimerInitiated;
static void loadFfzEmotes(); static void loadFfzEmotes();
static void loadBttvEmotes(); static void loadBttvEmotes();

View file

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

View file

@ -7,7 +7,7 @@
namespace chatterino { namespace chatterino {
namespace messages { namespace messages {
class LazyLoadedImage class LazyLoadedImage : QObject
{ {
public: public:
explicit LazyLoadedImage(const QString &url, qreal scale = 1, explicit LazyLoadedImage(const QString &url, qreal scale = 1,
@ -15,7 +15,7 @@ public:
const QString &tooltip = "", const QString &tooltip = "",
const QMargins &margin = QMargins(), const QMargins &margin = QMargins(),
bool isHat = false); bool isHat = false);
explicit LazyLoadedImage(QPixmap *pixmap, qreal scale = 1, explicit LazyLoadedImage(QPixmap *currentPixmap, qreal scale = 1,
const QString &name = "", const QString &name = "",
const QString &tooltip = "", const QString &tooltip = "",
const QMargins &margin = QMargins(), const QMargins &margin = QMargins(),
@ -29,7 +29,7 @@ public:
loadImage(); loadImage();
} }
return pixmap; return currentPixmap;
} }
qreal qreal
@ -77,23 +77,30 @@ public:
int int
getWidth() const getWidth() const
{ {
if (pixmap == NULL) { if (currentPixmap == NULL) {
return 16; return 16;
} }
return pixmap->width(); return currentPixmap->width();
} }
int int
getHeight() const getHeight() const
{ {
if (pixmap == NULL) { if (currentPixmap == NULL) {
return 16; return 16;
} }
return pixmap->height(); return currentPixmap->height();
} }
private: private:
QPixmap *pixmap; struct FrameData {
QPixmap *image;
float duration;
};
QPixmap *currentPixmap;
std::vector<FrameData> allFrames;
int currentFrame;
QString url; QString url;
QString name; QString name;
@ -106,6 +113,8 @@ private:
bool isLoading; bool isLoading;
void loadImage(); void loadImage();
void gifUpdateTimout();
}; };
} }
} }