Falkon Develop
Cross-platform Qt-based web browser
QjtMouseGestureFilter.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the mouse gesture package.
3 * Copyright (C) 2006 Johan Thelin <e8johan@gmail.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the
8 * following conditions are met:
9 *
10 * - Redistributions of source code must retain the above
11 * copyright notice, this list of conditions and the
12 * following disclaimer.
13 * - Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions and
15 * the following disclaimer in the documentation and/or
16 * other materials provided with the distribution.
17 * - The names of its contributors may be used to endorse
18 * or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
39#include "QjtMouseGesture.h"
40#include <QWidget>
41#include <QPainter>
42#include <QPixmap>
44
45/*
46 * Internal support class to bridge the
47 * toolkit independent mouse gesture
48 * recognizer and the Qt specific
49 * implementation.
50 */
52{
53public:
55 m_object = object;
56 }
57
58 void callback() override {
59 m_object->emitGestured();
60 }
61
62private:
63 QjtMouseGesture* m_object;
64};
65
66typedef QList<QjtMouseGesture*> GestureList;
67typedef QList<GestureCallbackToSignal> BridgeList;
68
69/*
70 * Private members of the QjtMouseGestureFilter class
71 */
73{
74public:
75 Qt::MouseButton gestureButton;
76 bool tracing;
77
79 QPixmap px;
82};
83
84/**********************************************************/
85
86QjtMouseGestureFilter::QjtMouseGestureFilter(bool allowDiagonals, Qt::MouseButton gestureButton, int minimumMovement, double minimumMatch, QObject* parent) : QObject(parent)
87{
88 d = new Private;
89
90 d->gestureButton = gestureButton;
91 d->tracing = false;
92
93 d->mgr = new Gesture::MouseGestureRecognizer(minimumMovement, minimumMatch, allowDiagonals);
94}
95
97{
98 delete d->mgr;
99 delete d;
100}
101
102/*
103 * Converts the DirectionList to a Gesture::DirecionList,
104 * creates a bridge and adds the gesture to the recognizer.
105 */
107{
109
110 for (DirectionList::const_iterator source = gesture->directions().constBegin(); source != gesture->directions().end(); ++source) {
111 dl.push_back(*source);
112 }
113
114 d->bridges.append(GestureCallbackToSignal(gesture));
115 d->gestures.append(gesture);
116
117 d->mgr->addGestureDefinition(Gesture::GestureDefinition(dl, &(d->bridges[ d->bridges.size() - 1 ])));
118}
119
121{
122 if (deleteGestures)
123 for (GestureList::const_iterator i = d->gestures.constBegin(); i != d->gestures.constEnd(); ++i) {
124 delete *i;
125 }
126
127 d->gestures.clear();
128 d->bridges.clear();
130}
131
132bool QjtMouseGestureFilter::eventFilter(QObject* obj, QEvent* event)
133{
134 switch (event->type()) {
135 case QEvent::MouseButtonPress:
136 if (mouseButtonPressEvent(static_cast<QMouseEvent*>(event), obj)) {
137 return true;
138 }
139
140 break;
141
142 case QEvent::MouseButtonRelease:
143 if (mouseButtonReleaseEvent(static_cast<QMouseEvent*>(event), obj)) {
144 return true;
145 }
146
147 break;
148
149 case QEvent::MouseMove:
150 if (mouseMoveEvent(static_cast<QMouseEvent*>(event), obj)) {
151 return true;
152 }
153
154 break;
155
156 case QEvent::Paint:
157 if (paintEvent(obj, static_cast<QPaintEvent*>(event))) {
158 return true;
159 }
160
161 default:
162 break;
163 }
164
165 return QObject::eventFilter(obj, event);
166}
167
168bool QjtMouseGestureFilter::mouseButtonPressEvent(QMouseEvent* event, QObject* obj)
169{
170 Q_UNUSED(obj)
171
172 if (event->button() == d->gestureButton) {
173// d->px = QPixmap::grabWidget(static_cast<QWidget*>(obj));
174 d->mgr->startGesture(event->position().toPoint().x(), event->position().toPoint().y());
175 d->tracing = true;
176 }
177
178 return false;
179}
180
181bool QjtMouseGestureFilter::mouseButtonReleaseEvent(QMouseEvent* event, QObject* obj)
182{
183 Q_UNUSED(obj)
184
185 if (d->tracing && event->button() == d->gestureButton) {
186 d->tracing = false;
187 return d->mgr->endGesture(event->position().toPoint().x(), event->position().toPoint().y());
188// d->px = QPixmap();
189// static_cast<QWidget*>(obj)->update();
190 }
191
192 return false;
193}
194
195bool QjtMouseGestureFilter::mouseMoveEvent(QMouseEvent* event, QObject* obj)
196{
197 Q_UNUSED(obj)
198
199 if (d->tracing) {
200 d->mgr->addPoint(event->position().toPoint().x(), event->position().toPoint().y());
201// static_cast<QWidget*>(obj)->update();
202 }
203
204 return false;
205}
206
207bool QjtMouseGestureFilter::paintEvent(QObject* obj, QPaintEvent* event)
208{
209 Q_UNUSED(event)
210 if (d->tracing) {
211 QWidget* wid = static_cast<QWidget*>(obj);
212 QPainter painter(wid);
213 painter.drawPixmap(0, 0, d->px);
214 const Gesture::PosList points = d->mgr->currentPath();
215 painter.save();
216 QPen pe;
217 pe.setColor(Qt::red);
218 pe.setWidth(2);
219 painter.setPen(pe);
220 QVector<QPoint> pointPairs;
221 for (Gesture::PosList::const_iterator iter = points.begin(); iter != points.end(); ++iter) {
222 pointPairs << QPoint(iter->x, iter->y);
223 }
224 painter.setRenderHint(QPainter::Antialiasing, true);
225 painter.drawPolyline(&pointPairs[0], pointPairs.count());
226 painter.restore();
227 painter.end();
228 return true;
229 }
230 else {
231 return false;
232 }
233}
QList< GestureCallbackToSignal > BridgeList
QList< QjtMouseGesture * > GestureList
void addGestureDefinition(const GestureDefinition &gesture)
GestureCallbackToSignal(QjtMouseGesture *object)
Gesture::MouseGestureRecognizer * mgr
bool mouseButtonReleaseEvent(QMouseEvent *event, QObject *obj=nullptr)
bool mouseMoveEvent(QMouseEvent *event, QObject *obj=nullptr)
QjtMouseGestureFilter(bool allowDiagonals=false, Qt::MouseButton gestureButton=Qt::RightButton, int minimumMovement=5, double minimumMatch=0.9, QObject *parent=nullptr)
void addGesture(QjtMouseGesture *gesture)
void clearGestures(bool deleteGestures=false)
bool eventFilter(QObject *obj, QEvent *event) override
bool mouseButtonPressEvent(QMouseEvent *event, QObject *obj=nullptr)
const DirectionList directions() const
std::vector< Pos > PosList
std::list< Direction > DirectionList
i
Definition: i18n.py:23