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.

194 lines
4.4KB

  1. /*
  2. * Carla Style, based on Qt5 fusion style
  3. * Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies)
  4. * Copyright (C) 2013-2014 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. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
  32. # pragma clang diagnostic push
  33. # pragma clang diagnostic ignored "-Winconsistent-missing-override"
  34. #endif
  35. Q_OBJECT
  36. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
  37. # pragma clang diagnostic pop
  38. #endif
  39. public:
  40. CarlaStyleAnimation(QObject* target)
  41. : QAbstractAnimation(),
  42. _delay(0),
  43. _duration(-1),
  44. _startTime(QTime::currentTime())
  45. {
  46. if (target != nullptr)
  47. {
  48. moveToThread(target->thread());
  49. setParent(target);
  50. }
  51. connect(this, SIGNAL(finished()), SLOT(deleteLater()));
  52. }
  53. QObject* target() const
  54. {
  55. return parent();
  56. }
  57. int duration() const override
  58. {
  59. return _duration;
  60. }
  61. void setDuration(int duration)
  62. {
  63. _duration = duration;
  64. }
  65. int delay() const
  66. {
  67. return _delay;
  68. }
  69. void setDelay(int delay)
  70. {
  71. _delay = delay;
  72. }
  73. QTime startTime() const
  74. {
  75. return _startTime;
  76. }
  77. void setStartTime(const QTime& time)
  78. {
  79. _startTime = time;
  80. }
  81. void updateTarget()
  82. {
  83. QEvent event(QEvent::HoverEnter);
  84. QCoreApplication::sendEvent(target(), &event);
  85. }
  86. protected:
  87. virtual bool isUpdateNeeded() const
  88. {
  89. return currentTime() > _delay;
  90. }
  91. virtual void updateCurrentTime(int /*time*/) override
  92. {
  93. if (QObject* tgt = target())
  94. {
  95. if (tgt->isWidgetType())
  96. {
  97. QWidget* widget = static_cast<QWidget*>(tgt);
  98. if (widget->window()->isMinimized() || ! widget->isVisible())
  99. stop();
  100. }
  101. if (isUpdateNeeded())
  102. updateTarget();
  103. }
  104. }
  105. private:
  106. int _delay;
  107. int _duration;
  108. QTime _startTime;
  109. };
  110. class CarlaProgressStyleAnimation : public CarlaStyleAnimation
  111. {
  112. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
  113. # pragma clang diagnostic push
  114. # pragma clang diagnostic ignored "-Winconsistent-missing-override"
  115. #endif
  116. Q_OBJECT
  117. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) && defined(__clang_major__) && __clang_major__ >= 4
  118. # pragma clang diagnostic pop
  119. #endif
  120. public:
  121. CarlaProgressStyleAnimation(int speed, QObject* target)
  122. : CarlaStyleAnimation(target),
  123. _speed(speed),
  124. _step(-1)
  125. {
  126. }
  127. int animationStep() const
  128. {
  129. return currentTime() / (1000.0 / _speed);
  130. }
  131. int progressStep(int width) const
  132. {
  133. int step = animationStep();
  134. int progress = (step * width / _speed) % width;
  135. if (((step * width / _speed) % (2 * width)) >= width)
  136. progress = width - progress;
  137. return progress;
  138. }
  139. int speed() const
  140. {
  141. return _speed;
  142. }
  143. void setSpeed(int speed)
  144. {
  145. _speed = speed;
  146. }
  147. protected:
  148. bool isUpdateNeeded() const override
  149. {
  150. if (CarlaStyleAnimation::isUpdateNeeded())
  151. {
  152. int current = animationStep();
  153. if (_step == -1 || _step != current)
  154. {
  155. _step = current;
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. private:
  162. int _speed;
  163. mutable int _step;
  164. };
  165. #endif // CARLA_STYLE_ANIMATIONS_HPP_INCLUDED