Vidalia 0.2.12

TorMapWidgetPopupMenu.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 /*
00012 ** \file TorMapWidgetPopupMenu.cpp
00013 ** \brief Popup menu displayed when the user mouse clicks on a map placemark
00014 */
00015 
00016 #include "TorMapWidgetPopupMenu.h"
00017 #include "Vidalia.h"
00018 
00019 #include <MarbleModel.h>
00020 #include <MarblePlacemarkModel.h>
00021 
00022 #include <QChar>
00023 #include <QVector>
00024 #include <QModelIndex>
00025 
00026 using namespace Marble;
00027 
00028 
00029 TorMapWidgetPopupMenu::TorMapWidgetPopupMenu(TorMapWidget *widget)
00030   : QObject(widget),
00031     _widget(widget)
00032 {
00033   _leftClickMenu = new QMenu(widget);
00034   connect(_leftClickMenu, SIGNAL(triggered(QAction*)),
00035           this, SLOT(relaySelected(QAction*)));
00036 }
00037 
00038 void
00039 TorMapWidgetPopupMenu::featureClicked(const QPoint &pos, Qt::MouseButton btn)
00040 {
00041   switch (btn) {
00042     case Qt::LeftButton:
00043       featureLeftClicked(pos);
00044       break;
00045 
00046     case Qt::RightButton:
00047       break;
00048 
00049     default:
00050       break;
00051   }
00052 }
00053 
00054 void
00055 TorMapWidgetPopupMenu::featureLeftClicked(const QPoint &pos)
00056 {
00057   QVector<QModelIndex>::const_iterator it;
00058   QVector<QModelIndex> features = _widget->model()->whichFeatureAt(pos);
00059   QString name, id;
00060   int numRelays = 0;
00061 
00062   _leftClickMenu->clear();
00063   for (it = features.constBegin(); it != features.constEnd(); ++it) {
00064     QChar role = (*it).data(MarblePlacemarkModel::GeoTypeRole).toChar();
00065     if (role == '1') {
00066       /* Normal Tor Relay */
00067       name = (*it).data().toString();
00068       id   = (*it).data(MarblePlacemarkModel::DescriptionRole).toString();
00069 
00070       QAction *action = _leftClickMenu->addAction(name);
00071       action->setData(id);
00072       numRelays++;
00073     }
00074   }
00075 
00076   if (numRelays == 1)
00077     emit displayRouterInfo(id);
00078   else if (numRelays > 1)
00079     _leftClickMenu->popup(_widget->mapToGlobal(pos));
00080 }
00081 
00082 void
00083 TorMapWidgetPopupMenu::relaySelected(QAction *action)
00084 {
00085   QString id = action->data().toString();
00086   if (! id.isEmpty())
00087     emit displayRouterInfo(id);
00088 }
00089