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

398 lines
11 KiB
C++
Raw Normal View History

2016-11-22 09:48:13 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2016-11-22 09:48:13 +00:00
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2016-11-22 09:48:13 +00:00
*/
#include "ui/widgets/continuous_sliders.h"
2019-01-18 11:26:43 +00:00
#include "base/timer.h"
2019-09-17 16:13:12 +00:00
#include "base/platform/base_platform_info.h"
2019-01-18 11:26:43 +00:00
2016-11-22 09:48:13 +00:00
namespace Ui {
namespace {
constexpr auto kByWheelFinishedTimeout = 1000;
} // namespace
2018-06-15 18:56:17 +00:00
ContinuousSlider::ContinuousSlider(QWidget *parent) : RpWidget(parent) {
2016-11-22 09:48:13 +00:00
setCursor(style::cur_pointer);
}
void ContinuousSlider::setDisabled(bool disabled) {
if (_disabled != disabled) {
_disabled = disabled;
setCursor(_disabled ? style::cur_default : style::cur_pointer);
update();
}
}
void ContinuousSlider::setMoveByWheel(bool move) {
if (move != moveByWheel()) {
if (move) {
2019-01-18 11:26:43 +00:00
_byWheelFinished = std::make_unique<base::Timer>([=] {
2016-11-22 09:48:13 +00:00
if (_changeFinishedCallback) {
_changeFinishedCallback(getCurrentValue());
2016-11-22 09:48:13 +00:00
}
});
} else {
2019-01-18 11:26:43 +00:00
_byWheelFinished = nullptr;
2016-11-22 09:48:13 +00:00
}
}
}
QRect ContinuousSlider::getSeekRect() const {
const auto decrease = getSeekDecreaseSize();
return isHorizontal()
? QRect(decrease.width() / 2, 0, width() - decrease.width(), height())
: QRect(0, decrease.height() / 2, width(), height() - decrease.width());
}
float64 ContinuousSlider::value() const {
return getCurrentValue();
}
void ContinuousSlider::setValue(float64 value) {
2019-03-12 05:09:53 +00:00
setValue(value, -1);
}
void ContinuousSlider::setValue(float64 value, float64 receivedTill) {
if (_value != value || _receivedTill != receivedTill) {
_value = value;
_receivedTill = receivedTill;
update();
}
2016-11-22 09:48:13 +00:00
}
void ContinuousSlider::setFadeOpacity(float64 opacity) {
_fadeOpacity = opacity;
update();
}
void ContinuousSlider::mouseMoveEvent(QMouseEvent *e) {
if (_mouseDown) {
updateDownValueFromPos(e->pos());
}
}
float64 ContinuousSlider::computeValue(const QPoint &pos) const {
2018-06-15 18:56:17 +00:00
const auto seekRect = myrtlrect(getSeekRect());
const auto result = isHorizontal() ?
2016-11-22 09:48:13 +00:00
(pos.x() - seekRect.x()) / float64(seekRect.width()) :
(1. - (pos.y() - seekRect.y()) / float64(seekRect.height()));
const auto snapped = std::clamp(result, 0., 1.);
2018-06-15 18:56:17 +00:00
return _adjustCallback ? _adjustCallback(snapped) : snapped;
2016-11-22 09:48:13 +00:00
}
void ContinuousSlider::mousePressEvent(QMouseEvent *e) {
_mouseDown = true;
_downValue = computeValue(e->pos());
update();
if (_changeProgressCallback) {
_changeProgressCallback(_downValue);
}
}
void ContinuousSlider::mouseReleaseEvent(QMouseEvent *e) {
if (_mouseDown) {
_mouseDown = false;
if (_changeFinishedCallback) {
_changeFinishedCallback(_downValue);
}
_value = _downValue;
2016-11-22 09:48:13 +00:00
update();
}
}
void ContinuousSlider::wheelEvent(QWheelEvent *e) {
if (_mouseDown || !moveByWheel()) {
return;
}
constexpr auto step = static_cast<int>(QWheelEvent::DefaultDeltasPerStep);
constexpr auto coef = 1. / (step * 10.);
auto deltaX = e->angleDelta().x(), deltaY = e->angleDelta().y();
if (Platform::IsMac()) {
2016-11-22 09:48:13 +00:00
deltaY *= -1;
} else {
deltaX *= -1;
}
auto delta = (qAbs(deltaX) > qAbs(deltaY)) ? deltaX : deltaY;
auto finalValue = std::clamp(_value + delta * coef, 0., 1.);
setValue(finalValue);
2016-11-22 09:48:13 +00:00
if (_changeProgressCallback) {
_changeProgressCallback(finalValue);
}
2019-01-18 11:26:43 +00:00
_byWheelFinished->callOnce(kByWheelFinishedTimeout);
2016-11-22 09:48:13 +00:00
}
void ContinuousSlider::updateDownValueFromPos(const QPoint &pos) {
_downValue = computeValue(pos);
update();
if (_changeProgressCallback) {
_changeProgressCallback(_downValue);
}
}
void ContinuousSlider::enterEventHook(QEvent *e) {
2016-11-22 09:48:13 +00:00
setOver(true);
}
void ContinuousSlider::leaveEventHook(QEvent *e) {
2016-11-22 09:48:13 +00:00
setOver(false);
}
void ContinuousSlider::setOver(bool over) {
if (_over == over) return;
_over = over;
auto from = _over ? 0. : 1., to = _over ? 1. : 0.;
2019-04-01 14:53:18 +00:00
_overAnimation.start([=] { update(); }, from, to, getOverDuration());
2016-11-22 09:48:13 +00:00
}
FilledSlider::FilledSlider(QWidget *parent, const style::FilledSlider &st) : ContinuousSlider(parent)
, _st(st) {
}
QSize FilledSlider::getSeekDecreaseSize() const {
return QSize(0, 0);
2016-11-22 09:48:13 +00:00
}
float64 FilledSlider::getOverDuration() const {
return _st.duration;
}
void FilledSlider::paintEvent(QPaintEvent *e) {
Painter p(this);
PainterHighQualityEnabler hq(p);
2016-11-22 09:48:13 +00:00
p.setPen(Qt::NoPen);
const auto masterOpacity = fadeOpacity();
const auto disabled = isDisabled();
2019-03-07 11:35:28 +00:00
const auto over = getCurrentOverFactor();
const auto lineWidth = _st.lineWidth + ((_st.fullWidth - _st.lineWidth) * over);
const auto lineWidthRounded = qFloor(lineWidth);
const auto lineWidthPartial = lineWidth - lineWidthRounded;
const auto seekRect = getSeekRect();
const auto value = getCurrentValue();
const auto from = seekRect.x();
const auto mid = qRound(from + value * seekRect.width());
const auto end = from + seekRect.width();
2016-11-22 09:48:13 +00:00
if (mid > from) {
p.setOpacity(masterOpacity);
p.fillRect(from, height() - lineWidthRounded, (mid - from), lineWidthRounded, disabled ? _st.disabledFg : _st.activeFg);
if (lineWidthPartial > 0.01) {
p.setOpacity(masterOpacity * lineWidthPartial);
p.fillRect(from, height() - lineWidthRounded - 1, (mid - from), 1, disabled ? _st.disabledFg : _st.activeFg);
}
}
if (end > mid && over > 0) {
p.setOpacity(masterOpacity * over);
p.fillRect(mid, height() - lineWidthRounded, (end - mid), lineWidthRounded, _st.inactiveFg);
if (lineWidthPartial > 0.01) {
p.setOpacity(masterOpacity * over * lineWidthPartial);
p.fillRect(mid, height() - lineWidthRounded - 1, (end - mid), 1, _st.inactiveFg);
}
}
}
MediaSlider::MediaSlider(QWidget *parent, const style::MediaSlider &st) : ContinuousSlider(parent)
, _st(st) {
}
QSize MediaSlider::getSeekDecreaseSize() const {
return _alwaysDisplayMarker ? _st.seekSize : QSize();
2016-11-22 09:48:13 +00:00
}
float64 MediaSlider::getOverDuration() const {
return _st.duration;
}
void MediaSlider::disablePaint(bool disabled) {
_paintDisabled = disabled;
}
void MediaSlider::addDivider(float64 atValue, const QSize &size) {
_dividers.push_back(Divider{ atValue, size });
}
void MediaSlider::setActiveFgOverride(std::optional<QColor> color) {
_activeFgOverride = color;
update();
}
2016-11-22 09:48:13 +00:00
void MediaSlider::paintEvent(QPaintEvent *e) {
if (_paintDisabled) {
return;
}
2016-11-22 09:48:13 +00:00
Painter p(this);
PainterHighQualityEnabler hq(p);
2016-11-22 09:48:13 +00:00
p.setPen(Qt::NoPen);
p.setOpacity(fadeOpacity());
const auto horizontal = isHorizontal();
const auto radius = _st.width / 2;
const auto disabled = isDisabled();
2019-03-07 11:35:28 +00:00
const auto over = getCurrentOverFactor();
const auto seekRect = getSeekRect();
2016-11-22 09:48:13 +00:00
// invert colors and value for vertical
const auto value = horizontal
? getCurrentValue()
: (1. - getCurrentValue());
// receivedTill is not supported for vertical
const auto receivedTill = horizontal
? getCurrentReceivedTill()
: value;
const auto markerFrom = (horizontal ? seekRect.x() : seekRect.y());
const auto markerLength = horizontal
? seekRect.width()
: seekRect.height();
const auto from = 0;
const auto length = (horizontal ? width() : height());
2021-06-23 16:14:22 +00:00
const auto alwaysSeekSize = horizontal
? _st.seekSize.width()
: _st.seekSize.height();
const auto mid = _alwaysDisplayMarker
? qRound(from
+ (alwaysSeekSize / 2.)
+ value * (length - alwaysSeekSize))
: qRound(from + value * length);
const auto till = horizontal
2021-06-26 09:33:18 +00:00
? std::max(mid, qRound(from + receivedTill * length))
: mid;
const auto end = from + length;
const auto activeFg = disabled
? _st.activeFgDisabled
: _activeFgOverride
? QBrush(*_activeFgOverride)
: anim::brush(_st.activeFg, _st.activeFgOver, over);
const auto receivedTillFg = _st.receivedTillFg;
const auto inactiveFg = disabled
? _st.inactiveFgDisabled
: anim::brush(_st.inactiveFg, _st.inactiveFgOver, over);
2016-11-22 09:48:13 +00:00
if (mid > from) {
const auto fromClipRect = horizontal
? QRect(0, 0, mid, height())
: QRect(0, 0, width(), mid);
const auto till = std::min(mid + radius, end);
const auto fromRect = horizontal
? QRect(from, (height() - _st.width) / 2, till - from, _st.width)
: QRect((width() - _st.width) / 2, from, _st.width, till - from);
2016-11-22 09:48:13 +00:00
p.setClipRect(fromClipRect);
p.setBrush(horizontal ? activeFg : inactiveFg);
p.drawRoundedRect(fromRect, radius, radius);
}
if (till > mid) {
Assert(horizontal);
auto clipRect = QRect(mid, 0, till - mid, height());
const auto left = std::max(mid - radius, from);
const auto right = std::min(till + radius, end);
const auto rect = QRect(
left,
(height() - _st.width) / 2,
right - left,
_st.width);
p.setClipRect(clipRect);
p.setBrush(receivedTillFg);
p.drawRoundedRect(rect, radius, radius);
}
if (end > till) {
const auto endClipRect = horizontal
? QRect(till, 0, width() - till, height())
: QRect(0, till, width(), height() - till);
const auto begin = std::max(till - radius, from);
const auto endRect = horizontal
? QRect(
begin,
(height() - _st.width) / 2,
end - begin,
_st.width)
: QRect(
(width() - _st.width) / 2,
begin,
_st.width,
end - begin);
2016-11-22 09:48:13 +00:00
p.setClipRect(endClipRect);
p.setBrush(horizontal ? inactiveFg : activeFg);
p.drawRoundedRect(endRect, radius, radius);
}
if (!_dividers.empty()) {
p.setClipRect(rect());
for (const auto &divider : _dividers) {
const auto dividerValue = horizontal
? divider.atValue
: (1. - divider.atValue);
const auto dividerMid = std::round(from
+ dividerValue * length);
const auto &size = divider.size;
const auto rect = horizontal
? QRect(
dividerMid - size.width() / 2,
(height() - size.height()) / 2,
size.width(),
size.height())
: QRect(
(width() - size.height()) / 2,
dividerMid - size.width() / 2,
size.height(),
size.width());
p.setBrush(((value < dividerValue) == horizontal)
? inactiveFg
: activeFg);
const auto dividerRadius = size.width() / 2.;
p.drawRoundedRect(rect, dividerRadius, dividerRadius);
}
}
const auto markerSizeRatio = disabled
? 0.
: (_alwaysDisplayMarker ? 1. : over);
2016-11-22 09:48:13 +00:00
if (markerSizeRatio > 0) {
const auto position = qRound(markerFrom + value * markerLength)
- (horizontal
? (_st.seekSize.width() / 2)
: (_st.seekSize.height() / 2));
const auto seekButton = horizontal
? QRect(
position,
(height() - _st.seekSize.height()) / 2,
_st.seekSize.width(),
_st.seekSize.height())
: QRect(
(width() - _st.seekSize.width()) / 2,
position,
_st.seekSize.width(),
_st.seekSize.height());
const auto size = horizontal
? _st.seekSize.width()
: _st.seekSize.height();
const auto remove = static_cast<int>(
((1. - markerSizeRatio) * size) / 2.);
2016-11-22 09:48:13 +00:00
if (remove * 2 < size) {
p.setClipRect(rect());
p.setBrush(activeFg);
const auto xshift = horizontal
? std::max(
seekButton.x() + seekButton.width() - remove - width(),
0) + std::min(seekButton.x() + remove, 0)
: 0;
const auto yshift = horizontal
? 0
: std::max(
seekButton.y() + seekButton.height() - remove - height(),
0) + std::min(seekButton.y() + remove, 0);
p.drawEllipse(seekButton.marginsRemoved(
QMargins(remove, remove, remove, remove)
).translated(-xshift, -yshift));
2016-11-22 09:48:13 +00:00
}
}
}
} // namespace Ui