Debian Codegraph @

src/Gui/Navigation/NavigationAnimator.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * Copyright (c) 2023 Bas Ruigrok (Rexbas) <[email protected]> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 ***************************************************************************/
23
24
25 #include "NavigationAnimator.h"
26 #include "NavigationAnimation.h"
27 #include <QEventLoop>
28
29 using namespace Gui;
30
31 NavigationAnimator::NavigationAnimator()
32 : activeAnimation(nullptr)
33 {}
34
35 NavigationAnimator::~NavigationAnimator()
36 {
37 stop();
38 }
39
40 /**
41 * @brief Start an animation
42 *
43 * @param animation The animation to start
44 */
45 void NavigationAnimator::start(const std::shared_ptr<NavigationAnimation>& animation)
46 {
47 stop();
48 activeAnimation = animation;
49 activeAnimation->initialize();
50
51 connect(activeAnimation.get(), &NavigationAnimation::finished, this, [this]() {
52 activeAnimation->onStop(true);
53 activeAnimation.reset();
54 });
55
56 activeAnimation->start();
57 }
58
59 /**
60 * @brief Start an animation and wait for it to finish
61 *
62 * @param animation The animation to start
63 * @return True if the animation finished, false if interrupted
64 */
65 bool NavigationAnimator::startAndWait(const std::shared_ptr<NavigationAnimation>& animation)
66 {
67 stop();
68 bool finished = true;
69 QEventLoop loop;
70 connect(animation.get(), &NavigationAnimation::finished, &loop, &QEventLoop::quit);
71 connect(animation.get(), &NavigationAnimation::interrupted, &loop, [&finished, &loop]() {
72 finished = false;
73 loop.quit();
74 });
75 start(animation);
76 loop.exec();
77 return finished;
78 }
79
80 /**
81 * @brief Stops an active animation and releases shared ownership of the animation
82 */
83 void NavigationAnimator::stop()
84 {
85 if (activeAnimation && activeAnimation->state() != QAbstractAnimation::State::Stopped) {
86 disconnect(activeAnimation.get(), &NavigationAnimation::finished, 0, 0);
87 Q_EMIT activeAnimation->interrupted();
88 activeAnimation->stop();
89 activeAnimation->onStop(false);
90 activeAnimation.reset();
91 }
92 }
93
94 /**
95 * @return Whether or not an animation is active
96 */
97 bool NavigationAnimator::isAnimating() const
98 {
99 if (activeAnimation != nullptr) {
100 return activeAnimation->state() == QAbstractAnimation::State::Running;
101 }
102
103 return false;
104 }