KDeclarative

eventgenerator.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Eike Hein <[email protected]>
3  SPDX-FileCopyrightText: 2015 Marco Martin <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "eventgenerator.h"
9 
10 #include <QGuiApplication>
11 #include <QQuickItem>
12 #include <QQuickWindow>
13 
14 EventGenerator::EventGenerator(QObject *parent) : QObject(parent)
15 {
16 }
17 
18 EventGenerator::~EventGenerator()
19 {
20 }
21 
22 void EventGenerator::sendMouseEvent(QQuickItem *item, EventGenerator::MouseEvent type, int x, int y, int button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
23 {
24  if (!item) {
25  return;
26  }
27 
28  QEvent::Type eventType;
29  switch (type) {
30  case MouseButtonPress:
31  eventType = QEvent::MouseButtonPress;
32  break;
33  case MouseButtonRelease:
34  eventType = QEvent::MouseButtonRelease;
35  break;
36  case MouseMove:
37  eventType = QEvent::MouseMove;
38  break;
39  default:
40  return;
41  }
42  QMouseEvent ev(eventType, QPointF(x, y), static_cast<Qt::MouseButton>(button), buttons, modifiers);
43 
44  QGuiApplication::sendEvent(item, &ev);
45 }
46 
47 void EventGenerator::sendMouseEventRecursive(QQuickItem *parentItem, EventGenerator::MouseEvent type, int x, int y, int button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
48 {
49  if (!parentItem) {
50  return;
51  }
52 
53  const QList<QQuickItem *> items = allChildItemsRecursive(parentItem);
54 
55  for (QQuickItem *item : items) {
56  sendMouseEvent(item, type, x, y, button, buttons, modifiers);
57  }
58 }
59 
60 void EventGenerator::sendWheelEvent(QQuickItem *item, int x, int y, const QPoint &pixelDelta, const QPoint &angleDelta, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
61 {
62  if (!item || !item->window()) {
63  return;
64  }
65 
66  QPointF pos(x, y);
67  QPointF globalPos(item->window()->mapToGlobal(item->mapToScene(pos).toPoint()));
68  QWheelEvent ev(pos, globalPos, pixelDelta, angleDelta, buttons, modifiers, Qt::ScrollUpdate, false /*not inverted*/);
69  QGuiApplication::sendEvent(item, &ev);
70 }
71 
72 void EventGenerator::sendWheelEventRecursive(QQuickItem *parentItem, int x, int y, const QPoint &pixelDelta, const QPoint &angleDelta, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
73 {
74  if (!parentItem) {
75  return;
76  }
77 
78  const QList<QQuickItem *> items = allChildItemsRecursive(parentItem);
79 
80  for (QQuickItem *item : items) {
81  sendWheelEvent(item, x, y, pixelDelta, angleDelta, buttons, modifiers);
82  }
83 }
84 
85 void EventGenerator::sendGrabEvent(QQuickItem *item, EventGenerator::GrabEvent type)
86 {
87  if (!item) {
88  return;
89  }
90 
91  switch (type) {
92  case GrabMouse:
93  item->grabMouse();
94  break;
95  case UngrabMouse: {
97  QGuiApplication::sendEvent(item, &ev);
98  return;
99  }
100  default:
101  return;
102  }
103 }
104 
105 void EventGenerator::sendGrabEventRecursive(QQuickItem *parentItem, EventGenerator::GrabEvent type)
106 {
107  if (!parentItem) {
108  return;
109  }
110 
111  const QList<QQuickItem *> items = allChildItemsRecursive(parentItem);
112 
113  for (QQuickItem *item : items) {
114  sendGrabEvent(item, type);
115  }
116 }
117 
118 QList<QQuickItem *> EventGenerator::allChildItemsRecursive(QQuickItem *parentItem)
119 {
120  QList<QQuickItem *> itemList;
121 
122  const auto childsItems = parentItem->childItems();
123  itemList.append(childsItems);
124 
125 
126  for (QQuickItem *childItem : childsItems) {
127  itemList.append(allChildItemsRecursive(childItem));
128  }
129 
130  return itemList;
131 }
typedef KeyboardModifiers
QList< QQuickItem * > childItems() const const
QPoint mapToGlobal(const QPoint &pos) const const
typedef MouseButtons
void append(const T &value)
void grabMouse()
QQuickWindow * window() const const
bool sendEvent(QObject *receiver, QEvent *event)
QPoint toPoint() const const
ScrollUpdate
QPointF mapToScene(const QPointF &point) const const
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.