File indexing completed on 2022-12-06 18:50:09

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #include "animation.h"
0007 
0008 #include <QMovie>
0009 #include <QDebug>
0010 
0011 Animation::Animation(QObject* parent) : QObject(parent)
0012 {
0013 
0014 }
0015 
0016 Animation::~Animation()
0017 {
0018     if(m_movie)
0019         m_movie->stop();
0020 }
0021 
0022 void Animation::setMovie(QMovie* movie)
0023 {
0024     disconnect(nullptr, nullptr, this, SLOT(movieFrameChanged()));
0025     m_movie=movie;
0026     connect(movie, SIGNAL(frameChanged(int)), this, SLOT(movieFrameChanged()));
0027 }
0028 
0029 QMovie* Animation::movie()
0030 {
0031     return m_movie;
0032 }
0033 
0034 void Animation::setPosition(const QTextCursor& cursor)
0035 {
0036     m_position=cursor;
0037 }
0038 
0039 QTextCursor Animation::position()
0040 {
0041     return m_position;
0042 }
0043 
0044 void Animation::movieFrameChanged()
0045 {
0046     QTextCursor cursor = m_position;
0047     cursor.setPosition(m_position.position()+1, QTextCursor::KeepAnchor);
0048 
0049     const QString& text=cursor.selectedText();
0050 
0051     if (text==QString(QChar::ObjectReplacementCharacter)) {
0052         // update a bogus property, which will trigger a paint
0053         QTextCharFormat format2;
0054         format2.setProperty(QTextFormat::UserFormat + 2, m_movie->currentFrameNumber());
0055         cursor.mergeCharFormat(format2);
0056     } else {
0057         // we got removed from the document
0058         qDebug()<<"animation got removed";
0059         disconnect(m_movie.data(), &QMovie::frameChanged, this, &Animation::movieFrameChanged);
0060     }
0061 }
0062 
0063 
0064 
0065 AnimationHelperItem::AnimationHelperItem( ) : m_animation(new Animation())
0066 {
0067 }
0068 
0069 AnimationHelperItem::AnimationHelperItem(const AnimationHelperItem& other)
0070 {
0071     m_animation=other.m_animation;
0072 }
0073 
0074 void AnimationHelperItem::setPosition(const QTextCursor& cursor)
0075 {
0076     m_animation->setPosition(cursor);
0077 }
0078 
0079 QTextCursor AnimationHelperItem::position() const
0080 {
0081     return m_animation->position();
0082 }
0083 
0084 void AnimationHelperItem::setMovie(QMovie* movie)
0085 {
0086     m_animation->setMovie(movie);
0087 }
0088 
0089 QMovie* AnimationHelperItem::movie() const
0090 {
0091     return m_animation->movie();
0092 }
0093 
0094