[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [vidalia/alpha] Port TorMapWidget and the rest to work with the latest Marble



commit 56dbd017aa1e8c5af851aafa7357ff90aaa70b0a
Author: Tomás Touceda <chiiph@xxxxxxxxxxxxxx>
Date:   Mon Feb 13 12:45:00 2012 -0300

    Port TorMapWidget and the rest to work with the latest Marble
---
 src/vidalia/CMakeLists.txt                       |    2 -
 src/vidalia/network/TorMapWidget.cpp             |  107 ++++++-----------
 src/vidalia/network/TorMapWidget.h               |    7 +-
 src/vidalia/network/TorMapWidgetInputHandler.cpp |  138 ----------------------
 src/vidalia/network/TorMapWidgetInputHandler.h   |   55 ---------
 src/vidalia/network/TorMapWidgetPopupMenu.cpp    |   37 ++----
 src/vidalia/network/TorMapWidgetPopupMenu.h      |   20 +---
 7 files changed, 58 insertions(+), 308 deletions(-)

diff --git a/src/vidalia/CMakeLists.txt b/src/vidalia/CMakeLists.txt
index 4a28504..315bf37 100644
--- a/src/vidalia/CMakeLists.txt
+++ b/src/vidalia/CMakeLists.txt
@@ -260,12 +260,10 @@ qt4_wrap_cpp(vidalia_SRCS
 if (USE_MARBLE)
   set(vidalia_SRCS ${vidalia_SRCS}
     network/TorMapWidget.cpp
-    network/TorMapWidgetInputHandler.cpp
     network/TorMapWidgetPopupMenu.cpp
   )
   qt4_wrap_cpp(vidalia_SRCS
     network/TorMapWidget.h
-    network/TorMapWidgetInputHandler.h
     network/TorMapWidgetPopupMenu.h
   )
 else(USE_MARBLE)
diff --git a/src/vidalia/network/TorMapWidget.cpp b/src/vidalia/network/TorMapWidget.cpp
index e222218..aa8abd7 100644
--- a/src/vidalia/network/TorMapWidget.cpp
+++ b/src/vidalia/network/TorMapWidget.cpp
@@ -14,11 +14,15 @@
 */
 
 #include "TorMapWidget.h"
-#include "TorMapWidgetInputHandler.h"
 #include "TorMapWidgetPopupMenu.h"
 #include "Vidalia.h"
 
 #include <MarbleModel.h>
+#include <MarbleWidgetInputHandler.h>
+#include <GeoDataPlacemark.h>
+#include <GeoDataTreeModel.h>
+#include <GeoDataDocument.h>
+#include <GeoDataFolder.h>
 #include <HttpDownloadManager.h>
 
 #include <QStringList>
@@ -26,8 +30,8 @@
 using namespace Marble;
 
 /** QPens to use for drawing different map elements */
-#define CIRCUIT_NORMAL_PEN      QPen(Qt::blue,  2.0)
-#define CIRCUIT_SELECTED_PEN    QPen(Qt::green, 3.0)
+#define CIRCUIT_NORMAL_PEN      QPen(QBrush(QColor(0,51,102)),  2.0)
+#define CIRCUIT_SELECTED_PEN    QPen(QBrush(QColor(65,146,75)), 3.0)
 
 
 /** Default constructor */
@@ -42,22 +46,24 @@ TorMapWidget::TorMapWidget(QWidget *parent)
 
   model()->downloadManager()->setDownloadEnabled(false);
 
-  TorMapWidgetInputHandler *handler = new TorMapWidgetInputHandler();
+  _document = new Marble::GeoDataDocument();
+  _folder = new Marble::GeoDataFolder();
+  model()->treeModel()->addDocument(_document);
+  model()->treeModel()->addFeature(_document, _folder);
+
   TorMapWidgetPopupMenu *popupMenu  = new TorMapWidgetPopupMenu(this);
 
-  connect(handler, SIGNAL(featureClicked(QPoint,Qt::MouseButton)),
-          popupMenu, SLOT(featureClicked(QPoint,Qt::MouseButton)));
-  connect(popupMenu, SIGNAL(displayRouterInfo(QString)),
-          this, SIGNAL(displayRouterInfo(QString)));
+  // Properly disable all right click menus
+  inputHandler()->setMouseButtonPopupEnabled(Qt::RightButton, false);
 
-  /* We can't call setInputHandler() until MarbleWidget has called its
-   * internal _q_initGui() method, which doesn't happen until a
-   * QTimer::singleShot(0, this, SLOT(_q_initGui())) timer set in its
-   * constructor times out. So force that event to process now. */ 
-  vApp->processEvents(QEventLoop::ExcludeUserInputEvents
-                        | QEventLoop::ExcludeSocketNotifiers);
+  // Hack to disable Marble's menus
+  disconnect(inputHandler(), SIGNAL(lmbRequest(int,int)), 
+             0,0);
 
-  setInputHandler(handler);
+  connect(inputHandler(), SIGNAL(lmbRequest(int,int)),
+          popupMenu, SLOT(featureLeftClicked(int,int)));
+  connect(popupMenu, SIGNAL(displayRouterInfo(QString)),
+          this, SIGNAL(displayRouterInfo(QString)));
 }
 
 /** Destructor */
@@ -70,38 +76,23 @@ TorMapWidget::~TorMapWidget()
 void
 TorMapWidget::addRouter(const RouterDescriptor &desc, const GeoIpRecord &geoip)
 {
-  QString kml;
   qreal lon = geoip.longitude();
   qreal lat = geoip.latitude();
   quint64 bw;
-  
+
   bw = qMin(desc.averageBandwidth(), desc.burstBandwidth());
   bw = qMin(bw, desc.observedBandwidth());
 
-  kml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
-             "<kml xmlns=\"http://earth.google.com/kml/2.0\";>"
-             "<Document>"
-             "  <Style id=\"normalPlacemark\">"
-             "    <IconStyle><Icon><href>:/images/icons/placemark-relay.png</href></Icon></IconStyle>"
-             "  </Style>"
-             );
-
-  kml.append("<Placemark>");
-  kml.append("<styleUrl>#normalPlacemark</styleUrl>");
-  kml.append(QString("<name>%1</name>").arg(desc.name()));
-  kml.append(QString("<description>%1</description>").arg(desc.id()));
-  kml.append(QString("<role>1</role>"));
-  kml.append(QString("<address>%1</address>").arg(geoip.toString()));
-  kml.append(QString("<CountryNameCode>%1</CountryNameCode>").arg(geoip.country()));
-  kml.append(QString("<pop>%1</pop>").arg(10 * bw));
-  kml.append(QString("<Point>"
-                     "  <coordinates>%1,%2</coordinates>"
-                     "</Point>").arg(lon).arg(lat));
-  kml.append("</Placemark>");
-  kml.append("</Document></kml>");
-
   QString id = desc.id();
-  addPlacemarkData(kml, id);
+
+  GeoDataPlacemark *pm = new GeoDataPlacemark(desc.name());
+  pm->setDescription(desc.id());
+  pm->setRole("1");
+  pm->setAddress(geoip.toString());
+  pm->setCountryCode(geoip.country());
+  pm->setPopularity(10 * bw);
+  pm->setCoordinate(lon, lat, 0.0, GeoDataCoordinates::Degree);
+  model()->treeModel()->addFeature(_folder, pm);
   _routers.insert(id, GeoDataCoordinates(lon, lat, 0.0,
                                          GeoDataCoordinates::Degree));
 }
@@ -154,19 +145,6 @@ TorMapWidget::removeCircuit(const CircuitId &circid)
   repaint();
 }
 
-/** Selects and highlights the router on the map. */
-void
-TorMapWidget::selectRouter(const QString &id)
-{
-#if 0
-  if (_routers.contains(id)) {
-    QPair<QPointF, bool> *routerPair = _routers.value(id);
-    routerPair->second = true;
-  }
-  repaint();
-#endif
-}
-
 /** Selects and highlights the circuit with the id <b>circid</b> 
  * on the map. */
 void
@@ -184,13 +162,6 @@ TorMapWidget::selectCircuit(const CircuitId &circid)
 void
 TorMapWidget::deselectAll()
 {
-#if 0
-  /* Deselect all router points */
-  foreach (QString router, _routers.keys()) {
-    QPair<QPointF,bool> *routerPair = _routers.value(router);
-    routerPair->second = false;
-  }
-#endif
   /* Deselect all circuit paths */
   foreach (CircuitGeoPath *path, _circuits.values()) {
     path->second = false;
@@ -204,7 +175,7 @@ void
 TorMapWidget::clear()
 {
   foreach (QString id, _routers.keys()) {
-    removePlacemarkKey(id);
+    model()->removeGeoData(id);
   }
 
   foreach (CircuitId circid, _circuits.keys()) {
@@ -234,18 +205,10 @@ TorMapWidget::zoomToFit()
 void
 TorMapWidget::zoomToCircuit(const CircuitId &circid)
 {
-#if 0
   if (_circuits.contains(circid)) {
-    QPair<QPainterPath*,bool> *pair = _circuits.value(circid);
-    QRectF rect = ((QPainterPath *)pair->first)->boundingRect();
-    if (!rect.isNull()) {
-      float zoomLevel = 1.0 - qMax(rect.height()/float(MAP_HEIGHT),
-                                   rect.width()/float(MAP_WIDTH));
-
-      zoom(rect.center().toPoint(), zoomLevel+0.2);
-    }
+    CircuitGeoPath *pair = _circuits.value(circid);
+    centerOn(pair->first.latLonAltBox(), true);
   }
-#endif
 }
 
 /** Zooms in on the router with the given <b>id</b>. */
@@ -257,7 +220,7 @@ TorMapWidget::zoomToRouter(const QString &id)
     GeoDataCoordinates coords = _routers.value(id);
     coords.geoCoordinates(lon, lat, GeoDataPoint::Degree);
 
-    zoomView(maximumZoom());
+    zoomView(zoomFromDistance(1000));
     centerOn(lon, lat, true);
   }
 }
diff --git a/src/vidalia/network/TorMapWidget.h b/src/vidalia/network/TorMapWidget.h
index f371198..bfc946b 100644
--- a/src/vidalia/network/TorMapWidget.h
+++ b/src/vidalia/network/TorMapWidget.h
@@ -33,6 +33,8 @@
 
 typedef QPair<Marble::GeoDataLineString, bool> CircuitGeoPath;
 
+class Marble::GeoDataDocument;
+class Marble::GeoDataFolder;
 
 class TorMapWidget : public Marble::MarbleWidget
 {
@@ -48,8 +50,6 @@ public:
   void addRouter(const RouterDescriptor &desc, const GeoIpRecord &geoip);
   /** Plots the given circuit on the map. */
   void addCircuit(const CircuitId &circid, const QStringList &path);
-  /** Selects and hightlights a router on the map. */
-  void selectRouter(const QString &id);
   /** Selects and highlights a circuit on the map. */
   void selectCircuit(const CircuitId &circid);
 
@@ -82,6 +82,9 @@ private:
   QHash<QString, Marble::GeoDataCoordinates> _routers;
   /** Stores circuit information */
   QHash<CircuitId, CircuitGeoPath*> _circuits;
+
+  Marble::GeoDataDocument *_document;
+  Marble::GeoDataFolder *_folder;
 };
 
 #endif
diff --git a/src/vidalia/network/TorMapWidgetInputHandler.cpp b/src/vidalia/network/TorMapWidgetInputHandler.cpp
deleted file mode 100644
index 467594b..0000000
--- a/src/vidalia/network/TorMapWidgetInputHandler.cpp
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
-**  This file is part of Vidalia, and is subject to the license terms in the
-**  LICENSE file, found in the top level directory of this distribution. If you
-**  did not receive the LICENSE file with this file, you may obtain it from the
-**  Vidalia source package distributed by the Vidalia Project at
-**  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
-**  including this file, may be copied, modified, propagated, or distributed 
-**  except according to the terms described in the LICENSE file.
-*/
-
-#include "TorMapWidgetInputHandler.h"
-
-#include <MarbleWidget.h>
-#include <MarbleMap.h>
-#include <MarbleModel.h>
-#include <ViewParams.h>
-#include <ViewportParams.h>
-
-#include <QTimer>
-#include <QMouseEvent>
-#include <QWheelEvent>
-#include <QPersistentModelIndex>
-
-using namespace Marble;
-
-
-/** Amount to zoom in or out when responding to mouse double clicks. This
- * value was taken from MarbleMap.cpp.
- */
-#define MAP_ZOOM_STEP   40
-
-/** Number of units the mouse must be clicked and dragged before it will
- * force a map rotation and repaint.
-*/
-#define MIN_DRAG_THRESHOLD 3
-
-
-TorMapWidgetInputHandler::TorMapWidgetInputHandler()
-  : MarbleWidgetInputHandler()
-{
-}
-
-bool
-TorMapWidgetInputHandler::eventFilter(QObject *obj, QEvent *e)
-{
-  Q_UNUSED(obj);
-
-  QWheelEvent *wheelEvent = 0;
-  QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
-
-  switch (e->type()) {
-    case QEvent::MouseButtonPress:
-      _mousePressedX = mouseEvent->x();
-      _mousePressedY = mouseEvent->y();
-      _mousePressedLon = m_widget->centerLongitude();
-      _mousePressedLat = m_widget->centerLatitude();
-
-      if (pointHasFeatures(mouseEvent->pos()))
-        emit featureClicked(mouseEvent->pos(), mouseEvent->button());
-      else
-        m_widget->setCursor(Qt::ClosedHandCursor);
-      break;
-
-    case QEvent::MouseButtonRelease:
-      if (! pointHasFeatures(mouseEvent->pos()))
-        m_widget->setCursor(Qt::OpenHandCursor);
-      else
-        m_widget->setCursor(Qt::PointingHandCursor);
-      break;
-
-    case QEvent::MouseMove:
-      if (mouseEvent->buttons() & Qt::LeftButton) {
-        // Pan the map if the left button is pressed while dragging
-        int dx = mouseEvent->x() - _mousePressedX;
-        int dy = mouseEvent->y() - _mousePressedY;
-
-        if (abs(dx) <= MIN_DRAG_THRESHOLD && abs(dy) <= MIN_DRAG_THRESHOLD)
-          return true;
-        m_widget->setCursor(Qt::ClosedHandCursor);
-
-        qreal dir = 1;
-        if (m_widget->projection() == Spherical) {
-          if (m_widget->map()->viewParams()->viewport()->polarity() > 0) {
-            if (mouseEvent->y() < (-m_widget->northPoleY() + m_widget->height()/2))
-              dir = -1;
-          } else {
-            if (mouseEvent->y() > (+m_widget->northPoleY() + m_widget->height()/2))
-              dir = -1;
-          }
-        }
-
-        qreal radius = (qreal)(m_widget->radius());
-        qreal lon = (qreal)(_mousePressedLon) - 90.0 * dir * dx / radius;
-        qreal lat = (qreal)(_mousePressedLat) + 90.0 * dy / radius;
-        m_widget->centerOn(lon, lat, false);
-
-        return true;
-      } else {
-        // Change the mouse cursor if we're hovering over a relay placemark
-        if (pointHasFeatures(mouseEvent->pos()) > 0)
-          m_widget->setCursor(Qt::PointingHandCursor);
-        else
-          m_widget->setCursor(Qt::OpenHandCursor);
-      }
-      break;
-
-    case QEvent::MouseButtonDblClick:
-      // Adjust the zoom level on the map
-      if (mouseEvent->button() == Qt::LeftButton) {
-        m_widget->zoomViewBy(MAP_ZOOM_STEP);
-        return true;
-      } else if (mouseEvent->button() == Qt::RightButton) {
-        m_widget->zoomViewBy(-MAP_ZOOM_STEP);
-        return true;
-      }
-      break;
-
-    case QEvent::Wheel:
-      // Adjust the zoom level on the map
-      m_widget->setViewContext(Marble::Animation);
-
-      wheelEvent = static_cast<QWheelEvent*>(e);
-      m_widget->zoomViewBy((int)(wheelEvent->delta() / 3));
-      m_mouseWheelTimer->start(400);
-      return true;
-
-    default:
-      break;
-  }
-  return MarbleWidgetInputHandler::eventFilter(obj, e);
-}
-
-bool
-TorMapWidgetInputHandler::pointHasFeatures(const QPoint &point) const
-{
-  return (m_widget->model()->whichFeatureAt(point).size() > 0);
-}
-
diff --git a/src/vidalia/network/TorMapWidgetInputHandler.h b/src/vidalia/network/TorMapWidgetInputHandler.h
deleted file mode 100644
index 066a4dc..0000000
--- a/src/vidalia/network/TorMapWidgetInputHandler.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
-**  This file is part of Vidalia, and is subject to the license terms in the
-**  LICENSE file, found in the top level directory of this distribution. If you
-**  did not receive the LICENSE file with this file, you may obtain it from the
-**  Vidalia source package distributed by the Vidalia Project at
-**  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
-**  including this file, may be copied, modified, propagated, or distributed 
-**  except according to the terms described in the LICENSE file.
-*/
-
-#ifndef _TORMAPWIDGETINPUTHANDLER_H
-#define _TORMAPWIDGETINPUTHANDLER_H
-
-#include "MarbleWidgetInputHandler.h"
-
-#include <QEvent>
-#include <QObject>
-#include <QPoint>
-
-
-class TorMapWidgetInputHandler : public Marble::MarbleWidgetInputHandler
-{
-  Q_OBJECT
-
-public:
-  /** Default constructor.
-   */
-  TorMapWidgetInputHandler();
-
-signals:
-  /** Emitted when the user clicks on a map feature located at <b>point</b>.
-   * <b>button</b> indicates which mouse button was clicked.
-   */
-  void featureClicked(const QPoint &point, Qt::MouseButton button);
-
-protected:
-  /** Filter and handles event <b>e</b> that was sent to widget <b>obj</b>.
-   * <b>obj</b> is always a MarbleWidget object.
-   */
-  virtual bool eventFilter(QObject *obj, QEvent *e);
-
-private:
-  /** Returns true if the map has one or more features located at the screen
-   * position <b>point</b>.
-   */
-  bool pointHasFeatures(const QPoint &point) const;
-
-  int   _mousePressedX;
-  int   _mousePressedY;
-  qreal _mousePressedLon;
-  qreal _mousePressedLat;
-};
-
-#endif
-
diff --git a/src/vidalia/network/TorMapWidgetPopupMenu.cpp b/src/vidalia/network/TorMapWidgetPopupMenu.cpp
index a98c7e6..983b16e 100644
--- a/src/vidalia/network/TorMapWidgetPopupMenu.cpp
+++ b/src/vidalia/network/TorMapWidgetPopupMenu.cpp
@@ -18,6 +18,7 @@
 
 #include <MarbleModel.h>
 #include <MarblePlacemarkModel.h>
+#include <MarbleWidgetInputHandler.h>
 
 #include <QChar>
 #include <QVector>
@@ -31,41 +32,29 @@ TorMapWidgetPopupMenu::TorMapWidgetPopupMenu(TorMapWidget *widget)
     _widget(widget)
 {
   _leftClickMenu = new QMenu(widget);
+
+  _widget->inputHandler()->setMouseButtonPopupEnabled(Qt::LeftButton, true);
+
   connect(_leftClickMenu, SIGNAL(triggered(QAction*)),
           this, SLOT(relaySelected(QAction*)));
 }
 
 void
-TorMapWidgetPopupMenu::featureClicked(const QPoint &pos, Qt::MouseButton btn)
+TorMapWidgetPopupMenu::featureLeftClicked(int x, int y)
 {
-  switch (btn) {
-    case Qt::LeftButton:
-      featureLeftClicked(pos);
-      break;
-
-    case Qt::RightButton:
-      break;
-
-    default:
-      break;
-  }
-}
+  QVector<const GeoDataPlacemark*> features = _widget->whichFeatureAt(QPoint(x,y));
+  QVector<const GeoDataPlacemark*>::const_iterator it = features.constBegin();
+  QVector<const GeoDataPlacemark*>::const_iterator const itEnd = features.constEnd();
 
-void
-TorMapWidgetPopupMenu::featureLeftClicked(const QPoint &pos)
-{
-  QVector<QModelIndex>::const_iterator it;
-  QVector<QModelIndex> features = _widget->model()->whichFeatureAt(pos);
   QString name, id;
   int numRelays = 0;
 
   _leftClickMenu->clear();
-  for (it = features.constBegin(); it != features.constEnd(); ++it) {
-    QChar role = (*it).data(MarblePlacemarkModel::GeoTypeRole).toChar();
-    if (role == '1') {
+  for (; it != itEnd; ++it) {
+    if ((*it)->role() == "1") {
       /* Normal Tor Relay */
-      name = (*it).data().toString();
-      id   = (*it).data(MarblePlacemarkModel::DescriptionRole).toString();
+      name = (*it)->name();
+      id   = (*it)->description();
 
       QAction *action = _leftClickMenu->addAction(name);
       action->setData(id);
@@ -76,7 +65,7 @@ TorMapWidgetPopupMenu::featureLeftClicked(const QPoint &pos)
   if (numRelays == 1)
     emit displayRouterInfo(id);
   else if (numRelays > 1)
-    _leftClickMenu->popup(_widget->mapToGlobal(pos));
+    _leftClickMenu->popup(_widget->mapToGlobal(QPoint(x,y)));
 }
 
 void
diff --git a/src/vidalia/network/TorMapWidgetPopupMenu.h b/src/vidalia/network/TorMapWidgetPopupMenu.h
index c83ded3..4381b72 100644
--- a/src/vidalia/network/TorMapWidgetPopupMenu.h
+++ b/src/vidalia/network/TorMapWidgetPopupMenu.h
@@ -35,14 +35,14 @@ public:
   TorMapWidgetPopupMenu(TorMapWidget *widget);
 
 public slots:
-  /** Called when the user clicks on one or more map features located at mouse
-   * position <b>pos</b>. <b>button</b> specifies the mouse button clicked.
-   * A popup menu will be displayed depending on which mouse button was
-   * clicked.
+  /** Called when the user left-clicks on one or more placemarks at mouse
+   * position <b>pos</b>. If only one relay placemark exists at <b>pos</b>,
+   * then the displayRouterInfo() signal will be emitted. Otherwise, a
+   * popup menu will be displayed listing all placemarks at this location.
    *
    * \sa featureLeftClicked
    */
-  void featureClicked(const QPoint &pos, Qt::MouseButton button);
+  void featureLeftClicked(int x, int y);
 
 signals:
   /** Emitted when the user selects the router placemark whose fingerprint
@@ -50,16 +50,6 @@ signals:
    */
   void displayRouterInfo(const QString &id);
 
-protected:
-  /** Called when the user left-clicks on one or more placemarks at mouse
-   * position <b>pos</b>. If only one relay placemark exists at <b>pos</b>,
-   * then the displayRouterInfo() signal will be emitted. Otherwise, a
-   * popup menu will be displayed listing all placemarks at this location.
-   *
-   * \sa featureLeftClicked
-   */
-  virtual void featureLeftClicked(const QPoint &pos);
-
 private slots:
   /** Called when the user selects a relay from the popup menu used to
    * disambiguate a location with multiple relay placemarks.



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits