MapWidget.cc 11.2 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

pixhawk's avatar
pixhawk committed
37 38
#include "MG.h"

pixhawk's avatar
pixhawk committed
39 40 41
MapWidget::MapWidget(QWidget *parent) :
        QWidget(parent),
        zoomLevel(0),
42 43
        uasIcons(),
        uasTrails(),
pixhawk's avatar
pixhawk committed
44 45 46
        m_ui(new Ui::MapWidget)
{
    m_ui->setupUi(this);
47

pixhawk's avatar
pixhawk committed
48 49 50 51 52 53
    // Accept focus by clicking or keyboard
    this->setFocusPolicy(Qt::StrongFocus);

    // create MapControl
    mc = new MapControl(QSize(320, 240));
    mc->showScale(true);
54
    mc->showCoord(true);
pixhawk's avatar
pixhawk committed
55
    mc->enablePersistentCache();
56
    mc->setMouseTracking(true); // required to update the mouse position for diplay and capture
pixhawk's avatar
pixhawk committed
57 58 59 60 61

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

    // create a layer with the mapadapter and type MapLayer
lm's avatar
lm committed
62
    osmLayer = new Layer("Custom Layer", osmAdapter, Layer::MapLayer);
63 64
    // create a layer with the mapadapter and type GeometryLayer (for waypoints)
    geomLayer = new Layer("Geom Layer", osmAdapter, Layer::GeometryLayer);
pixhawk's avatar
pixhawk committed
65

66
    // add Layers to the MapControl and set zoom level
pixhawk's avatar
pixhawk committed
67
    mc->addLayer(osmLayer);
68
    mc->addLayer(geomLayer);
69
    mc->setZoom(3);
pixhawk's avatar
pixhawk committed
70 71 72 73 74 75 76

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

77
    // create buttons to control the map (zoom, GPS tracking and WP capture)
pixhawk's avatar
pixhawk committed
78 79
    QPushButton* zoomin = new QPushButton(QIcon(":/images/actions/list-add.svg"), "", this);
    QPushButton* zoomout = new QPushButton(QIcon(":/images/actions/list-remove.svg"), "", this);
80
    createPath = new QPushButton(QIcon(":/images/actions/go-bottom.svg"), "", this);
pixhawk's avatar
pixhawk committed
81
    followgps = new QPushButton(QIcon(":/images/actions/system-lock-screen.svg"), "", this);
82

pixhawk's avatar
pixhawk committed
83 84
    zoomin->setMaximumWidth(50);
    zoomout->setMaximumWidth(50);
85
    createPath->setMaximumWidth(50);
pixhawk's avatar
pixhawk committed
86 87
    followgps->setMaximumWidth(50);

88 89 90 91 92 93 94
    // Set checkable buttons
    // TODO: Currently checked buttons are are very difficult to distinguish when checked.
    //       create a style and the slots to change the background so it is easier to distinguish
    followgps->setCheckable(true);
    createPath->setCheckable(true);

    // add buttons to control the map (zoom, GPS tracking and WP capture)
pixhawk's avatar
pixhawk committed
95 96 97 98
    QVBoxLayout* innerlayout = new QVBoxLayout;
    innerlayout->addWidget(zoomin);
    innerlayout->addWidget(zoomout);
    innerlayout->addWidget(followgps);
99
    innerlayout->addWidget(createPath);
pixhawk's avatar
pixhawk committed
100 101
    mc->setLayout(innerlayout);

102 103

    // Connect the required signals-slots
104 105 106 107 108 109
    connect(zoomin, SIGNAL(clicked(bool)),
            mc, SLOT(zoomIn()));

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

110 111 112 113 114 115 116 117 118
    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)),
            this, SLOT(addUAS(UASInterface*)));

    connect(mc, SIGNAL(mouseEventCoordinate(const QMouseEvent*, const QPointF)),
            this, SLOT(captureMapClick(const QMouseEvent*, const QPointF)));

    connect(createPath, SIGNAL(clicked(bool)),
            this, SLOT(createPathButtonClicked()));

119 120 121 122 123 124 125 126 127 128
    connect(geomLayer, SIGNAL(geometryClicked(Geometry*,QPoint)),
            this, SLOT(captureGeometryClick(Geometry*, QPoint)));


    // Configure the WP Path's pen
    pointPen = new QPen(QColor(0, 255,0));
    pointPen->setWidth(3);

    path = new LineString (wps, "UAV Path", pointPen);
    mc->layer("Geom Layer")->addGeometry(path);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

    this->setVisible(false);


    // Attic (Code that was commented)
    // ==============================
    //uasIcons = QMap<int, CirclePoint*>();

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

    //GoogleSatMapAdapter* gSatAdapter = new GoogleSatMapAdapter();
    //Layer* gSatLayer = new Layer("Custom Layer", gSatAdapter, Layer::MapLayer);
    //mc->addLayer(gSatLayer);

    // gpsposition = new QLabel();
    //gpsposition->setFont(QFont("Arial", 10));
pixhawk's avatar
pixhawk committed
146 147 148 149 150
    //GPS_Neo* gm = new GPS_Neo();
    //connect(gm, SIGNAL(new_position(float, QPointF)),
    //                  this, SLOT(updatePosition(float, QPointF)));
    //gm->start();

151
}
lm's avatar
lm committed
152

153
void MapWidget::createPathButtonClicked(){
154 155 156 157 158 159 160 161 162 163 164 165 166 167
  if (createPath->isChecked()){
    // change the cursor shape
    this->setCursor(Qt::PointingHandCursor);

    // Clear the previous WP track
    // TODO: Move this to an actual clear track button and add a warning dialog
    mc->layer("Geom Layer")->clearGeometries();
    wps.clear();
    path->setPoints(wps);
    mc->layer("Geom Layer")->addGeometry(path);
  } else {
    this->setCursor(Qt::ArrowCursor);
  }

168
}
169

170 171

void MapWidget::captureMapClick(const QMouseEvent* event, const QPointF coordinate){
172

173
  if (QEvent::MouseButtonRelease == event->type() && createPath->isChecked()){
174 175 176 177 178 179 180

    // Create waypoint name
    QString str;
    str = QString("WP%1").arg(path->numberOfPoints()+1);


    qDebug()<< "Waypoint " << str;
181 182
    qDebug()<< "Lat: " << coordinate.y();
    qDebug()<< "Lon: " << coordinate.x();
183 184 185 186 187 188 189

    // create the WP and set everything in the LineString to display the path
    mc->layer("Geom Layer")->addGeometry(new CirclePoint(coordinate.x(), coordinate.y(), 10, str));
    wps.append(new Point(coordinate.x(), coordinate.y(),str));
    path->addPoint(new Point(coordinate.x(), coordinate.y(),str));

    mc->updateRequestNew();
190
  }
pixhawk's avatar
pixhawk committed
191 192
}

193 194 195 196 197 198 199
void MapWidget::captureGeometryClick(Geometry* geom, QPoint point){
  Q_UNUSED(point);

  qDebug ()<< geom->name();
  qDebug() << geom->GeometryType;
}

pixhawk's avatar
pixhawk committed
200 201 202 203
MapWidget::~MapWidget()
{
    delete m_ui;
}
lm's avatar
lm committed
204 205 206 207 208 209 210
/**
 *
 * @param uas the UAS/MAV to monitor/display with the HUD
 */
void MapWidget::addUAS(UASInterface* uas)
{
    mav = uas;
211
    connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64)));
lm's avatar
lm committed
212 213 214 215 216
}

void MapWidget::updateGlobalPosition(UASInterface* uas, double lat, double lon, double alt, quint64 usec)
{
    Q_UNUSED(usec);
217
    Q_UNUSED(alt); // FIXME Use altitude
pixhawk's avatar
pixhawk committed
218
    quint64 currTime = MG::TIME::getGroundTimeNow();
219
    if (currTime - lastUpdate > 90)
pixhawk's avatar
pixhawk committed
220 221
    {
        lastUpdate = currTime;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        // create a LineString
        //QList<Point*> points;
        // Points with a circle
        // A QPen can be used to customize the
        //pointpen->setWidth(3);
        //points.append(new CirclePoint(lat, lon, 10, uas->getUASName(), Point::Middle, pointpen));

        if (!uasIcons.contains(uas->getUASID()))
        {
            // Get the UAS color
            QColor uasColor = uas->getColor();

            // Icon
            QPen* pointpen = new QPen(uasColor);
            CirclePoint* p = new CirclePoint(lat, lon, 10, uas->getUASName(), Point::Middle, pointpen);
            uasIcons.insert(uas->getUASID(), p);
            osmLayer->addGeometry(p);

            // Line
            // A QPen also can use transparency

            QList<Point*> points;
            points.append(new Point(lat, lon, QString("lat: %1 lon: %2").arg(lat, lon)));
            QPen* linepen = new QPen(uasColor.darker());
            linepen->setWidth(2);
            // Add the Points and the QPen to a LineString
            LineString* ls = new LineString(points, uas->getUASName(), linepen);
            uasTrails.insert(uas->getUASID(), ls);

            // Add the LineString to the layer
            osmLayer->addGeometry(ls);
        }
        else
        {
            CirclePoint* p = uasIcons.value(uas->getUASID());
            p->setCoordinate(QPointF(lat, lon));
            // Extend trail
            uasTrails.value(uas->getUASID())->addPoint(new Point(lat, lon, QString("lat: %1 lon: %2").arg(lat, lon)));
        }

        //    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"));

        // Connect click events of the layer to this object
269
        // connect(osmLayer, SIGNAL(geometryClicked(Geometry*, QPoint)),
270 271 272 273 274 275 276 277
        //                  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
278 279
    }
}
pixhawk's avatar
pixhawk committed
280 281


lm's avatar
lm committed
282
void MapWidget::updatePosition(float time, double lat, double lon)
pixhawk's avatar
pixhawk committed
283
{
lm's avatar
lm committed
284
    Q_UNUSED(time);
285
    //gpsposition->setText(QString::number(time) + " / " + QString::number(lat) + " / " + QString::number(lon));
pixhawk's avatar
pixhawk committed
286 287
    if (followgps->isChecked())
    {
lm's avatar
lm committed
288
        mc->setView(QPointF(lat, lon));
pixhawk's avatar
pixhawk committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    }
}

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);
    }
}

331
void MapWidget::resizeEvent(QResizeEvent* event )
pixhawk's avatar
pixhawk committed
332
{
333 334
    Q_UNUSED(event);
    mc->resize(this->size());
pixhawk's avatar
pixhawk committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348
}


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