1 /*
2 SPDX-License-Identifier: GPL-2.0-or-later
3 SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
4 */
5
6 #include "animation.h"
7
8 #include <QMovie>
9 #include <QDebug>
10
Animation(QObject * parent)11 Animation::Animation(QObject* parent) : QObject(parent)
12 {
13
14 }
15
~Animation()16 Animation::~Animation()
17 {
18 if(m_movie)
19 m_movie->stop();
20 }
21
setMovie(QMovie * movie)22 void Animation::setMovie(QMovie* movie)
23 {
24 disconnect(nullptr, nullptr, this, SLOT(movieFrameChanged()));
25 m_movie=movie;
26 connect(movie, SIGNAL(frameChanged(int)), this, SLOT(movieFrameChanged()));
27 }
28
movie()29 QMovie* Animation::movie()
30 {
31 return m_movie;
32 }
33
setPosition(const QTextCursor & cursor)34 void Animation::setPosition(const QTextCursor& cursor)
35 {
36 m_position=cursor;
37 }
38
position()39 QTextCursor Animation::position()
40 {
41 return m_position;
42 }
43
movieFrameChanged()44 void Animation::movieFrameChanged()
45 {
46 QTextCursor cursor = m_position;
47 cursor.setPosition(m_position.position()+1, QTextCursor::KeepAnchor);
48
49 const QString& text=cursor.selectedText();
50
51 if (text==QString(QChar::ObjectReplacementCharacter)) {
52 // update a bogus property, which will trigger a paint
53 QTextCharFormat format2;
54 format2.setProperty(QTextFormat::UserFormat + 2, m_movie->currentFrameNumber());
55 cursor.mergeCharFormat(format2);
56 } else {
57 // we got removed from the document
58 qDebug()<<"animation got removed";
59 disconnect(m_movie.data(), &QMovie::frameChanged, this, &Animation::movieFrameChanged);
60 }
61 }
62
63
64
AnimationHelperItem()65 AnimationHelperItem::AnimationHelperItem( ) : m_animation(new Animation())
66 {
67 }
68
AnimationHelperItem(const AnimationHelperItem & other)69 AnimationHelperItem::AnimationHelperItem(const AnimationHelperItem& other)
70 {
71 m_animation=other.m_animation;
72 }
73
setPosition(const QTextCursor & cursor)74 void AnimationHelperItem::setPosition(const QTextCursor& cursor)
75 {
76 m_animation->setPosition(cursor);
77 }
78
position() const79 QTextCursor AnimationHelperItem::position() const
80 {
81 return m_animation->position();
82 }
83
setMovie(QMovie * movie)84 void AnimationHelperItem::setMovie(QMovie* movie)
85 {
86 m_animation->setMovie(movie);
87 }
88
movie() const89 QMovie* AnimationHelperItem::movie() const
90 {
91 return m_animation->movie();
92 }
93
94
95