1 | /* $Id: UIAnimationFramework.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIAnimationFramework class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QPropertyAnimation>
|
---|
30 | #include <QSignalTransition>
|
---|
31 | #include <QStateMachine>
|
---|
32 | #include <QWidget>
|
---|
33 |
|
---|
34 | /* GUI includes: */
|
---|
35 | #include "UIAnimationFramework.h"
|
---|
36 |
|
---|
37 | /* Other VBox includes: */
|
---|
38 | #include "iprt/assert.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /*********************************************************************************************************************************
|
---|
42 | * Class UIAnimation implementation. *
|
---|
43 | *********************************************************************************************************************************/
|
---|
44 |
|
---|
45 | /* static */
|
---|
46 | UIAnimation* UIAnimation::installPropertyAnimation(QWidget *pTarget, const char *pszPropertyName,
|
---|
47 | const char *pszValuePropertyNameStart, const char *pszValuePropertyNameFinal,
|
---|
48 | const char *pszSignalForward, const char *pszSignalReverse,
|
---|
49 | bool fReverse /* = false */, int iAnimationDuration /* = 300 */)
|
---|
50 | {
|
---|
51 | /* Return newly created animation-machine: */
|
---|
52 | return new UIAnimation(pTarget, pszPropertyName,
|
---|
53 | pszValuePropertyNameStart, pszValuePropertyNameFinal,
|
---|
54 | pszSignalForward, pszSignalReverse,
|
---|
55 | fReverse, iAnimationDuration);
|
---|
56 | }
|
---|
57 |
|
---|
58 | void UIAnimation::update()
|
---|
59 | {
|
---|
60 | /* Update 'forward' animation: */
|
---|
61 | m_pForwardAnimation->setStartValue(parent()->property(m_pszValuePropertyNameStart));
|
---|
62 | m_pForwardAnimation->setEndValue(parent()->property(m_pszValuePropertyNameFinal));
|
---|
63 | m_pStateStart->assignProperty(parent(), m_pszPropertyName, parent()->property(m_pszValuePropertyNameStart));
|
---|
64 | /* Update 'reverse' animation: */
|
---|
65 | m_pReverseAnimation->setStartValue(parent()->property(m_pszValuePropertyNameFinal));
|
---|
66 | m_pReverseAnimation->setEndValue(parent()->property(m_pszValuePropertyNameStart));
|
---|
67 | m_pStateFinal->assignProperty(parent(), m_pszPropertyName, parent()->property(m_pszValuePropertyNameFinal));
|
---|
68 | }
|
---|
69 |
|
---|
70 | UIAnimation::UIAnimation(QWidget *pParent, const char *pszPropertyName,
|
---|
71 | const char *pszValuePropertyNameStart, const char *pszValuePropertyNameFinal,
|
---|
72 | const char *pszSignalForward, const char *pszSignalReverse,
|
---|
73 | bool fReverse, int iAnimationDuration)
|
---|
74 | : QObject(pParent)
|
---|
75 | , m_pszPropertyName(pszPropertyName)
|
---|
76 | , m_pszValuePropertyNameStart(pszValuePropertyNameStart), m_pszValuePropertyNameFinal(pszValuePropertyNameFinal)
|
---|
77 | , m_pszSignalForward(pszSignalForward), m_pszSignalReverse(pszSignalReverse)
|
---|
78 | , m_fReverse(fReverse), m_iAnimationDuration(iAnimationDuration)
|
---|
79 | , m_pAnimationMachine(0), m_pForwardAnimation(0), m_pReverseAnimation(0)
|
---|
80 | {
|
---|
81 | /* Prepare: */
|
---|
82 | prepare();
|
---|
83 | }
|
---|
84 |
|
---|
85 | void UIAnimation::prepare()
|
---|
86 | {
|
---|
87 | /* Make sure parent asigned: */
|
---|
88 | AssertPtrReturnVoid(parent());
|
---|
89 |
|
---|
90 | /* Prepare animation-machine: */
|
---|
91 | m_pAnimationMachine = new QStateMachine(this);
|
---|
92 | /* Create 'start' state: */
|
---|
93 | m_pStateStart = new QState(m_pAnimationMachine);
|
---|
94 | m_pStateStart->assignProperty(parent(), "AnimationState", QString("Start"));
|
---|
95 | connect(m_pStateStart, &QState::propertiesAssigned, this, &UIAnimation::sigStateEnteredStart);
|
---|
96 | /* Create 'final' state: */
|
---|
97 | m_pStateFinal = new QState(m_pAnimationMachine);
|
---|
98 | m_pStateFinal->assignProperty(parent(), "AnimationState", QString("Final"));
|
---|
99 | connect(m_pStateFinal, &QState::propertiesAssigned, this, &UIAnimation::sigStateEnteredFinal);
|
---|
100 |
|
---|
101 | /* Prepare 'forward' animation: */
|
---|
102 | m_pForwardAnimation = new QPropertyAnimation(parent(), m_pszPropertyName, m_pAnimationMachine);
|
---|
103 | m_pForwardAnimation->setEasingCurve(QEasingCurve(QEasingCurve::InOutCubic));
|
---|
104 | m_pForwardAnimation->setDuration(m_iAnimationDuration);
|
---|
105 | /* Prepare 'reverse' animation: */
|
---|
106 | m_pReverseAnimation = new QPropertyAnimation(parent(), m_pszPropertyName, m_pAnimationMachine);
|
---|
107 | m_pReverseAnimation->setEasingCurve(QEasingCurve(QEasingCurve::InOutCubic));
|
---|
108 | m_pReverseAnimation->setDuration(m_iAnimationDuration);
|
---|
109 |
|
---|
110 | /* Prepare state-transitions: */
|
---|
111 | QSignalTransition *pStartToFinal = m_pStateStart->addTransition(parent(), m_pszSignalForward, m_pStateFinal);
|
---|
112 | AssertPtrReturnVoid(pStartToFinal);
|
---|
113 | pStartToFinal->addAnimation(m_pForwardAnimation);
|
---|
114 | QSignalTransition *pFinalToStart = m_pStateFinal->addTransition(parent(), m_pszSignalReverse, m_pStateStart);
|
---|
115 | AssertPtrReturnVoid(pFinalToStart);
|
---|
116 | pFinalToStart->addAnimation(m_pReverseAnimation);
|
---|
117 |
|
---|
118 | /* Fetch animation-borders: */
|
---|
119 | update();
|
---|
120 |
|
---|
121 | /* Choose initial state: */
|
---|
122 | m_pAnimationMachine->setInitialState(!m_fReverse ? m_pStateStart : m_pStateFinal);
|
---|
123 | /* Start animation-machine: */
|
---|
124 | m_pAnimationMachine->start();
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /*********************************************************************************************************************************
|
---|
129 | * Class UIAnimation implementation. *
|
---|
130 | *********************************************************************************************************************************/
|
---|
131 |
|
---|
132 | /* static */
|
---|
133 | UIAnimationLoop* UIAnimationLoop::installAnimationLoop(QWidget *pTarget, const char *pszPropertyName,
|
---|
134 | const char *pszValuePropertyNameStart, const char *pszValuePropertyNameFinal,
|
---|
135 | int iAnimationDuration /* = 300*/)
|
---|
136 | {
|
---|
137 | /* Return newly created animation-loop: */
|
---|
138 | return new UIAnimationLoop(pTarget, pszPropertyName,
|
---|
139 | pszValuePropertyNameStart, pszValuePropertyNameFinal,
|
---|
140 | iAnimationDuration);
|
---|
141 | }
|
---|
142 |
|
---|
143 | void UIAnimationLoop::update()
|
---|
144 | {
|
---|
145 | /* Update animation: */
|
---|
146 | m_pAnimation->setStartValue(parent()->property(m_pszValuePropertyNameStart));
|
---|
147 | m_pAnimation->setEndValue(parent()->property(m_pszValuePropertyNameFinal));
|
---|
148 | }
|
---|
149 |
|
---|
150 | void UIAnimationLoop::start()
|
---|
151 | {
|
---|
152 | /* Start animation: */
|
---|
153 | m_pAnimation->start();
|
---|
154 | }
|
---|
155 |
|
---|
156 | void UIAnimationLoop::stop()
|
---|
157 | {
|
---|
158 | /* Stop animation: */
|
---|
159 | m_pAnimation->stop();
|
---|
160 | }
|
---|
161 |
|
---|
162 | UIAnimationLoop::UIAnimationLoop(QWidget *pParent, const char *pszPropertyName,
|
---|
163 | const char *pszValuePropertyNameStart, const char *pszValuePropertyNameFinal,
|
---|
164 | int iAnimationDuration)
|
---|
165 | : QObject(pParent)
|
---|
166 | , m_pszPropertyName(pszPropertyName)
|
---|
167 | , m_pszValuePropertyNameStart(pszValuePropertyNameStart), m_pszValuePropertyNameFinal(pszValuePropertyNameFinal)
|
---|
168 | , m_iAnimationDuration(iAnimationDuration)
|
---|
169 | , m_pAnimation(0)
|
---|
170 | {
|
---|
171 | /* Prepare: */
|
---|
172 | prepare();
|
---|
173 | }
|
---|
174 |
|
---|
175 | void UIAnimationLoop::prepare()
|
---|
176 | {
|
---|
177 | /* Prepare loop: */
|
---|
178 | m_pAnimation = new QPropertyAnimation(parent(), m_pszPropertyName, this);
|
---|
179 | m_pAnimation->setDuration(m_iAnimationDuration);
|
---|
180 | m_pAnimation->setLoopCount(-1);
|
---|
181 |
|
---|
182 | /* Fetch animation-borders: */
|
---|
183 | update();
|
---|
184 | }
|
---|
185 |
|
---|