1 | /* |
2 | SPDX-FileCopyrightText: 2013 Dominik Haumann <dhaumann@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #include "katefadeeffect.h" |
8 | |
9 | #include <QGraphicsOpacityEffect> |
10 | #include <QTimeLine> |
11 | #include <QWidget> |
12 | |
13 | KateFadeEffect::KateFadeEffect(QWidget *widget) |
14 | : QObject(widget) |
15 | , m_widget(widget) |
16 | , m_effect(nullptr) // effect only exists during fading animation |
17 | { |
18 | m_timeLine = new QTimeLine(500, this); |
19 | m_timeLine->setUpdateInterval(40); |
20 | |
21 | connect(sender: m_timeLine, signal: &QTimeLine::valueChanged, context: this, slot: &KateFadeEffect::opacityChanged); |
22 | connect(sender: m_timeLine, signal: &QTimeLine::finished, context: this, slot: &KateFadeEffect::animationFinished); |
23 | } |
24 | |
25 | bool KateFadeEffect::isHideAnimationRunning() const |
26 | { |
27 | return (m_timeLine->direction() == QTimeLine::Backward) && (m_timeLine->state() == QTimeLine::Running); |
28 | } |
29 | |
30 | bool KateFadeEffect::isShowAnimationRunning() const |
31 | { |
32 | return (m_timeLine->direction() == QTimeLine::Forward) && (m_timeLine->state() == QTimeLine::Running); |
33 | } |
34 | |
35 | void KateFadeEffect::fadeIn() |
36 | { |
37 | // stop time line if still running |
38 | if (m_timeLine->state() == QTimeLine::Running) { |
39 | QTimeLine::Direction direction = m_timeLine->direction(); |
40 | m_timeLine->stop(); |
41 | if (direction == QTimeLine::Backward) { |
42 | // fadeOut animation interrupted |
43 | Q_EMIT hideAnimationFinished(); |
44 | } |
45 | } |
46 | |
47 | // assign new graphics effect, old one is deleted in setGraphicsEffect() |
48 | m_effect = new QGraphicsOpacityEffect(this); |
49 | m_effect->setOpacity(0.0); |
50 | m_widget->setGraphicsEffect(m_effect); |
51 | |
52 | // show widget and start fade in animation |
53 | m_widget->show(); |
54 | m_timeLine->setDirection(QTimeLine::Forward); |
55 | m_timeLine->start(); |
56 | } |
57 | |
58 | void KateFadeEffect::fadeOut() |
59 | { |
60 | // stop time line if still running |
61 | if (m_timeLine->state() == QTimeLine::Running) { |
62 | QTimeLine::Direction direction = m_timeLine->direction(); |
63 | m_timeLine->stop(); |
64 | if (direction == QTimeLine::Forward) { |
65 | // fadeIn animation interrupted |
66 | Q_EMIT showAnimationFinished(); |
67 | } |
68 | } |
69 | |
70 | // assign new graphics effect, old one is deleted in setGraphicsEffect() |
71 | m_effect = new QGraphicsOpacityEffect(this); |
72 | m_effect->setOpacity(1.0); |
73 | m_widget->setGraphicsEffect(m_effect); |
74 | |
75 | // start fade out animation |
76 | m_timeLine->setDirection(QTimeLine::Backward); |
77 | m_timeLine->start(); |
78 | } |
79 | |
80 | void KateFadeEffect::opacityChanged(qreal value) |
81 | { |
82 | Q_ASSERT(m_effect); |
83 | m_effect->setOpacity(value); |
84 | } |
85 | |
86 | void KateFadeEffect::animationFinished() |
87 | { |
88 | // fading finished: remove graphics effect, deletes the effect as well |
89 | m_widget->setGraphicsEffect(nullptr); |
90 | Q_ASSERT(!m_effect); |
91 | |
92 | if (m_timeLine->direction() == QTimeLine::Backward) { |
93 | m_widget->hide(); |
94 | Q_EMIT hideAnimationFinished(); |
95 | } else { |
96 | Q_EMIT showAnimationFinished(); |
97 | } |
98 | } |
99 | |
100 | #include "moc_katefadeeffect.cpp" |
101 | |