From 043ddfafa69d0ad423f6482dba61431024848c13 Mon Sep 17 00:00:00 2001 From: fourtf Date: Fri, 14 Apr 2017 14:52:31 +0200 Subject: [PATCH] refactored lazyloadedimage --- messages/lazyloadedimage.cpp | 78 +++++++++++++++--------------------- messages/lazyloadedimage.h | 66 +++++++++++++++--------------- messages/messageref.cpp | 2 +- 3 files changed, 67 insertions(+), 79 deletions(-) diff --git a/messages/lazyloadedimage.cpp b/messages/lazyloadedimage.cpp index b929a549e..1d9734672 100644 --- a/messages/lazyloadedimage.cpp +++ b/messages/lazyloadedimage.cpp @@ -3,6 +3,7 @@ #include "asyncexec.h" #include "emotemanager.h" #include "ircmanager.h" +#include "util/urlfetch.h" #include "windowmanager.h" #include @@ -18,49 +19,39 @@ namespace messages { LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale, const QString &name, const QString &tooltip, const QMargins &margin, bool isHat) - : currentPixmap(NULL) - , allFrames() - , currentFrame(0) - , currentFrameOffset(0) - , url(url) - , name(name) - , tooltip(tooltip) - , animated(false) - , margin(margin) - , ishat(isHat) - , scale(scale) - , isLoading(false) + : _currentPixmap(NULL) + , _currentFrame(0) + , _currentFrameOffset(0) + , _url(url) + , _name(name) + , _tooltip(tooltip) + , _animated(false) + , _margin(margin) + , _ishat(isHat) + , _scale(scale) + , _isLoading(false) { } LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale, const QString &name, const QString &tooltip, const QMargins &margin, bool isHat) - : currentPixmap(image) - , allFrames() - , currentFrame(0) - , currentFrameOffset(0) - , url() - , name(name) - , tooltip(tooltip) - , animated(false) - , margin(margin) - , ishat(isHat) - , scale(scale) - , isLoading(true) + : _currentPixmap(image) + , _currentFrame(0) + , _currentFrameOffset(0) + , _name(name) + , _tooltip(tooltip) + , _animated(false) + , _margin(margin) + , _ishat(isHat) + , _scale(scale) + , _isLoading(true) { } void LazyLoadedImage::loadImage() { - QNetworkAccessManager *manager = new QNetworkAccessManager(); - - QUrl url(this->url); - QNetworkRequest request(url); - - QNetworkReply *reply = manager->get(request); - - QObject::connect(reply, &QNetworkReply::finished, [=] { - QByteArray array = reply->readAll(); + util::urlFetch(_url, [=](QNetworkReply &reply) { + QByteArray array = reply.readAll(); QBuffer buffer(&array); buffer.open(QIODevice::ReadOnly); @@ -75,45 +66,42 @@ void LazyLoadedImage::loadImage() if (first) { first = false; - this->currentPixmap = pixmap; + _currentPixmap = pixmap; } FrameData data; data.duration = std::max(20, reader.nextImageDelay()); data.image = pixmap; - allFrames.push_back(data); + _allFrames.push_back(data); } } - if (allFrames.size() > 1) { - this->animated = true; + if (_allFrames.size() > 1) { + _animated = true; EmoteManager::getInstance().getGifUpdateSignal().connect([this] { gifUpdateTimout(); }); } EmoteManager::getInstance().incGeneration(); WindowManager::getInstance().layoutVisibleChatWidgets(); - - reply->deleteLater(); - manager->deleteLater(); }); } void LazyLoadedImage::gifUpdateTimout() { - this->currentFrameOffset += GIF_FRAME_LENGTH; + _currentFrameOffset += GIF_FRAME_LENGTH; while (true) { - if (this->currentFrameOffset > this->allFrames.at(this->currentFrame).duration) { - this->currentFrameOffset -= this->allFrames.at(this->currentFrame).duration; - this->currentFrame = (this->currentFrame + 1) % this->allFrames.size(); + if (_currentFrameOffset > _allFrames.at(_currentFrame).duration) { + _currentFrameOffset -= _allFrames.at(_currentFrame).duration; + _currentFrame = (_currentFrame + 1) % _allFrames.size(); } else { break; } } - this->currentPixmap = this->allFrames[this->currentFrame].image; + _currentPixmap = _allFrames[_currentFrame].image; } } // namespace messages } // namespace chatterino diff --git a/messages/lazyloadedimage.h b/messages/lazyloadedimage.h index e3e1d3b09..54b5bea56 100644 --- a/messages/lazyloadedimage.h +++ b/messages/lazyloadedimage.h @@ -10,72 +10,72 @@ namespace messages { class LazyLoadedImage : QObject { public: - explicit LazyLoadedImage(const QString &url, qreal scale = 1, const QString &name = "", - const QString &tooltip = "", const QMargins &margin = QMargins(), + explicit LazyLoadedImage(const QString &_url, qreal _scale = 1, const QString &_name = "", + const QString &_tooltip = "", const QMargins &_margin = QMargins(), bool isHat = false); - explicit LazyLoadedImage(QPixmap *currentPixmap, qreal scale = 1, const QString &name = "", - const QString &tooltip = "", const QMargins &margin = QMargins(), + explicit LazyLoadedImage(QPixmap *_currentPixmap, qreal _scale = 1, const QString &_name = "", + const QString &_tooltip = "", const QMargins &_margin = QMargins(), bool isHat = false); const QPixmap *getPixmap() { - if (!isLoading) { - isLoading = true; + if (!_isLoading) { + _isLoading = true; loadImage(); } - return currentPixmap; + return _currentPixmap; } qreal getScale() const { - return scale; + return _scale; } const QString &getUrl() const { - return url; + return _url; } const QString &getName() const { - return name; + return _name; } const QString &getTooltip() const { - return tooltip; + return _tooltip; } const QMargins &getMargin() const { - return margin; + return _margin; } bool getAnimated() const { - return animated; + return _animated; } - bool getIsHat() const + bool isHat() const { - return ishat; + return _ishat; } int getWidth() const { - if (currentPixmap == NULL) { + if (_currentPixmap == NULL) { return 16; } - return currentPixmap->width(); + return _currentPixmap->width(); } int getHeight() const { - if (currentPixmap == NULL) { + if (_currentPixmap == NULL) { return 16; } - return currentPixmap->height(); + return _currentPixmap->height(); } private: @@ -84,26 +84,26 @@ private: int duration; }; - QPixmap *currentPixmap; - std::vector allFrames; - int currentFrame; - int currentFrameOffset; + QPixmap *_currentPixmap; + std::vector _allFrames; + int _currentFrame; + int _currentFrameOffset; - QString url; - QString name; - QString tooltip; - bool animated; - QMargins margin; - bool ishat; - qreal scale; + QString _url; + QString _name; + QString _tooltip; + bool _animated; + QMargins _margin; + bool _ishat; + qreal _scale; - bool isLoading; + bool _isLoading; void loadImage(); void gifUpdateTimout(); }; -} -} +} // namespace messages +} // namespace chatterino #endif // LAZYLOADEDIMAGE_H diff --git a/messages/messageref.cpp b/messages/messageref.cpp index d9d6797b0..145bc6d47 100644 --- a/messages/messageref.cpp +++ b/messages/messageref.cpp @@ -115,7 +115,7 @@ bool MessageRef::layout(int width, bool enableEmoteMargins) int xOffset = 0, yOffset = 0; if (enableEmoteMargins) { - if (word.isImage() && word.getImage().getIsHat()) { + if (word.isImage() && word.getImage().isHat()) { xOffset = -word.getWidth() + 2; } else { xOffset = word.getXOffset();