MapWidget.cc 7.07 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

    PIXHAWK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PIXHAWK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PIXHAWK. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Implementation of map view
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include "MapWidget.h"
#include "ui_MapWidget.h"
lm's avatar
lm committed
34 35
#include "UASInterface.h"
#include "UASManager.h"
pixhawk's avatar
pixhawk committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

MapWidget::MapWidget(QWidget *parent) :
        QWidget(parent),
        zoomLevel(0),
        m_ui(new Ui::MapWidget)
{
    m_ui->setupUi(this);
    // Accept focus by clicking or keyboard
    this->setFocusPolicy(Qt::StrongFocus);

    // create MapControl
    mc = new MapControl(QSize(320, 240));
    mc->showScale(true);
    mc->enablePersistentCache();

    //QSize(480,640)
    //      ImageManager::instance()->setProxy("www-cache", 8080);

    // create MapAdapter to get maps from
    TileMapAdapter* osmAdapter = new TileMapAdapter("tile.openstreetmap.org", "/%1/%2/%3.png", 256, 0, 17);
    //GoogleSatMapAdapter* gSatAdapter = new GoogleSatMapAdapter();

    // create a layer with the mapadapter and type MapLayer
lm's avatar
lm committed
59
    osmLayer = new Layer("Custom Layer", osmAdapter, Layer::MapLayer);
pixhawk's avatar
pixhawk committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    //Layer* gSatLayer = new Layer("Custom Layer", gSatAdapter, Layer::MapLayer);

    // add Layer to the MapControl
    mc->addLayer(osmLayer);
    //mc->addLayer(gSatLayer);

    // display the MapControl in the application
    QHBoxLayout* layout = new QHBoxLayout;
    layout->setMargin(0);
    layout->addWidget(mc);
    setLayout(layout);

    // create buttons as controls for zoom
    QPushButton* zoomin = new QPushButton(QIcon(":/images/actions/list-add.svg"), "", this);
    QPushButton* zoomout = new QPushButton(QIcon(":/images/actions/list-remove.svg"), "", this);
    followgps = new QPushButton(QIcon(":/images/actions/system-lock-screen.svg"), "", this);
    followgps->setCheckable(true);
77
   // gpsposition = new QLabel();
pixhawk's avatar
pixhawk committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    zoomin->setMaximumWidth(50);
    zoomout->setMaximumWidth(50);
    followgps->setMaximumWidth(50);
    //gpsposition->setFont(QFont("Arial", 10));

    connect(zoomin, SIGNAL(clicked(bool)),
            mc, SLOT(zoomIn()));
    connect(zoomout, SIGNAL(clicked(bool)),
            mc, SLOT(zoomOut()));

    // add zoom buttons to the layout of the MapControl
    QVBoxLayout* innerlayout = new QVBoxLayout;
    innerlayout->addWidget(zoomin);
    innerlayout->addWidget(zoomout);
    innerlayout->addWidget(followgps);
    //innerlayout->addWidget(gpsposition);
    mc->setLayout(innerlayout);

    //GPS_Neo* gm = new GPS_Neo();
    //connect(gm, SIGNAL(new_position(float, QPointF)),
    //                  this, SLOT(updatePosition(float, QPointF)));
    //gm->start();

    mc->setZoom(3);
lm's avatar
lm committed
102 103

    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(addUAS(UASInterface*)));
pixhawk's avatar
pixhawk committed
104 105 106 107 108 109
}

MapWidget::~MapWidget()
{
    delete m_ui;
}
lm's avatar
lm committed
110 111 112 113 114 115 116
/**
 *
 * @param uas the UAS/MAV to monitor/display with the HUD
 */
void MapWidget::addUAS(UASInterface* uas)
{
    mav = uas;
117
    connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64)));
lm's avatar
lm committed
118 119 120 121 122 123 124 125 126
}

void MapWidget::updateGlobalPosition(UASInterface* uas, double lat, double lon, double alt, quint64 usec)
{
    Q_UNUSED(usec);
    // create a LineString
    QList<Point*> points;
    // Points with a circle
    // A QPen can be used to customize the
127
    QPen* pointpen = new QPen(uas->getColor());
lm's avatar
lm committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    pointpen->setWidth(3);
    points.append(new CirclePoint(lat, lon, alt, uas->getUASName(), Point::Middle, pointpen));
//    points.append(new CirclePoint(8.275145, 50.016992, 15, "Wiesbaden-Mainz-Kastel, Johannes-Goßner-Straße", Point::Middle, pointpen));
//    points.append(new CirclePoint(8.270476, 50.021426, 15, "Wiesbaden-Mainz-Kastel, Ruthof", Point::Middle, pointpen));
//    // "Blind" Points
//    points.append(new Point(8.266445, 50.025913, "Wiesbaden-Mainz-Kastel, Mudra Kaserne"));
//    points.append(new Point(8.260378, 50.030345, "Wiesbaden-Mainz-Amoneburg, Dyckerhoffstraße"));

    // A QPen also can use transparency
    QPen* linepen = new QPen(QColor(0, 0, 255, 100));
    linepen->setWidth(5);
    // Add the Points and the QPen to a LineString
    LineString* ls = new LineString(points, "Path", linepen);

    // Add the LineString to the layer
    osmLayer->addGeometry(ls);

    // Connect click events of the layer to this object
    //connect(osmLayer, SIGNAL(geometryClicked(Geometry*, QPoint)),
    //                  this, SLOT(geometryClicked(Geometry*, QPoint)));

    // Sets the view to the interesting area
    //QList<QPointF> view;
    //view.append(QPointF(8.24764, 50.0319));
    //view.append(QPointF(8.28412, 49.9998));
   // mc->setView(view);
    updatePosition(0, lat, lon);
}
pixhawk's avatar
pixhawk committed
156 157


lm's avatar
lm committed
158
void MapWidget::updatePosition(float time, double lat, double lon)
pixhawk's avatar
pixhawk committed
159
{
160
    //gpsposition->setText(QString::number(time) + " / " + QString::number(lat) + " / " + QString::number(lon));
pixhawk's avatar
pixhawk committed
161 162
    if (followgps->isChecked())
    {
lm's avatar
lm committed
163
        mc->setView(QPointF(lat, lon));
pixhawk's avatar
pixhawk committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    }
}

void MapWidget::wheelEvent(QWheelEvent *event)
{
    int numDegrees = event->delta() / 8;
    int numSteps = numDegrees / 15;
    // Calculate new zoom level
    int newZoom = mc->currentZoom()+numSteps;
    // Set new zoom level, level is bounded by map control
    mc->setZoom(newZoom);
    // Detail zoom level is the number of steps zoomed in further
    // after the bounding has taken effect
    detailZoom = qAbs(qMin(0, mc->currentZoom()-newZoom));
}

void MapWidget::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Plus:
        mc->zoomIn();
        break;
    case Qt::Key_Minus:
        mc->zoomOut();
        break;
    case Qt::Key_Left:
        mc->scrollLeft(this->width()/scrollStep);
        break;
    case Qt::Key_Right:
        mc->scrollRight(this->width()/scrollStep);
        break;
    case Qt::Key_Down:
        mc->scrollDown(this->width()/scrollStep);
        break;
    case Qt::Key_Up:
        mc->scrollUp(this->width()/scrollStep);
        break;
    default:
        QWidget::keyPressEvent(event);
    }
}

206
void MapWidget::resizeEvent(QResizeEvent* event )
pixhawk's avatar
pixhawk committed
207
{
208 209
    Q_UNUSED(event);
    mc->resize(this->size());
pixhawk's avatar
pixhawk committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223
}


void MapWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}