Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
3.8KB

  1. /*
  2. * Carla Style, based on Qt5 fusion style
  3. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  4. * Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 3 of
  9. * the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * For a full copy of the GNU General Public License see the GPL3.txt file
  17. */
  18. #ifndef __CARLA_STYLE_ANIMATIONS_HPP__
  19. #define __CARLA_STYLE_ANIMATIONS_HPP__
  20. #include "CarlaStyle.hpp"
  21. #include <QtCore/QAbstractAnimation>
  22. #include <QtCore/QCoreApplication>
  23. #include <QtCore/QDateTime>
  24. #include <QtGui/QImage>
  25. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  26. # include <QtWidgets/QWidget>
  27. #else
  28. # include <QtGui/QWidget>
  29. #endif
  30. class CarlaStyleAnimation : public QAbstractAnimation
  31. {
  32. Q_OBJECT
  33. public:
  34. CarlaStyleAnimation(QObject* target)
  35. : QAbstractAnimation(),
  36. _delay(0),
  37. _duration(-1),
  38. _startTime(QTime::currentTime())
  39. {
  40. if (target != nullptr)
  41. {
  42. moveToThread(target->thread());
  43. setParent(target);
  44. }
  45. connect(this, SIGNAL(finished()), SLOT(deleteLater()));
  46. }
  47. virtual ~CarlaStyleAnimation()
  48. {
  49. }
  50. QObject* target() const
  51. {
  52. return parent();
  53. }
  54. int duration() const
  55. {
  56. return _duration;
  57. }
  58. void setDuration(int duration)
  59. {
  60. _duration = duration;
  61. }
  62. int delay() const
  63. {
  64. return _delay;
  65. }
  66. void setDelay(int delay)
  67. {
  68. _delay = delay;
  69. }
  70. QTime startTime() const
  71. {
  72. return _startTime;
  73. }
  74. void setStartTime(const QTime& time)
  75. {
  76. _startTime = time;
  77. }
  78. void updateTarget()
  79. {
  80. QEvent event(QEvent::HoverEnter);
  81. QCoreApplication::sendEvent(target(), &event);
  82. }
  83. protected:
  84. virtual bool isUpdateNeeded() const
  85. {
  86. return currentTime() > _delay;
  87. }
  88. virtual void updateCurrentTime(int /*time*/)
  89. {
  90. if (QObject* tgt = target())
  91. {
  92. if (tgt->isWidgetType())
  93. {
  94. QWidget* widget = static_cast<QWidget*>(tgt);
  95. if (widget->window()->isMinimized() || ! widget->isVisible())
  96. stop();
  97. }
  98. if (isUpdateNeeded())
  99. updateTarget();
  100. }
  101. }
  102. private:
  103. int _delay;
  104. int _duration;
  105. QTime _startTime;
  106. };
  107. class CarlaProgressStyleAnimation : public CarlaStyleAnimation
  108. {
  109. Q_OBJECT
  110. public:
  111. CarlaProgressStyleAnimation(int speed, QObject* target)
  112. : CarlaStyleAnimation(target),
  113. _speed(speed),
  114. _step(-1)
  115. {
  116. }
  117. int animationStep() const
  118. {
  119. return currentTime() / (1000.0 / _speed);
  120. }
  121. int progressStep(int width) const
  122. {
  123. int step = animationStep();
  124. int progress = (step * width / _speed) % width;
  125. if (((step * width / _speed) % (2 * width)) >= width)
  126. progress = width - progress;
  127. return progress;
  128. }
  129. int speed() const
  130. {
  131. return _speed;
  132. }
  133. void setSpeed(int speed)
  134. {
  135. _speed = speed;
  136. }
  137. protected:
  138. bool isUpdateNeeded() const
  139. {
  140. if (CarlaStyleAnimation::isUpdateNeeded())
  141. {
  142. int current = animationStep();
  143. if (_step == -1 || _step != current)
  144. {
  145. _step = current;
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. private:
  152. int _speed;
  153. mutable int _step;
  154. };
  155. #endif // __CARLA_STYLE_ANIMATIONS_HPP__