Skip to content
MapWidget.cc 43.4 KiB
Newer Older
lm's avatar
lm committed

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

            // Icon
            //QPen* pointpen = new QPen(uasColor);
            qDebug() << "2D MAP: ADDING" << uas->getUASName() << __FILE__ << __LINE__;
            p = new MAV2DIcon(uas, 68, uas->getSystemType(), uas->getColor(), QString("%1").arg(uas->getUASID()), qmapcontrol::Point::Middle);
            uasIcons.insert(uas->getUASID(), p);
            mc->layer("Waypoints")->addGeometry(p);

            // Line
            // A QPen also can use transparency

            //        QList<qmapcontrol::Point*> points;
            //        points.append(new qmapcontrol::Point(coordinate.x(), coordinate.y()));
            //        QPen* linepen = new QPen(uasColor.darker());
            //        linepen->setWidth(2);

            //        // Create tracking line string
            //        qmapcontrol::LineString* ls = new qmapcontrol::LineString(points, QString("%1").arg(uas->getUASID()), linepen);
            //        uasTrails.insert(uas->getUASID(), ls);

            //        // Add the LineString to the layer
            //        mc->layer("Waypoints")->addGeometry(ls);
        }
        else
        {
            //        p = dynamic_cast<MAV2DIcon*>(uasIcons.value(uas->getUASID()));
            //        if (p)
            //        {
            p = uasIcons.value(uas->getUASID());
            p->setCoordinate(QPointF(lon, lat));
            //p->setYaw(uas->getYaw());
            //        }
            // Extend trail
            //        uasTrails.value(uas->getUASID())->addPoint(new qmapcontrol::Point(coordinate.x(), coordinate.y()));
        }
lm's avatar
lm committed
        if (isVisible()) mc->updateRequest(p->boundingBox().toRect());
lm's avatar
lm committed
        //if (isVisible()) mc->updateRequestNew();//(uasTrails.value(uas->getUASID())->boundingBox().toRect());
lm's avatar
lm committed
        if (this->mav && uas->getUASID() == this->mav->getUASID())
lm's avatar
lm committed
            // Limit the position update rate
            quint64 currTime = MG::TIME::getGroundTimeNow();
            if (currTime - lastUpdate > 120)
lm's avatar
lm committed
                lastUpdate = currTime;
                // Sets the view to the interesting area
                if (followgps->isChecked())
                {
                    updatePosition(0, lon, lat);
                }
                else
                {
                    // Refresh the screen
                    //if (isVisible()) mc->updateRequestNew();
                }
pixhawk's avatar
pixhawk committed
    }
}
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
/**
 * Center the view on this position
 */
lm's avatar
lm committed
void MapWidget::updatePosition(float time, double lat, double lon)
pixhawk's avatar
pixhawk committed
{
lm's avatar
lm committed
    Q_UNUSED(time);
    //gpsposition->setText(QString::number(time) + " / " + QString::number(lat) + " / " + QString::number(lon));
lm's avatar
lm committed
    if (followgps->isChecked() && isVisible() && mc)
pixhawk's avatar
pixhawk committed
    {
        if (mc) mc->setView(QPointF(lat, lon));
pixhawk's avatar
pixhawk committed
    }
}

void MapWidget::wheelEvent(QWheelEvent *event)
{
lm's avatar
lm committed
    if (mc)
    {
        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));

        // visual field of camera
        //updateCameraPosition(20*newZoom,0,"no");
pixhawk's avatar
pixhawk committed
}

void MapWidget::keyPressEvent(QKeyEvent *event)
{
lm's avatar
lm committed
    if (mc)
    {
        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);
        }
pixhawk's avatar
pixhawk committed
    }
}

void MapWidget::resizeEvent(QResizeEvent* event )
pixhawk's avatar
pixhawk committed
{
lm's avatar
lm committed
        if (!initialized)
        {
            init();
        }
    if (mc) mc->resize(this->size());
pixhawk's avatar
pixhawk committed
}

pixhawk's avatar
pixhawk committed
void MapWidget::showEvent(QShowEvent* event)
{
    Q_UNUSED(event);
lm's avatar
lm committed
//    if (isVisible())
//    {
//	if (!initialized)
//	{
//            init();
//	}
//    }
pixhawk's avatar
pixhawk committed
}

void MapWidget::hideEvent(QHideEvent* event)
{
    Q_UNUSED(event);
lm's avatar
lm committed
    if (mc)
    {
        QSettings settings;
        settings.beginGroup("QGC_MAPWIDGET");
        QPointF currentPos = mc->currentCoordinate();
        settings.setValue("LAST_LATITUDE", currentPos.y());
        settings.setValue("LAST_LONGITUDE", currentPos.x());
        settings.setValue("LAST_ZOOM", mc->currentZoom());
        settings.endGroup();

        settings.beginGroup("QGC_MAPINDEX");
        settings.setValue("MAP_INDEX", index);
        settings.endGroup();

        settings.beginGroup("QGC_HOMEPOSITION");
        settings.setValue("HOME_LATITUDE", homeCoordinate.y());
        settings.setValue("HOME_LONGITUDE", homeCoordinate.x());
        settings.endGroup();

lm's avatar
lm committed
        settings.sync();
pixhawk's avatar
pixhawk committed

void MapWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
lm's avatar
lm committed
    if (mc)
lm's avatar
lm committed
        Q_UNUSED(uas);
        // Clear the previous WP track
lm's avatar
lm committed
        //mc->layer("Waypoints")->clearGeometries();
        wps.clear();
        foreach (Point* p, wpIcons)
        {
            mc->layer("Waypoints")->removeGeometry(p);
        }
        wpIcons.clear();
lm's avatar
lm committed
        // Get bounding box of this object BEFORE deleting the content
        QRect box = waypointPath->boundingBox().toRect();
lm's avatar
lm committed
        // Delete the content
        waypointPath->points().clear();
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
        //delete waypointPath;
        //waypointPath = new
        //mc->layer("Waypoints")->addGeometry(waypointPath);
        //wpIndex.clear();
        if (isVisible()) mc->updateRequest(box);//(waypointPath->boundingBox().toRect());
lm's avatar
lm committed
        if(createPath->isChecked())
        {
            createPath->click();
        }
    }
pixhawk's avatar
pixhawk committed

pixhawk's avatar
pixhawk committed
    Q_UNUSED(uas);
lm's avatar
lm committed
    if (mc)
lm's avatar
lm committed
        mc->layer("Tracking")->clearGeometries();
        foreach (qmapcontrol::LineString* ls, uasTrails)
        {
            QPen* linepen = ls->pen();
            delete ls;
            qmapcontrol::LineString* lsNew = new qmapcontrol::LineString(QList<qmapcontrol::Point*>(), "", linepen);
            mc->layer("Tracking")->addGeometry(lsNew);
        }
        // FIXME update this with update request only for bounding box of trails
        if (isVisible()) mc->updateRequestNew();//(QRect(0, 0, width(), height()));
void MapWidget::createHomePosition(const QMouseEvent *event, const QPointF coordinate)
{
    if (QEvent::MouseButtonRelease == event->type() && setHome->isChecked())
    {
        this->createHomePosition(coordinate);
    }
}
void MapWidget::createHomePosition(const QPointF coordinate)
{
    homeCoordinate= coordinate;
    Waypoint2DIcon* tempCirclePoint;
    double latitude = homeCoordinate.y();
    double longitude = homeCoordinate.x();
    tempCirclePoint = new Waypoint2DIcon(
             longitude,
             latitude,
             20, "g", qmapcontrol::Point::Middle);
    QPen* pencil = new QPen(Qt::blue);
    tempCirclePoint->setPen(pencil);
    mc->layer("Station")->clearGeometries();
    mc->layer("Station")->addGeometry(tempCirclePoint);
    qmapcontrol::Point* tempPoint = new qmapcontrol::Point(latitude, longitude,"g");

    if (isVisible())
    {
        mc->updateRequest(tempPoint->boundingBox().toRect());
    }
}

void MapWidget::createHomePositionClick(bool click)
{
    Q_UNUSED(click);

    if (!setHome->isChecked())
    {
        if(mav)
        {
            UASManager::instance()->setHomePosition(
                    static_cast<double>(homeCoordinate.y()),
                    static_cast<double>(homeCoordinate.x()), 0);

            //qDebug()<<"Set home position "<<homeCoordinate.y()<<" "<<homeCoordinate.x();