Fix crash with clap plugins without MIDI input
[carla.git] / source / theme / CarlaStyleAnimations.hpp
blob050b31f07f44a23813db5c40a6bccf776c8ffdb7
1 /*
2 * Carla Style, based on Qt5 fusion style
3 * Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies)
4 * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * For a full copy of the license see the doc/LGPL.txt file
18 #ifndef CARLA_STYLE_ANIMATIONS_HPP_INCLUDED
19 #define CARLA_STYLE_ANIMATIONS_HPP_INCLUDED
21 #include "CarlaStyle.hpp"
23 #include <QtCore/QAbstractAnimation>
24 #include <QtCore/QCoreApplication>
25 #include <QtCore/QDateTime>
26 #include <QtGui/QImage>
28 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
29 # include <QtWidgets/QWidget>
30 #else
31 # include <QtGui/QWidget>
32 #endif
34 class CarlaStyleAnimation : public QAbstractAnimation
36 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
37 # pragma clang diagnostic push
38 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
39 #endif
40 Q_OBJECT
41 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
42 # pragma clang diagnostic pop
43 #endif
45 public:
46 CarlaStyleAnimation(QObject* target)
47 : QAbstractAnimation(),
48 _delay(0),
49 _duration(-1),
50 _startTime(QTime::currentTime())
52 if (target != nullptr)
54 moveToThread(target->thread());
55 setParent(target);
58 connect(this, SIGNAL(finished()), SLOT(deleteLater()));
61 QObject* target() const
63 return parent();
66 int duration() const override
68 return _duration;
71 void setDuration(int duration)
73 _duration = duration;
76 int delay() const
78 return _delay;
81 void setDelay(int delay)
83 _delay = delay;
86 QTime startTime() const
88 return _startTime;
91 void setStartTime(const QTime& time)
93 _startTime = time;
96 void updateTarget()
98 QEvent event(QEvent::HoverEnter);
99 QCoreApplication::sendEvent(target(), &event);
102 protected:
103 virtual bool isUpdateNeeded() const
105 return currentTime() > _delay;
108 virtual void updateCurrentTime(int /*time*/) override
110 if (QObject* tgt = target())
112 if (tgt->isWidgetType())
114 QWidget* widget = static_cast<QWidget*>(tgt);
115 if (widget->window()->isMinimized() || ! widget->isVisible())
116 stop();
119 if (isUpdateNeeded())
120 updateTarget();
124 private:
125 int _delay;
126 int _duration;
127 QTime _startTime;
130 class CarlaProgressStyleAnimation : public CarlaStyleAnimation
132 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
133 # pragma clang diagnostic push
134 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
135 #endif
136 Q_OBJECT
137 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
138 # pragma clang diagnostic pop
139 #endif
141 public:
142 CarlaProgressStyleAnimation(int speed, QObject* target)
143 : CarlaStyleAnimation(target),
144 _speed(speed),
145 _step(-1)
149 int animationStep() const
151 return currentTime() / (1000.0 / _speed);
154 int progressStep(int width) const
156 int step = animationStep();
157 int progress = (step * width / _speed) % width;
158 if (((step * width / _speed) % (2 * width)) >= width)
159 progress = width - progress;
160 return progress;
163 int speed() const
165 return _speed;
168 void setSpeed(int speed)
170 _speed = speed;
173 protected:
174 bool isUpdateNeeded() const override
176 if (CarlaStyleAnimation::isUpdateNeeded())
178 int current = animationStep();
179 if (_step == -1 || _step != current)
181 _step = current;
182 return true;
185 return false;
188 private:
189 int _speed;
190 mutable int _step;
193 #endif // CARLA_STYLE_ANIMATIONS_HPP_INCLUDED