Kirigami2

wheelhandler.cpp
1/*
2 * SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "wheelhandler.h"
8#include "settings.h"
9
10#include <QQmlEngine>
11#include <QQuickItem>
12#include <QQuickWindow>
13#include <QWheelEvent>
14
15KirigamiWheelEvent::KirigamiWheelEvent(QObject *parent)
16 : QObject(parent)
17{
18}
19
20KirigamiWheelEvent::~KirigamiWheelEvent()
21{
22}
23
24void KirigamiWheelEvent::initializeFromEvent(QWheelEvent *event)
25{
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();
32 m_accepted = false;
33 m_inverted = event->inverted();
34}
35
36qreal KirigamiWheelEvent::x() const
37{
38 return m_x;
39}
40
41qreal KirigamiWheelEvent::y() const
42{
43 return m_y;
44}
45
47{
48 return m_angleDelta;
49}
50
52{
53 return m_pixelDelta;
54}
55
57{
58 return m_buttons;
59}
60
62{
63 return m_modifiers;
64}
65
67{
68 return m_inverted;
69}
70
71bool KirigamiWheelEvent::isAccepted()
72{
73 return m_accepted;
74}
75
76void KirigamiWheelEvent::setAccepted(bool accepted)
77{
78 m_accepted = accepted;
79}
80
81///////////////////////////////
82
83WheelFilterItem::WheelFilterItem(QQuickItem *parent)
84 : QQuickItem(parent)
85{
86 setEnabled(false);
87}
88
89///////////////////////////////
90
91WheelHandler::WheelHandler(QObject *parent)
92 : QObject(parent)
93 , m_filterItem(new WheelFilterItem(nullptr))
94{
95 m_filterItem->installEventFilter(this);
96
97 m_wheelScrollingTimer.setSingleShot(true);
98 m_wheelScrollingTimer.setInterval(m_wheelScrollingDuration);
99 m_wheelScrollingTimer.callOnTimeout([this]() {
100 setScrolling(false);
101 });
102
103 m_xScrollAnimation.setEasingCurve(QEasingCurve::OutCubic);
104 m_yScrollAnimation.setEasingCurve(QEasingCurve::OutCubic);
105
107 m_defaultPixelStepSize = 20 * scrollLines;
108 if (!m_explicitVStepSize && m_verticalStepSize != m_defaultPixelStepSize) {
109 m_verticalStepSize = m_defaultPixelStepSize;
110 Q_EMIT verticalStepSizeChanged();
111 }
112 if (!m_explicitHStepSize && m_horizontalStepSize != m_defaultPixelStepSize) {
113 m_horizontalStepSize = m_defaultPixelStepSize;
114 Q_EMIT horizontalStepSizeChanged();
115 }
116 });
117}
118
119WheelHandler::~WheelHandler()
120{
121 delete m_filterItem;
122}
123
125{
126 return m_flickable;
127}
128
129void WheelHandler::setTarget(QQuickItem *target)
130{
131 if (m_flickable == target) {
132 return;
133 }
134
135 if (target && !target->inherits("QQuickFlickable")) {
136 qmlWarning(this) << "target must be a QQuickFlickable";
137 return;
138 }
139
140 if (m_flickable) {
141 m_flickable->removeEventFilter(this);
142 disconnect(m_flickable, nullptr, m_filterItem, nullptr);
143 disconnect(m_flickable, &QQuickItem::parentChanged, this, &WheelHandler::_k_rebindScrollBars);
144 }
145
146 m_flickable = target;
147 m_filterItem->setParentItem(target);
148 if (m_xScrollAnimation.targetObject()) {
149 m_xScrollAnimation.stop();
150 }
151 m_xScrollAnimation.setTargetObject(target);
152 if (m_yScrollAnimation.targetObject()) {
153 m_yScrollAnimation.stop();
154 }
155 m_yScrollAnimation.setTargetObject(target);
156
157 if (target) {
158 target->installEventFilter(this);
159
160 // Stack WheelFilterItem over the Flickable's scrollable content
161 m_filterItem->stackAfter(target->property("contentItem").value<QQuickItem *>());
162 // Make it fill the Flickable
163 m_filterItem->setWidth(target->width());
164 m_filterItem->setHeight(target->height());
165 connect(target, &QQuickItem::widthChanged, m_filterItem, [this, target]() {
166 m_filterItem->setWidth(target->width());
167 });
168 connect(target, &QQuickItem::heightChanged, m_filterItem, [this, target]() {
169 m_filterItem->setHeight(target->height());
170 });
171 }
172
173 _k_rebindScrollBars();
174
175 Q_EMIT targetChanged();
176}
177
178void WheelHandler::_k_rebindScrollBars()
179{
180 struct ScrollBarAttached {
181 QObject *attached = nullptr;
182 QQuickItem *vertical = nullptr;
183 QQuickItem *horizontal = nullptr;
184 };
185
186 ScrollBarAttached attachedToFlickable;
187 ScrollBarAttached attachedToScrollView;
188
189 if (m_flickable) {
190 // Get ScrollBars so that we can filter them too, even if they're not
191 // in the bounds of the Flickable
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 *>();
198 break;
199 }
200 }
201
202 // Check ScrollView if there are no scrollbars attached to the Flickable.
203 // We need to check if the parent inherits QQuickScrollView in case the
204 // parent is another Flickable that already has a Kirigami WheelHandler.
205 auto flickableParent = m_flickable->parentItem();
206 if (m_scrollView && m_scrollView != flickableParent) {
207 m_scrollView->removeEventFilter(this);
208 }
209 if (flickableParent && flickableParent->inherits("QQuickScrollView")) {
210 if (m_scrollView != flickableParent) {
211 m_scrollView = flickableParent;
212 m_scrollView->installEventFilter(this);
213 }
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 *>();
220 break;
221 }
222 }
223 }
224 }
225
226 // Dilemma: ScrollBars can be attached to both ScrollView and Flickable,
227 // but only one of them should be shown anyway. Let's prefer Flickable.
228
229 struct ChosenScrollBar {
230 QObject *attached = nullptr;
231 QQuickItem *scrollBar = nullptr;
232 };
233
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;
241 }
242
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;
250 }
251
252 // Flickable may get re-parented to or out of a ScrollView, so we need to
253 // redo the discovery process. This is especially important for
254 // Kirigami.ScrollablePage component.
255 if (m_flickable) {
256 if (attachedToFlickable.horizontal && attachedToFlickable.vertical) {
257 // But if both scrollbars are already those from the preferred
258 // Flickable, there's no need for rediscovery.
259 disconnect(m_flickable, &QQuickItem::parentChanged, this, &WheelHandler::_k_rebindScrollBars);
260 } else {
261 connect(m_flickable, &QQuickItem::parentChanged, this, &WheelHandler::_k_rebindScrollBars, Qt::UniqueConnection);
262 }
263 }
264
265 if (m_verticalScrollBar != vertical.scrollBar) {
266 if (m_verticalScrollBar) {
267 m_verticalScrollBar->removeEventFilter(this);
268 disconnect(m_verticalChangedConnection);
269 }
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()));
274 }
275 }
276
277 if (m_horizontalScrollBar != horizontal.scrollBar) {
278 if (m_horizontalScrollBar) {
279 m_horizontalScrollBar->removeEventFilter(this);
280 disconnect(m_horizontalChangedConnection);
281 }
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()));
286 }
287 }
288}
289
291{
292 return m_verticalStepSize;
293}
294
295void WheelHandler::setVerticalStepSize(qreal stepSize)
296{
297 m_explicitVStepSize = true;
298 if (qFuzzyCompare(m_verticalStepSize, stepSize)) {
299 return;
300 }
301 // Mimic the behavior of QQuickScrollBar when stepSize is 0
302 if (qFuzzyIsNull(stepSize)) {
303 resetVerticalStepSize();
304 return;
305 }
306 m_verticalStepSize = stepSize;
307 Q_EMIT verticalStepSizeChanged();
308}
309
310void WheelHandler::resetVerticalStepSize()
311{
312 m_explicitVStepSize = false;
313 if (qFuzzyCompare(m_verticalStepSize, m_defaultPixelStepSize)) {
314 return;
315 }
316 m_verticalStepSize = m_defaultPixelStepSize;
317 Q_EMIT verticalStepSizeChanged();
318}
319
321{
322 return m_horizontalStepSize;
323}
324
325void WheelHandler::setHorizontalStepSize(qreal stepSize)
326{
327 m_explicitHStepSize = true;
328 if (qFuzzyCompare(m_horizontalStepSize, stepSize)) {
329 return;
330 }
331 // Mimic the behavior of QQuickScrollBar when stepSize is 0
332 if (qFuzzyIsNull(stepSize)) {
333 resetHorizontalStepSize();
334 return;
335 }
336 m_horizontalStepSize = stepSize;
337 Q_EMIT horizontalStepSizeChanged();
338}
339
340void WheelHandler::resetHorizontalStepSize()
341{
342 m_explicitHStepSize = false;
343 if (qFuzzyCompare(m_horizontalStepSize, m_defaultPixelStepSize)) {
344 return;
345 }
346 m_horizontalStepSize = m_defaultPixelStepSize;
347 Q_EMIT horizontalStepSizeChanged();
348}
349
351{
352 return m_pageScrollModifiers;
353}
354
355void WheelHandler::setPageScrollModifiers(Qt::KeyboardModifiers modifiers)
356{
357 if (m_pageScrollModifiers == modifiers) {
358 return;
359 }
360 m_pageScrollModifiers = modifiers;
361 Q_EMIT pageScrollModifiersChanged();
362}
363
364void WheelHandler::resetPageScrollModifiers()
365{
366 setPageScrollModifiers(m_defaultPageScrollModifiers);
367}
368
370{
371 return m_filterMouseEvents;
372}
373
374void WheelHandler::setFilterMouseEvents(bool enabled)
375{
376 if (m_filterMouseEvents == enabled) {
377 return;
378 }
379 m_filterMouseEvents = enabled;
380 Q_EMIT filterMouseEventsChanged();
381}
382
384{
385 return m_keyNavigationEnabled;
386}
387
388void WheelHandler::setKeyNavigationEnabled(bool enabled)
389{
390 if (m_keyNavigationEnabled == enabled) {
391 return;
392 }
393 m_keyNavigationEnabled = enabled;
394 Q_EMIT keyNavigationEnabledChanged();
395}
396
397void WheelHandler::classBegin()
398{
399 // Initializes smooth scrolling
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");
403}
404
405void WheelHandler::componentComplete()
406{
407}
408
409void WheelHandler::setScrolling(bool scrolling)
410{
411 if (m_wheelScrolling == scrolling) {
412 if (m_wheelScrolling) {
413 m_wheelScrollingTimer.start();
414 }
415 return;
416 }
417 m_wheelScrolling = scrolling;
418 m_filterItem->setEnabled(m_wheelScrolling);
419}
420
421bool WheelHandler::scrollFlickable(QPointF pixelDelta, QPointF angleDelta, Qt::KeyboardModifiers modifiers)
422{
423 if (!m_flickable || (pixelDelta.isNull() && angleDelta.isNull())) {
424 return false;
425 }
426
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;
441 const auto window = m_flickable->window();
442 const auto screen = window ? window->screen() : nullptr;
443 const qreal devicePixelRatio = window != nullptr ? window->devicePixelRatio() : qGuiApp->devicePixelRatio();
444 const qreal refreshRate = screen ? screen->refreshRate() : 0;
445
446 // HACK: Only transpose deltas when not using xcb in order to not conflict with xcb's own delta transposing
447 if (modifiers & m_defaultHorizontalScrollModifiers && qGuiApp->platformName() != QLatin1String("xcb")) {
448 angleDelta = angleDelta.transposed();
449 pixelDelta = pixelDelta.transposed();
450 }
451
452 const qreal xTicks = angleDelta.x() / 120;
453 const qreal yTicks = angleDelta.y() / 120;
454 bool scrolled = false;
455
456 auto getChange = [pageScrollModifiers = modifiers & m_pageScrollModifiers](qreal ticks, qreal pixelDelta, qreal stepSize, qreal pageSize) {
457 // Use page size with pageScrollModifiers. Matches QScrollBar, which uses QAbstractSlider behavior.
459 return qBound(-pageSize, ticks * pageSize, pageSize);
460 } else if (pixelDelta != 0) {
461 return pixelDelta;
462 } else {
463 return ticks * stepSize;
464 }
465 };
466
467 auto getPosition = [devicePixelRatio](qreal size,
468 qreal contentSize,
469 qreal contentPos,
470 qreal originPos,
471 qreal pageSize,
472 qreal leadingMargin,
473 qreal trailingMargin,
474 qreal change,
475 const QPropertyAnimation &animation) {
476 if (contentSize <= pageSize) {
477 return contentPos;
478 }
479
480 // contentX and contentY use reversed signs from what x and y would normally use, so flip the signs
481
482 qreal minExtent = leadingMargin - originPos;
483 qreal maxExtent = size - (contentSize + trailingMargin + originPos);
484
485 qreal newContentPos =
486 std::clamp((animation.state() == QPropertyAnimation::Running ? animation.endValue().toReal() : contentPos) - change, -minExtent, -maxExtent);
487
488 // Flickable::pixelAligned rounds the position, so round to mimic that behavior.
489 // Rounding prevents fractional positioning from causing text to be
490 // clipped off on the top and bottom.
491 // Multiply by devicePixelRatio before rounding and divide by devicePixelRatio
492 // after to make position match pixels on the screen more closely.
493 return std::round(newContentPos * devicePixelRatio) / devicePixelRatio;
494 };
495
496 auto setPosition = [this, devicePixelRatio, refreshRate](qreal oldPos, qreal newPos, qreal stepSize, const char *property, QPropertyAnimation &animation) {
497 animation.stop();
498 if (oldPos == newPos) {
499 return false;
500 }
501 if (!m_settings->smoothScroll() || !m_engine || refreshRate <= 0) {
502 animation.setDuration(0);
503 m_flickable->setProperty(property, newPos);
504 return true;
505 }
506
507 // Can't use wheelEvent->deviceType() to determine device type since
508 // on Wayland mouse is always regarded as touchpad:
509 // https://invent.kde.org/qt/qt/qtwayland/-/blob/e695a39519a7629c1549275a148cfb9ab99a07a9/src/client/qwaylandinputdevice.cpp#L445
510 // Mouse wheel can generate angle delta like 240, 360 and so on when
511 // scrolling very fast on some mice such as the Logitech M150.
512 // Mice with hi-res mouse wheels such as the Logitech MX Master 3 can
513 // generate angle deltas as small as 16.
514 // On X11, trackpads can also generate very fine angle deltas.
515
516 // Duration is based on the duration and movement for 120 angle delta.
517 // Shorten duration for smaller movements, limit duration for big movements.
518 // We don't want fine deltas to feel extra slow and fast scrolling should still feel fast.
519 // Minimum 3 frames for a 60hz display if delta > 2 physical pixels
520 // (start already rendered -> 1/3 rendered -> 2/3 rendered -> end rendered).
521 // Skip animation if <= 2 real frames for low refresh rate screens.
522 // Otherwise, we don't scale the duration based on refresh rate or
523 // device pixel ratio to avoid making the animation unexpectedly
524 // longer or shorter on different screens.
525
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())
529 : 0;
530 animation.setDuration(duration <= qCeil(1000.0 / refreshRate * 2) ? 0 : duration);
531 if (animation.duration() > 0) {
532 animation.setEndValue(newPos);
534 } else {
535 m_flickable->setProperty(property, newPos);
536 }
537 return true;
538 };
539
540 qreal xChange = getChange(xTicks, pixelDelta.x(), m_horizontalStepSize, pageWidth);
541 qreal newContentX = getPosition(width, contentWidth, contentX, originX, pageWidth, leftMargin, rightMargin, xChange, m_xScrollAnimation);
542
543 qreal yChange = getChange(yTicks, pixelDelta.y(), m_verticalStepSize, pageHeight);
544 qreal newContentY = getPosition(height, contentHeight, contentY, originY, pageHeight, topMargin, bottomMargin, yChange, m_yScrollAnimation);
545
546 // Don't use `||` because we need the position to be set for contentX and contentY.
547 scrolled |= setPosition(contentX, newContentX, m_horizontalStepSize, "contentX", m_xScrollAnimation);
548 scrolled |= setPosition(contentY, newContentY, m_verticalStepSize, "contentY", m_yScrollAnimation);
549
550 return scrolled;
551}
552
553bool WheelHandler::scrollUp(qreal stepSize)
554{
555 if (qFuzzyIsNull(stepSize)) {
556 return false;
557 } else if (stepSize < 0) {
558 stepSize = m_verticalStepSize;
559 }
560 // contentY uses reversed sign
561 return scrollFlickable(QPointF(0, stepSize));
562}
563
564bool WheelHandler::scrollDown(qreal stepSize)
565{
566 if (qFuzzyIsNull(stepSize)) {
567 return false;
568 } else if (stepSize < 0) {
569 stepSize = m_verticalStepSize;
570 }
571 // contentY uses reversed sign
572 return scrollFlickable(QPointF(0, -stepSize));
573}
574
575bool WheelHandler::scrollLeft(qreal stepSize)
576{
577 if (qFuzzyIsNull(stepSize)) {
578 return false;
579 } else if (stepSize < 0) {
580 stepSize = m_horizontalStepSize;
581 }
582 // contentX uses reversed sign
583 return scrollFlickable(QPoint(stepSize, 0));
584}
585
586bool WheelHandler::scrollRight(qreal stepSize)
587{
588 if (qFuzzyIsNull(stepSize)) {
589 return false;
590 } else if (stepSize < 0) {
591 stepSize = m_horizontalStepSize;
592 }
593 // contentX uses reversed sign
594 return scrollFlickable(QPoint(-stepSize, 0));
595}
596
597bool WheelHandler::eventFilter(QObject *watched, QEvent *event)
598{
599 auto item = qobject_cast<QQuickItem *>(watched);
600 if (!item || !item->isEnabled()) {
601 return false;
602 }
603
604 qreal contentWidth = 0;
605 qreal contentHeight = 0;
606 qreal pageWidth = 0;
607 qreal pageHeight = 0;
608 if (m_flickable) {
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();
613 }
614
615 // The code handling touch, mouse and hover events is mostly copied/adapted from QQuickScrollView::childMouseEventFilter()
616 switch (event->type()) {
617 case QEvent::Wheel: {
618 // QQuickScrollBar::interactive handling Matches behavior in QQuickScrollView::eventFilter()
619 if (m_filterMouseEvents) {
620 if (m_verticalScrollBar) {
621 m_verticalScrollBar->setProperty("interactive", true);
622 }
623 if (m_horizontalScrollBar) {
624 m_horizontalScrollBar->setProperty("interactive", true);
625 }
626 }
627 QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
628
629 // NOTE: On X11 with libinput, pixelDelta is identical to angleDelta when using a mouse that shouldn't use pixelDelta.
630 // If faulty pixelDelta, reset pixelDelta to (0,0).
631 if (wheelEvent->pixelDelta() == wheelEvent->angleDelta()) {
632 // In order to change any of the data, we have to create a whole new QWheelEvent from its constructor.
633 QWheelEvent newWheelEvent(wheelEvent->position(),
634 wheelEvent->globalPosition(),
635 QPoint(0, 0), // pixelDelta
636 wheelEvent->angleDelta(),
637 wheelEvent->buttons(),
638 wheelEvent->modifiers(),
639 wheelEvent->phase(),
640 wheelEvent->inverted(),
641 wheelEvent->source());
642 m_kirigamiWheelEvent.initializeFromEvent(&newWheelEvent);
643 } else {
644 m_kirigamiWheelEvent.initializeFromEvent(wheelEvent);
645 }
646
647 Q_EMIT wheel(&m_kirigamiWheelEvent);
648
649 if (m_kirigamiWheelEvent.isAccepted()) {
650 return true;
651 }
652
653 bool scrolled = false;
654 if (m_scrollFlickableTarget || (contentHeight <= pageHeight && contentWidth <= pageWidth)) {
655 // Don't use pixelDelta from the event unless angleDelta is not available
656 // because scrolling by pixelDelta is too slow on Wayland with libinput.
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()));
659 }
660 setScrolling(scrolled);
661
662 // NOTE: Wheel events created by touchpad gestures with pixel deltas will cause scrolling to jump back
663 // to where scrolling started unless the event is always accepted before it reaches the Flickable.
664 bool flickableWillUseGestureScrolling = !(wheelEvent->source() == Qt::MouseEventNotSynthesized || wheelEvent->pixelDelta().isNull());
665 return scrolled || m_blockTargetWheel || flickableWillUseGestureScrolling;
666 }
667
668 case QEvent::TouchBegin: {
669 m_wasTouched = true;
670 if (!m_filterMouseEvents) {
671 break;
672 }
673 if (m_verticalScrollBar) {
674 m_verticalScrollBar->setProperty("interactive", false);
675 }
676 if (m_horizontalScrollBar) {
677 m_horizontalScrollBar->setProperty("interactive", false);
678 }
679 break;
680 }
681
682 case QEvent::TouchEnd: {
683 m_wasTouched = false;
684 break;
685 }
686
688 // NOTE: Flickable does not handle touch events, only synthesized mouse events
689 m_wasTouched = static_cast<QMouseEvent *>(event)->source() != Qt::MouseEventNotSynthesized;
690 if (!m_filterMouseEvents) {
691 break;
692 }
693 if (!m_wasTouched) {
694 if (m_verticalScrollBar) {
695 m_verticalScrollBar->setProperty("interactive", true);
696 }
697 if (m_horizontalScrollBar) {
698 m_horizontalScrollBar->setProperty("interactive", true);
699 }
700 break;
701 }
702 return !m_wasTouched && item == m_flickable;
703 }
704
707 setScrolling(false);
708 if (!m_filterMouseEvents) {
709 break;
710 }
711 if (static_cast<QMouseEvent *>(event)->source() == Qt::MouseEventNotSynthesized && item == m_flickable) {
712 return true;
713 }
714 break;
715 }
716
718 case QEvent::HoverMove: {
719 if (!m_filterMouseEvents) {
720 break;
721 }
722 if (m_wasTouched && (item == m_verticalScrollBar || item == m_horizontalScrollBar)) {
723 if (m_verticalScrollBar) {
724 m_verticalScrollBar->setProperty("interactive", true);
725 }
726 if (m_horizontalScrollBar) {
727 m_horizontalScrollBar->setProperty("interactive", true);
728 }
729 }
730 break;
731 }
732
733 case QEvent::KeyPress: {
734 if (!m_keyNavigationEnabled) {
735 break;
736 }
737 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
738 bool horizontalScroll = keyEvent->modifiers() & m_defaultHorizontalScrollModifiers;
739 switch (keyEvent->key()) {
740 case Qt::Key_Up:
741 return scrollUp();
742 case Qt::Key_Down:
743 return scrollDown();
744 case Qt::Key_Left:
745 return scrollLeft();
746 case Qt::Key_Right:
747 return scrollRight();
748 case Qt::Key_PageUp:
749 return horizontalScroll ? scrollLeft(pageWidth) : scrollUp(pageHeight);
750 case Qt::Key_PageDown:
751 return horizontalScroll ? scrollRight(pageWidth) : scrollDown(pageHeight);
752 case Qt::Key_Home:
753 return horizontalScroll ? scrollLeft(contentWidth) : scrollUp(contentHeight);
754 case Qt::Key_End:
755 return horizontalScroll ? scrollRight(contentWidth) : scrollDown(contentHeight);
756 default:
757 break;
758 }
759 break;
760 }
761
762 default:
763 break;
764 }
765
766 return false;
767}
768
769#include "moc_wheelhandler.cpp"
QML_ELEMENTqreal x
bool filterMouseEvents
qreal horizontalStepSize
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)
qreal verticalStepSize
Q_INVOKABLE bool scrollUp(qreal stepSize=-1)
Q_INVOKABLE bool scrollLeft(qreal stepSize=-1)
QWidget * window(QObject *job)
KREPORT_EXPORT QPageSize::PageSizeId pageSize(const QString &key)
QStyleHints * styleHints()
Qt::KeyboardModifiers modifiers() const const
Q_EMITQ_EMIT
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
qreal x() const const
qreal y() const const
void heightChanged()
void parentChanged(QQuickItem *)
void widthChanged()
Qt::MouseButtons buttons() const const
QPointF globalPosition() const const
QPointF position() const const
void wheelScrollLinesChanged(int scrollLines)
UniqueConnection
typedef KeyboardModifiers
MouseEventNotSynthesized
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
QScreen * screen() const const
QWidget * window() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 12:02:16 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.