libosmscout 1.1.1
Loading...
Searching...
No Matches
InputHandler.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_CLIENT_QT_INPUTHANDLER_H
2#define OSMSCOUT_CLIENT_QT_INPUTHANDLER_H
3
4/*
5 OSMScout - a Qt backend for libosmscout and libosmscout-map
6 Copyright (C) 2016 Lukas Karas
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <QObject>
24#include <QVector2D>
25#include <QTouchEvent>
26#include <QTimer>
27#include <QElapsedTimer>
28#include <QQueue>
29
33
36
37namespace osmscout {
38
59 Q_OBJECT
60
61private:
62 enum TapRecState{
63 INACTIVE = 0,
64 PRESSED = 1, // timer started with hold interval, if expired - long-tap is emitted
65 RELEASED = 2, // timer started with tap interval, if expired - tap is emitted
66 PRESSED2 = 3, // timer started with hold interval, if expired - tap-long-tap
67 };
68
69 int startFingerId;
70 int startX;
71 int startY;
72
73 QTimer timer;
74 TapRecState state;
75 int holdIntervalMs;
76 int hold2IntervalMs;
77 int tapIntervalMs;
78 int moveTolerance;
79
80private slots:
81 void onTimeout();
82
83public:
85 state(INACTIVE),
86 holdIntervalMs(1000),
87 hold2IntervalMs(500),
88 tapIntervalMs(200),
89 moveTolerance(15)
90 {
91 timer.setSingleShot(true);
92 connect(&timer, &QTimer::timeout, this, &TapRecognizer::onTimeout);
93 }
94
95 ~TapRecognizer() override = default;
96
97 void touch(const QTouchEvent &event);
98
99 void setPhysicalDpi(double physicalDpi)
100 {
101 moveTolerance = physicalDpi / 10.0; // ~ 2.5 mm
102 }
103
104signals:
105 void tap(const QPoint p);
106 void doubleTap(const QPoint p);
107 void longTap(const QPoint p);
108 void tapLongTap(const QPoint p);
109 void tapAndDrag(const QPoint p);
110};
111
116{
117 QPointF pos;
118 QElapsedTimer timer;
119};
120
128class MoveAccumulator : public QObject{
129 Q_OBJECT
130
131private:
132 int memory; // ms
133 QQueue<AccumulatorEvent> events;
134 double factor;
135 double vectorLengthTreshold;
136
137public:
138
145 MoveAccumulator(int memory = 100, double factor = 4, double vectorLengthTreshold = 5):
146 memory(memory), factor(factor), vectorLengthTreshold(vectorLengthTreshold)
147 {
148 }
149 ~MoveAccumulator() override = default;
150
151 MoveAccumulator& operator+=(const QPointF p);
152 QVector2D collect();
153};
154
160class OSMSCOUT_CLIENT_QT_API MapView: public QObject
161{
162 Q_OBJECT
163
164 Q_PROPERTY(double lat READ GetLat CONSTANT)
165 Q_PROPERTY(double lon READ GetLon CONSTANT)
166 Q_PROPERTY(double angle READ GetAngle CONSTANT)
167 Q_PROPERTY(double mag READ GetMag CONSTANT)
168 Q_PROPERTY(uint32_t magLevel READ GetMagLevel CONSTANT)
169 Q_PROPERTY(double mapDpi READ GetMapDpi CONSTANT)
170
171public:
172 explicit inline MapView(QObject *parent=nullptr): QObject(parent) {}
173
174 MapView(QObject* parent,
175 const osmscout::GeoCoord& center,
176 const Bearing& angle,
177 const osmscout::Magnification& magnification,
178 double mapDpi)
179 :
180 QObject(parent),
181 center(center),
182 angle(angle),
185 {}
186
187 MapView(const osmscout::GeoCoord& center,
188 const Bearing& angle,
189 const osmscout::Magnification& magnification,
190 double mapDpi)
191 :
192 center(center),
193 angle(angle),
196 {}
197
203 MapView(const MapView& mv)
204 :
205 QObject(),
206 center(mv.center),
207 angle(mv.angle),
209 mapDpi(mv.mapDpi)
210 {}
211
212 ~MapView() override = default;
213
214 double GetLat() const{ return center.GetLat(); }
215 double GetLon() const{ return center.GetLon(); }
216 double GetAngle() const{ return angle.AsRadians(); }
217 double GetMag() const{ return magnification.GetMagnification(); }
218 double GetMagLevel() const{ return magnification.GetLevel(); }
219 double GetMapDpi() const{ return mapDpi; }
220
221 bool IsValid() const{ return mapDpi > 0; }
222
224 {
225 center = mv.center;
226 angle = mv.angle;
228 mapDpi = mv.mapDpi;
229
230 return *this;
231 }
232
233 osmscout::GeoCoord center;
234 Bearing angle; // canvas clockwise
235 osmscout::Magnification magnification;
236 double mapDpi{0};
237};
238
239inline bool operator==(const MapView& a, const MapView& b)
240{
241 return a.center == b.center && a.angle == b.angle && a.magnification == b.magnification && a.mapDpi == b.mapDpi;
242}
243inline bool operator!=(const MapView& a, const MapView& b)
244{
245 return ! (a == b);
246}
247
266 Q_OBJECT
267public:
268 explicit InputHandler(const MapView &view);
269 ~InputHandler() override = default;
270
271 virtual void painted();
272 virtual bool animationInProgress();
273
274 virtual bool showCoordinates(const osmscout::GeoCoord &coord, const osmscout::Magnification &magnification, const osmscout::Bearing &bearing);
275 virtual bool zoom(double zoomFactor, const QPoint &widgetPosition, const QRect &widgetDimension);
276 virtual bool move(const QVector2D &vector); // move vector in pixels
277 virtual bool rotateTo(double angle);
278 virtual bool rotateBy(double angleChange);
279 virtual bool touch(const QTouchEvent &event);
280 virtual bool currentPosition(bool locationValid, osmscout::GeoCoord currentPosition);
281 virtual bool vehiclePosition(const VehiclePosition &vehiclePosition, bool autoRotateMap);
282 virtual bool isLockedToPosition();
283 virtual bool isFollowVehicle();
284 virtual bool focusOutEvent(QFocusEvent *event);
285 virtual void widgetResized(const QSizeF &widgetSize);
286
287signals:
289
290protected:
292};
293
301 Q_OBJECT
302
303private:
304 QElapsedTimer animationStart;
305 QTimer timer;
306 MapView startMapView;
307 QVector2D _move;
308 osmscout::Magnification targetMagnification;
309 double targetAngle;
310 int animationDuration;
311
312 const int MOVE_ANIMATION_DURATION = 1000; // ms
313 const int ZOOM_ANIMATION_DURATION = 500; // ms
314 const int ROTATE_ANIMATION_DURATION = 1000; //ms
315 const int ANIMATION_TICK = 16;
316
317private slots:
318 void onTimeout();
319
320public:
321 explicit MoveHandler(const MapView &view);
322 ~MoveHandler() override = default;
323
324 bool animationInProgress() override;
325
332 bool moveNow(const QVector2D &vector); // move vector in pixels, without animation
333
334 bool zoom(double zoomFactor, const QPoint &widgetPosition, const QRect &widgetDimension) override;
335 bool move(const QVector2D &vector) override; // move vector in pixels
336 bool rotateTo(double angle) override;
337 bool rotateBy(double angleChange) override;
338 bool touch(const QTouchEvent &event) override;
339};
340
347{
348 Q_OBJECT
349private:
350 Magnification startMag;
351 QPoint gestureStart;
352 double zoomDistance;
353
354public:
360 ZoomGestureHandler(const MapView &view, const QPoint &p, double zoomDistance);
361 ~ZoomGestureHandler() override = default;
362
363 bool touch(const QTouchEvent &event) override;
364};
365
372 Q_OBJECT
373
374private:
375 QElapsedTimer animationStart;
376 QTimer timer;
377 MapView startMapView;
378 MapView targetMapView;
379 double angleDiff; // radians
380
381 double moveAnimationDuration;
382 double zoomAnimationDuration;
383
384 static constexpr int ANIMATION_DURATION = 1000; // ms
385 static constexpr int ANIMATION_TICK = 16; // ms
386
387private slots:
388 void onTimeout();
389
390public:
391 explicit JumpHandler(const MapView &view,
392 double moveAnimationDuration = (double)ANIMATION_DURATION,
393 double zoomAnimationDuration = (double)ANIMATION_DURATION);
394
395 ~JumpHandler() override = default;
396
397 bool animationInProgress() override;
398 bool showCoordinates(const osmscout::GeoCoord &coord, const osmscout::Magnification &magnification, const osmscout::Bearing &bearing) override;
399};
400
407 Q_OBJECT
408public:
409 explicit DragHandler(const MapView &view);
410 ~DragHandler() override = default;
411
412 bool animationInProgress() override;
413
414 bool zoom(double zoomFactor, const QPoint &widgetPosition, const QRect &widgetDimension) override;
415 bool move(const QVector2D &vector) override; // move vector in pixels
416 bool rotateBy(double angleChange) override;
417
418 bool touch(const QTouchEvent &event) override;
419
420private:
421 bool moving;
422 MapView startView;
423 int fingerId;
424 int startX;
425 int startY;
426 bool ended;
427 MoveAccumulator moveAccumulator;
428};
429
437 Q_OBJECT
438public:
440 ~MultitouchHandler() override = default;
441
442 bool animationInProgress() override;
443
444 bool zoom(double zoomFactor, const QPoint &widgetPosition, const QRect &widgetDimension) override;
445 bool move(const QVector2D &vector) override; // move vector in pixels
446 bool rotateBy(double angleChange) override;
447
448 bool touch(const QTouchEvent &event) override;
449
450private:
451 bool moving;
452 MapView startView;
453 bool initialized;
454 bool ended;
455 MoveAccumulator moveAccumulator;
456
457 // we take only first two touch points into account
458 QTouchEvent::TouchPoint startPointA;
459 QTouchEvent::TouchPoint startPointB;
460};
461
468 Q_OBJECT
469public:
470 LockHandler(const MapView &view, const QSizeF &widgetSize):
471 JumpHandler(view), window(widgetSize)
472 {};
473
474 ~LockHandler() override = default;
475
476 bool currentPosition(bool locationValid, osmscout::GeoCoord currentPosition) override;
477 bool showCoordinates(const osmscout::GeoCoord &coord, const osmscout::Magnification &magnification, const osmscout::Bearing &bearing) override;
478 bool isLockedToPosition() override;
479 bool focusOutEvent(QFocusEvent *event) override;
480 void widgetResized(const QSizeF &widgetSize) override;
481private:
482 QSizeF window;
483};
484
491Q_OBJECT
492public:
493 VehicleFollowHandler(const MapView &view, const QSizeF &widgetSize);
494 ~VehicleFollowHandler() override = default;
495
496 bool vehiclePosition(const VehiclePosition &vehiclePosition, bool autoRotateMap) override;
497 bool isLockedToPosition() override;
498 bool isFollowVehicle() override;
499 void widgetResized(const QSizeF &widgetSize) override;
500
501private:
502 QSizeF window;
503};
504
505}
506
507Q_DECLARE_METATYPE(osmscout::AccumulatorEvent)
508Q_DECLARE_METATYPE(osmscout::MapView)
509
510#endif /* OSMSCOUT_CLIENT_QT_INPUTHANDLER_H */
#define OSMSCOUT_CLIENT_QT_API
Definition ClientQtImportExport.h:45
~DragHandler() override=default
bool touch(const QTouchEvent &event) override
bool animationInProgress() override
bool zoom(double zoomFactor, const QPoint &widgetPosition, const QRect &widgetDimension) override
bool rotateBy(double angleChange) override
bool move(const QVector2D &vector) override
DragHandler(const MapView &view)
virtual bool rotateTo(double angle)
InputHandler(const MapView &view)
virtual bool rotateBy(double angleChange)
void viewChanged(const MapView &view)
virtual bool move(const QVector2D &vector)
~InputHandler() override=default
virtual bool touch(const QTouchEvent &event)
virtual void widgetResized(const QSizeF &widgetSize)
virtual bool vehiclePosition(const VehiclePosition &vehiclePosition, bool autoRotateMap)
virtual bool isLockedToPosition()
virtual void painted()
virtual bool isFollowVehicle()
virtual bool focusOutEvent(QFocusEvent *event)
virtual bool currentPosition(bool locationValid, osmscout::GeoCoord currentPosition)
virtual bool animationInProgress()
virtual bool zoom(double zoomFactor, const QPoint &widgetPosition, const QRect &widgetDimension)
virtual bool showCoordinates(const osmscout::GeoCoord &coord, const osmscout::Magnification &magnification, const osmscout::Bearing &bearing)
MapView view
Definition InputHandler.h:291
bool animationInProgress() override
JumpHandler(const MapView &view, double moveAnimationDuration=(double) ANIMATION_DURATION, double zoomAnimationDuration=(double) ANIMATION_DURATION)
~JumpHandler() override=default
bool showCoordinates(const osmscout::GeoCoord &coord, const osmscout::Magnification &magnification, const osmscout::Bearing &bearing) override
bool showCoordinates(const osmscout::GeoCoord &coord, const osmscout::Magnification &magnification, const osmscout::Bearing &bearing) override
bool isLockedToPosition() override
~LockHandler() override=default
bool focusOutEvent(QFocusEvent *event) override
bool currentPosition(bool locationValid, osmscout::GeoCoord currentPosition) override
void widgetResized(const QSizeF &widgetSize) override
LockHandler(const MapView &view, const QSizeF &widgetSize)
Definition InputHandler.h:470
Definition InputHandler.h:161
double GetAngle() const
Definition InputHandler.h:216
MapView(QObject *parent, const osmscout::GeoCoord &center, const Bearing &angle, const osmscout::Magnification &magnification, double mapDpi)
Definition InputHandler.h:174
double GetMagLevel() const
Definition InputHandler.h:218
double mag
Definition InputHandler.h:167
MapView(QObject *parent=nullptr)
Definition InputHandler.h:172
double GetLon() const
Definition InputHandler.h:215
MapView(const MapView &mv)
Definition InputHandler.h:203
osmscout::GeoCoord center
Definition InputHandler.h:233
double lon
Definition InputHandler.h:165
bool IsValid() const
Definition InputHandler.h:221
MapView & operator=(const MapView &mv)
Definition InputHandler.h:223
MapView(const osmscout::GeoCoord &center, const Bearing &angle, const osmscout::Magnification &magnification, double mapDpi)
Definition InputHandler.h:187
~MapView() override=default
double mapDpi
Definition InputHandler.h:169
double GetLat() const
Definition InputHandler.h:214
double angle
Definition InputHandler.h:166
double lat
Definition InputHandler.h:164
double GetMapDpi() const
Definition InputHandler.h:219
uint32_t magLevel
Definition InputHandler.h:168
osmscout::Magnification magnification
Definition InputHandler.h:235
double GetMag() const
Definition InputHandler.h:217
Definition InputHandler.h:128
MoveAccumulator & operator+=(const QPointF p)
~MoveAccumulator() override=default
MoveAccumulator(int memory=100, double factor=4, double vectorLengthTreshold=5)
Definition InputHandler.h:145
bool moveNow(const QVector2D &vector)
bool zoom(double zoomFactor, const QPoint &widgetPosition, const QRect &widgetDimension) override
MoveHandler(const MapView &view)
~MoveHandler() override=default
bool rotateBy(double angleChange) override
bool rotateTo(double angle) override
bool move(const QVector2D &vector) override
bool touch(const QTouchEvent &event) override
bool animationInProgress() override
~MultitouchHandler() override=default
bool animationInProgress() override
bool move(const QVector2D &vector) override
bool touch(const QTouchEvent &event) override
MultitouchHandler(const MapView &view)
bool rotateBy(double angleChange) override
bool zoom(double zoomFactor, const QPoint &widgetPosition, const QRect &widgetDimension) override
void tapAndDrag(const QPoint p)
void setPhysicalDpi(double physicalDpi)
Definition InputHandler.h:99
TapRecognizer()
Definition InputHandler.h:84
void tapLongTap(const QPoint p)
void longTap(const QPoint p)
void doubleTap(const QPoint p)
void tap(const QPoint p)
void touch(const QTouchEvent &event)
~TapRecognizer() override=default
void widgetResized(const QSizeF &widgetSize) override
bool isLockedToPosition() override
~VehicleFollowHandler() override=default
VehicleFollowHandler(const MapView &view, const QSizeF &widgetSize)
bool vehiclePosition(const VehiclePosition &vehiclePosition, bool autoRotateMap) override
Definition VehiclePosition.h:41
~ZoomGestureHandler() override=default
ZoomGestureHandler(const MapView &view, const QPoint &p, double zoomDistance)
bool touch(const QTouchEvent &event) override
Definition Area.h:39
bool operator!=(const MapView &a, const MapView &b)
Definition InputHandler.h:243
bool operator==(const MapView &a, const MapView &b)
Definition InputHandler.h:239
Definition InputHandler.h:116
QElapsedTimer timer
Definition InputHandler.h:118
QPointF pos
Definition InputHandler.h:117