KTextAddons

textautogeneratemessagewaitingansweranimation.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "textautogeneratemessagewaitingansweranimation.h"
8
9#include <QParallelAnimationGroup>
10#include <QPropertyAnimation>
11#include <QSequentialAnimationGroup>
12
13using namespace Qt::Literals::StringLiterals;
14using namespace TextAutogenerateText;
15TextAutogenerateMessageWaitingAnswerAnimation::TextAutogenerateMessageWaitingAnswerAnimation(QObject *parent)
16 : QObject{parent}
17{
18 createAnimations();
19}
20
21TextAutogenerateMessageWaitingAnswerAnimation::~TextAutogenerateMessageWaitingAnswerAnimation() = default;
22
23QPersistentModelIndex TextAutogenerateMessageWaitingAnswerAnimation::modelIndex() const
24{
25 return mModelIndex;
26}
27
28void TextAutogenerateMessageWaitingAnswerAnimation::setModelIndex(const QPersistentModelIndex &newModelIndex)
29{
30 mModelIndex = newModelIndex;
31}
32
33void TextAutogenerateMessageWaitingAnswerAnimation::start()
34{
35 mSequencials->setLoopCount(-1);
36 mSequencials->start();
37}
38
39void TextAutogenerateMessageWaitingAnswerAnimation::stopAndDelete()
40{
41 mSequencials->stop();
43}
44
45int TextAutogenerateMessageWaitingAnswerAnimation::count() const
46{
47 return mScaleOpacities.count();
48}
49
50void TextAutogenerateMessageWaitingAnswerAnimation::createAnimations()
51{
52 mSequencials = new QSequentialAnimationGroup(this);
53 const int duration = 300; // Ms
54 mSequencials->addPause(duration);
55 for (int i = 0; i < 3; i++) {
56 mSequencials->addAnimation(createAnimation(i, duration));
57 }
58}
59
60TextAutogenerateMessageWaitingAnswerAnimation::ScaleAndOpacity TextAutogenerateMessageWaitingAnswerAnimation::value(int i) const
61{
62 if (i >= 0 && i < mScaleOpacities.count()) {
63 return mScaleOpacities.at(i);
64 }
65 return {};
66}
67
68QSequentialAnimationGroup *TextAutogenerateMessageWaitingAnswerAnimation::createAnimation(int index, int duration)
69{
70 ScaleAndOpacity s;
71 mScaleOpacities.append(s);
72 auto scaleAnimationUp = new QPropertyAnimation(this);
73
74 scaleAnimationUp->setTargetObject(this);
75 scaleAnimationUp->setStartValue(1.0);
76 scaleAnimationUp->setEndValue(1.33);
77 scaleAnimationUp->setDuration(duration);
78 connect(scaleAnimationUp, &QPropertyAnimation::valueChanged, this, [this, index](const QVariant &value) {
79 ScaleAndOpacity &s = mScaleOpacities[index];
80 s.scale = value.toDouble();
81 Q_EMIT valueChanged();
82 });
83
84 auto opacityAnimationUp = new QPropertyAnimation(this);
85 opacityAnimationUp->setTargetObject(this);
86 opacityAnimationUp->setStartValue(0.5);
87 opacityAnimationUp->setEndValue(1.0);
88 opacityAnimationUp->setDuration(duration);
89 connect(opacityAnimationUp, &QPropertyAnimation::valueChanged, this, [this, index](const QVariant &value) {
90 ScaleAndOpacity &s = mScaleOpacities[index];
91 s.opacity = value.toDouble();
92 Q_EMIT valueChanged();
93 });
94
95 auto scaleAnimationDown = new QPropertyAnimation(this);
96
97 scaleAnimationDown->setTargetObject(this);
98 scaleAnimationDown->setStartValue(1.33);
99 scaleAnimationDown->setEndValue(1.0);
100 scaleAnimationDown->setDuration(duration);
101 connect(scaleAnimationDown, &QPropertyAnimation::valueChanged, this, [this, index](const QVariant &value) {
102 ScaleAndOpacity &s = mScaleOpacities[index];
103 s.scale = value.toDouble();
104 Q_EMIT valueChanged();
105 });
106
107 auto opacityAnimationDown = new QPropertyAnimation(this);
108 opacityAnimationDown->setTargetObject(this);
109 opacityAnimationDown->setStartValue(1.0);
110 opacityAnimationDown->setEndValue(0.5);
111 opacityAnimationDown->setDuration(duration);
112 connect(opacityAnimationDown, &QPropertyAnimation::valueChanged, this, [this, index](const QVariant &value) {
113 ScaleAndOpacity &s = mScaleOpacities[index];
114 s.opacity = value.toDouble();
115 Q_EMIT valueChanged();
116 });
117
118 auto groupUp = new QParallelAnimationGroup(this);
119 groupUp->addAnimation(scaleAnimationUp);
120 groupUp->addAnimation(opacityAnimationUp);
121
122 auto groupDown = new QParallelAnimationGroup(this);
123 groupDown->addAnimation(scaleAnimationDown);
124 groupDown->addAnimation(opacityAnimationDown);
125
126 auto sequencial = new QSequentialAnimationGroup(this);
127 const auto value = duration * (index + 1) / 2;
128 sequencial->addPause(value);
129 sequencial->addAnimation(groupUp);
130 sequencial->addAnimation(groupDown);
131 sequencial->setLoopCount(-1);
132 sequencial->start();
133 return sequencial;
134}
135
136QList<TextAutogenerateMessageWaitingAnswerAnimation::ScaleAndOpacity> TextAutogenerateMessageWaitingAnswerAnimation::scaleOpacities() const
137{
138 return mScaleOpacities;
139}
140
141QDebug operator<<(QDebug d, const TextAutogenerateText::TextAutogenerateMessageWaitingAnswerAnimation::ScaleAndOpacity &t)
142{
143 d.space() << "scale:" << t.scale;
144 d.space() << "opacity:" << t.opacity;
145 return d;
146}
147
148#include "moc_textautogeneratemessagewaitingansweranimation.cpp"
QDebug operator<<(QDebug dbg, const PerceptualColor::MultiSpinBoxSection &value)
QDebug & space()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
void valueChanged(const QVariant &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 12:06:03 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.