canvas_click_filter.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #include <QMouseEvent>
31 #include <QLineF>
32 #include <QDateTime>
34 
35 namespace mapviz_plugins
36 {
38  is_mouse_down_(false),
39  max_ms_(Q_INT64_C(500)),
40  max_distance_(2.0)
41  { }
42 
44  {
45  max_ms_ = max_ms;
46  }
47 
48  void CanvasClickFilter::setMaxClickMovement(qreal max_distance)
49  {
50  max_distance_ = max_distance;
51  }
52 
53  bool CanvasClickFilter::eventFilter(QObject* object, QEvent* event)
54  {
55  if (event->type() == QEvent::MouseButtonPress)
56  {
57  is_mouse_down_ = true;
58  QMouseEvent* me = static_cast<QMouseEvent*>(event);
59 #if QT_VERSION >= 0x050000
60  mouse_down_pos_ = me->localPos();
61 #else
62  mouse_down_pos_ = me->posF();
63 #endif
64  mouse_down_time_ = QDateTime::currentMSecsSinceEpoch();
65  }
66  else if (event->type() == QEvent::MouseButtonRelease)
67  {
68  if (is_mouse_down_)
69  {
70  QMouseEvent* me = static_cast<QMouseEvent*>(event);
71 
72 #if QT_VERSION >= 0x050000
73  qreal distance = QLineF(mouse_down_pos_, me->localPos()).length();
74 #else
75  qreal distance = QLineF(mouse_down_pos_, me->posF()).length();
76 #endif
77  qint64 msecsDiff = QDateTime::currentMSecsSinceEpoch() - mouse_down_time_;
78 
79  // Only fire the event if the mouse has moved less than the maximum distance
80  // and was held for shorter than the maximum time.. This prevents click
81  // events from being fired if the user is dragging the mouse across the map
82  // or just holding the cursor in place.
83  if (msecsDiff < max_ms_ && distance <= max_distance_)
84  {
85 #if QT_VERSION >= 0x050000
86  Q_EMIT pointClicked(me->localPos());
87 #else
88  Q_EMIT pointClicked(me->posF());
89 #endif
90  }
91  }
92  is_mouse_down_ = false;
93  }
94  return false;
95  }
96 }
void pointClicked(const QPointF &)
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
void setMaxClickMovement(qreal max_distance)
bool eventFilter(QObject *object, QEvent *event)


mapviz_plugins
Author(s): Marc Alban
autogenerated on Fri Mar 19 2021 02:44:32