2016-11-22 09:48:13 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-11-22 09:48:13 +00:00
|
|
|
|
2018-01-03 10:23:14 +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) {
|
2017-05-18 16:10:39 +00:00
|
|
|
_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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 11:06:54 +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());
|
|
|
|
}
|
|
|
|
|
2017-05-18 16:10:39 +00:00
|
|
|
void ContinuousSlider::setValue(float64 value) {
|
2019-03-12 05:09:53 +00:00
|
|
|
setValue(value, -1);
|
2019-03-05 11:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
2021-01-23 03:29:50 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-05-18 16:10:39 +00:00
|
|
|
_value = _downValue;
|
2016-11-22 09:48:13 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContinuousSlider::wheelEvent(QWheelEvent *e) {
|
|
|
|
if (_mouseDown || !moveByWheel()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef OS_MAC_OLD
|
|
|
|
constexpr auto step = 120;
|
|
|
|
#else // OS_MAC_OLD
|
|
|
|
constexpr auto step = static_cast<int>(QWheelEvent::DefaultDeltasPerStep);
|
|
|
|
#endif // OS_MAC_OLD
|
|
|
|
constexpr auto coef = 1. / (step * 10.);
|
|
|
|
|
|
|
|
auto deltaX = e->angleDelta().x(), deltaY = e->angleDelta().y();
|
2019-06-03 14:41:23 +00:00
|
|
|
if (Platform::IsMac()) {
|
2016-11-22 09:48:13 +00:00
|
|
|
deltaY *= -1;
|
|
|
|
} else {
|
|
|
|
deltaX *= -1;
|
|
|
|
}
|
|
|
|
auto delta = (qAbs(deltaX) > qAbs(deltaY)) ? deltaX : deltaY;
|
2021-01-23 03:29:50 +00:00
|
|
|
auto finalValue = std::clamp(_value + delta * coef, 0., 1.);
|
2017-05-18 16:10:39 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void ContinuousSlider::enterEventHook(QEvent *e) {
|
2016-11-22 09:48:13 +00:00
|
|
|
setOver(true);
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
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) {
|
|
|
|
}
|
|
|
|
|
2019-03-05 11:06:54 +00:00
|
|
|
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);
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
2016-11-22 09:48:13 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
|
2019-03-05 11:06:54 +00:00
|
|
|
const auto masterOpacity = fadeOpacity();
|
|
|
|
const auto disabled = isDisabled();
|
2019-03-07 11:35:28 +00:00
|
|
|
const auto over = getCurrentOverFactor();
|
2019-03-05 11:06:54 +00:00
|
|
|
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) {
|
|
|
|
}
|
|
|
|
|
2019-03-05 11:06:54 +00:00
|
|
|
QSize MediaSlider::getSeekDecreaseSize() const {
|
|
|
|
return _alwaysDisplayMarker ? _st.seekSize : QSize();
|
2016-11-22 09:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float64 MediaSlider::getOverDuration() const {
|
|
|
|
return _st.duration;
|
|
|
|
}
|
|
|
|
|
2018-06-06 08:56:39 +00:00
|
|
|
void MediaSlider::disablePaint(bool disabled) {
|
|
|
|
_paintDisabled = disabled;
|
|
|
|
}
|
|
|
|
|
2016-11-22 09:48:13 +00:00
|
|
|
void MediaSlider::paintEvent(QPaintEvent *e) {
|
2018-06-06 08:56:39 +00:00
|
|
|
if (_paintDisabled) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-22 09:48:13 +00:00
|
|
|
Painter p(this);
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
2016-11-22 09:48:13 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setOpacity(fadeOpacity());
|
|
|
|
|
2019-03-05 11:06:54 +00:00
|
|
|
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();
|
2019-03-05 11:06:54 +00:00
|
|
|
const auto seekRect = getSeekRect();
|
2016-11-22 09:48:13 +00:00
|
|
|
|
|
|
|
// invert colors and value for vertical
|
2019-03-05 11:06:54 +00:00
|
|
|
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());
|
|
|
|
const auto mid = qRound(from + value * length);
|
|
|
|
const auto till = std::max(mid, qRound(from + receivedTill * length));
|
|
|
|
const auto end = from + length;
|
|
|
|
const auto activeFg = disabled ? _st.activeFgDisabled : 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) {
|
2019-03-05 11:06:54 +00:00
|
|
|
const auto fromClipRect = horizontal ? QRect(0, 0, mid, height()) : QRect(0, 0, width(), mid);
|
2018-10-15 08:06:27 +00:00
|
|
|
const auto till = std::min(mid + radius, end);
|
2019-03-05 11:06:54 +00:00
|
|
|
const auto fromRect = horizontal
|
2018-10-15 08:06:27 +00:00
|
|
|
? 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);
|
|
|
|
}
|
2019-03-05 11:06:54 +00:00
|
|
|
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
|
2018-10-15 08:06:27 +00:00
|
|
|
? 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);
|
|
|
|
}
|
2019-03-05 11:06:54 +00:00
|
|
|
const auto markerSizeRatio = disabled ? 0. : (_alwaysDisplayMarker ? 1. : over);
|
2016-11-22 09:48:13 +00:00
|
|
|
if (markerSizeRatio > 0) {
|
2019-03-05 11:06:54 +00:00
|
|
|
const auto position = qRound(markerFrom + value * markerLength) - (horizontal ? (_st.seekSize.width() / 2) : (_st.seekSize.height() / 2));
|
|
|
|
const auto seekButton = horizontal
|
2016-11-22 09:48:13 +00:00
|
|
|
? 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());
|
2019-03-05 11:06:54 +00:00
|
|
|
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);
|
2019-03-05 11:06:54 +00:00
|
|
|
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
|