QmlMaterial 0.1.0
Loading...
Searching...
No Matches
wheelhandler.h
1/* SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
2 * SPDX-FileCopyrightText: 2021 Noah Davis <noahadvs@gmail.com>
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6#pragma once
7
8#include <QGuiApplication>
9#include <QObject>
10#include <QPoint>
11#include <QPropertyAnimation>
12#include <QQmlParserStatus>
13#include <QQuickItem>
14#include <QStyleHints>
15#include <QTimer>
16#include <QQmlProperty>
17
18class QWheelEvent;
19class QQmlEngine;
20class WheelHandler;
21
25class KirigamiWheelEvent : public QObject {
26 Q_OBJECT
27
33 Q_PROPERTY(qreal x READ x CONSTANT FINAL)
34
35
40 Q_PROPERTY(qreal y READ y CONSTANT FINAL)
41
50 Q_PROPERTY(QPointF angleDelta READ angleDelta CONSTANT FINAL)
51
57 Q_PROPERTY(QPointF pixelDelta READ pixelDelta CONSTANT FINAL)
58
65 Q_PROPERTY(int buttons READ buttons CONSTANT FINAL)
66
76 Q_PROPERTY(int modifiers READ modifiers CONSTANT FINAL)
77
84 Q_PROPERTY(bool inverted READ inverted CONSTANT FINAL)
85
110 Q_PROPERTY(bool accepted READ isAccepted WRITE setAccepted FINAL)
111
112public:
113 KirigamiWheelEvent(QObject* parent = nullptr);
114 ~KirigamiWheelEvent() override;
115
116 void initializeFromEvent(QWheelEvent* event);
117
118 qreal x() const;
119 qreal y() const;
120 QPointF angleDelta() const;
121 QPointF pixelDelta() const;
122 int buttons() const;
123 int modifiers() const;
124 bool inverted() const;
125 bool isAccepted();
126 void setAccepted(bool accepted);
127
128private:
129 qreal m_x = 0;
130 qreal m_y = 0;
131 QPointF m_angleDelta;
132 QPointF m_pixelDelta;
133 Qt::MouseButtons m_buttons = Qt::NoButton;
134 Qt::KeyboardModifiers m_modifiers = Qt::NoModifier;
135 bool m_inverted = false;
136 bool m_accepted = false;
137};
138
139class WheelFilterItem : public QQuickItem {
140 Q_OBJECT
141public:
142 WheelFilterItem(QQuickItem* parent = nullptr);
143};
144
179class WheelHandler : public QObject, public QQmlParserStatus {
180 Q_OBJECT
181 Q_INTERFACES(QQmlParserStatus)
182 QML_ELEMENT
183
187 Q_PROPERTY(QQuickItem* target READ target WRITE setTarget NOTIFY targetChanged FINAL)
188
189 Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged FINAL)
190
191
201 Q_PROPERTY(qreal verticalStepSize READ verticalStepSize WRITE setVerticalStepSize RESET
202 resetVerticalStepSize NOTIFY verticalStepSizeChanged FINAL)
203
214 Q_PROPERTY(qreal horizontalStepSize READ horizontalStepSize WRITE setHorizontalStepSize RESET
215 resetHorizontalStepSize NOTIFY horizontalStepSizeChanged FINAL)
216
225 Q_PROPERTY(Qt::KeyboardModifiers pageScrollModifiers READ pageScrollModifiers WRITE
226 setPageScrollModifiers RESET resetPageScrollModifiers NOTIFY
227 pageScrollModifiersChanged FINAL)
228
244 Q_PROPERTY(bool filterMouseEvents READ filterMouseEvents WRITE setFilterMouseEvents NOTIFY
245 filterMouseEventsChanged FINAL)
246
264 Q_PROPERTY(bool keyNavigationEnabled READ keyNavigationEnabled WRITE setKeyNavigationEnabled
265 NOTIFY keyNavigationEnabledChanged FINAL)
266
280 Q_PROPERTY(bool blockTargetWheel MEMBER m_blockTargetWheel NOTIFY blockTargetWheelChanged FINAL)
281
288 Q_PROPERTY(bool scrollFlickableTarget MEMBER m_scrollFlickableTarget NOTIFY
289 scrollFlickableTargetChanged FINAL)
290
291 Q_PROPERTY(
292 bool useAnimation READ useAnimation WRITE setUseAnimation NOTIFY useAnimationChanged FINAL)
293
294public:
295 explicit WheelHandler(QObject* parent = nullptr);
296 ~WheelHandler() override;
297
298 QQuickItem* target() const;
299 void setTarget(QQuickItem* target);
300
301 auto active() const -> bool;
302 void setActive(bool);
303 qreal verticalStepSize() const;
304 void setVerticalStepSize(qreal stepSize);
305 void resetVerticalStepSize();
306
307 qreal horizontalStepSize() const;
308 void setHorizontalStepSize(qreal stepSize);
309 void resetHorizontalStepSize();
310
311 Qt::KeyboardModifiers pageScrollModifiers() const;
312 void setPageScrollModifiers(Qt::KeyboardModifiers modifiers);
313 void resetPageScrollModifiers();
314
315 bool filterMouseEvents() const;
316 void setFilterMouseEvents(bool enabled);
317
318 bool keyNavigationEnabled() const;
319 void setKeyNavigationEnabled(bool enabled);
320
321 bool useAnimation() const;
322 void setUseAnimation(bool);
323
324 Q_INVOKABLE bool scrollUp(qreal stepSize = -1);
325 Q_INVOKABLE bool scrollDown(qreal stepSize = -1);
326
327 Q_INVOKABLE bool scrollLeft(qreal stepSize = -1);
328 Q_INVOKABLE bool scrollRight(qreal stepSize = -1);
329
330Q_SIGNALS:
331 void targetChanged();
332 void activeChanged();
333 void verticalStepSizeChanged();
334 void horizontalStepSizeChanged();
335 void pageScrollModifiersChanged();
336 void filterMouseEventsChanged();
337 void keyNavigationEnabledChanged();
338 void blockTargetWheelChanged();
339 void scrollFlickableTargetChanged();
340 void useAnimationChanged();
341
348 void wheel(KirigamiWheelEvent* wheel);
349 void wheelMoved();
350
351protected:
352 bool eventFilter(QObject* watched, QEvent* event) override;
353
354private Q_SLOTS:
355 void refreshAttach();
356 void attach();
357 void detach();
358 void rebindScrollBarV();
359 void rebindScrollBarH();
360
361private:
362 struct ScrollBar;
363 void rebindScrollBar(ScrollBar& scrollBar);
364 void classBegin() override;
365 void componentComplete() override;
366
367 void setScrolling(bool scrolling);
368 bool scrollFlickable(QPointF pixelDelta, QPointF angleDelta = {},
369 Qt::KeyboardModifiers modifiers = Qt::NoModifier);
370
371 QPointer<QQuickItem> m_target;
372
373 bool m_active;
374 QMetaObject::Connection m_verticalChangedConnection;
375 QMetaObject::Connection m_horizontalChangedConnection;
376
377 // Matches QScrollArea and QTextEdit
378 qreal m_defaultPixelStepSize = 20 * QGuiApplication::styleHints()->wheelScrollLines();
379 qreal m_verticalStepSize = m_defaultPixelStepSize;
380 qreal m_horizontalStepSize = m_defaultPixelStepSize;
381 bool m_explicitVStepSize = false;
382 bool m_explicitHStepSize = false;
383 bool m_wheelScrolling = false;
384 constexpr static qreal m_wheelScrollingDuration = 400;
385 bool m_filterMouseEvents = false;
386 bool m_keyNavigationEnabled = false;
387 bool m_blockTargetWheel = true;
388 bool m_scrollFlickableTarget = true;
389 // Same as QXcbWindow.
390 constexpr static Qt::KeyboardModifiers m_defaultHorizontalScrollModifiers = Qt::AltModifier;
391 // Same as QScrollBar/QAbstractSlider.
392 constexpr static Qt::KeyboardModifiers m_defaultPageScrollModifiers =
393 Qt::ControlModifier | Qt::ShiftModifier;
394 Qt::KeyboardModifiers m_pageScrollModifiers = m_defaultPageScrollModifiers;
395 QTimer m_wheelScrollingTimer;
396 KirigamiWheelEvent m_kirigamiWheelEvent;
397
398 // Smooth scrolling
399 QQmlEngine* m_engine = nullptr;
400 bool m_wasTouched = false;
401
402 struct Flickable {
403 QQmlProperty originX, originY;
404 QQmlProperty leftMargin, rightMargin;
405 QQmlProperty topMargin, bottomMargin;
406 QQmlProperty contentX, contentY;
407 QQmlProperty contentHeight, contentWidth;
408 QQmlProperty height, width;
409 QQmlProperty interactive;
410 } m_flickable;
411
412 struct ScrollBar {
413 QQmlProperty scrollBar;
414 QQmlProperty stepSize;
415 QMetaMethod increaseMethod;
416 QMetaMethod decreaseMethod;
417 QQuickItem* item = nullptr;
418 bool valid() const { return item != nullptr; };
419 } m_scrollBarV, m_scrollBarH;
420
421 bool m_useAnimation = false;
422};
Definition wheelhandler.h:25
QPointF pixelDelta
Definition wheelhandler.h:57
QPointF angleDelta
Definition wheelhandler.h:50
int buttons
Definition wheelhandler.h:65
bool inverted
Definition wheelhandler.h:84
qreal y
Definition wheelhandler.h:40
int modifiers
Definition wheelhandler.h:76
qreal x
Definition wheelhandler.h:33
bool accepted
Definition wheelhandler.h:110
Handles scrolling for a Flickable and 2 attached ScrollBars.
Definition wheelhandler.h:179