Lomiri
Loading...
Searching...
No Matches
inputwatcher.cpp
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 */
17
18#include "inputwatcher.h"
19
20#include <QMouseEvent>
21
22InputWatcher::InputWatcher(QObject *parent)
23 : QObject(parent)
24 , m_mousePressed(false)
25 , m_touchPressed(false)
26{
27}
28
29QObject *InputWatcher::target() const
30{
31 return m_target;
32}
33
34void InputWatcher::setTarget(QObject *value)
35{
36 if (m_target == value) {
37 return;
38 }
39
40 if (m_target) {
41 m_target->removeEventFilter(this);
42 }
43
44 setMousePressed(false);
45 setTouchPressed(false);
46
47 m_target = value;
48 if (m_target) {
49 m_target->installEventFilter(this);
50 }
51
52 Q_EMIT targetChanged(value);
53}
54
55bool InputWatcher::targetPressed() const
56{
57 return m_mousePressed || m_touchPressed;
58}
59
60bool InputWatcher::eventFilter(QObject* /*watched*/, QEvent *event)
61{
62 switch (event->type()) {
63 case QEvent::TouchBegin:
64 setTouchPressed(true);
65 break;
66 case QEvent::TouchEnd:
67 setTouchPressed(false);
68 break;
69 case QEvent::MouseButtonPress:
70 {
71 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
72 if (mouseEvent->button() == Qt::LeftButton) {
73 setMousePressed(true);
74 }
75 }
76 break;
77 case QEvent::MouseButtonRelease:
78 {
79 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
80 if (mouseEvent->button() == Qt::LeftButton) {
81 setMousePressed(false);
82 }
83 }
84 break;
85 default:
86 // Not interested
87 break;
88 }
89
90 // We never filter them out. We are just watching.
91 return false;
92}
93
94void InputWatcher::setMousePressed(bool value)
95{
96 if (value == m_mousePressed) {
97 return;
98 }
99
100 bool oldPressed = targetPressed();
101 m_mousePressed = value;
102 if (targetPressed() != oldPressed) {
103 Q_EMIT targetPressedChanged(targetPressed());
104 }
105}
106
107void InputWatcher::setTouchPressed(bool value)
108{
109 if (value == m_touchPressed) {
110 return;
111 }
112
113 bool oldPressed = targetPressed();
114 m_touchPressed = value;
115 if (targetPressed() != oldPressed) {
116 Q_EMIT targetPressedChanged(targetPressed());
117 }
118}