tdesktop/Telegram/SourceFiles/ui/widgets/continuous_sliders.h

234 lines
5.5 KiB
C
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
2019-03-07 11:35:28 +00:00
#include "ui/effects/animations.h"
2018-06-15 18:56:17 +00:00
#include "ui/rp_widget.h"
2016-11-22 09:48:13 +00:00
2019-01-18 11:26:43 +00:00
namespace base {
class Timer;
} // namespace base
namespace style {
struct FilledSlider;
struct MediaSlider;
} // namespace style
namespace Ui {
2018-06-15 18:56:17 +00:00
class ContinuousSlider : public RpWidget {
public:
ContinuousSlider(QWidget *parent);
enum class Direction {
Horizontal,
Vertical,
};
void setDirection(Direction direction) {
_direction = direction;
update();
}
float64 value() const;
void setValue(float64 value);
void setValue(float64 value, float64 receivedTill);
void setFadeOpacity(float64 opacity);
void setDisabled(bool disabled);
bool isDisabled() const {
return _disabled;
}
2018-06-15 18:56:17 +00:00
void setAdjustCallback(Fn<float64(float64)> callback) {
_adjustCallback = std::move(callback);
}
void setChangeProgressCallback(Fn<void(float64)> callback) {
_changeProgressCallback = std::move(callback);
}
2018-06-15 18:56:17 +00:00
void setChangeFinishedCallback(Fn<void(float64)> callback) {
_changeFinishedCallback = std::move(callback);
}
bool isChanging() const {
return _mouseDown;
}
void setMoveByWheel(bool move);
protected:
void mouseMoveEvent(QMouseEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
void wheelEvent(QWheelEvent *e) override;
void enterEventHook(QEnterEvent *e) override;
void leaveEventHook(QEvent *e) override;
float64 fadeOpacity() const {
return _fadeOpacity;
}
float64 getCurrentValue() const {
return _mouseDown ? _downValue : _value;
}
float64 getCurrentReceivedTill() const {
return _receivedTill;
}
2019-03-07 11:35:28 +00:00
float64 getCurrentOverFactor() {
2019-04-01 14:53:18 +00:00
return _disabled ? 0. : _overAnimation.value(_over ? 1. : 0.);
}
Direction getDirection() const {
return _direction;
}
bool isHorizontal() const {
return (_direction == Direction::Horizontal);
}
QRect getSeekRect() const;
virtual QSize getSeekDecreaseSize() const = 0;
private:
virtual float64 getOverDuration() const = 0;
bool moveByWheel() const {
return _byWheelFinished != nullptr;
}
void setOver(bool over);
float64 computeValue(const QPoint &pos) const;
void updateDownValueFromPos(const QPoint &pos);
Direction _direction = Direction::Horizontal;
bool _disabled = false;
2019-01-18 11:26:43 +00:00
std::unique_ptr<base::Timer> _byWheelFinished;
2018-06-15 18:56:17 +00:00
Fn<float64(float64)> _adjustCallback;
Fn<void(float64)> _changeProgressCallback;
Fn<void(float64)> _changeFinishedCallback;
bool _over = false;
2019-04-01 14:53:18 +00:00
Ui::Animations::Simple _overAnimation;
float64 _value = 0.;
float64 _receivedTill = 0.;
bool _mouseDown = false;
float64 _downValue = 0.;
float64 _fadeOpacity = 1.;
};
2016-11-22 09:48:13 +00:00
class FilledSlider : public ContinuousSlider {
public:
FilledSlider(QWidget *parent, const style::FilledSlider &st);
protected:
void paintEvent(QPaintEvent *e) override;
private:
QSize getSeekDecreaseSize() const override;
2016-11-22 09:48:13 +00:00
float64 getOverDuration() const override;
const style::FilledSlider &_st;
};
class MediaSlider : public ContinuousSlider {
public:
MediaSlider(QWidget *parent, const style::MediaSlider &st);
void setAlwaysDisplayMarker(bool alwaysDisplayMarker) {
_alwaysDisplayMarker = alwaysDisplayMarker;
update();
}
void disablePaint(bool disabled);
2016-11-22 09:48:13 +00:00
template <
typename Value,
typename Convert,
2023-01-05 10:48:46 +00:00
typename Progress,
typename = std::enable_if_t<
2023-01-05 10:48:46 +00:00
rpl::details::is_callable_plain_v<Progress, Value>
&& std::is_same_v<Value, decltype(std::declval<Convert>()(1))>>>
void setPseudoDiscrete(
int valuesCount,
Convert &&convert,
Value current,
2023-01-05 10:48:46 +00:00
Progress &&progress) {
Expects(valuesCount > 1);
setAlwaysDisplayMarker(true);
setDirection(Ui::ContinuousSlider::Direction::Horizontal);
const auto sectionsCount = (valuesCount - 1);
2019-03-01 14:20:04 +00:00
setValue(1.);
for (auto index = index_type(); index != valuesCount; ++index) {
if (current <= convert(index)) {
setValue(index / float64(sectionsCount));
break;
}
}
setAdjustCallback([=](float64 value) {
return base::SafeRound(value * sectionsCount) / sectionsCount;
});
2023-01-05 10:48:46 +00:00
setChangeProgressCallback([=](float64 value) {
const auto index = int(base::SafeRound(value * sectionsCount));
2023-01-05 10:48:46 +00:00
progress(convert(index));
});
}
2023-01-05 10:48:46 +00:00
template <
typename Value,
typename Convert,
typename Progress,
typename Finished,
typename = std::enable_if_t<
rpl::details::is_callable_plain_v<Progress, Value>
&& rpl::details::is_callable_plain_v<Finished, Value>
&& std::is_same_v<Value, decltype(std::declval<Convert>()(1))>>>
void setPseudoDiscrete(
int valuesCount,
Convert &&convert,
Value current,
Progress &&progress,
Finished &&finished) {
setPseudoDiscrete(
valuesCount,
std::forward<Convert>(convert),
current,
std::forward<Progress>(progress));
setChangeFinishedCallback([=](float64 value) {
const auto sectionsCount = (valuesCount - 1);
const auto index = int(base::SafeRound(value * sectionsCount));
finished(convert(index));
});
}
void setActiveFgOverride(std::optional<QColor> color);
void addDivider(float64 atValue, const QSize &size);
2016-11-22 09:48:13 +00:00
protected:
void paintEvent(QPaintEvent *e) override;
private:
struct Divider {
const float64 atValue;
const QSize size;
};
QSize getSeekDecreaseSize() const override;
2016-11-22 09:48:13 +00:00
float64 getOverDuration() const override;
const style::MediaSlider &_st;
bool _alwaysDisplayMarker = false;
bool _paintDisabled = false;
2016-11-22 09:48:13 +00:00
std::vector<Divider> _dividers;
std::optional<QColor> _activeFgOverride;
2016-11-22 09:48:13 +00:00
};
} // namespace Ui