00001 // ***************************************************************************** 00002 // 00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®) 00004 // All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above copyright 00011 // notice, this list of conditions and the following disclaimer in the 00012 // documentation and/or other materials provided with the distribution. 00013 // * Neither the name of Southwest Research Institute® (SwRI®) nor the 00014 // names of its contributors may be used to endorse or promote products 00015 // derived from this software without specific prior written permission. 00016 // 00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 // 00028 // ***************************************************************************** 00029 00030 #include <QMouseEvent> 00031 #include <QLineF> 00032 #include <QDateTime> 00033 #include "mapviz_plugins/canvas_click_filter.h" 00034 00035 namespace mapviz_plugins 00036 { 00037 CanvasClickFilter::CanvasClickFilter() : 00038 is_mouse_down_(false), 00039 max_ms_(Q_INT64_C(500)), 00040 max_distance_(2.0) 00041 { } 00042 00043 void CanvasClickFilter::setMaxClickTime(qint64 max_ms) 00044 { 00045 max_ms_ = max_ms; 00046 } 00047 00048 void CanvasClickFilter::setMaxClickMovement(qreal max_distance) 00049 { 00050 max_distance_ = max_distance; 00051 } 00052 00053 bool CanvasClickFilter::eventFilter(QObject* object, QEvent* event) 00054 { 00055 if (event->type() == QEvent::MouseButtonPress) 00056 { 00057 is_mouse_down_ = true; 00058 QMouseEvent* me = static_cast<QMouseEvent*>(event); 00059 mouse_down_pos_ = me->posF(); 00060 mouse_down_time_ = QDateTime::currentMSecsSinceEpoch(); 00061 } 00062 else if (event->type() == QEvent::MouseButtonRelease) 00063 { 00064 if (is_mouse_down_) 00065 { 00066 QMouseEvent* me = static_cast<QMouseEvent*>(event); 00067 00068 qreal distance = QLineF(mouse_down_pos_, me->posF()).length(); 00069 qint64 msecsDiff = QDateTime::currentMSecsSinceEpoch() - mouse_down_time_; 00070 00071 // Only fire the event if the mouse has moved less than the maximum distance 00072 // and was held for shorter than the maximum time.. This prevents click 00073 // events from being fired if the user is dragging the mouse across the map 00074 // or just holding the cursor in place. 00075 if (msecsDiff < max_ms_ && distance <= max_distance_) 00076 { 00077 Q_EMIT pointClicked(me->posF()); 00078 } 00079 } 00080 is_mouse_down_ = false; 00081 } 00082 return false; 00083 } 00084 }