2018-01-11 20:16:25 +01:00
|
|
|
#include "messages/image.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/emotemanager.hpp"
|
|
|
|
#include "singletons/ircmanager.hpp"
|
|
|
|
#include "singletons/windowmanager.hpp"
|
2017-10-27 20:09:02 +02:00
|
|
|
#include "util/networkmanager.hpp"
|
2017-06-11 09:31:45 +02:00
|
|
|
#include "util/urlfetch.hpp"
|
2017-01-11 18:52:09 +01:00
|
|
|
|
2017-02-06 17:42:28 +01:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QImageReader>
|
2017-01-11 18:52:09 +01:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2017-02-06 17:42:28 +01:00
|
|
|
#include <QTimer>
|
2017-06-06 21:18:05 +02:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
#include <functional>
|
2017-10-27 20:09:02 +02:00
|
|
|
#include <thread>
|
2017-01-11 18:52:09 +01:00
|
|
|
|
2017-01-18 21:30:23 +01:00
|
|
|
namespace chatterino {
|
|
|
|
namespace messages {
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
Image::Image(const QString &url, qreal scale, const QString &name, const QString &tooltip,
|
|
|
|
const QMargins &margin, bool isHat)
|
2017-12-17 02:18:13 +01:00
|
|
|
: currentPixmap(nullptr)
|
2017-09-12 19:06:16 +02:00
|
|
|
, url(url)
|
|
|
|
, name(name)
|
|
|
|
, tooltip(tooltip)
|
|
|
|
, margin(margin)
|
|
|
|
, ishat(isHat)
|
|
|
|
, scale(scale)
|
|
|
|
, isLoading(false)
|
2017-01-05 16:07:20 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
Image::Image(QPixmap *image, qreal scale, const QString &name, const QString &tooltip,
|
|
|
|
const QMargins &margin, bool isHat)
|
2017-12-17 02:18:13 +01:00
|
|
|
: currentPixmap(image)
|
2017-09-12 19:06:16 +02:00
|
|
|
, name(name)
|
|
|
|
, tooltip(tooltip)
|
|
|
|
, margin(margin)
|
|
|
|
, ishat(isHat)
|
|
|
|
, scale(scale)
|
|
|
|
, isLoading(true)
|
2017-01-11 18:52:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
void Image::loadImage()
|
2017-01-04 15:12:31 +01:00
|
|
|
{
|
2017-10-27 20:09:02 +02:00
|
|
|
util::NetworkRequest req(this->getUrl());
|
|
|
|
req.setCaller(this);
|
|
|
|
req.get([lli = this](QNetworkReply * reply) {
|
|
|
|
QByteArray array = reply->readAll();
|
|
|
|
QBuffer buffer(&array);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
QImage image;
|
|
|
|
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;
|
|
|
|
lli->currentPixmap = pixmap;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
chatterino::messages::Image::FrameData data;
|
2017-10-27 20:09:02 +02:00
|
|
|
data.duration = std::max(20, reader.nextImageDelay());
|
|
|
|
data.image = pixmap;
|
|
|
|
|
|
|
|
lli->allFrames.push_back(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lli->allFrames.size() > 1) {
|
|
|
|
lli->animated = true;
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::EmoteManager::getInstance().incGeneration();
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::WindowManager::getInstance().layoutVisibleChatWidgets();
|
2017-10-27 20:09:02 +02:00
|
|
|
});
|
2017-09-28 22:24:03 +02:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::EmoteManager::getInstance().getGifUpdateSignal().connect([=]() {
|
2017-10-08 15:18:47 +02:00
|
|
|
this->gifUpdateTimout();
|
|
|
|
}); // For some reason when Boost signal is in thread scope and thread deletes the signal
|
|
|
|
// doesn't work, so this is the fix.
|
2017-02-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
void Image::gifUpdateTimout()
|
2017-02-06 17:42:28 +01:00
|
|
|
{
|
2017-10-08 15:18:47 +02:00
|
|
|
if (animated) {
|
|
|
|
this->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();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->currentPixmap = this->allFrames[this->currentFrame].image;
|
|
|
|
}
|
2017-01-04 15:12:31 +01:00
|
|
|
}
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
const QPixmap *Image::getPixmap()
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
if (!this->isLoading) {
|
|
|
|
this->isLoading = true;
|
|
|
|
|
|
|
|
loadImage();
|
|
|
|
}
|
|
|
|
return this->currentPixmap;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
qreal Image::getScale() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
return this->scale;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
const QString &Image::getUrl() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
return this->url;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
const QString &Image::getName() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
return this->name;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
const QString &Image::getTooltip() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
return this->tooltip;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
const QMargins &Image::getMargin() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
return this->margin;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
bool Image::isAnimated() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
return this->animated;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
bool Image::isHat() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
return this->ishat;
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
int Image::getWidth() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
if (this->currentPixmap == nullptr) {
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
return this->currentPixmap->width();
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
int Image::getScaledWidth() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
2018-01-11 20:16:25 +01:00
|
|
|
return static_cast<int>(this->getWidth() * this->scale);
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
int Image::getHeight() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
if (this->currentPixmap == nullptr) {
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
return this->currentPixmap->height();
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
int Image::getScaledHeight() const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
2018-01-11 20:16:25 +01:00
|
|
|
return static_cast<int>(this->getHeight() * this->scale);
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace messages
|
|
|
|
} // namespace chatterino
|