2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-08-29 06:20:23 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QBuffer>
|
2022-09-30 05:03:39 +03:00
|
|
|
#include <QMovie>
|
2021-08-29 06:20:23 +03:00
|
|
|
#include <QObject>
|
|
|
|
#include <QQuickItem>
|
|
|
|
|
2021-12-28 22:09:08 +03:00
|
|
|
#include "timeline/TimelineModel.h"
|
2021-08-29 06:20:23 +03:00
|
|
|
|
|
|
|
// This is an AnimatedImage, that can draw encrypted images
|
|
|
|
class MxcAnimatedImage : public QQuickItem
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(TimelineModel *roomm READ room WRITE setRoom NOTIFY roomChanged REQUIRED)
|
|
|
|
Q_PROPERTY(QString eventId READ eventId WRITE setEventId NOTIFY eventIdChanged)
|
|
|
|
Q_PROPERTY(bool animatable READ animatable NOTIFY animatableChanged)
|
|
|
|
Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged)
|
|
|
|
Q_PROPERTY(bool play READ play WRITE setPlay NOTIFY playChanged)
|
2021-08-29 06:20:23 +03:00
|
|
|
public:
|
2021-09-18 01:22:33 +03:00
|
|
|
MxcAnimatedImage(QQuickItem *parent = nullptr)
|
|
|
|
: QQuickItem(parent)
|
|
|
|
{
|
|
|
|
connect(this, &MxcAnimatedImage::eventIdChanged, &MxcAnimatedImage::startDownload);
|
|
|
|
connect(this, &MxcAnimatedImage::roomChanged, &MxcAnimatedImage::startDownload);
|
2022-09-30 05:03:39 +03:00
|
|
|
connect(&movie, &QMovie::frameChanged, this, &MxcAnimatedImage::newFrame);
|
2021-09-18 01:22:33 +03:00
|
|
|
setFlag(QQuickItem::ItemHasContents);
|
|
|
|
// setAcceptHoverEvents(true);
|
|
|
|
}
|
2021-08-29 06:20:23 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
bool animatable() const { return animatable_; }
|
|
|
|
bool loaded() const { return buffer.size() > 0; }
|
|
|
|
bool play() const { return play_; }
|
|
|
|
QString eventId() const { return eventId_; }
|
|
|
|
TimelineModel *room() const { return room_; }
|
|
|
|
void setEventId(QString newEventId)
|
|
|
|
{
|
|
|
|
if (eventId_ != newEventId) {
|
|
|
|
eventId_ = newEventId;
|
|
|
|
emit eventIdChanged();
|
2021-08-29 06:20:23 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
void setRoom(TimelineModel *room)
|
|
|
|
{
|
|
|
|
if (room_ != room) {
|
|
|
|
room_ = room;
|
|
|
|
emit roomChanged();
|
2021-08-29 06:20:23 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
void setPlay(bool newPlay)
|
|
|
|
{
|
|
|
|
if (play_ != newPlay) {
|
|
|
|
play_ = newPlay;
|
2022-09-30 05:03:39 +03:00
|
|
|
movie.setPaused(!play_);
|
2021-09-18 01:22:33 +03:00
|
|
|
emit playChanged();
|
2021-08-29 16:33:39 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-08-29 06:20:23 +03:00
|
|
|
|
2022-01-02 23:46:29 +03:00
|
|
|
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
|
2021-09-18 01:22:33 +03:00
|
|
|
QSGNode *updatePaintNode(QSGNode *oldNode,
|
|
|
|
QQuickItem::UpdatePaintNodeData *updatePaintNodeData) override;
|
2021-08-29 06:20:23 +03:00
|
|
|
|
|
|
|
signals:
|
2021-09-18 01:22:33 +03:00
|
|
|
void roomChanged();
|
|
|
|
void eventIdChanged();
|
|
|
|
void animatableChanged();
|
|
|
|
void loadedChanged();
|
|
|
|
void playChanged();
|
2021-08-29 06:20:23 +03:00
|
|
|
|
|
|
|
private slots:
|
2021-09-18 01:22:33 +03:00
|
|
|
void startDownload();
|
2022-09-30 05:03:39 +03:00
|
|
|
void newFrame(int frame)
|
2021-09-18 01:22:33 +03:00
|
|
|
{
|
2022-09-30 05:03:39 +03:00
|
|
|
currentFrame = frame;
|
|
|
|
imageDirty = true;
|
2021-09-18 01:22:33 +03:00
|
|
|
update();
|
|
|
|
}
|
2021-08-29 06:20:23 +03:00
|
|
|
|
|
|
|
private:
|
2021-09-18 01:22:33 +03:00
|
|
|
TimelineModel *room_ = nullptr;
|
|
|
|
QString eventId_;
|
|
|
|
QString filename_;
|
|
|
|
bool animatable_ = false;
|
|
|
|
QBuffer buffer;
|
2022-09-30 05:03:39 +03:00
|
|
|
QMovie movie;
|
|
|
|
int currentFrame = 0;
|
|
|
|
bool imageDirty = true;
|
|
|
|
bool play_ = true;
|
2021-08-29 06:20:23 +03:00
|
|
|
};
|