LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MouseEventEater.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2010-2013 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef MOUSEEVENTEATER_H
23 #define MOUSEEVENTEATER_H
24 
25 #include <QApplication>
26 #include <QGraphicsSceneMouseEvent>
27 #include <QMouseEvent>
28 #include <QObject>
29 #include <QWidget>
30 #include "HostBase.h"
31 #include "WindowServer.h"
32 
33 class MouseEventEater : public QObject
34 {
35  Q_OBJECT
36 
37 public:
38  MouseEventEater(QObject *parent = 0) : QObject(parent) {}
39 
40 protected:
41  virtual bool eventFilter(QObject *o, QEvent *e) {
42 
43 #if defined(TARGET_DESKTOP)
44  bool insideGestureStrip(false);
45 
46  static QString gestureStrip("GestureStrip");
47  QString objectClassName(o->metaObject()->className());
48 
49  if (objectClassName == gestureStrip) {
50  insideGestureStrip = true;
51  } else if (o->parent()) {
52  QString parentClassName(o->parent()->metaObject()->className());
53  if (parentClassName == gestureStrip) {
54  insideGestureStrip = true;
55  }
56  }
57 
58  if (insideGestureStrip) {
59  e->ignore();
60  return false;
61  }
62 #else
63  Q_UNUSED(o);
64 #endif
65 
66 
67  bool handled = false;
68 
69  if (e->type() == QEvent::MouseButtonRelease ||
70  e->type() == QEvent::MouseButtonPress ||
71  e->type() == QEvent::MouseButtonDblClick ||
72  e->type() == QEvent::MouseMove) {
73  QMouseEvent *me = static_cast<QMouseEvent *>(e);
74  HostInfo info = HostBase::instance()->getInfo();
75  int frameHeight = 0;
76  QWidget *w = QApplication::activeWindow();
77 
78  if (w) {
79  frameHeight = w->frameGeometry().height() -
80  w->geometry().height();
81  }
82 
83  // Try sending incoming mouse events to QGraphicsScene first, then
84  // filter out the ones not handled by the scene. They will be
85  // converted to touch events by Qt
86  if (me->pos().y() < (info.displayHeight + frameHeight)) {
87  QGraphicsScene *scene = WindowServer::instance()->scene();
88  QPointF pos(me->pos());
89 
90  QGraphicsSceneMouseEvent *event = 0;
91 
92  if (me->type() == QEvent::MouseButtonPress) {
93  event = new QGraphicsSceneMouseEvent(QEvent::GraphicsSceneMousePress);
94  } else if (me->type() == QEvent::MouseButtonRelease) {
95  event = new QGraphicsSceneMouseEvent(QEvent::GraphicsSceneMouseRelease);
96  } else if (me->type() == QEvent::MouseButtonDblClick) {
97  event = new QGraphicsSceneMouseEvent(QEvent::GraphicsSceneMouseDoubleClick);
98  } else if (me->type() == QEvent::MouseMove) {
99  event = new QGraphicsSceneMouseEvent(QEvent::GraphicsSceneMouseMove);
100  }
101 
102  if (event) {
103  event->setScenePos(pos);
104  event->setScreenPos(me->globalPos());
105  event->setButton(me->button());
106  event->setButtons(me->buttons());
107  event->setModifiers(me->modifiers());
108  handled = QApplication::sendEvent(scene, event);
109  delete event;
110  }
111 
112  if (handled) {
113  e->ignore();
114  }
115  }
116  }
117 
118  return handled;
119  }
120 };
121 
122 #endif /* MOUSEEVENTEATER_H */