Skip to content
MapWidget.cc 42.4 KiB
Newer Older
lm's avatar
lm committed
    {
        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.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::updateCameraPosition(double radio, double bearing, QString dir)
{
pixhawk's avatar
pixhawk committed
    Q_UNUSED(dir);
    Q_UNUSED(bearing);
lm's avatar
lm committed
    if (mc)
    {
        // FIXME Mariano
        //camPoints.clear();
        QPointF currentPos = mc->currentCoordinate();
        //    QPointF actualPos = getPointxBearing_Range(currentPos.y(),currentPos.x(),bearing,distance);
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
        //    qmapcontrol::Point* tempPoint1 = new qmapcontrol::Point(currentPos.x(), currentPos.y(),"inicial",qmapcontrol::Point::Middle);
        //    qmapcontrol::Point* tempPoint2 = new qmapcontrol::Point(actualPos.x(), actualPos.y(),"final",qmapcontrol::Point::Middle);
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
        //    camPoints.append(tempPoint1);
        //    camPoints.append(tempPoint2);
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
        //    camLine->setPoints(camPoints);
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
        QPen* camBorderPen = new QPen(QColor(255,0,0));
        camBorderPen->setWidth(2);
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
        //radio = mc->currentZoom()
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
        if(drawCamBorder)
        {
            //clear camera borders
            mc->layer("Camera")->clearGeometries();
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
            //create a camera borders
            qmapcontrol::CirclePoint* camBorder = new qmapcontrol::CirclePoint(currentPos.x(), currentPos.y(), radio, "camBorder", qmapcontrol::Point::Middle, camBorderPen);
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
            //camBorder->setCoordinate(currentPos);
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
            mc->layer("Camera")->addGeometry(camBorder);
            // mc->layer("Camera")->addGeometry(camLine);
            if (isVisible()) mc->updateRequestNew();
pixhawk's avatar
pixhawk committed

lm's avatar
lm committed
        }
        else
        {
            //clear camera borders
            mc->layer("Camera")->clearGeometries();
            if (isVisible()) mc->updateRequestNew();
pixhawk's avatar
pixhawk committed

}

void MapWidget::drawBorderCamAtMap(bool status)
{
    drawCamBorder = status;
    updateCameraPosition(20,0,"no");
pixhawk's avatar
pixhawk committed

}

QPointF MapWidget::getPointxBearing_Range(double lat1, double lon1, double bearing, double distance)
{
    QPointF temp;
pixhawk's avatar
pixhawk committed

    double rad = M_PI/180;
pixhawk's avatar
pixhawk committed

    bearing = bearing*rad;
    temp.setX((lon1 + ((distance/60) * (sin(bearing)))));
    temp.setY((lat1 + ((distance/60) * (cos(bearing)))));
pixhawk's avatar
pixhawk committed

void MapWidget::createHomePosition(const QMouseEvent *event, const QPointF coordinate)
{
    if (QEvent::MouseButtonRelease == event->type() && setHome->isChecked())
    {
        homeCoordinate= coordinate;
        Waypoint2DIcon* tempCirclePoint;

        double latitud = homeCoordinate.x();
        double longitud = homeCoordinate.y();

        tempCirclePoint = new Waypoint2DIcon(
                 latitud,
                 longitud,
                 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(latitud, longitud,"g");

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

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

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

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