• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • shortcuts
kgesturemap.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2006,2007 Andreas Hartmetz (ahartmetz@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 "kgesturemap.h"
21
22#include <kapplication.h>
23#include <kaction.h>
24#include <QtGui/QActionEvent>
25
26#include <kglobal.h>
27
28#include <kdebug.h>
29
30/*
31 This is a class for internal use by the KDE libraries only. This class
32 may change or go away without notice so don't try to use it in non-kdelibs
33 code.
34 */
35
36class KGestureMapContainer {
37public:
38 KGestureMap gestureMap;
39};
40
41
42K_GLOBAL_STATIC(KGestureMapContainer, g_instance)
43
44
45KGestureMap::~KGestureMap()
46{
47}
48
49
50KGestureMap *KGestureMap::self()
51{
52 return &g_instance->gestureMap;
53}
54
55
56KGestureMap::KGestureMap()
57{
58 m_gestureTimeout.setSingleShot(true);
59 connect(&m_gestureTimeout, SIGNAL(timeout()), this, SLOT(stopAcquisition()));
60 //It would be nice to install the filter on demand. Unfortunately,
61 //undesired behavior might result due to changing invocation
62 //orders of different event filters.
63 if (qApp)
64 qApp->installEventFilter(this);
65}
66
67
68void KGestureMap::addGesture(const KShapeGesture &gesture, KAction *act)
69{
70 if (!gesture.isValid() || !act)
71 return;
72 kDebug(283) << "KGestureMap::addGesture(KShapeGesture ...)";
73 if (!m_shapeGestures.contains(gesture))
74 m_shapeGestures.insert(gesture, act);
75 else
76 kDebug(283) << "Tried to register an action for a gesture already taken";
77}
78
79
80void KGestureMap::addGesture(const KRockerGesture &gesture, KAction *act)
81{
82 if (!gesture.isValid() || !act)
83 return;
84 kDebug(283) << "KGestureMap::addGesture(KRockerGesture ...)";
85 if (!m_rockerGestures.contains(gesture))
86 m_rockerGestures.insert(gesture, act);
87 else
88 kDebug(283) << "Tried to register an action for a gesture already taken";
89}
90
91
92void KGestureMap::removeGesture(const KShapeGesture &gesture, KAction *act)
93{
94 if (!gesture.isValid())
95 return;
96 kDebug(283) << "KGestureMap::removeGesture(KShapeGesture ...)";
97 KAction *oldAct = m_shapeGestures.value(gesture);
98 if (oldAct == act || !act /*wildcard*/)
99 m_shapeGestures.remove(gesture);
100}
101
102
103void KGestureMap::removeGesture(const KRockerGesture &gesture, KAction *act)
104{
105 if (!gesture.isValid())
106 return;
107 kDebug(283) << "KGestureMap::removeGesture(KRockerGesture ...)";
108 KAction *oldAct = m_rockerGestures.value(gesture);
109 if (oldAct == act || !act /*wildcard*/)
110 m_rockerGestures.remove(gesture);
111}
112
113
114KAction *KGestureMap::findAction(const KShapeGesture &gesture) const
115{
116 return m_shapeGestures.value(gesture);
117}
118
119
120KAction *KGestureMap::findAction(const KRockerGesture &gesture) const
121{
122 return m_rockerGestures.value(gesture);
123}
124
125
126void KGestureMap::installEventFilterOnMe(KApplication *app)
127{
128 app->installEventFilter(this);
129}
130
131
132inline int KGestureMap::bitCount(int n)
133{
134 int count = 0;
135 while (n) {
136 n &= (n - 1);
137 count++;
138 }
139 return count;
140}
141
142
143void KGestureMap::handleAction(KAction *kact)
144{
145 if (!kact)
146 return;
147 kDebug(283) << "handleAction";
148 //TODO: only activate in the action's context, just like keyboard shortcuts
149 kact->trigger();
150 return;
151}
152
153
154void KGestureMap::matchShapeGesture()
155{
156 //TODO: tune and tweak until satisfied with result :)
157 m_shapeGesture.setShape(m_points);
158 float dist, minDist = 20.0;
159 KAction *bestMatch = 0;
160
161 for (QHash<KShapeGesture, KAction *>::const_iterator it = m_shapeGestures.constBegin();
162 it != m_shapeGestures.constEnd(); ++it) {
163 dist = m_shapeGesture.distance(it.key(), 1000.0);
164 if (dist < minDist) {
165 minDist = dist;
166 bestMatch = it.value();
167 }
168 }
169 handleAction(bestMatch);
170}
171
172
173//slot
174void KGestureMap::stopAcquisition()
175{
176 m_gestureTimeout.stop();
177 m_acquiring = false;
178}
179
180
181//TODO: Probably kwin, kded and others should not have a gesture map.
182//Maybe making them friends and providing a private "die()" function would work.
183/*
184 * Act on rocker gestures immediately and collect movement data for evaluation.
185 * The decision when to consume and when to relay an event is quite tricky.
186 * I decided to only consume clicks that belong to completed rocker gestures.
187 * A user might e.g. go back in a browser several times using rocker gestures,
188 * thus changing what's under the cursor every time. This might lead to
189 * unintended clicks on links where there was free space before.
190 */
191
192bool KGestureMap::eventFilter(QObject *obj, QEvent *e)
193{
194 //disable until it does not interfere with other input any more
195 return false;
196 Q_UNUSED(obj);
197 int type = e->type();
198
199 //catch right-clicks disguised as context menu events. if we ignore a
200 //context menu event caused by a right-click, it should get resent
201 //as a right-click event, according to documentation.
202 //### this is preliminary
203 if (type == QEvent::ContextMenu) {
204 QContextMenuEvent *cme = static_cast<QContextMenuEvent *>(e);
205 if (cme->reason() == QContextMenuEvent::Mouse) {
206 cme->ignore();
207 return true;
208 }
209 return false;
210 }
211
212 if (type < QEvent::MouseButtonPress || type > QEvent::MouseMove)
213 return false;
214
215 QMouseEvent *me = static_cast<QMouseEvent *>(e);
216 if (type == QEvent::MouseButtonPress) {
217 int nButtonsDown = bitCount(me->buttons());
218 kDebug(283) << "number of buttons down:" << nButtonsDown;
219
220 //right button down starts gesture acquisition
221 if (nButtonsDown == 1 && me->button() == Qt::RightButton) {
222 //"startAcquisition()"
223 m_acquiring = true;
224 m_gestureTimeout.start(4000);
225 kDebug(283) << "========================";
226 m_points.clear();
227 m_points.append(me->pos());
228 return true;
229 } else if (nButtonsDown != 2)
230 return false;
231
232 //rocker gestures. do not trigger any movement gestures from now on.
233 stopAcquisition();
234 int buttonHeld = me->buttons() ^ me->button();
235 m_rockerGesture.setButtons(static_cast<Qt::MouseButton>(buttonHeld), me->button());
236 KAction *match = m_rockerGestures.value(m_rockerGesture);
237 if (!match)
238 return false;
239 handleAction(match);
240 return true;
241 }
242
243 if (m_acquiring) {
244 if (type == QEvent::MouseMove) {
245 m_points.append(me->pos());
246 //abort to avoid using too much memory. 1010 points should be enough
247 //for everyone! :)
248 //next reallocation of m_points would happen at 1012 items
249 if (m_points.size() > 1010)
250 stopAcquisition();
251 return true;
252 } else if (type == QEvent::MouseButtonRelease && me->button() == Qt::RightButton) {
253 stopAcquisition();
254
255 //TODO: pre-selection of gestures by length (optimization), if necessary
256 //possibly apply other heuristics
257 //then try all remaining gestures for sufficiently small distance
258 int dist = 0;
259 for (int i = 1; i < m_points.size(); i++) {
260 dist += (m_points[i] - m_points[i-1]).manhattanLength();
261 if (dist > 40) {
262 matchShapeGesture();
263 return true;
264 }
265 //this was probably a small glitch while right-clicking if we get here.
266 //TODO: open the context menu or do whatever happens on right-click (how?)
267 }
268 return false;
269 }
270 }
271 return false;
272}
273
274#include "kgesturemap.moc"
KAction
Class to encapsulate user-driven action or event.
Definition: kaction.h:217
KApplication
Controls and provides information to all KDE applications.
Definition: kapplication.h:83
KGestureMap
Definition: kgesturemap.h:35
KGestureMap::eventFilter
virtual bool eventFilter(QObject *obj, QEvent *e)
Definition: kgesturemap.cpp:192
KGestureMap::findAction
KAction * findAction(const KShapeGesture &gesture) const
Definition: kgesturemap.cpp:114
KGestureMap::self
static KGestureMap * self()
Definition: kgesturemap.cpp:50
KGestureMap::removeGesture
void removeGesture(const KShapeGesture &gesture, KAction *kact)
Definition: kgesturemap.cpp:92
KGestureMap::addGesture
void addGesture(const KShapeGesture &gesture, KAction *kact)
Definition: kgesturemap.cpp:68
KRockerGesture
Definition: kgesture.h:153
KRockerGesture::isValid
bool isValid() const
Return true if this gesture is valid.
Definition: kgesture.cpp:544
KRockerGesture::setButtons
void setButtons(Qt::MouseButton hold, Qt::MouseButton thenPush)
set button combination to trigger
Definition: kgesture.cpp:476
KShapeGesture
Definition: kgesture.h:38
KShapeGesture::setShape
void setShape(const QPolygon &shape)
Set the shape to draw to trigger this gesture.
Definition: kgesture.cpp:105
KShapeGesture::distance
float distance(const KShapeGesture &other, float abortThreshold) const
Return a difference measurement betwenn this gesture and the other gesture.
Definition: kgesture.cpp:225
KShapeGesture::isValid
bool isValid() const
Return true if this gesture is valid.
Definition: kgesture.cpp:158
QHash
QObject
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
kDebug
#define kDebug
kaction.h
kapplication.h
kdebug.h
kgesturemap.h
kglobal.h
timeout
int timeout
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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