Revert accidental animated image change

This commit is contained in:
Nicolas Werner 2022-09-30 04:03:39 +02:00
parent 1ea5449c18
commit 78784babd1
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
2 changed files with 31 additions and 47 deletions

View file

@ -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()
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()
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()
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
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
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());

View file

@ -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
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:
{ {
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:
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:
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;
}; };