Vidalia  0.3.1
TorMapWidgetInputHandler.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
12 
13 #include <MarbleWidget.h>
14 #include <MarbleMap.h>
15 #include <MarbleModel.h>
16 #include <ViewParams.h>
17 #include <ViewportParams.h>
18 
19 #include <QTimer>
20 #include <QMouseEvent>
21 #include <QWheelEvent>
22 #include <QPersistentModelIndex>
23 
24 using namespace Marble;
25 
26 
27 /** Amount to zoom in or out when responding to mouse double clicks. This
28  * value was taken from MarbleMap.cpp.
29  */
30 #define MAP_ZOOM_STEP 40
31 
32 /** Number of units the mouse must be clicked and dragged before it will
33  * force a map rotation and repaint.
34 */
35 #define MIN_DRAG_THRESHOLD 3
36 
37 
39  : MarbleWidgetInputHandler()
40 {
41 }
42 
43 bool
44 TorMapWidgetInputHandler::eventFilter(QObject *obj, QEvent *e)
45 {
46  Q_UNUSED(obj);
47 
48  QWheelEvent *wheelEvent = 0;
49  QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
50 
51  switch (e->type()) {
52  case QEvent::MouseButtonPress:
53  _mousePressedX = mouseEvent->x();
54  _mousePressedY = mouseEvent->y();
55  _mousePressedLon = m_widget->centerLongitude();
56  _mousePressedLat = m_widget->centerLatitude();
57 
58  if (pointHasFeatures(mouseEvent->pos()))
59  emit featureClicked(mouseEvent->pos(), mouseEvent->button());
60  else
61  m_widget->setCursor(Qt::ClosedHandCursor);
62  break;
63 
64  case QEvent::MouseButtonRelease:
65  if (! pointHasFeatures(mouseEvent->pos()))
66  m_widget->setCursor(Qt::OpenHandCursor);
67  else
68  m_widget->setCursor(Qt::PointingHandCursor);
69  break;
70 
71  case QEvent::MouseMove:
72  if (mouseEvent->buttons() & Qt::LeftButton) {
73  // Pan the map if the left button is pressed while dragging
74  int dx = mouseEvent->x() - _mousePressedX;
75  int dy = mouseEvent->y() - _mousePressedY;
76 
77  if (abs(dx) <= MIN_DRAG_THRESHOLD && abs(dy) <= MIN_DRAG_THRESHOLD)
78  return true;
79  m_widget->setCursor(Qt::ClosedHandCursor);
80 
81  qreal dir = 1;
82  if (m_widget->projection() == Spherical) {
83  if (m_widget->map()->viewParams()->viewport()->polarity() > 0) {
84  if (mouseEvent->y() < (-m_widget->northPoleY() + m_widget->height()/2))
85  dir = -1;
86  } else {
87  if (mouseEvent->y() > (+m_widget->northPoleY() + m_widget->height()/2))
88  dir = -1;
89  }
90  }
91 
92  qreal radius = (qreal)(m_widget->radius());
93  qreal lon = (qreal)(_mousePressedLon) - 90.0 * dir * dx / radius;
94  qreal lat = (qreal)(_mousePressedLat) + 90.0 * dy / radius;
95  m_widget->centerOn(lon, lat, false);
96 
97  return true;
98  } else {
99  // Change the mouse cursor if we're hovering over a relay placemark
100  if (pointHasFeatures(mouseEvent->pos()) > 0)
101  m_widget->setCursor(Qt::PointingHandCursor);
102  else
103  m_widget->setCursor(Qt::OpenHandCursor);
104  }
105  break;
106 
107  case QEvent::MouseButtonDblClick:
108  // Adjust the zoom level on the map
109  if (mouseEvent->button() == Qt::LeftButton) {
110  m_widget->zoomViewBy(MAP_ZOOM_STEP);
111  return true;
112  } else if (mouseEvent->button() == Qt::RightButton) {
113  m_widget->zoomViewBy(-MAP_ZOOM_STEP);
114  return true;
115  }
116  break;
117 
118  case QEvent::Wheel:
119  // Adjust the zoom level on the map
120  m_widget->setViewContext(Marble::Animation);
121 
122  wheelEvent = static_cast<QWheelEvent*>(e);
123  m_widget->zoomViewBy((int)(wheelEvent->delta() / 3));
124  m_mouseWheelTimer->start(400);
125  return true;
126 
127  default:
128  break;
129  }
130  return MarbleWidgetInputHandler::eventFilter(obj, e);
131 }
132 
133 bool
135 {
136  return (m_widget->model()->whichFeatureAt(point).size() > 0);
137 }
138 
virtual bool eventFilter(QObject *obj, QEvent *e)
#define MIN_DRAG_THRESHOLD
bool pointHasFeatures(const QPoint &point) const
void featureClicked(const QPoint &point, Qt::MouseButton button)
#define MAP_ZOOM_STEP