continuous_sliders.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "ui/effects/animations.h"
  9. #include "ui/rp_widget.h"
  10. namespace base {
  11. class Timer;
  12. } // namespace base
  13. namespace style {
  14. struct FilledSlider;
  15. struct MediaSlider;
  16. } // namespace style
  17. namespace Ui {
  18. class ContinuousSlider : public RpWidget {
  19. public:
  20. ContinuousSlider(QWidget *parent);
  21. enum class Direction {
  22. Horizontal,
  23. Vertical,
  24. };
  25. void setDirection(Direction direction) {
  26. _direction = direction;
  27. update();
  28. }
  29. float64 value() const;
  30. void setValue(float64 value);
  31. void setValue(float64 value, float64 receivedTill);
  32. void setFadeOpacity(float64 opacity);
  33. void setDisabled(bool disabled);
  34. bool isDisabled() const {
  35. return _disabled;
  36. }
  37. void setAdjustCallback(Fn<float64(float64)> callback) {
  38. _adjustCallback = std::move(callback);
  39. }
  40. void setChangeProgressCallback(Fn<void(float64)> callback) {
  41. _changeProgressCallback = std::move(callback);
  42. }
  43. void setChangeFinishedCallback(Fn<void(float64)> callback) {
  44. _changeFinishedCallback = std::move(callback);
  45. }
  46. bool isChanging() const {
  47. return _mouseDown;
  48. }
  49. void setMoveByWheel(bool move);
  50. protected:
  51. void mouseMoveEvent(QMouseEvent *e) override;
  52. void mousePressEvent(QMouseEvent *e) override;
  53. void mouseReleaseEvent(QMouseEvent *e) override;
  54. void wheelEvent(QWheelEvent *e) override;
  55. void enterEventHook(QEnterEvent *e) override;
  56. void leaveEventHook(QEvent *e) override;
  57. float64 fadeOpacity() const {
  58. return _fadeOpacity;
  59. }
  60. float64 getCurrentValue() const {
  61. return _mouseDown ? _downValue : _value;
  62. }
  63. float64 getCurrentReceivedTill() const {
  64. return _receivedTill;
  65. }
  66. float64 getCurrentOverFactor() {
  67. return _disabled ? 0. : _overAnimation.value(_over ? 1. : 0.);
  68. }
  69. Direction getDirection() const {
  70. return _direction;
  71. }
  72. bool isHorizontal() const {
  73. return (_direction == Direction::Horizontal);
  74. }
  75. QRect getSeekRect() const;
  76. virtual QSize getSeekDecreaseSize() const = 0;
  77. private:
  78. virtual float64 getOverDuration() const = 0;
  79. bool moveByWheel() const {
  80. return _byWheelFinished != nullptr;
  81. }
  82. void setOver(bool over);
  83. float64 computeValue(const QPoint &pos) const;
  84. void updateDownValueFromPos(const QPoint &pos);
  85. Direction _direction = Direction::Horizontal;
  86. bool _disabled = false;
  87. std::unique_ptr<base::Timer> _byWheelFinished;
  88. Fn<float64(float64)> _adjustCallback;
  89. Fn<void(float64)> _changeProgressCallback;
  90. Fn<void(float64)> _changeFinishedCallback;
  91. bool _over = false;
  92. Ui::Animations::Simple _overAnimation;
  93. float64 _value = 0.;
  94. float64 _receivedTill = 0.;
  95. bool _mouseDown = false;
  96. float64 _downValue = 0.;
  97. float64 _fadeOpacity = 1.;
  98. };
  99. class FilledSlider : public ContinuousSlider {
  100. public:
  101. FilledSlider(QWidget *parent, const style::FilledSlider &st);
  102. protected:
  103. void paintEvent(QPaintEvent *e) override;
  104. private:
  105. QSize getSeekDecreaseSize() const override;
  106. float64 getOverDuration() const override;
  107. const style::FilledSlider &_st;
  108. };
  109. class MediaSlider : public ContinuousSlider {
  110. public:
  111. MediaSlider(QWidget *parent, const style::MediaSlider &st);
  112. void setAlwaysDisplayMarker(bool alwaysDisplayMarker) {
  113. _alwaysDisplayMarker = alwaysDisplayMarker;
  114. update();
  115. }
  116. void disablePaint(bool disabled);
  117. template <
  118. typename Value,
  119. typename Convert,
  120. typename Progress,
  121. typename = std::enable_if_t<
  122. rpl::details::is_callable_plain_v<Progress, Value>
  123. && std::is_same_v<Value, decltype(std::declval<Convert>()(1))>>>
  124. void setPseudoDiscrete(
  125. int valuesCount,
  126. Convert &&convert,
  127. Value current,
  128. Progress &&progress,
  129. int indexMin = 0) {
  130. Expects(valuesCount > 1);
  131. setAlwaysDisplayMarker(true);
  132. setDirection(Ui::ContinuousSlider::Direction::Horizontal);
  133. const auto sectionsCount = (valuesCount - 1);
  134. setValue(1.);
  135. for (auto index = index_type(); index != valuesCount; ++index) {
  136. if (current <= convert(index)) {
  137. setValue(index / float64(sectionsCount));
  138. break;
  139. }
  140. }
  141. setAdjustCallback([=](float64 value) {
  142. return std::max(
  143. base::SafeRound(value * sectionsCount),
  144. indexMin * 1.
  145. ) / sectionsCount;
  146. });
  147. setChangeProgressCallback([=](float64 value) {
  148. const auto index = std::max(
  149. int(base::SafeRound(value * sectionsCount)),
  150. indexMin);
  151. progress(convert(index));
  152. });
  153. }
  154. template <
  155. typename Value,
  156. typename Convert,
  157. typename Progress,
  158. typename Finished,
  159. typename = std::enable_if_t<
  160. rpl::details::is_callable_plain_v<Progress, Value>
  161. && rpl::details::is_callable_plain_v<Finished, Value>
  162. && std::is_same_v<Value, decltype(std::declval<Convert>()(1))>>>
  163. void setPseudoDiscrete(
  164. int valuesCount,
  165. Convert &&convert,
  166. Value current,
  167. Progress &&progress,
  168. Finished &&finished,
  169. int indexMin = 0) {
  170. setPseudoDiscrete(
  171. valuesCount,
  172. std::forward<Convert>(convert),
  173. current,
  174. std::forward<Progress>(progress),
  175. indexMin);
  176. setChangeFinishedCallback([=](float64 value) {
  177. const auto sectionsCount = (valuesCount - 1);
  178. const auto index = std::max(
  179. int(base::SafeRound(value * sectionsCount)),
  180. indexMin);
  181. finished(convert(index));
  182. });
  183. }
  184. void setActiveFgOverride(std::optional<QColor> color);
  185. void addDivider(float64 atValue, const QSize &size);
  186. protected:
  187. void paintEvent(QPaintEvent *e) override;
  188. private:
  189. struct Divider {
  190. const float64 atValue;
  191. const QSize size;
  192. };
  193. QSize getSeekDecreaseSize() const override;
  194. float64 getOverDuration() const override;
  195. const style::MediaSlider &_st;
  196. bool _alwaysDisplayMarker = false;
  197. bool _paintDisabled = false;
  198. std::vector<Divider> _dividers;
  199. std::optional<QColor> _activeFgOverride;
  200. };
  201. class MediaSliderWheelless : public MediaSlider {
  202. public:
  203. using Ui::MediaSlider::MediaSlider;
  204. protected:
  205. void wheelEvent(QWheelEvent *e) override {
  206. e->ignore();
  207. }
  208. };
  209. } // namespace Ui