7#include "wheelhandler.h"
12#include <QQuickWindow>
15KirigamiWheelEvent::KirigamiWheelEvent(
QObject *parent)
20KirigamiWheelEvent::~KirigamiWheelEvent()
24void KirigamiWheelEvent::initializeFromEvent(
QWheelEvent *event)
26 m_x =
event->position().x();
27 m_y =
event->position().y();
28 m_angleDelta =
event->angleDelta();
29 m_pixelDelta =
event->pixelDelta();
30 m_buttons =
event->buttons();
31 m_modifiers =
event->modifiers();
33 m_inverted =
event->inverted();
71bool KirigamiWheelEvent::isAccepted()
76void KirigamiWheelEvent::setAccepted(
bool accepted)
83WheelFilterItem::WheelFilterItem(
QQuickItem *parent)
91WheelHandler::WheelHandler(
QObject *parent)
93 , m_filterItem(new WheelFilterItem(nullptr))
97 m_wheelScrollingTimer.setSingleShot(
true);
98 m_wheelScrollingTimer.setInterval(m_wheelScrollingDuration);
99 m_wheelScrollingTimer.callOnTimeout([
this]() {
107 m_defaultPixelStepSize = 20 * scrollLines;
108 if (!m_explicitVStepSize && m_verticalStepSize != m_defaultPixelStepSize) {
109 m_verticalStepSize = m_defaultPixelStepSize;
110 Q_EMIT verticalStepSizeChanged();
112 if (!m_explicitHStepSize && m_horizontalStepSize != m_defaultPixelStepSize) {
113 m_horizontalStepSize = m_defaultPixelStepSize;
114 Q_EMIT horizontalStepSizeChanged();
119WheelHandler::~WheelHandler()
129void WheelHandler::setTarget(
QQuickItem *target)
131 if (m_flickable ==
target) {
136 qmlWarning(
this) <<
"target must be a QQuickFlickable";
141 m_flickable->removeEventFilter(
this);
142 disconnect(m_flickable,
nullptr, m_filterItem,
nullptr);
147 m_filterItem->setParentItem(
target);
148 if (m_xScrollAnimation.targetObject()) {
149 m_xScrollAnimation.stop();
151 m_xScrollAnimation.setTargetObject(
target);
152 if (m_yScrollAnimation.targetObject()) {
153 m_yScrollAnimation.stop();
155 m_yScrollAnimation.setTargetObject(
target);
158 target->installEventFilter(
this);
161 m_filterItem->stackAfter(
target->property(
"contentItem").value<QQuickItem *>());
163 m_filterItem->setWidth(
target->width());
164 m_filterItem->setHeight(
target->height());
166 m_filterItem->setWidth(
target->width());
169 m_filterItem->setHeight(
target->height());
173 _k_rebindScrollBars();
178void WheelHandler::_k_rebindScrollBars()
180 struct ScrollBarAttached {
181 QObject *attached =
nullptr;
182 QQuickItem *vertical =
nullptr;
183 QQuickItem *horizontal =
nullptr;
186 ScrollBarAttached attachedToFlickable;
187 ScrollBarAttached attachedToScrollView;
192 const auto flickableChildren = m_flickable->children();
193 for (
const auto child : flickableChildren) {
194 if (child->inherits(
"QQuickScrollBarAttached")) {
195 attachedToFlickable.attached = child;
196 attachedToFlickable.vertical = child->property(
"vertical").value<QQuickItem *>();
197 attachedToFlickable.horizontal = child->property(
"horizontal").value<QQuickItem *>();
205 auto flickableParent = m_flickable->parentItem();
206 if (m_scrollView && m_scrollView != flickableParent) {
207 m_scrollView->removeEventFilter(
this);
209 if (flickableParent && flickableParent->inherits(
"QQuickScrollView")) {
210 if (m_scrollView != flickableParent) {
211 m_scrollView = flickableParent;
212 m_scrollView->installEventFilter(
this);
214 const auto siblings = m_scrollView->children();
215 for (
const auto child : siblings) {
216 if (child->inherits(
"QQuickScrollBarAttached")) {
217 attachedToScrollView.attached = child;
218 attachedToScrollView.vertical = child->property(
"vertical").value<QQuickItem *>();
219 attachedToScrollView.horizontal = child->property(
"horizontal").value<QQuickItem *>();
229 struct ChosenScrollBar {
230 QObject *attached =
nullptr;
231 QQuickItem *scrollBar =
nullptr;
234 ChosenScrollBar vertical;
235 if (attachedToFlickable.vertical) {
236 vertical.attached = attachedToFlickable.attached;
237 vertical.scrollBar = attachedToFlickable.vertical;
238 }
else if (attachedToScrollView.vertical) {
239 vertical.attached = attachedToScrollView.attached;
240 vertical.scrollBar = attachedToScrollView.vertical;
243 ChosenScrollBar horizontal;
244 if (attachedToFlickable.horizontal) {
245 horizontal.attached = attachedToFlickable.attached;
246 horizontal.scrollBar = attachedToFlickable.horizontal;
247 }
else if (attachedToScrollView.horizontal) {
248 horizontal.attached = attachedToScrollView.attached;
249 horizontal.scrollBar = attachedToScrollView.horizontal;
256 if (attachedToFlickable.horizontal && attachedToFlickable.vertical) {
265 if (m_verticalScrollBar != vertical.scrollBar) {
266 if (m_verticalScrollBar) {
267 m_verticalScrollBar->removeEventFilter(
this);
270 m_verticalScrollBar = vertical.scrollBar;
271 if (vertical.scrollBar) {
272 vertical.scrollBar->installEventFilter(
this);
273 m_verticalChangedConnection =
connect(vertical.attached, SIGNAL(verticalChanged()),
this, SLOT(_k_rebindScrollBars()));
277 if (m_horizontalScrollBar != horizontal.scrollBar) {
278 if (m_horizontalScrollBar) {
279 m_horizontalScrollBar->removeEventFilter(
this);
282 m_horizontalScrollBar = horizontal.scrollBar;
283 if (horizontal.scrollBar) {
284 horizontal.scrollBar->installEventFilter(
this);
285 m_horizontalChangedConnection =
connect(horizontal.attached, SIGNAL(horizontalChanged()),
this, SLOT(_k_rebindScrollBars()));
292 return m_verticalStepSize;
295void WheelHandler::setVerticalStepSize(qreal stepSize)
297 m_explicitVStepSize =
true;
298 if (qFuzzyCompare(m_verticalStepSize, stepSize)) {
302 if (qFuzzyIsNull(stepSize)) {
303 resetVerticalStepSize();
306 m_verticalStepSize = stepSize;
307 Q_EMIT verticalStepSizeChanged();
310void WheelHandler::resetVerticalStepSize()
312 m_explicitVStepSize =
false;
313 if (qFuzzyCompare(m_verticalStepSize, m_defaultPixelStepSize)) {
316 m_verticalStepSize = m_defaultPixelStepSize;
317 Q_EMIT verticalStepSizeChanged();
322 return m_horizontalStepSize;
325void WheelHandler::setHorizontalStepSize(qreal stepSize)
327 m_explicitHStepSize =
true;
328 if (qFuzzyCompare(m_horizontalStepSize, stepSize)) {
332 if (qFuzzyIsNull(stepSize)) {
333 resetHorizontalStepSize();
336 m_horizontalStepSize = stepSize;
337 Q_EMIT horizontalStepSizeChanged();
340void WheelHandler::resetHorizontalStepSize()
342 m_explicitHStepSize =
false;
343 if (qFuzzyCompare(m_horizontalStepSize, m_defaultPixelStepSize)) {
346 m_horizontalStepSize = m_defaultPixelStepSize;
347 Q_EMIT horizontalStepSizeChanged();
352 return m_pageScrollModifiers;
357 if (m_pageScrollModifiers == modifiers) {
360 m_pageScrollModifiers = modifiers;
361 Q_EMIT pageScrollModifiersChanged();
364void WheelHandler::resetPageScrollModifiers()
366 setPageScrollModifiers(m_defaultPageScrollModifiers);
371 return m_filterMouseEvents;
374void WheelHandler::setFilterMouseEvents(
bool enabled)
376 if (m_filterMouseEvents == enabled) {
379 m_filterMouseEvents = enabled;
380 Q_EMIT filterMouseEventsChanged();
385 return m_keyNavigationEnabled;
388void WheelHandler::setKeyNavigationEnabled(
bool enabled)
390 if (m_keyNavigationEnabled == enabled) {
393 m_keyNavigationEnabled = enabled;
394 Q_EMIT keyNavigationEnabledChanged();
397void WheelHandler::classBegin()
400 m_engine = qmlEngine(
this);
401 m_units = m_engine->singletonInstance<Kirigami::Platform::Units *>(
"org.kde.kirigami.platform",
"Units");
402 m_settings = m_engine->singletonInstance<Kirigami::Platform::Settings *>(
"org.kde.kirigami.platform",
"Settings");
405void WheelHandler::componentComplete()
409void WheelHandler::setScrolling(
bool scrolling)
411 if (m_wheelScrolling == scrolling) {
412 if (m_wheelScrolling) {
413 m_wheelScrollingTimer.start();
417 m_wheelScrolling = scrolling;
418 m_filterItem->setEnabled(m_wheelScrolling);
423 if (!m_flickable || (pixelDelta.
isNull() && angleDelta.
isNull())) {
427 const qreal width = m_flickable->width();
428 const qreal height = m_flickable->height();
429 const qreal contentWidth = m_flickable->property(
"contentWidth").toReal();
430 const qreal contentHeight = m_flickable->property(
"contentHeight").toReal();
431 const qreal contentX = m_flickable->property(
"contentX").toReal();
432 const qreal contentY = m_flickable->property(
"contentY").toReal();
433 const qreal topMargin = m_flickable->property(
"topMargin").toReal();
434 const qreal bottomMargin = m_flickable->property(
"bottomMargin").toReal();
435 const qreal leftMargin = m_flickable->property(
"leftMargin").toReal();
436 const qreal rightMargin = m_flickable->property(
"rightMargin").toReal();
437 const qreal originX = m_flickable->property(
"originX").toReal();
438 const qreal originY = m_flickable->property(
"originY").toReal();
439 const qreal pageWidth = width - leftMargin - rightMargin;
440 const qreal pageHeight = height - topMargin - bottomMargin;
444 const qreal refreshRate = screen ? screen->refreshRate() : 0;
447 if (modifiers & m_defaultHorizontalScrollModifiers && qGuiApp->platformName() != QLatin1String(
"xcb")) {
452 const qreal xTicks = angleDelta.
x() / 120;
453 const qreal yTicks = angleDelta.
y() / 120;
454 bool scrolled =
false;
456 auto getChange = [
pageScrollModifiers = modifiers & m_pageScrollModifiers](qreal ticks, qreal pixelDelta, qreal stepSize, qreal
pageSize) {
459 return qBound(-pageSize, ticks * pageSize, pageSize);
460 }
else if (pixelDelta != 0) {
463 return ticks * stepSize;
467 auto getPosition = [devicePixelRatio](qreal size,
473 qreal trailingMargin,
475 const QPropertyAnimation &animation) {
476 if (contentSize <= pageSize) {
482 qreal minExtent = leadingMargin - originPos;
483 qreal maxExtent = size - (contentSize + trailingMargin + originPos);
485 qreal newContentPos =
486 std::clamp((animation.state() ==
QPropertyAnimation::Running ? animation.endValue().toReal() : contentPos) - change, -minExtent, -maxExtent);
493 return std::round(newContentPos * devicePixelRatio) / devicePixelRatio;
496 auto setPosition = [
this, devicePixelRatio, refreshRate](qreal oldPos, qreal newPos, qreal stepSize,
const char *
property, QPropertyAnimation &animation) {
498 if (oldPos == newPos) {
501 if (!m_settings->smoothScroll() || !m_engine || refreshRate <= 0) {
502 animation.setDuration(0);
503 m_flickable->setProperty(
property, newPos);
526 qreal absPixelDelta = std::abs(newPos - oldPos);
527 int duration = absPixelDelta * devicePixelRatio > 2
528 ? std::clamp(qRound(absPixelDelta * m_units->longDuration() / stepSize), qCeil(1000.0 / 60.0 * 3), m_units->longDuration())
530 animation.setDuration(duration <= qCeil(1000.0 / refreshRate * 2) ? 0 : duration);
531 if (animation.duration() > 0) {
532 animation.setEndValue(newPos);
535 m_flickable->setProperty(
property, newPos);
540 qreal xChange = getChange(xTicks, pixelDelta.x(), m_horizontalStepSize, pageWidth);
541 qreal newContentX = getPosition(width, contentWidth, contentX, originX, pageWidth, leftMargin, rightMargin, xChange, m_xScrollAnimation);
543 qreal yChange = getChange(yTicks, pixelDelta.y(), m_verticalStepSize, pageHeight);
544 qreal newContentY = getPosition(height, contentHeight, contentY, originY, pageHeight, topMargin, bottomMargin, yChange, m_yScrollAnimation);
547 scrolled |= setPosition(contentX, newContentX, m_horizontalStepSize,
"contentX", m_xScrollAnimation);
548 scrolled |= setPosition(contentY, newContentY, m_verticalStepSize,
"contentY", m_yScrollAnimation);
555 if (qFuzzyIsNull(stepSize)) {
557 }
else if (stepSize < 0) {
558 stepSize = m_verticalStepSize;
561 return scrollFlickable(QPointF(0, stepSize));
566 if (qFuzzyIsNull(stepSize)) {
568 }
else if (stepSize < 0) {
569 stepSize = m_verticalStepSize;
572 return scrollFlickable(QPointF(0, -stepSize));
577 if (qFuzzyIsNull(stepSize)) {
579 }
else if (stepSize < 0) {
580 stepSize = m_horizontalStepSize;
583 return scrollFlickable(QPoint(stepSize, 0));
588 if (qFuzzyIsNull(stepSize)) {
590 }
else if (stepSize < 0) {
591 stepSize = m_horizontalStepSize;
594 return scrollFlickable(QPoint(-stepSize, 0));
597bool WheelHandler::eventFilter(
QObject *watched,
QEvent *event)
600 if (!item || !item->isEnabled()) {
604 qreal contentWidth = 0;
605 qreal contentHeight = 0;
607 qreal pageHeight = 0;
609 contentWidth = m_flickable->property(
"contentWidth").toReal();
610 contentHeight = m_flickable->property(
"contentHeight").toReal();
611 pageWidth = m_flickable->width() - m_flickable->property(
"leftMargin").toReal() - m_flickable->property(
"rightMargin").toReal();
612 pageHeight = m_flickable->height() - m_flickable->property(
"topMargin").toReal() - m_flickable->property(
"bottomMargin").toReal();
616 switch (
event->type()) {
619 if (m_filterMouseEvents) {
620 if (m_verticalScrollBar) {
621 m_verticalScrollBar->setProperty(
"interactive",
true);
623 if (m_horizontalScrollBar) {
624 m_horizontalScrollBar->setProperty(
"interactive",
true);
627 QWheelEvent *wheelEvent =
static_cast<QWheelEvent *
>(
event);
633 QWheelEvent newWheelEvent(wheelEvent->
position(),
642 m_kirigamiWheelEvent.initializeFromEvent(&newWheelEvent);
644 m_kirigamiWheelEvent.initializeFromEvent(wheelEvent);
649 if (m_kirigamiWheelEvent.isAccepted()) {
653 bool scrolled =
false;
654 if (m_scrollFlickableTarget || (contentHeight <= pageHeight && contentWidth <= pageWidth)) {
657 QPointF pixelDelta = m_kirigamiWheelEvent.angleDelta().isNull() ? m_kirigamiWheelEvent.pixelDelta() : QPoint(0, 0);
658 scrolled = scrollFlickable(pixelDelta, m_kirigamiWheelEvent.angleDelta(),
Qt::KeyboardModifiers(m_kirigamiWheelEvent.modifiers()));
660 setScrolling(scrolled);
665 return scrolled || m_blockTargetWheel || flickableWillUseGestureScrolling;
670 if (!m_filterMouseEvents) {
673 if (m_verticalScrollBar) {
674 m_verticalScrollBar->setProperty(
"interactive",
false);
676 if (m_horizontalScrollBar) {
677 m_horizontalScrollBar->setProperty(
"interactive",
false);
683 m_wasTouched =
false;
690 if (!m_filterMouseEvents) {
694 if (m_verticalScrollBar) {
695 m_verticalScrollBar->setProperty(
"interactive",
true);
697 if (m_horizontalScrollBar) {
698 m_horizontalScrollBar->setProperty(
"interactive",
true);
702 return !m_wasTouched && item == m_flickable;
708 if (!m_filterMouseEvents) {
719 if (!m_filterMouseEvents) {
722 if (m_wasTouched && (item == m_verticalScrollBar || item == m_horizontalScrollBar)) {
723 if (m_verticalScrollBar) {
724 m_verticalScrollBar->setProperty(
"interactive",
true);
726 if (m_horizontalScrollBar) {
727 m_horizontalScrollBar->setProperty(
"interactive",
true);
734 if (!m_keyNavigationEnabled) {
738 bool horizontalScroll =
keyEvent->modifiers() & m_defaultHorizontalScrollModifiers;
769#include "moc_wheelhandler.cpp"
Qt::KeyboardModifiers pageScrollModifiers
bool keyNavigationEnabled
Q_INVOKABLE bool scrollDown(qreal stepSize=-1)
QML_ELEMENTQQuickItem * target
Q_INVOKABLE bool scrollRight(qreal stepSize=-1)
void wheel(KirigamiWheelEvent *wheel)
Q_INVOKABLE bool scrollUp(qreal stepSize=-1)
Q_INVOKABLE bool scrollLeft(qreal stepSize=-1)
KREPORT_EXPORT QPageSize::PageSizeId pageSize(const QString &key)
QStyleHints * styleHints()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
virtual bool event(QEvent *e)
void installEventFilter(QObject *filterObj)
QVariant property(const char *name) const const
T qobject_cast(QObject *object)
qreal devicePixelRatio() const const
bool isNull() const const
bool isNull() const const
QPointF transposed() const const
void parentChanged(QQuickItem *)
QPointF globalPosition() const const
QPointF position() const const
typedef KeyboardModifiers
void keyEvent(KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier, int delay)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
Qt::MouseEventSource source() const const
QPoint angleDelta() const const
bool inverted() const const
Qt::ScrollPhase phase() const const
QPoint pixelDelta() const const