on fcd86f3 help sign in
unity8/plugins/Utils/inputwatcher.cpp
114 lines 2.7 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
18#include "inputwatcher.h"
20#include <QMouseEvent>
22InputWatcher::InputWatcher(QObject *parent)
23 : QObject(parent)
24 , m_mousePressed(false)
25 , m_touchPressed(false)
29QObject *InputWatcher::target() const
31 return m_target;
34void InputWatcher::setTarget(QObject *value)
36 if (m_target == value) {
37 return;
38 }
40 if (m_target) {
41 m_target->removeEventFilter(this);
42 }
44 setMousePressed(false);
45 setTouchPressed(false);
47 m_target = value;
48 m_target->installEventFilter(this);
51bool InputWatcher::targetPressed() const
53 return m_mousePressed || m_touchPressed;
56bool InputWatcher::eventFilter(QObject* /*watched*/, QEvent *event)
58 switch (event->type()) {
59 case QEvent::TouchBegin:
60 setTouchPressed(true);
61 break;
62 case QEvent::TouchEnd:
63 setTouchPressed(false);
64 break;
65 case QEvent::MouseButtonPress:
66 {
67 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
68 if (mouseEvent->button() == Qt::LeftButton) {
69 setMousePressed(true);
70 }
71 }
72 break;
73 case QEvent::MouseButtonRelease:
74 {
75 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
76 if (mouseEvent->button() == Qt::LeftButton) {
77 setMousePressed(false);
78 }
79 }
80 break;
81 default:
82 // Not interested
83 break;
84 }
86 // We never filter them out. We are just watching.
87 return false;
90void InputWatcher::setMousePressed(bool value)
92 if (value == m_mousePressed) {
93 return;
94 }
96 bool oldPressed = targetPressed();
97 m_mousePressed = value;
98 if (targetPressed() != oldPressed) {
99 Q_EMIT targetPressedChanged(targetPressed());
100 }
103void InputWatcher::setTouchPressed(bool value)
105 if (value == m_touchPressed) {
106 return;
107 }
109 bool oldPressed = targetPressed();
110 m_touchPressed = value;
111 if (targetPressed() != oldPressed) {
112 Q_EMIT targetPressedChanged(targetPressed());
113 }