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.

180 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. QObject* target() const
  47. {
  48. return parent();
  49. }
  50. int duration() const override
  51. {
  52. return _duration;
  53. }
  54. void setDuration(int duration)
  55. {
  56. _duration = duration;
  57. }
  58. int delay() const
  59. {
  60. return _delay;
  61. }
  62. void setDelay(int delay)
  63. {
  64. _delay = delay;
  65. }
  66. QTime startTime() const
  67. {
  68. return _startTime;
  69. }
  70. void setStartTime(const QTime& time)
  71. {
  72. _startTime = time;
  73. }
  74. void updateTarget()
  75. {
  76. QEvent event(QEvent::HoverEnter);
  77. QCoreApplication::sendEvent(target(), &event);
  78. }
  79. protected:
  80. virtual bool isUpdateNeeded() const
  81. {
  82. return currentTime() > _delay;
  83. }
  84. virtual void updateCurrentTime(int /*time*/)
  85. {
  86. if (QObject* tgt = target())
  87. {
  88. if (tgt->isWidgetType())
  89. {
  90. QWidget* widget = static_cast<QWidget*>(tgt);
  91. if (widget->window()->isMinimized() || ! widget->isVisible())
  92. stop();
  93. }
  94. if (isUpdateNeeded())
  95. updateTarget();
  96. }
  97. }
  98. private:
  99. int _delay;
  100. int _duration;
  101. QTime _startTime;
  102. };
  103. class CarlaProgressStyleAnimation : public CarlaStyleAnimation
  104. {
  105. Q_OBJECT
  106. public:
  107. CarlaProgressStyleAnimation(int speed, QObject* target)
  108. : CarlaStyleAnimation(target),
  109. _speed(speed),
  110. _step(-1)
  111. {
  112. }
  113. int animationStep() const
  114. {
  115. return currentTime() / (1000.0 / _speed);
  116. }
  117. int progressStep(int width) const
  118. {
  119. int step = animationStep();
  120. int progress = (step * width / _speed) % width;
  121. if (((step * width / _speed) % (2 * width)) >= width)
  122. progress = width - progress;
  123. return progress;
  124. }
  125. int speed() const
  126. {
  127. return _speed;
  128. }
  129. void setSpeed(int speed)
  130. {
  131. _speed = speed;
  132. }
  133. protected:
  134. bool isUpdateNeeded() const override
  135. {
  136. if (CarlaStyleAnimation::isUpdateNeeded())
  137. {
  138. int current = animationStep();
  139. if (_step == -1 || _step != current)
  140. {
  141. _step = current;
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. private:
  148. int _speed;
  149. mutable int _step;
  150. };
  151. #endif // CARLA_STYLE_ANIMATIONS_HPP_INCLUDED