2014-05-30 08:53:19 +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.
|
2014-05-30 08:53:19 +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
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
// flick scroll taken from http://qt-project.org/doc/qt-4.8/demos-embedded-anomaly-src-flickcharm-cpp.html
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
ScrollShadow::ScrollShadow(ScrollArea *parent, const style::ScrollArea *st) : QWidget(parent), _st(st) {
|
2014-05-30 08:53:19 +00:00
|
|
|
setVisible(false);
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_st != nullptr);
|
|
|
|
Assert(_st->shColor.v() != nullptr);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollShadow::paintEvent(QPaintEvent *e) {
|
2016-12-31 13:34:41 +00:00
|
|
|
Painter p(this);
|
|
|
|
p.fillRect(rect(), _st->shColor);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollShadow::changeVisibility(bool shown) {
|
|
|
|
setVisible(shown);
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
ScrollBar::ScrollBar(ScrollArea *parent, bool vert, const style::ScrollArea *st) : TWidget(parent)
|
2015-12-08 12:33:37 +00:00
|
|
|
, _st(st)
|
|
|
|
, _vertical(vert)
|
2016-12-07 13:32:25 +00:00
|
|
|
, _hiding(_st->hiding != 0)
|
2015-12-08 12:33:37 +00:00
|
|
|
, _connected(vert ? parent->verticalScrollBar() : parent->horizontalScrollBar())
|
2016-12-07 13:32:25 +00:00
|
|
|
, _scrollMax(_connected->maximum()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
recountSize();
|
|
|
|
|
|
|
|
_hideTimer.setSingleShot(true);
|
|
|
|
connect(&_hideTimer, SIGNAL(timeout()), this, SLOT(onHideTimer()));
|
|
|
|
|
2015-07-17 19:17:37 +00:00
|
|
|
connect(_connected, SIGNAL(valueChanged(int)), this, SLOT(onValueChanged()));
|
2016-12-21 15:05:58 +00:00
|
|
|
connect(_connected, SIGNAL(rangeChanged(int, int)), this, SLOT(onRangeChanged()));
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
updateBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::recountSize() {
|
2015-09-29 13:24:39 +00:00
|
|
|
setGeometry(_vertical ? QRect(rtl() ? 0 : (area()->width() - _st->width), _st->deltat, _st->width, area()->height() - _st->deltat - _st->deltab) : QRect(_st->deltat, area()->height() - _st->width, area()->width() - _st->deltat - _st->deltab, _st->width));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 19:17:37 +00:00
|
|
|
void ScrollBar::onValueChanged() {
|
2015-09-29 13:24:39 +00:00
|
|
|
area()->onScrolled();
|
2015-07-17 19:17:37 +00:00
|
|
|
updateBar();
|
|
|
|
}
|
|
|
|
|
2016-12-21 15:05:58 +00:00
|
|
|
void ScrollBar::onRangeChanged() {
|
|
|
|
area()->onInnerResized();
|
|
|
|
updateBar();
|
|
|
|
}
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
void ScrollBar::updateBar(bool force) {
|
2014-05-30 08:53:19 +00:00
|
|
|
QRect newBar;
|
|
|
|
if (_connected->maximum() != _scrollMax) {
|
|
|
|
int32 oldMax = _scrollMax, newMax = _connected->maximum();
|
|
|
|
_scrollMax = newMax;
|
2015-09-29 13:24:39 +00:00
|
|
|
area()->rangeChanged(oldMax, newMax, _vertical);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
if (_vertical) {
|
2015-09-29 13:24:39 +00:00
|
|
|
int sh = area()->scrollHeight(), rh = height(), h = sh ? int32((rh * int64(area()->height())) / sh) : 0;
|
|
|
|
if (h >= rh || !area()->scrollTopMax() || rh < _st->minHeight) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!isHidden()) hide();
|
2015-04-02 10:33:19 +00:00
|
|
|
bool newTopSh = (_st->topsh < 0), newBottomSh = (_st->bottomsh < 0);
|
|
|
|
if (newTopSh != _topSh || force) emit topShadowVisibility(_topSh = newTopSh);
|
|
|
|
if (newBottomSh != _bottomSh || force) emit bottomShadowVisibility(_bottomSh = newBottomSh);
|
2014-05-30 08:53:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (h <= _st->minHeight) h = _st->minHeight;
|
2015-09-29 13:24:39 +00:00
|
|
|
int stm = area()->scrollTopMax(), y = stm ? int32(((rh - h) * int64(area()->scrollTop())) / stm) : 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
if (y > rh - h) y = rh - h;
|
|
|
|
|
2015-05-19 15:46:45 +00:00
|
|
|
newBar = QRect(_st->deltax, y, width() - 2 * _st->deltax, h);
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2015-09-29 13:24:39 +00:00
|
|
|
int sw = area()->scrollWidth(), rw = width(), w = sw ? int32((rw * int64(area()->width())) / sw) : 0;
|
|
|
|
if (w >= rw || !area()->scrollLeftMax() || rw < _st->minHeight) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!isHidden()) hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w <= _st->minHeight) w = _st->minHeight;
|
2015-09-29 13:24:39 +00:00
|
|
|
int slm = area()->scrollLeftMax(), x = slm ? int32(((rw - w) * int64(area()->scrollLeft())) / slm) : 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
if (x > rw - w) x = rw - w;
|
|
|
|
|
2015-05-19 15:46:45 +00:00
|
|
|
newBar = QRect(x, _st->deltax, w, height() - 2 * _st->deltax);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
if (newBar != _bar) {
|
|
|
|
_bar = newBar;
|
2015-09-29 13:24:39 +00:00
|
|
|
update();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
if (_vertical) {
|
2015-09-29 13:24:39 +00:00
|
|
|
bool newTopSh = (_st->topsh < 0) || (area()->scrollTop() > _st->topsh), newBottomSh = (_st->bottomsh < 0) || (area()->scrollTop() < area()->scrollTopMax() - _st->bottomsh);
|
2015-04-02 10:33:19 +00:00
|
|
|
if (newTopSh != _topSh || force) emit topShadowVisibility(_topSh = newTopSh);
|
|
|
|
if (newBottomSh != _bottomSh || force) emit bottomShadowVisibility(_bottomSh = newBottomSh);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
if (isHidden()) show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::onHideTimer() {
|
2016-12-07 13:32:25 +00:00
|
|
|
if (!_hiding) {
|
|
|
|
_hiding = true;
|
|
|
|
_a_opacity.start([this] { update(); }, 1., 0., _st->duration);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 13:24:39 +00:00
|
|
|
ScrollArea *ScrollBar::area() {
|
|
|
|
return static_cast<ScrollArea*>(parentWidget());
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:32:25 +00:00
|
|
|
void ScrollBar::setOver(bool over) {
|
|
|
|
if (_over != over) {
|
|
|
|
auto wasOver = (_over || _moving);
|
|
|
|
_over = over;
|
|
|
|
auto nowOver = (_over || _moving);
|
|
|
|
if (wasOver != nowOver) {
|
|
|
|
_a_over.start([this] { update(); }, nowOver ? 0. : 1., nowOver ? 1. : 0., _st->duration);
|
|
|
|
}
|
|
|
|
if (nowOver && _hiding) {
|
|
|
|
_hiding = false;
|
|
|
|
_a_opacity.start([this] { update(); }, 0., 1., _st->duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::setOverBar(bool overbar) {
|
|
|
|
if (_overbar != overbar) {
|
|
|
|
auto wasBarOver = (_overbar || _moving);
|
|
|
|
_overbar = overbar;
|
|
|
|
auto nowBarOver = (_overbar || _moving);
|
|
|
|
if (wasBarOver != nowBarOver) {
|
|
|
|
_a_barOver.start([this] { update(); }, nowBarOver ? 0. : 1., nowBarOver ? 1. : 0., _st->duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::setMoving(bool moving) {
|
|
|
|
if (_moving != moving) {
|
|
|
|
auto wasOver = (_over || _moving);
|
|
|
|
auto wasBarOver = (_overbar || _moving);
|
|
|
|
_moving = moving;
|
|
|
|
auto nowBarOver = (_overbar || _moving);
|
|
|
|
if (wasBarOver != nowBarOver) {
|
|
|
|
_a_barOver.start([this] { update(); }, nowBarOver ? 0. : 1., nowBarOver ? 1. : 0., _st->duration);
|
|
|
|
}
|
|
|
|
auto nowOver = (_over || _moving);
|
|
|
|
if (wasOver != nowOver) {
|
|
|
|
_a_over.start([this] { update(); }, nowOver ? 0. : 1., nowOver ? 1. : 0., _st->duration);
|
|
|
|
}
|
|
|
|
if (!nowOver && _st->hiding && !_hiding) {
|
|
|
|
_hideTimer.start(_hideIn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void ScrollBar::paintEvent(QPaintEvent *e) {
|
|
|
|
if (!_bar.width() && !_bar.height()) {
|
|
|
|
hide();
|
|
|
|
return;
|
|
|
|
}
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2016-12-07 13:32:25 +00:00
|
|
|
auto opacity = _a_opacity.current(ms, _hiding ? 0. : 1.);
|
|
|
|
if (opacity == 0.) return;
|
2016-11-07 11:24:19 +00:00
|
|
|
|
2016-12-03 12:10:35 +00:00
|
|
|
Painter p(this);
|
2016-11-07 11:24:19 +00:00
|
|
|
auto deltal = _vertical ? _st->deltax : 0, deltar = _vertical ? _st->deltax : 0;
|
|
|
|
auto deltat = _vertical ? 0 : _st->deltax, deltab = _vertical ? 0 : _st->deltax;
|
2014-05-30 08:53:19 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
2016-12-21 15:05:58 +00:00
|
|
|
auto bg = anim::color(_st->bg, _st->bgOver, _a_over.current(ms, (_over || _moving) ? 1. : 0.));
|
2016-12-07 13:32:25 +00:00
|
|
|
bg.setAlpha(anim::interpolate(0, bg.alpha(), opacity));
|
2016-12-21 15:05:58 +00:00
|
|
|
auto bar = anim::color(_st->barBg, _st->barBgOver, _a_barOver.current(ms, (_overbar || _moving) ? 1. : 0.));
|
2016-12-07 13:32:25 +00:00
|
|
|
bar.setAlpha(anim::interpolate(0, bar.alpha(), opacity));
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_st->round) {
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
2016-11-07 11:24:19 +00:00
|
|
|
p.setBrush(bg);
|
2015-05-19 15:46:45 +00:00
|
|
|
p.drawRoundedRect(QRect(deltal, deltat, width() - deltal - deltar, height() - deltat - deltab), _st->round, _st->round);
|
2016-11-07 11:24:19 +00:00
|
|
|
p.setBrush(bar);
|
2014-05-30 08:53:19 +00:00
|
|
|
p.drawRoundedRect(_bar, _st->round, _st->round);
|
|
|
|
} else {
|
2016-11-07 11:24:19 +00:00
|
|
|
p.fillRect(QRect(deltal, deltat, width() - deltal - deltar, height() - deltat - deltab), bg);
|
|
|
|
p.fillRect(_bar, bar);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void ScrollBar::hideTimeout(crl::time dt) {
|
2016-12-07 13:32:25 +00:00
|
|
|
if (_hiding && dt > 0) {
|
|
|
|
_hiding = false;
|
|
|
|
_a_opacity.start([this] { update(); }, 0., 1., _st->duration);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
_hideIn = dt;
|
2016-12-07 13:32:25 +00:00
|
|
|
if (!_moving) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_hideTimer.start(_hideIn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void ScrollBar::enterEventHook(QEvent *e) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_hideTimer.stop();
|
|
|
|
setMouseTracking(true);
|
2016-12-07 13:32:25 +00:00
|
|
|
setOver(true);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void ScrollBar::leaveEventHook(QEvent *e) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!_moving) {
|
|
|
|
setMouseTracking(false);
|
|
|
|
}
|
2016-12-07 13:32:25 +00:00
|
|
|
setOver(false);
|
|
|
|
setOverBar(false);
|
|
|
|
if (_st->hiding && !_hiding) {
|
|
|
|
_hideTimer.start(_hideIn);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::mouseMoveEvent(QMouseEvent *e) {
|
2016-12-07 13:32:25 +00:00
|
|
|
setOverBar(_bar.contains(e->pos()));
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_moving) {
|
2015-09-29 13:24:39 +00:00
|
|
|
int delta = 0, barDelta = _vertical ? (area()->height() - _bar.height()) : (area()->width() - _bar.width());
|
2015-05-19 15:46:45 +00:00
|
|
|
if (barDelta > 0) {
|
2014-05-30 08:53:19 +00:00
|
|
|
QPoint d = (e->globalPos() - _dragStart);
|
2015-09-29 13:24:39 +00:00
|
|
|
delta = int32((_vertical ? (d.y() * int64(area()->scrollTopMax())) : (d.x() * int64(area()->scrollLeftMax()))) / barDelta);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
_connected->setValue(_startFrom + delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::mousePressEvent(QMouseEvent *e) {
|
|
|
|
if (!width() || !height()) return;
|
|
|
|
|
|
|
|
_dragStart = e->globalPos();
|
2016-12-07 13:32:25 +00:00
|
|
|
setMoving(true);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_overbar) {
|
|
|
|
_startFrom = _connected->value();
|
|
|
|
} else {
|
2015-05-19 15:46:45 +00:00
|
|
|
int32 val = _vertical ? e->pos().y() : e->pos().x(), div = _vertical ? height() : width();
|
|
|
|
val = (val <= _st->deltat) ? 0 : (val - _st->deltat);
|
|
|
|
div = (div <= _st->deltat + _st->deltab) ? 1 : (div - _st->deltat - _st->deltab);
|
2015-09-29 13:24:39 +00:00
|
|
|
_startFrom = _vertical ? int32((val * int64(area()->scrollTopMax())) / div) : ((val * int64(area()->scrollLeftMax())) / div);
|
2014-05-30 08:53:19 +00:00
|
|
|
_connected->setValue(_startFrom);
|
2016-12-07 13:32:25 +00:00
|
|
|
setOverBar(true);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-06-15 17:13:46 +00:00
|
|
|
|
|
|
|
area()->setMovingByScrollBar(true);
|
2015-09-29 13:24:39 +00:00
|
|
|
emit area()->scrollStarted();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::mouseReleaseEvent(QMouseEvent *e) {
|
|
|
|
if (_moving) {
|
2016-12-07 13:32:25 +00:00
|
|
|
setMoving(false);
|
2016-06-15 17:13:46 +00:00
|
|
|
|
|
|
|
area()->setMovingByScrollBar(false);
|
2015-09-29 13:24:39 +00:00
|
|
|
emit area()->scrollFinished();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
if (!_over) {
|
|
|
|
setMouseTracking(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::resizeEvent(QResizeEvent *e) {
|
|
|
|
updateBar();
|
|
|
|
}
|
|
|
|
|
2015-09-29 13:24:39 +00:00
|
|
|
void SplittedWidget::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
if (rtl()) {
|
|
|
|
p.translate(-otherWidth(), 0);
|
|
|
|
paintRegion(p, e->region().translated(otherWidth(), 0), false);
|
|
|
|
} else {
|
|
|
|
paintRegion(p, e->region(), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplittedWidget::update(const QRect &r) {
|
|
|
|
if (rtl()) {
|
|
|
|
TWidget::update(r.translated(-otherWidth(), 0).intersected(rect()));
|
|
|
|
emit updateOther(r);
|
|
|
|
} else {
|
|
|
|
TWidget::update(r.intersected(rect()));
|
|
|
|
emit updateOther(r.translated(-width(), 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplittedWidget::update(const QRegion &r) {
|
|
|
|
if (rtl()) {
|
|
|
|
TWidget::update(r.translated(-otherWidth(), 0).intersected(rect()));
|
|
|
|
emit updateOther(r);
|
|
|
|
} else {
|
|
|
|
TWidget::update(r.intersected(rect()));
|
|
|
|
emit updateOther(r.translated(-width(), 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplittedWidgetOther::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
2016-12-01 19:20:33 +00:00
|
|
|
auto s = static_cast<SplittedWidget*>(static_cast<ScrollArea*>(parentWidget())->widget());
|
2015-09-29 13:24:39 +00:00
|
|
|
if (rtl()) {
|
|
|
|
s->paintRegion(p, e->region(), true);
|
|
|
|
} else {
|
|
|
|
p.translate(-s->width(), 0);
|
|
|
|
s->paintRegion(p, e->region().translated(s->width(), 0), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 16:06:53 +00:00
|
|
|
ScrollArea::ScrollArea(QWidget *parent, const style::ScrollArea &st, bool handleTouch)
|
|
|
|
: RpWidgetWrap<QScrollArea>(parent)
|
2016-09-03 16:35:12 +00:00
|
|
|
, _st(st)
|
|
|
|
, _horizontalBar(this, false, &_st)
|
|
|
|
, _verticalBar(this, true, &_st)
|
|
|
|
, _topShadow(this, &_st)
|
|
|
|
, _bottomShadow(this, &_st)
|
|
|
|
, _touchEnabled(handleTouch) {
|
2015-05-08 14:44:07 +00:00
|
|
|
setLayoutDirection(cLangDir());
|
2016-11-05 08:36:24 +00:00
|
|
|
setFocusPolicy(Qt::NoFocus);
|
2015-05-08 14:44:07 +00:00
|
|
|
|
2016-09-03 16:35:12 +00:00
|
|
|
connect(_verticalBar, SIGNAL(topShadowVisibility(bool)), _topShadow, SLOT(changeVisibility(bool)));
|
|
|
|
connect(_verticalBar, SIGNAL(bottomShadowVisibility(bool)), _bottomShadow, SLOT(changeVisibility(bool)));
|
|
|
|
_verticalBar->updateBar(true);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
|
|
|
setFrameStyle(QFrame::NoFrame | QFrame::Plain);
|
|
|
|
viewport()->setAutoFillBackground(false);
|
|
|
|
|
2016-09-03 16:35:12 +00:00
|
|
|
_horizontalValue = horizontalScrollBar()->value();
|
|
|
|
_verticalValue = verticalScrollBar()->value();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
if (_touchEnabled) {
|
|
|
|
viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
|
|
|
|
_touchTimer.setSingleShot(true);
|
|
|
|
connect(&_touchTimer, SIGNAL(timeout()), this, SLOT(onTouchTimer()));
|
|
|
|
connect(&_touchScrollTimer, SIGNAL(timeout()), this, SLOT(onTouchScrollTimer()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::touchDeaccelerate(int32 elapsed) {
|
|
|
|
int32 x = _touchSpeed.x();
|
|
|
|
int32 y = _touchSpeed.y();
|
|
|
|
_touchSpeed.setX((x == 0) ? x : (x > 0) ? qMax(0, x - elapsed) : qMin(0, x + elapsed));
|
|
|
|
_touchSpeed.setY((y == 0) ? y : (y > 0) ? qMax(0, y - elapsed) : qMin(0, y + elapsed));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::onScrolled() {
|
2017-12-26 12:41:48 +00:00
|
|
|
if (const auto inner = widget()) {
|
|
|
|
SendPendingMoveResizeEvents(inner);
|
|
|
|
}
|
2015-07-17 19:17:37 +00:00
|
|
|
|
2015-05-08 12:45:14 +00:00
|
|
|
bool em = false;
|
2016-09-03 16:35:12 +00:00
|
|
|
int horizontalValue = horizontalScrollBar()->value();
|
|
|
|
int verticalValue = verticalScrollBar()->value();
|
|
|
|
if (_horizontalValue != horizontalValue) {
|
2015-05-08 12:45:14 +00:00
|
|
|
if (_disabled) {
|
2016-09-03 16:35:12 +00:00
|
|
|
horizontalScrollBar()->setValue(_horizontalValue);
|
2015-05-08 12:45:14 +00:00
|
|
|
} else {
|
2016-09-03 16:35:12 +00:00
|
|
|
_horizontalValue = horizontalValue;
|
2015-05-08 12:45:14 +00:00
|
|
|
if (_st.hiding) {
|
2016-09-03 16:35:12 +00:00
|
|
|
_horizontalBar->hideTimeout(_st.hiding);
|
2015-05-08 12:45:14 +00:00
|
|
|
}
|
|
|
|
em = true;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-03 16:35:12 +00:00
|
|
|
if (_verticalValue != verticalValue) {
|
2015-05-08 12:45:14 +00:00
|
|
|
if (_disabled) {
|
2016-09-03 16:35:12 +00:00
|
|
|
verticalScrollBar()->setValue(_verticalValue);
|
2015-05-08 12:45:14 +00:00
|
|
|
} else {
|
2016-09-03 16:35:12 +00:00
|
|
|
_verticalValue = verticalValue;
|
2015-05-08 12:45:14 +00:00
|
|
|
if (_st.hiding) {
|
2016-09-03 16:35:12 +00:00
|
|
|
_verticalBar->hideTimeout(_st.hiding);
|
2015-05-08 12:45:14 +00:00
|
|
|
}
|
|
|
|
em = true;
|
2017-09-13 16:57:44 +00:00
|
|
|
_scrollTopUpdated.fire_copy(_verticalValue);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-15 17:13:46 +00:00
|
|
|
if (em) {
|
|
|
|
emit scrolled();
|
|
|
|
if (!_movingByScrollBar) {
|
|
|
|
sendSynteticMouseEvent(this, QEvent::MouseMove, Qt::NoButton);
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 15:05:58 +00:00
|
|
|
void ScrollArea::onInnerResized() {
|
|
|
|
emit innerResized();
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
int ScrollArea::scrollWidth() const {
|
2015-07-17 19:17:37 +00:00
|
|
|
QWidget *w(widget());
|
|
|
|
return w ? qMax(w->width(), width()) : width();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ScrollArea::scrollHeight() const {
|
2015-07-17 19:17:37 +00:00
|
|
|
QWidget *w(widget());
|
|
|
|
return w ? qMax(w->height(), height()) : height();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ScrollArea::scrollLeftMax() const {
|
2015-07-17 19:17:37 +00:00
|
|
|
return scrollWidth() - width();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ScrollArea::scrollTopMax() const {
|
2015-07-17 19:17:37 +00:00
|
|
|
return scrollHeight() - height();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ScrollArea::scrollLeft() const {
|
2016-09-03 16:35:12 +00:00
|
|
|
return _horizontalValue;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ScrollArea::scrollTop() const {
|
2016-09-03 16:35:12 +00:00
|
|
|
return _verticalValue;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::onTouchTimer() {
|
|
|
|
_touchRightButton = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::onTouchScrollTimer() {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto nowTime = crl::now();
|
2016-11-16 10:44:06 +00:00
|
|
|
if (_touchScrollState == TouchScrollState::Acceleration && _touchWaitingAcceleration && (nowTime - _touchAccelerationTime) > 40) {
|
|
|
|
_touchScrollState = TouchScrollState::Manual;
|
2014-05-30 08:53:19 +00:00
|
|
|
touchResetSpeed();
|
2016-11-16 10:44:06 +00:00
|
|
|
} else if (_touchScrollState == TouchScrollState::Auto || _touchScrollState == TouchScrollState::Acceleration) {
|
2014-05-30 08:53:19 +00:00
|
|
|
int32 elapsed = int32(nowTime - _touchTime);
|
|
|
|
QPoint delta = _touchSpeed * elapsed / 1000;
|
|
|
|
bool hasScrolled = touchScroll(delta);
|
|
|
|
|
|
|
|
if (_touchSpeed.isNull() || !hasScrolled) {
|
2016-11-16 10:44:06 +00:00
|
|
|
_touchScrollState = TouchScrollState::Manual;
|
2014-05-30 08:53:19 +00:00
|
|
|
_touchScroll = false;
|
|
|
|
_touchScrollTimer.stop();
|
|
|
|
} else {
|
|
|
|
_touchTime = nowTime;
|
|
|
|
}
|
|
|
|
touchDeaccelerate(elapsed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::touchUpdateSpeed() {
|
2019-02-19 06:57:53 +00:00
|
|
|
const auto nowTime = crl::now();
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_touchPrevPosValid) {
|
|
|
|
const int elapsed = nowTime - _touchSpeedTime;
|
|
|
|
if (elapsed) {
|
|
|
|
const QPoint newPixelDiff = (_touchPos - _touchPrevPos);
|
|
|
|
const QPoint pixelsPerSecond = newPixelDiff * (1000 / elapsed);
|
|
|
|
|
|
|
|
// fingers are inacurates, we ignore small changes to avoid stopping the autoscroll because
|
|
|
|
// of a small horizontal offset when scrolling vertically
|
|
|
|
const int newSpeedY = (qAbs(pixelsPerSecond.y()) > FingerAccuracyThreshold) ? pixelsPerSecond.y() : 0;
|
|
|
|
const int newSpeedX = (qAbs(pixelsPerSecond.x()) > FingerAccuracyThreshold) ? pixelsPerSecond.x() : 0;
|
2016-11-16 10:44:06 +00:00
|
|
|
if (_touchScrollState == TouchScrollState::Auto) {
|
2014-05-30 08:53:19 +00:00
|
|
|
const int oldSpeedY = _touchSpeed.y();
|
|
|
|
const int oldSpeedX = _touchSpeed.x();
|
2014-06-16 09:31:10 +00:00
|
|
|
if ((oldSpeedY <= 0 && newSpeedY <= 0) || ((oldSpeedY >= 0 && newSpeedY >= 0)
|
|
|
|
&& (oldSpeedX <= 0 && newSpeedX <= 0)) || (oldSpeedX >= 0 && newSpeedX >= 0)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_touchSpeed.setY(snap((oldSpeedY + (newSpeedY / 4)), -MaxScrollAccelerated, +MaxScrollAccelerated));
|
|
|
|
_touchSpeed.setX(snap((oldSpeedX + (newSpeedX / 4)), -MaxScrollAccelerated, +MaxScrollAccelerated));
|
|
|
|
} else {
|
|
|
|
_touchSpeed = QPoint();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we average the speed to avoid strange effects with the last delta
|
|
|
|
if (!_touchSpeed.isNull()) {
|
|
|
|
_touchSpeed.setX(snap((_touchSpeed.x() / 4) + (newSpeedX * 3 / 4), -MaxScrollFlick, +MaxScrollFlick));
|
|
|
|
_touchSpeed.setY(snap((_touchSpeed.y() / 4) + (newSpeedY * 3 / 4), -MaxScrollFlick, +MaxScrollFlick));
|
|
|
|
} else {
|
|
|
|
_touchSpeed = QPoint(newSpeedX, newSpeedY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_touchPrevPosValid = true;
|
|
|
|
}
|
|
|
|
_touchSpeedTime = nowTime;
|
|
|
|
_touchPrevPos = _touchPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::touchResetSpeed() {
|
|
|
|
_touchSpeed = QPoint();
|
|
|
|
_touchPrevPosValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollArea::eventFilter(QObject *obj, QEvent *e) {
|
|
|
|
bool res = QScrollArea::eventFilter(obj, e);
|
|
|
|
if (e->type() == QEvent::TouchBegin || e->type() == QEvent::TouchUpdate || e->type() == QEvent::TouchEnd || e->type() == QEvent::TouchCancel) {
|
|
|
|
QTouchEvent *ev = static_cast<QTouchEvent*>(e);
|
|
|
|
if (_touchEnabled && ev->device()->type() == QTouchDevice::TouchScreen) {
|
|
|
|
if (obj == widget()) {
|
2016-06-15 17:13:46 +00:00
|
|
|
touchEvent(ev);
|
|
|
|
return true;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollArea::viewportEvent(QEvent *e) {
|
|
|
|
if (e->type() == QEvent::TouchBegin || e->type() == QEvent::TouchUpdate || e->type() == QEvent::TouchEnd || e->type() == QEvent::TouchCancel) {
|
|
|
|
QTouchEvent *ev = static_cast<QTouchEvent*>(e);
|
|
|
|
if (_touchEnabled && ev->device()->type() == QTouchDevice::TouchScreen) {
|
2016-06-15 17:13:46 +00:00
|
|
|
touchEvent(ev);
|
|
|
|
return true;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QScrollArea::viewportEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::touchEvent(QTouchEvent *e) {
|
|
|
|
if (!e->touchPoints().isEmpty()) {
|
|
|
|
_touchPrevPos = _touchPos;
|
|
|
|
_touchPos = e->touchPoints().cbegin()->screenPos().toPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (e->type()) {
|
2017-11-30 18:18:39 +00:00
|
|
|
case QEvent::TouchBegin: {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_touchPress || e->touchPoints().isEmpty()) return;
|
|
|
|
_touchPress = true;
|
2016-11-16 10:44:06 +00:00
|
|
|
if (_touchScrollState == TouchScrollState::Auto) {
|
|
|
|
_touchScrollState = TouchScrollState::Acceleration;
|
2014-05-30 08:53:19 +00:00
|
|
|
_touchWaitingAcceleration = true;
|
2019-02-19 06:57:53 +00:00
|
|
|
_touchAccelerationTime = crl::now();
|
2014-05-30 08:53:19 +00:00
|
|
|
touchUpdateSpeed();
|
|
|
|
_touchStart = _touchPos;
|
|
|
|
} else {
|
|
|
|
_touchScroll = false;
|
|
|
|
_touchTimer.start(QApplication::startDragTime());
|
|
|
|
}
|
|
|
|
_touchStart = _touchPrevPos = _touchPos;
|
|
|
|
_touchRightButton = false;
|
2017-11-30 18:18:39 +00:00
|
|
|
} break;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-11-30 18:18:39 +00:00
|
|
|
case QEvent::TouchUpdate: {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!_touchPress) return;
|
|
|
|
if (!_touchScroll && (_touchPos - _touchStart).manhattanLength() >= QApplication::startDragDistance()) {
|
|
|
|
_touchTimer.stop();
|
|
|
|
_touchScroll = true;
|
|
|
|
touchUpdateSpeed();
|
|
|
|
}
|
|
|
|
if (_touchScroll) {
|
2016-11-16 10:44:06 +00:00
|
|
|
if (_touchScrollState == TouchScrollState::Manual) {
|
2014-05-30 08:53:19 +00:00
|
|
|
touchScrollUpdated(_touchPos);
|
2016-11-16 10:44:06 +00:00
|
|
|
} else if (_touchScrollState == TouchScrollState::Acceleration) {
|
2014-05-30 08:53:19 +00:00
|
|
|
touchUpdateSpeed();
|
2019-02-19 06:57:53 +00:00
|
|
|
_touchAccelerationTime = crl::now();
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_touchSpeed.isNull()) {
|
2016-11-16 10:44:06 +00:00
|
|
|
_touchScrollState = TouchScrollState::Manual;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:18:39 +00:00
|
|
|
} break;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-11-30 18:18:39 +00:00
|
|
|
case QEvent::TouchEnd: {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!_touchPress) return;
|
|
|
|
_touchPress = false;
|
2017-11-30 18:18:39 +00:00
|
|
|
auto weak = make_weak(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_touchScroll) {
|
2016-11-16 10:44:06 +00:00
|
|
|
if (_touchScrollState == TouchScrollState::Manual) {
|
|
|
|
_touchScrollState = TouchScrollState::Auto;
|
2014-05-30 08:53:19 +00:00
|
|
|
_touchPrevPosValid = false;
|
|
|
|
_touchScrollTimer.start(15);
|
2019-02-19 06:57:53 +00:00
|
|
|
_touchTime = crl::now();
|
2016-11-16 10:44:06 +00:00
|
|
|
} else if (_touchScrollState == TouchScrollState::Auto) {
|
|
|
|
_touchScrollState = TouchScrollState::Manual;
|
2014-05-30 08:53:19 +00:00
|
|
|
_touchScroll = false;
|
|
|
|
touchResetSpeed();
|
2016-11-16 10:44:06 +00:00
|
|
|
} else if (_touchScrollState == TouchScrollState::Acceleration) {
|
|
|
|
_touchScrollState = TouchScrollState::Auto;
|
2014-05-30 08:53:19 +00:00
|
|
|
_touchWaitingAcceleration = false;
|
|
|
|
_touchPrevPosValid = false;
|
|
|
|
}
|
2016-06-15 17:13:46 +00:00
|
|
|
} else if (window()) { // one short tap -- like left mouse click, one long tap -- like right mouse click
|
2014-05-30 08:53:19 +00:00
|
|
|
Qt::MouseButton btn(_touchRightButton ? Qt::RightButton : Qt::LeftButton);
|
|
|
|
|
2017-11-30 18:18:39 +00:00
|
|
|
if (weak) sendSynteticMouseEvent(this, QEvent::MouseMove, Qt::NoButton, _touchStart);
|
|
|
|
if (weak) sendSynteticMouseEvent(this, QEvent::MouseButtonPress, btn, _touchStart);
|
|
|
|
if (weak) sendSynteticMouseEvent(this, QEvent::MouseButtonRelease, btn, _touchStart);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-11-30 18:18:39 +00:00
|
|
|
if (weak && _touchRightButton) {
|
2016-06-15 17:13:46 +00:00
|
|
|
auto windowHandle = window()->windowHandle();
|
|
|
|
auto localPoint = windowHandle->mapFromGlobal(_touchStart);
|
|
|
|
QContextMenuEvent ev(QContextMenuEvent::Mouse, localPoint, _touchStart, QGuiApplication::keyboardModifiers());
|
2019-02-19 06:57:53 +00:00
|
|
|
ev.setTimestamp(crl::now());
|
2016-06-15 17:13:46 +00:00
|
|
|
QGuiApplication::sendEvent(windowHandle, &ev);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:18:39 +00:00
|
|
|
if (weak) {
|
|
|
|
_touchTimer.stop();
|
|
|
|
_touchRightButton = false;
|
|
|
|
}
|
|
|
|
} break;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-11-30 18:18:39 +00:00
|
|
|
case QEvent::TouchCancel: {
|
2014-05-30 08:53:19 +00:00
|
|
|
_touchPress = false;
|
|
|
|
_touchScroll = false;
|
2016-11-16 10:44:06 +00:00
|
|
|
_touchScrollState = TouchScrollState::Manual;
|
2014-05-30 08:53:19 +00:00
|
|
|
_touchTimer.stop();
|
2017-11-30 18:18:39 +00:00
|
|
|
} break;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::touchScrollUpdated(const QPoint &screenPos) {
|
|
|
|
_touchPos = screenPos;
|
|
|
|
touchScroll(_touchPos - _touchPrevPos);
|
|
|
|
touchUpdateSpeed();
|
|
|
|
}
|
|
|
|
|
2015-05-08 12:45:14 +00:00
|
|
|
void ScrollArea::disableScroll(bool dis) {
|
|
|
|
_disabled = dis;
|
2015-10-01 14:05:05 +00:00
|
|
|
if (_disabled && _st.hiding) {
|
2016-09-03 16:35:12 +00:00
|
|
|
_horizontalBar->hideTimeout(0);
|
|
|
|
_verticalBar->hideTimeout(0);
|
2015-05-08 12:45:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::scrollContentsBy(int dx, int dy) {
|
|
|
|
if (_disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QScrollArea::scrollContentsBy(dx, dy);
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
bool ScrollArea::touchScroll(const QPoint &delta) {
|
|
|
|
int32 scTop = scrollTop(), scMax = scrollTopMax(), scNew = snap(scTop - delta.y(), 0, scMax);
|
|
|
|
if (scNew == scTop) return false;
|
|
|
|
|
|
|
|
scrollToY(scNew);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::resizeEvent(QResizeEvent *e) {
|
|
|
|
QScrollArea::resizeEvent(e);
|
2016-09-03 16:35:12 +00:00
|
|
|
_horizontalBar->recountSize();
|
|
|
|
_verticalBar->recountSize();
|
|
|
|
_topShadow->setGeometry(QRect(0, 0, width(), qAbs(_st.topsh)));
|
|
|
|
_bottomShadow->setGeometry(QRect(0, height() - qAbs(_st.bottomsh), width(), qAbs(_st.bottomsh)));
|
2019-01-18 11:26:43 +00:00
|
|
|
if (const auto w = qobject_cast<SplittedWidget*>(widget())) {
|
2015-09-29 13:24:39 +00:00
|
|
|
w->resize(width() - w->otherWidth(), w->height());
|
|
|
|
if (!rtl()) {
|
|
|
|
_other->move(w->width(), w->y());
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
emit geometryChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::moveEvent(QMoveEvent *e) {
|
|
|
|
QScrollArea::moveEvent(e);
|
|
|
|
emit geometryChanged();
|
|
|
|
}
|
|
|
|
|
2015-02-09 23:19:48 +00:00
|
|
|
void ScrollArea::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if ((e->key() == Qt::Key_Up || e->key() == Qt::Key_Down) && e->modifiers().testFlag(Qt::AltModifier)) {
|
|
|
|
e->ignore();
|
2016-07-02 21:43:54 +00:00
|
|
|
} else if(e->key() == Qt::Key_Escape || e->key() == Qt::Key_Back) {
|
|
|
|
((QObject*)widget())->event(e);
|
2015-02-09 23:19:48 +00:00
|
|
|
} else {
|
|
|
|
QScrollArea::keyPressEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 17:13:46 +00:00
|
|
|
void ScrollArea::enterEventHook(QEvent *e) {
|
2015-05-08 12:45:14 +00:00
|
|
|
if (_disabled) return;
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_st.hiding) {
|
2016-09-03 16:35:12 +00:00
|
|
|
_horizontalBar->hideTimeout(_st.hiding);
|
|
|
|
_verticalBar->hideTimeout(_st.hiding);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-05-29 18:52:43 +00:00
|
|
|
return QScrollArea::enterEvent(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 17:13:46 +00:00
|
|
|
void ScrollArea::leaveEventHook(QEvent *e) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_st.hiding) {
|
2016-09-03 16:35:12 +00:00
|
|
|
_horizontalBar->hideTimeout(0);
|
|
|
|
_verticalBar->hideTimeout(0);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-05-29 18:52:43 +00:00
|
|
|
return QScrollArea::leaveEvent(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 17:57:01 +00:00
|
|
|
void ScrollArea::scrollTo(ScrollToRequest request) {
|
|
|
|
scrollToY(request.ymin, request.ymax);
|
|
|
|
}
|
|
|
|
|
2017-11-10 17:15:37 +00:00
|
|
|
void ScrollArea::scrollToWidget(not_null<QWidget*> widget) {
|
|
|
|
if (auto local = this->widget()) {
|
|
|
|
auto globalPosition = widget->mapToGlobal(QPoint(0, 0));
|
|
|
|
auto localPosition = local->mapFromGlobal(globalPosition);
|
|
|
|
auto localTop = localPosition.y();
|
|
|
|
auto localBottom = localTop + widget->height();
|
|
|
|
scrollToY(localTop, localBottom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void ScrollArea::scrollToY(int toTop, int toBottom) {
|
2017-12-26 12:41:48 +00:00
|
|
|
if (const auto inner = widget()) {
|
|
|
|
SendPendingMoveResizeEvents(inner);
|
|
|
|
}
|
|
|
|
SendPendingMoveResizeEvents(this);
|
2015-07-17 19:17:37 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
int toMin = 0, toMax = scrollTopMax();
|
|
|
|
if (toTop < toMin) {
|
|
|
|
toTop = toMin;
|
|
|
|
} else if (toTop > toMax) {
|
|
|
|
toTop = toMax;
|
|
|
|
}
|
|
|
|
bool exact = (toBottom < 0);
|
|
|
|
|
|
|
|
int curTop = scrollTop(), curHeight = height(), curBottom = curTop + curHeight, scToTop = toTop;
|
|
|
|
if (!exact && toTop >= curTop) {
|
|
|
|
if (toBottom < toTop) toBottom = toTop;
|
|
|
|
if (toBottom <= curBottom) return;
|
|
|
|
|
|
|
|
scToTop = toBottom - curHeight;
|
|
|
|
if (scToTop > toTop) scToTop = toTop;
|
|
|
|
if (scToTop == curTop) return;
|
|
|
|
} else {
|
|
|
|
scToTop = toTop;
|
|
|
|
}
|
|
|
|
verticalScrollBar()->setValue(scToTop);
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void ScrollArea::doSetOwnedWidget(object_ptr<TWidget> w) {
|
2019-01-18 11:26:43 +00:00
|
|
|
const auto splitted = qobject_cast<SplittedWidget*>(w.data());
|
2014-05-30 08:53:19 +00:00
|
|
|
if (widget() && _touchEnabled) {
|
|
|
|
widget()->removeEventFilter(this);
|
|
|
|
if (!_widgetAcceptsTouch) widget()->setAttribute(Qt::WA_AcceptTouchEvents, false);
|
|
|
|
}
|
2015-09-29 13:24:39 +00:00
|
|
|
if (_other && !splitted) {
|
2016-12-13 17:07:56 +00:00
|
|
|
_other.destroy();
|
2015-09-29 13:24:39 +00:00
|
|
|
disconnect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(onVerticalScroll()));
|
|
|
|
} else if (!_other && splitted) {
|
2016-12-13 17:07:56 +00:00
|
|
|
_other.create(this);
|
2016-09-03 16:35:12 +00:00
|
|
|
_other->resize(_verticalBar->width(), _other->height());
|
2015-09-29 13:24:39 +00:00
|
|
|
connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(onVerticalScroll()));
|
2016-09-03 16:35:12 +00:00
|
|
|
_horizontalBar->raise();
|
|
|
|
_verticalBar->raise();
|
2015-09-29 13:24:39 +00:00
|
|
|
}
|
2017-02-21 13:45:56 +00:00
|
|
|
_widget = std::move(w);
|
2016-12-13 17:07:56 +00:00
|
|
|
QScrollArea::setWidget(_widget);
|
|
|
|
if (_widget) {
|
|
|
|
_widget->setAutoFillBackground(false);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_touchEnabled) {
|
2016-12-13 17:07:56 +00:00
|
|
|
_widget->installEventFilter(this);
|
|
|
|
_widgetAcceptsTouch = _widget->testAttribute(Qt::WA_AcceptTouchEvents);
|
|
|
|
_widget->setAttribute(Qt::WA_AcceptTouchEvents);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-09-29 13:24:39 +00:00
|
|
|
if (splitted) {
|
2016-09-03 16:35:12 +00:00
|
|
|
splitted->setOtherWidth(_verticalBar->width());
|
2016-12-13 17:07:56 +00:00
|
|
|
_widget->setGeometry(rtl() ? splitted->otherWidth() : 0, 0, width() - splitted->otherWidth(), _widget->height());
|
2015-09-29 13:24:39 +00:00
|
|
|
connect(splitted, SIGNAL(resizeOther()), this, SLOT(onResizeOther()));
|
|
|
|
connect(splitted, SIGNAL(updateOther(const QRect&)), this, SLOT(onUpdateOther(const QRect&)));
|
|
|
|
connect(splitted, SIGNAL(updateOther(const QRegion&)), this, SLOT(onUpdateOther(const QRegion&)));
|
|
|
|
onResizeOther();
|
|
|
|
splitted->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
object_ptr<TWidget> ScrollArea::doTakeWidget() {
|
2015-09-29 13:24:39 +00:00
|
|
|
if (_other) {
|
2016-12-13 17:07:56 +00:00
|
|
|
_other.destroy();
|
2015-09-29 13:24:39 +00:00
|
|
|
disconnect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(onVerticalScroll()));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
QScrollArea::takeWidget();
|
2017-02-21 13:45:56 +00:00
|
|
|
return std::move(_widget);
|
2015-09-29 13:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::onResizeOther() {
|
|
|
|
_other->resize(_other->width(), widget()->height());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::onUpdateOther(const QRect &r) {
|
|
|
|
_other->update(r.intersected(_other->rect()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::onUpdateOther(const QRegion &r) {
|
|
|
|
_other->update(r.intersected(_other->rect()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::onVerticalScroll() {
|
|
|
|
_other->move(_other->x(), widget()->y());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollArea::rangeChanged(int oldMax, int newMax, bool vertical) {
|
|
|
|
}
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2016-11-03 10:33:57 +00:00
|
|
|
void ScrollArea::updateBars() {
|
2018-03-31 01:45:40 +00:00
|
|
|
_horizontalBar->updateBar(true);
|
|
|
|
_verticalBar->updateBar(true);
|
2015-02-03 15:02:46 +00:00
|
|
|
}
|
2015-06-15 17:19:24 +00:00
|
|
|
|
2015-09-13 17:27:29 +00:00
|
|
|
bool ScrollArea::focusNextPrevChild(bool next) {
|
|
|
|
if (QWidget::focusNextPrevChild(next)) {
|
|
|
|
// if (QWidget *fw = focusWidget())
|
|
|
|
// ensureWidgetVisible(fw);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-15 17:13:46 +00:00
|
|
|
void ScrollArea::setMovingByScrollBar(bool movingByScrollBar) {
|
|
|
|
_movingByScrollBar = movingByScrollBar;
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:44:06 +00:00
|
|
|
} // namespace Ui
|