Skip to content
Snippets Groups Projects
Verified Commit 78784bab authored by Nicolas Werner's avatar Nicolas Werner
Browse files

Revert accidental animated image change

parent 1ea5449c
No related branches found
No related tags found
No related merge requests found
Pipeline #3735 passed
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QMimeDatabase> #include <QMimeDatabase>
#include <QMovie>
#include <QQuickWindow> #include <QQuickWindow>
#include <QSGImageNode> #include <QSGImageNode>
#include <QStandardPaths> #include <QStandardPaths>
...@@ -35,12 +34,8 @@ MxcAnimatedImage::startDownload() ...@@ -35,12 +34,8 @@ MxcAnimatedImage::startDownload()
QByteArray mimeType = QString::fromStdString(mtx::accessors::mimetype(*event)).toUtf8(); QByteArray mimeType = QString::fromStdString(mtx::accessors::mimetype(*event)).toUtf8();
static const auto movieFormats = QMovie::supportedFormats(); static const auto formats = QMovie::supportedFormats();
QByteArray imageFormat; animatable_ = formats.contains(mimeType.split('/').back());
const auto imageFormats = QImageReader::imageFormatsForMimeType(mimeType);
if (!imageFormats.isEmpty())
imageFormat = imageFormats.front();
animatable_ = movieFormats.contains(imageFormat);
animatableChanged(); animatableChanged();
if (!animatable_) if (!animatable_)
...@@ -71,14 +66,14 @@ MxcAnimatedImage::startDownload() ...@@ -71,14 +66,14 @@ MxcAnimatedImage::startDownload()
QPointer<MxcAnimatedImage> self = this; QPointer<MxcAnimatedImage> self = this;
auto processBuffer = [this, imageFormat, encryptionInfo, self](QIODevice &device) { auto processBuffer = [this, mimeType, encryptionInfo, self](QIODevice &device) {
if (!self) if (!self)
return; return;
try { try {
if (buffer.isOpen()) { if (buffer.isOpen()) {
frameTimer.stop(); movie.stop();
movie->setDevice(nullptr); movie.setDevice(nullptr);
buffer.close(); buffer.close();
} }
...@@ -97,22 +92,21 @@ MxcAnimatedImage::startDownload() ...@@ -97,22 +92,21 @@ MxcAnimatedImage::startDownload()
nhlog::net()->error("Failed to setup animated image buffer: {}", e.what()); nhlog::net()->error("Failed to setup animated image buffer: {}", e.what());
} }
QTimer::singleShot(0, this, [this, imageFormat] { QTimer::singleShot(0, this, [this, mimeType] {
nhlog::ui()->info( nhlog::ui()->info(
"Playing movie with size: {}, {}", buffer.bytesAvailable(), buffer.isOpen()); "Playing movie with size: {}, {}", buffer.bytesAvailable(), buffer.isOpen());
movie->setFormat(imageFormat); movie.setFormat(mimeType);
movie->setDevice(&buffer); movie.setDevice(&buffer);
if (height() != 0 && width() != 0) if (height() != 0 && width() != 0)
movie->setScaledSize(this->size().toSize()); movie.setScaledSize(this->size().toSize());
if (buffer.bytesAvailable() <
if (movie->supportsAnimation()) 4LL * 1024 * 1024 * 1024) // cache images smaller than 4MB in RAM
frameTimer.setInterval(movie->nextImageDelay()); movie.setCacheMode(QMovie::CacheAll);
if (play_) if (play_)
frameTimer.start(); movie.start();
else else
movie->jumpToImage(0); movie.jumpToFrame(0);
emit loadedChanged(); emit loadedChanged();
update(); update();
}); });
...@@ -165,9 +159,9 @@ MxcAnimatedImage::geometryChanged(const QRectF &newGeometry, const QRectF &oldGe ...@@ -165,9 +159,9 @@ MxcAnimatedImage::geometryChanged(const QRectF &newGeometry, const QRectF &oldGe
if (newGeometry.size() != oldGeometry.size()) { if (newGeometry.size() != oldGeometry.size()) {
if (height() != 0 && width() != 0) { if (height() != 0 && width() != 0) {
QSizeF r = movie->scaledSize(); QSizeF r = movie.scaledSize();
r.scale(newGeometry.size(), Qt::KeepAspectRatio); r.scale(newGeometry.size(), Qt::KeepAspectRatio);
movie->setScaledSize(r.toSize()); movie.setScaledSize(r.toSize());
imageDirty = true; imageDirty = true;
update(); update();
} }
...@@ -190,15 +184,16 @@ MxcAnimatedImage::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeD ...@@ -190,15 +184,16 @@ MxcAnimatedImage::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeD
n->setFlags(QSGNode::OwnedByParent); n->setFlags(QSGNode::OwnedByParent);
} }
n->setSourceRect(currentFrame.rect()); auto img = movie.currentImage();
if (!currentFrame.isNull()) n->setSourceRect(img.rect());
n->setTexture(window()->createTextureFromImage(currentFrame)); if (!img.isNull())
n->setTexture(window()->createTextureFromImage(std::move(img)));
else { else {
delete n; delete n;
return nullptr; return nullptr;
} }
QSizeF r = currentFrame.size(); QSizeF r = img.size();
r.scale(size(), Qt::KeepAspectRatio); r.scale(size(), Qt::KeepAspectRatio);
n->setRect((width() - r.width()) / 2, (height() - r.height()) / 2, r.width(), r.height()); n->setRect((width() - r.width()) / 2, (height() - r.height()) / 2, r.width(), r.height());
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#pragma once #pragma once
#include <QBuffer> #include <QBuffer>
#include <QImageReader> #include <QMovie>
#include <QObject> #include <QObject>
#include <QQuickItem> #include <QQuickItem>
...@@ -24,11 +24,10 @@ class MxcAnimatedImage : public QQuickItem ...@@ -24,11 +24,10 @@ class MxcAnimatedImage : public QQuickItem
public: public:
MxcAnimatedImage(QQuickItem *parent = nullptr) MxcAnimatedImage(QQuickItem *parent = nullptr)
: QQuickItem(parent) : QQuickItem(parent)
, movie(new QImageReader())
{ {
connect(this, &MxcAnimatedImage::eventIdChanged, &MxcAnimatedImage::startDownload); connect(this, &MxcAnimatedImage::eventIdChanged, &MxcAnimatedImage::startDownload);
connect(this, &MxcAnimatedImage::roomChanged, &MxcAnimatedImage::startDownload); connect(this, &MxcAnimatedImage::roomChanged, &MxcAnimatedImage::startDownload);
connect(&frameTimer, &QTimer::timeout, this, &MxcAnimatedImage::newFrame); connect(&movie, &QMovie::frameChanged, this, &MxcAnimatedImage::newFrame);
setFlag(QQuickItem::ItemHasContents); setFlag(QQuickItem::ItemHasContents);
// setAcceptHoverEvents(true); // setAcceptHoverEvents(true);
} }
...@@ -56,10 +55,7 @@ public: ...@@ -56,10 +55,7 @@ public:
{ {
if (play_ != newPlay) { if (play_ != newPlay) {
play_ = newPlay; play_ = newPlay;
if (play_) movie.setPaused(!play_);
frameTimer.start();
else
frameTimer.stop();
emit playChanged(); emit playChanged();
} }
} }
...@@ -77,16 +73,10 @@ signals: ...@@ -77,16 +73,10 @@ signals:
private slots: private slots:
void startDownload(); void startDownload();
void newFrame() void newFrame(int frame)
{ {
if (movie->currentImageNumber() > 0 && !movie->canRead() && movie->imageCount() > 1) { currentFrame = frame;
buffer.seek(0); imageDirty = true;
movie.reset(new QImageReader(movie->device(), movie->format()));
if (height() != 0 && width() != 0)
movie->setScaledSize(this->size().toSize());
}
movie->read(&currentFrame);
imageDirty = true;
update(); update();
} }
...@@ -96,9 +86,8 @@ private: ...@@ -96,9 +86,8 @@ private:
QString filename_; QString filename_;
bool animatable_ = false; bool animatable_ = false;
QBuffer buffer; QBuffer buffer;
std::unique_ptr<QImageReader> movie; QMovie movie;
bool imageDirty = true; int currentFrame = 0;
bool play_ = true; bool imageDirty = true;
QTimer frameTimer; bool play_ = true;
QImage currentFrame;
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment