refactored lazyloadedimage
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
#include "asyncexec.h"
|
#include "asyncexec.h"
|
||||||
#include "emotemanager.h"
|
#include "emotemanager.h"
|
||||||
#include "ircmanager.h"
|
#include "ircmanager.h"
|
||||||
|
#include "util/urlfetch.h"
|
||||||
#include "windowmanager.h"
|
#include "windowmanager.h"
|
||||||
|
|
||||||
#include <QBuffer>
|
#include <QBuffer>
|
||||||
@@ -18,49 +19,39 @@ namespace messages {
|
|||||||
|
|
||||||
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale, const QString &name,
|
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale, const QString &name,
|
||||||
const QString &tooltip, const QMargins &margin, bool isHat)
|
const QString &tooltip, const QMargins &margin, bool isHat)
|
||||||
: currentPixmap(NULL)
|
: _currentPixmap(NULL)
|
||||||
, allFrames()
|
, _currentFrame(0)
|
||||||
, currentFrame(0)
|
, _currentFrameOffset(0)
|
||||||
, currentFrameOffset(0)
|
, _url(url)
|
||||||
, url(url)
|
, _name(name)
|
||||||
, name(name)
|
, _tooltip(tooltip)
|
||||||
, tooltip(tooltip)
|
, _animated(false)
|
||||||
, animated(false)
|
, _margin(margin)
|
||||||
, margin(margin)
|
, _ishat(isHat)
|
||||||
, ishat(isHat)
|
, _scale(scale)
|
||||||
, scale(scale)
|
, _isLoading(false)
|
||||||
, isLoading(false)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale, const QString &name,
|
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale, const QString &name,
|
||||||
const QString &tooltip, const QMargins &margin, bool isHat)
|
const QString &tooltip, const QMargins &margin, bool isHat)
|
||||||
: currentPixmap(image)
|
: _currentPixmap(image)
|
||||||
, allFrames()
|
, _currentFrame(0)
|
||||||
, currentFrame(0)
|
, _currentFrameOffset(0)
|
||||||
, currentFrameOffset(0)
|
, _name(name)
|
||||||
, url()
|
, _tooltip(tooltip)
|
||||||
, name(name)
|
, _animated(false)
|
||||||
, tooltip(tooltip)
|
, _margin(margin)
|
||||||
, animated(false)
|
, _ishat(isHat)
|
||||||
, margin(margin)
|
, _scale(scale)
|
||||||
, ishat(isHat)
|
, _isLoading(true)
|
||||||
, scale(scale)
|
|
||||||
, isLoading(true)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void LazyLoadedImage::loadImage()
|
void LazyLoadedImage::loadImage()
|
||||||
{
|
{
|
||||||
QNetworkAccessManager *manager = new QNetworkAccessManager();
|
util::urlFetch(_url, [=](QNetworkReply &reply) {
|
||||||
|
QByteArray array = reply.readAll();
|
||||||
QUrl url(this->url);
|
|
||||||
QNetworkRequest request(url);
|
|
||||||
|
|
||||||
QNetworkReply *reply = manager->get(request);
|
|
||||||
|
|
||||||
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
|
||||||
QByteArray array = reply->readAll();
|
|
||||||
QBuffer buffer(&array);
|
QBuffer buffer(&array);
|
||||||
buffer.open(QIODevice::ReadOnly);
|
buffer.open(QIODevice::ReadOnly);
|
||||||
|
|
||||||
@@ -75,45 +66,42 @@ void LazyLoadedImage::loadImage()
|
|||||||
|
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
this->currentPixmap = pixmap;
|
_currentPixmap = pixmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
FrameData data;
|
FrameData data;
|
||||||
data.duration = std::max(20, reader.nextImageDelay());
|
data.duration = std::max(20, reader.nextImageDelay());
|
||||||
data.image = pixmap;
|
data.image = pixmap;
|
||||||
|
|
||||||
allFrames.push_back(data);
|
_allFrames.push_back(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allFrames.size() > 1) {
|
if (_allFrames.size() > 1) {
|
||||||
this->animated = true;
|
_animated = true;
|
||||||
|
|
||||||
EmoteManager::getInstance().getGifUpdateSignal().connect([this] { gifUpdateTimout(); });
|
EmoteManager::getInstance().getGifUpdateSignal().connect([this] { gifUpdateTimout(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
EmoteManager::getInstance().incGeneration();
|
EmoteManager::getInstance().incGeneration();
|
||||||
WindowManager::getInstance().layoutVisibleChatWidgets();
|
WindowManager::getInstance().layoutVisibleChatWidgets();
|
||||||
|
|
||||||
reply->deleteLater();
|
|
||||||
manager->deleteLater();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void LazyLoadedImage::gifUpdateTimout()
|
void LazyLoadedImage::gifUpdateTimout()
|
||||||
{
|
{
|
||||||
this->currentFrameOffset += GIF_FRAME_LENGTH;
|
_currentFrameOffset += GIF_FRAME_LENGTH;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (this->currentFrameOffset > this->allFrames.at(this->currentFrame).duration) {
|
if (_currentFrameOffset > _allFrames.at(_currentFrame).duration) {
|
||||||
this->currentFrameOffset -= this->allFrames.at(this->currentFrame).duration;
|
_currentFrameOffset -= _allFrames.at(_currentFrame).duration;
|
||||||
this->currentFrame = (this->currentFrame + 1) % this->allFrames.size();
|
_currentFrame = (_currentFrame + 1) % _allFrames.size();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this->currentPixmap = this->allFrames[this->currentFrame].image;
|
_currentPixmap = _allFrames[_currentFrame].image;
|
||||||
}
|
}
|
||||||
} // namespace messages
|
} // namespace messages
|
||||||
} // namespace chatterino
|
} // namespace chatterino
|
||||||
|
|||||||
+33
-33
@@ -10,72 +10,72 @@ namespace messages {
|
|||||||
class LazyLoadedImage : QObject
|
class LazyLoadedImage : QObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit LazyLoadedImage(const QString &url, qreal scale = 1, const QString &name = "",
|
explicit LazyLoadedImage(const QString &_url, qreal _scale = 1, const QString &_name = "",
|
||||||
const QString &tooltip = "", const QMargins &margin = QMargins(),
|
const QString &_tooltip = "", const QMargins &_margin = QMargins(),
|
||||||
bool isHat = false);
|
bool isHat = false);
|
||||||
explicit LazyLoadedImage(QPixmap *currentPixmap, qreal scale = 1, const QString &name = "",
|
explicit LazyLoadedImage(QPixmap *_currentPixmap, qreal _scale = 1, const QString &_name = "",
|
||||||
const QString &tooltip = "", const QMargins &margin = QMargins(),
|
const QString &_tooltip = "", const QMargins &_margin = QMargins(),
|
||||||
bool isHat = false);
|
bool isHat = false);
|
||||||
|
|
||||||
const QPixmap *getPixmap()
|
const QPixmap *getPixmap()
|
||||||
{
|
{
|
||||||
if (!isLoading) {
|
if (!_isLoading) {
|
||||||
isLoading = true;
|
_isLoading = true;
|
||||||
|
|
||||||
loadImage();
|
loadImage();
|
||||||
}
|
}
|
||||||
return currentPixmap;
|
return _currentPixmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
qreal getScale() const
|
qreal getScale() const
|
||||||
{
|
{
|
||||||
return scale;
|
return _scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString &getUrl() const
|
const QString &getUrl() const
|
||||||
{
|
{
|
||||||
return url;
|
return _url;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString &getName() const
|
const QString &getName() const
|
||||||
{
|
{
|
||||||
return name;
|
return _name;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString &getTooltip() const
|
const QString &getTooltip() const
|
||||||
{
|
{
|
||||||
return tooltip;
|
return _tooltip;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QMargins &getMargin() const
|
const QMargins &getMargin() const
|
||||||
{
|
{
|
||||||
return margin;
|
return _margin;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool getAnimated() const
|
bool getAnimated() const
|
||||||
{
|
{
|
||||||
return animated;
|
return _animated;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool getIsHat() const
|
bool isHat() const
|
||||||
{
|
{
|
||||||
return ishat;
|
return _ishat;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getWidth() const
|
int getWidth() const
|
||||||
{
|
{
|
||||||
if (currentPixmap == NULL) {
|
if (_currentPixmap == NULL) {
|
||||||
return 16;
|
return 16;
|
||||||
}
|
}
|
||||||
return currentPixmap->width();
|
return _currentPixmap->width();
|
||||||
}
|
}
|
||||||
|
|
||||||
int getHeight() const
|
int getHeight() const
|
||||||
{
|
{
|
||||||
if (currentPixmap == NULL) {
|
if (_currentPixmap == NULL) {
|
||||||
return 16;
|
return 16;
|
||||||
}
|
}
|
||||||
return currentPixmap->height();
|
return _currentPixmap->height();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -84,26 +84,26 @@ private:
|
|||||||
int duration;
|
int duration;
|
||||||
};
|
};
|
||||||
|
|
||||||
QPixmap *currentPixmap;
|
QPixmap *_currentPixmap;
|
||||||
std::vector<FrameData> allFrames;
|
std::vector<FrameData> _allFrames;
|
||||||
int currentFrame;
|
int _currentFrame;
|
||||||
int currentFrameOffset;
|
int _currentFrameOffset;
|
||||||
|
|
||||||
QString url;
|
QString _url;
|
||||||
QString name;
|
QString _name;
|
||||||
QString tooltip;
|
QString _tooltip;
|
||||||
bool animated;
|
bool _animated;
|
||||||
QMargins margin;
|
QMargins _margin;
|
||||||
bool ishat;
|
bool _ishat;
|
||||||
qreal scale;
|
qreal _scale;
|
||||||
|
|
||||||
bool isLoading;
|
bool _isLoading;
|
||||||
|
|
||||||
void loadImage();
|
void loadImage();
|
||||||
|
|
||||||
void gifUpdateTimout();
|
void gifUpdateTimout();
|
||||||
};
|
};
|
||||||
}
|
} // namespace messages
|
||||||
}
|
} // namespace chatterino
|
||||||
|
|
||||||
#endif // LAZYLOADEDIMAGE_H
|
#endif // LAZYLOADEDIMAGE_H
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
|
|||||||
int xOffset = 0, yOffset = 0;
|
int xOffset = 0, yOffset = 0;
|
||||||
|
|
||||||
if (enableEmoteMargins) {
|
if (enableEmoteMargins) {
|
||||||
if (word.isImage() && word.getImage().getIsHat()) {
|
if (word.isImage() && word.getImage().isHat()) {
|
||||||
xOffset = -word.getWidth() + 2;
|
xOffset = -word.getWidth() + 2;
|
||||||
} else {
|
} else {
|
||||||
xOffset = word.getXOffset();
|
xOffset = word.getXOffset();
|
||||||
|
|||||||
Reference in New Issue
Block a user