• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

QtExtraComponents

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • qtextracomponents
mouseeventlistener.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 Marco Martin <notmart@gmail.com>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "mouseeventlistener.h"
21 
22 #include <QApplication>
23 #include <QEvent>
24 #include <QGraphicsSceneMouseEvent>
25 #include <QtCore/QTimer>
26 
27 #include <KDebug>
28 
29 static const int PressAndHoldDelay = 800;
30 
31 MouseEventListener::MouseEventListener(QDeclarativeItem *parent)
32  : QDeclarativeItem(parent),
33  m_pressed(false),
34  m_pressAndHoldEvent(0),
35  m_lastEvent(0),
36  m_containsMouse(false)
37 {
38  m_pressAndHoldTimer = new QTimer(this);
39  m_pressAndHoldTimer->setSingleShot(true);
40  connect(m_pressAndHoldTimer, SIGNAL(timeout()),
41  this, SLOT(handlePressAndHold()));
42 
43  qmlRegisterType<KDeclarativeMouseEvent>();
44  qmlRegisterType<KDeclarativeWheelEvent>();
45 
46  setFiltersChildEvents(true);
47  setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton|Qt::MidButton|Qt::XButton1|Qt::XButton2);
48 }
49 
50 MouseEventListener::~MouseEventListener()
51 {
52 }
53 
54 void MouseEventListener::setHoverEnabled(bool enable)
55 {
56  if (enable == acceptHoverEvents()) {
57  return;
58  }
59 
60  setAcceptHoverEvents(enable);
61  emit hoverEnabledChanged(enable);
62 }
63 
64 bool MouseEventListener::hoverEnabled() const
65 {
66  return acceptHoverEvents();
67 }
68 
69 void MouseEventListener::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
70 {
71  Q_UNUSED(event);
72 
73  m_containsMouse = true;
74  emit containsMouseChanged(true);
75 }
76 
77 void MouseEventListener::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
78 {
79  Q_UNUSED(event);
80 
81  m_containsMouse = false;
82  emit containsMouseChanged(false);
83 }
84 
85 bool MouseEventListener::containsMouse() const
86 {
87  return m_containsMouse;
88 }
89 
90 void MouseEventListener::mousePressEvent(QGraphicsSceneMouseEvent *me)
91 {
92  if (m_lastEvent == me) {
93  m_lastEvent = 0;
94  return;
95  }
96 
97  //FIXME: when a popup window is visible: a click anywhere hides it: but the old qgraphicswidget will continue to think it's under the mouse
98  //doesn't seem to be any good way to properly reset this.
99  //this msolution will still caused a missed click after the popup is gone, but gets the situation unblocked.
100  if (!isUnderMouse()) {
101  me->ignore();
102  return;
103  }
104 
105  KDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
106  m_pressAndHoldEvent = new KDeclarativeMouseEvent(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
107  emit pressed(&dme);
108  m_pressed = true;
109 
110  m_pressAndHoldTimer->start(PressAndHoldDelay);
111 }
112 
113 void MouseEventListener::mouseMoveEvent(QGraphicsSceneMouseEvent *me)
114 {
115  if (m_lastEvent == me) {
116  m_lastEvent = 0;
117  return;
118  }
119 
120  KDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
121  emit positionChanged(&dme);
122 }
123 
124 void MouseEventListener::mouseReleaseEvent(QGraphicsSceneMouseEvent *me)
125 {
126  if (m_lastEvent == me) {
127  m_lastEvent = 0;
128  return;
129  }
130 
131  KDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
132  m_pressed = false;
133  emit released(&dme);
134 
135  if (boundingRect().contains(me->pos()) && m_pressAndHoldTimer->isActive()) {
136  emit clicked(&dme);
137  m_pressAndHoldTimer->stop();
138  }
139 }
140 
141 void MouseEventListener::wheelEvent(QGraphicsSceneWheelEvent *we)
142 {
143  if (m_lastEvent == we) {
144  m_lastEvent = 0;
145  return;
146  }
147 
148  KDeclarativeWheelEvent dwe(we->pos(), we->screenPos(), we->delta(), we->buttons(), we->modifiers(), we->orientation());
149  emit wheelMoved(&dwe);
150 }
151 
152 void MouseEventListener::handlePressAndHold()
153 {
154  if (m_pressed) {
155  emit pressAndHold(m_pressAndHoldEvent);
156  //delete m_pressAndHoldEvent;
157  }
158 }
159 
160 
161 bool MouseEventListener::sceneEventFilter(QGraphicsItem *item, QEvent *event)
162 {
163  if (!isEnabled()) {
164  return false;
165  }
166 
167  // If we already processed this event for another child, don't do it again.
168  if (m_lastEvent == event) {
169  return false;
170  }
171 
172  switch (event->type()) {
173  case QEvent::GraphicsSceneMousePress: {
174  m_lastEvent = event;
175  QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event);
176  //the parent will receive events in its own coordinates
177  const QPointF myPos = item->mapToItem(this, me->pos());
178  KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
179  m_pressAndHoldEvent = new KDeclarativeMouseEvent(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
180  //kDebug() << "pressed in sceneEventFilter";
181  emit pressed(&dme);
182  m_pressed = true;
183 
184  m_pressAndHoldTimer->start(PressAndHoldDelay);
185  break;
186  }
187  case QEvent::GraphicsSceneMouseMove: {
188  m_lastEvent = event;
189  QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event);
190  const QPointF myPos = item->mapToItem(this, me->pos());
191  KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
192  //kDebug() << "positionChanged..." << dme.x() << dme.y();
193  m_pressAndHoldEvent = new KDeclarativeMouseEvent(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
194  emit positionChanged(&dme);
195  break;
196  }
197  case QEvent::GraphicsSceneMouseRelease: {
198  m_lastEvent = event;
199  QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event);
200  const QPointF myPos = item->mapToItem(this, me->pos());
201  KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
202  m_pressed = false;
203 
204  emit released(&dme);
205 
206  if (QPointF(me->pos() - me->buttonDownPos(me->button())).manhattanLength() <= QApplication::startDragDistance() && m_pressAndHoldTimer->isActive()) {
207  emit clicked(&dme);
208  m_pressAndHoldTimer->stop();
209  }
210  break;
211  }
212  case QEvent::GraphicsSceneWheel: {
213  m_lastEvent = event;
214  QGraphicsSceneWheelEvent *we = static_cast<QGraphicsSceneWheelEvent *>(event);
215  KDeclarativeWheelEvent dwe(we->pos(), we->screenPos(), we->delta(), we->buttons(), we->modifiers(), we->orientation());
216  emit wheelMoved(&dwe);
217  break;
218  }
219  default:
220  break;
221  }
222 
223  return QDeclarativeItem::sceneEventFilter(item, event);
224 }
225 
226 #include "mouseeventlistener.moc"
227 
MouseEventListener::MouseEventListener
MouseEventListener(QDeclarativeItem *parent=0)
Definition: mouseeventlistener.cpp:31
MouseEventListener::mouseReleaseEvent
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Definition: mouseeventlistener.cpp:124
QEvent
QEvent::type
Type type() const
MouseEventListener::containsMouse
bool containsMouse() const
QGraphicsSceneMouseEvent::buttonDownPos
QPointF buttonDownPos(Qt::MouseButton button) const
QGraphicsItem::mapToItem
QPointF mapToItem(const QGraphicsItem *item, const QPointF &point) const
QGraphicsSceneMouseEvent::modifiers
Qt::KeyboardModifiers modifiers() const
MouseEventListener::released
void released(KDeclarativeMouseEvent *mouse)
QGraphicsSceneWheelEvent
MouseEventListener::wheelMoved
void wheelMoved(KDeclarativeWheelEvent *wheel)
QGraphicsItem::setAcceptHoverEvents
void setAcceptHoverEvents(bool enabled)
QGraphicsItem::setFiltersChildEvents
void setFiltersChildEvents(bool enabled)
MouseEventListener::hoverLeaveEvent
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Definition: mouseeventlistener.cpp:77
MouseEventListener::hoverEnabledChanged
void hoverEnabledChanged(bool hoverEnabled)
QGraphicsSceneMouseEvent::screenPos
QPoint screenPos() const
QGraphicsItem
MouseEventListener::setHoverEnabled
void setHoverEnabled(bool enable)
Definition: mouseeventlistener.cpp:54
QGraphicsItem::setAcceptedMouseButtons
void setAcceptedMouseButtons(QFlags< Qt::MouseButton > buttons)
MouseEventListener::clicked
void clicked(KDeclarativeMouseEvent *mouse)
QPoint::x
int x() const
QPoint::y
int y() const
QGraphicsSceneWheelEvent::screenPos
QPoint screenPos() const
QPointF
MouseEventListener::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *event)
Definition: mouseeventlistener.cpp:90
QObject::event
virtual bool event(QEvent *e)
MouseEventListener::pressed
void pressed(KDeclarativeMouseEvent *mouse)
QPointF::x
qreal x() const
QPointF::y
qreal y() const
QEvent::ignore
void ignore()
QTimer
QGraphicsSceneMouseEvent
MouseEventListener::wheelEvent
void wheelEvent(QGraphicsSceneWheelEvent *event)
Definition: mouseeventlistener.cpp:141
MouseEventListener::containsMouseChanged
void containsMouseChanged(bool containsMouseChanged)
QGraphicsItem::sceneEventFilter
virtual bool sceneEventFilter(QGraphicsItem *watched, QEvent *event)
MouseEventListener::mouseMoveEvent
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: mouseeventlistener.cpp:113
QGraphicsSceneMouseEvent::button
Qt::MouseButton button() const
QGraphicsSceneHoverEvent
QTimer::stop
void stop()
QGraphicsSceneWheelEvent::modifiers
Qt::KeyboardModifiers modifiers() const
MouseEventListener::~MouseEventListener
~MouseEventListener()
Definition: mouseeventlistener.cpp:50
KDeclarativeMouseEvent
Definition: mouseeventlistener.h:25
MouseEventListener::sceneEventFilter
bool sceneEventFilter(QGraphicsItem *i, QEvent *e)
Definition: mouseeventlistener.cpp:161
QGraphicsSceneWheelEvent::orientation
Qt::Orientation orientation() const
MouseEventListener::pressAndHold
void pressAndHold(KDeclarativeMouseEvent *mouse)
QGraphicsItem::isEnabled
bool isEnabled() const
MouseEventListener::positionChanged
void positionChanged(KDeclarativeMouseEvent *mouse)
QGraphicsItem::boundingRect
virtual QRectF boundingRect() const =0
QGraphicsSceneMouseEvent::buttons
Qt::MouseButtons buttons() const
MouseEventListener::hoverEnterEvent
void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Definition: mouseeventlistener.cpp:69
MouseEventListener::hoverEnabled
bool hoverEnabled() const
QGraphicsSceneWheelEvent::pos
QPointF pos() const
QTimer::start
void start(int msec)
QDeclarativeItem
QGraphicsItem::isUnderMouse
bool isUnderMouse() const
QGraphicsSceneMouseEvent::pos
QPointF pos() const
mouseeventlistener.h
QGraphicsSceneWheelEvent::delta
int delta() const
QTimer::isActive
bool isActive() const
QGraphicsItem::acceptHoverEvents
bool acceptHoverEvents() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
PressAndHoldDelay
static const int PressAndHoldDelay
Definition: mouseeventlistener.cpp:29
QGraphicsItem::contains
virtual bool contains(const QPointF &point) const
KDeclarativeWheelEvent
Definition: mouseeventlistener.h:72
QApplication::startDragDistance
int startDragDistance()
QPointF::manhattanLength
qreal manhattanLength() const
QTimer::setSingleShot
void setSingleShot(bool singleShot)
QGraphicsSceneWheelEvent::buttons
Qt::MouseButtons buttons() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:49 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

QtExtraComponents

Skip menu "QtExtraComponents"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal