Vidalia 0.2.12

TorMapWidgetInputHandler.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 #include "TorMapWidgetInputHandler.h"
00012 
00013 #include <MarbleWidget.h>
00014 #include <MarbleMap.h>
00015 #include <MarbleModel.h>
00016 #include <ViewParams.h>
00017 #include <ViewportParams.h>
00018 
00019 #include <QTimer>
00020 #include <QMouseEvent>
00021 #include <QWheelEvent>
00022 #include <QPersistentModelIndex>
00023 
00024 using namespace Marble;
00025 
00026 
00027 /** Amount to zoom in or out when responding to mouse double clicks. This
00028  * value was taken from MarbleMap.cpp.
00029  */
00030 #define MAP_ZOOM_STEP   40
00031 
00032 /** Number of units the mouse must be clicked and dragged before it will
00033  * force a map rotation and repaint.
00034 */
00035 #define MIN_DRAG_THRESHOLD 3
00036 
00037 
00038 TorMapWidgetInputHandler::TorMapWidgetInputHandler()
00039   : MarbleWidgetInputHandler()
00040 {
00041 }
00042 
00043 bool
00044 TorMapWidgetInputHandler::eventFilter(QObject *obj, QEvent *e)
00045 {
00046   Q_UNUSED(obj);
00047 
00048   QWheelEvent *wheelEvent = 0;
00049   QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
00050 
00051   switch (e->type()) {
00052     case QEvent::MouseButtonPress:
00053       _mousePressedX = mouseEvent->x();
00054       _mousePressedY = mouseEvent->y();
00055       _mousePressedLon = m_widget->centerLongitude();
00056       _mousePressedLat = m_widget->centerLatitude();
00057 
00058       if (pointHasFeatures(mouseEvent->pos()))
00059         emit featureClicked(mouseEvent->pos(), mouseEvent->button());
00060       else
00061         m_widget->setCursor(Qt::ClosedHandCursor);
00062       break;
00063 
00064     case QEvent::MouseButtonRelease:
00065       if (! pointHasFeatures(mouseEvent->pos()))
00066         m_widget->setCursor(Qt::OpenHandCursor);
00067       else
00068         m_widget->setCursor(Qt::PointingHandCursor);
00069       break;
00070 
00071     case QEvent::MouseMove:
00072       if (mouseEvent->buttons() & Qt::LeftButton) {
00073         // Pan the map if the left button is pressed while dragging
00074         int dx = mouseEvent->x() - _mousePressedX;
00075         int dy = mouseEvent->y() - _mousePressedY;
00076 
00077         if (abs(dx) <= MIN_DRAG_THRESHOLD && abs(dy) <= MIN_DRAG_THRESHOLD)
00078           return true;
00079         m_widget->setCursor(Qt::ClosedHandCursor);
00080 
00081         qreal dir = 1;
00082         if (m_widget->projection() == Spherical) {
00083           if (m_widget->map()->viewParams()->viewport()->polarity() > 0) {
00084             if (mouseEvent->y() < (-m_widget->northPoleY() + m_widget->height()/2))
00085               dir = -1;
00086           } else {
00087             if (mouseEvent->y() > (+m_widget->northPoleY() + m_widget->height()/2))
00088               dir = -1;
00089           }
00090         }
00091 
00092         qreal radius = (qreal)(m_widget->radius());
00093         qreal lon = (qreal)(_mousePressedLon) - 90.0 * dir * dx / radius;
00094         qreal lat = (qreal)(_mousePressedLat) + 90.0 * dy / radius;
00095         m_widget->centerOn(lon, lat, false);
00096 
00097         return true;
00098       } else {
00099         // Change the mouse cursor if we're hovering over a relay placemark
00100         if (pointHasFeatures(mouseEvent->pos()) > 0)
00101           m_widget->setCursor(Qt::PointingHandCursor);
00102         else
00103           m_widget->setCursor(Qt::OpenHandCursor);
00104       }
00105       break;
00106 
00107     case QEvent::MouseButtonDblClick:
00108       // Adjust the zoom level on the map
00109       if (mouseEvent->button() == Qt::LeftButton) {
00110         m_widget->zoomViewBy(MAP_ZOOM_STEP);
00111         return true;
00112       } else if (mouseEvent->button() == Qt::RightButton) {
00113         m_widget->zoomViewBy(-MAP_ZOOM_STEP);
00114         return true;
00115       }
00116       break;
00117 
00118     case QEvent::Wheel:
00119       // Adjust the zoom level on the map
00120       m_widget->setViewContext(Marble::Animation);
00121 
00122       wheelEvent = static_cast<QWheelEvent*>(e);
00123       m_widget->zoomViewBy((int)(wheelEvent->delta() / 3));
00124       m_mouseWheelTimer->start(400);
00125       return true;
00126 
00127     default:
00128       break;
00129   }
00130   return MarbleWidgetInputHandler::eventFilter(obj, e);
00131 }
00132 
00133 bool
00134 TorMapWidgetInputHandler::pointHasFeatures(const QPoint &point) const
00135 {
00136   return (m_widget->model()->whichFeatureAt(point).size() > 0);
00137 }
00138