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.

184 lines
3.8KB

  1. /*
  2. * Carla Style, based on Qt5 fusion style
  3. * Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies)
  4. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  5. *
  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.
  9. *
  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.
  14. *
  15. * For a full copy of the license see the doc/LGPL.txt file
  16. */
  17. #ifndef CARLA_STYLE_ANIMATIONS_HPP_INCLUDED
  18. #define CARLA_STYLE_ANIMATIONS_HPP_INCLUDED
  19. #include "CarlaStyle.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. ~CarlaStyleAnimation() override
  47. {
  48. }
  49. QObject* target() const
  50. {
  51. return parent();
  52. }
  53. int duration() const override
  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 override
  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_INCLUDED