KDeclarative

fallbacktaphandler.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Marco Martin <[email protected]>
3  SPDX-FileCopyrightText: 2014-2015 Eike Hein <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "fallbacktaphandler.h"
9 
10 #include <QGuiApplication>
11 #include <QStyleHints>
12 
13 FallbackTapHandlerMouseEvent::FallbackTapHandlerMouseEvent(Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, qreal x, qreal y)
14  : QObject(nullptr),
15  m_button(button),
16  m_buttons(buttons),
17  m_modifiers(modifiers),
18  m_x(x),
19  m_y(y)
20 {}
21 
22 
23 
24 FallbackTapHandler::FallbackTapHandler(QQuickItem *parent)
25  : QQuickItem(parent)
26 {
27  setFiltersChildMouseEvents(true);
28  setAcceptedMouseButtons(Qt::LeftButton);
29 }
30 
31 FallbackTapHandler::~FallbackTapHandler()
32 {
33 }
34 
35 void FallbackTapHandler::setAcceptedButtons(Qt::MouseButtons buttons)
36 {
37  setAcceptedMouseButtons(buttons);
38  Q_EMIT acceptedButtonsChanged();
39 }
40 
41 bool FallbackTapHandler::childMouseEventFilter(QQuickItem *item, QEvent *event)
42 {
43  if (event->type() == QEvent::MouseButtonPress && item->inherits("QQuickFlickable")) {
44  QMouseEvent *me = static_cast<QMouseEvent *>(event);
45  if (!(me->button() & acceptedMouseButtons()) && !(me->buttons() & acceptedMouseButtons())) {
46  return QQuickItem::childMouseEventFilter(item, event);
47  }
48  m_mouseDownItem = item;
49  m_mouseDownPos = me->windowPos();
50 
51  QPointF mappedPoint = mapFromItem(item, me->pos());
52  FallbackTapHandlerMouseEvent eventProxy(me->button(), me->buttons(), me->modifiers(), mappedPoint.x(), mappedPoint.y());
53  Q_EMIT pressed(&eventProxy);
54 
55  } else if (event->type() == QEvent::MouseButtonRelease && item->inherits("QQuickFlickable")) {
56  QMouseEvent *me = static_cast<QMouseEvent *>(event);
57 
58  if ((!(me->button() & acceptedMouseButtons()) && !(me->buttons() & acceptedMouseButtons())) ||
59  m_mouseDownItem != item) {
60  return QQuickItem::childMouseEventFilter(item, event);
61  }
62 
63  QPointF mappedPoint = mapFromItem(item, me->pos());
64  FallbackTapHandlerMouseEvent eventProxy(me->button(), me->buttons(), me->modifiers(), mappedPoint.x(), mappedPoint.y());
65  Q_EMIT released(&eventProxy);
66 
67  if (QPointF(m_mouseDownPos - me->windowPos()).manhattanLength() < QGuiApplication::styleHints()->startDragDistance()) {
68  Q_EMIT tapped(&eventProxy);
69  }
70 
71  m_mouseDownItem.clear();
72  m_mouseDownPos = QPointF();
73  }
74 
75  return QQuickItem::childMouseEventFilter(item, event);
76 }
77 
78 void FallbackTapHandler::mousePressEvent(QMouseEvent *event)
79 {
80  if ((!(event->button() & acceptedMouseButtons()) && !(event->buttons() & acceptedMouseButtons()))) {
81  event->ignore();
82  return;
83  }
84 
85  event->accept();
86  m_mouseDownItem = this;
87  m_mouseDownPos = event->windowPos();
88 
89  FallbackTapHandlerMouseEvent eventProxy(event->button(), event->buttons(), event->modifiers(), event->x(), event->y());
90  Q_EMIT pressed(&eventProxy);
91 }
92 
93 void FallbackTapHandler::mouseReleaseEvent(QMouseEvent *event)
94 {
95  if ((!(event->button() & acceptedMouseButtons()) && !(event->buttons() & acceptedMouseButtons()))) {
96  event->ignore();
97  return;
98  }
99 
100  FallbackTapHandlerMouseEvent eventProxy(event->button(), event->buttons(), event->modifiers(), event->x(), event->y());
101 
102  if (QPointF(m_mouseDownPos - event->windowPos()).manhattanLength() < QGuiApplication::styleHints()->startDragDistance()) {
103  Q_EMIT tapped(&eventProxy);
104  }
105 
106  m_mouseDownItem.clear();
107  m_mouseDownPos = QPointF();
108 
109  Q_EMIT released(&eventProxy);
110 }
111 
112 #include "moc_fallbacktaphandler.cpp"
typedef KeyboardModifiers
MouseButtonPress
QEvent::Type type() const const
MouseButton
Qt::MouseButtons buttons() const const
virtual bool event(QEvent *e)
QStyleHints * styleHints()
qreal x() const const
qreal y() const const
bool inherits(const char *className) const const
Qt::MouseButton button() const const
const QPointF & windowPos() const const
Qt::KeyboardModifiers modifiers() const const
virtual bool childMouseEventFilter(QQuickItem *item, QEvent *event)
QPoint pos() const const
QObject * parent() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.