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.

178 lines
3.7KB

  1. /*
  2. * Carla Qt4 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 <QtCore/QAbstractAnimation>
  21. #include <QtCore/QCoreApplication>
  22. #include <QtCore/QDateTime>
  23. #include <QtGui/QImage>
  24. #include <QtGui/QWidget>
  25. class CarlaStyleAnimation : public QAbstractAnimation
  26. {
  27. Q_OBJECT
  28. public:
  29. CarlaStyleAnimation(QObject* target)
  30. : QAbstractAnimation(),
  31. _delay(0),
  32. _duration(-1),
  33. _startTime(QTime::currentTime())
  34. {
  35. if (target != nullptr)
  36. {
  37. moveToThread(target->thread());
  38. setParent(target);
  39. }
  40. connect(this, SIGNAL(finished()), SLOT(deleteLater()));
  41. }
  42. virtual ~CarlaStyleAnimation()
  43. {
  44. }
  45. QObject* target() const
  46. {
  47. return parent();
  48. }
  49. int duration() const
  50. {
  51. return _duration;
  52. }
  53. void setDuration(int duration)
  54. {
  55. _duration = duration;
  56. }
  57. int delay() const
  58. {
  59. return _delay;
  60. }
  61. void setDelay(int delay)
  62. {
  63. _delay = delay;
  64. }
  65. QTime startTime() const
  66. {
  67. return _startTime;
  68. }
  69. void setStartTime(const QTime& time)
  70. {
  71. _startTime = time;
  72. }
  73. void updateTarget()
  74. {
  75. QEvent event(QEvent::HoverEnter);
  76. QCoreApplication::sendEvent(target(), &event);
  77. }
  78. protected:
  79. virtual bool isUpdateNeeded() const
  80. {
  81. return currentTime() > _delay;
  82. }
  83. virtual void updateCurrentTime(int /*time*/)
  84. {
  85. if (QObject* tgt = target())
  86. {
  87. if (tgt->isWidgetType())
  88. {
  89. QWidget* widget = static_cast<QWidget*>(tgt);
  90. if (widget->window()->isMinimized() || ! widget->isVisible())
  91. stop();
  92. }
  93. if (isUpdateNeeded())
  94. updateTarget();
  95. }
  96. }
  97. private:
  98. int _delay;
  99. int _duration;
  100. QTime _startTime;
  101. };
  102. class CarlaProgressStyleAnimation : public CarlaStyleAnimation
  103. {
  104. Q_OBJECT
  105. public:
  106. CarlaProgressStyleAnimation(int speed, QObject* target)
  107. : CarlaStyleAnimation(target),
  108. _speed(speed),
  109. _step(-1)
  110. {
  111. }
  112. int animationStep() const
  113. {
  114. return currentTime() / (1000.0 / _speed);
  115. }
  116. int progressStep(int width) const
  117. {
  118. int step = animationStep();
  119. int progress = (step * width / _speed) % width;
  120. if (((step * width / _speed) % (2 * width)) >= width)
  121. progress = width - progress;
  122. return progress;
  123. }
  124. int speed() const
  125. {
  126. return _speed;
  127. }
  128. void setSpeed(int speed)
  129. {
  130. _speed = speed;
  131. }
  132. protected:
  133. bool isUpdateNeeded() const
  134. {
  135. if (CarlaStyleAnimation::isUpdateNeeded())
  136. {
  137. int current = animationStep();
  138. if (_step == -1 || _step != current)
  139. {
  140. _step = current;
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. private:
  147. int _speed;
  148. mutable int _step;
  149. };
  150. #endif // __CARLA_STYLE_ANIMATIONS_HPP__