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.

183 lines
3.8KB

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