waypointlineitem.cpp 2.61 KB
Newer Older
1 2
#include "waypointlineitem.h"

lm's avatar
lm committed
3
namespace mapcontrol
4
{
lm's avatar
lm committed
5
WaypointLineItem::WaypointLineItem(WayPointItem* wp1, WayPointItem* wp2, QColor color, mapcontrol::MapGraphicItem* map) :
lm's avatar
lm committed
6
        QGraphicsLineItem(map),
lm's avatar
lm committed
7 8 9 10
    wp1(wp1),
    wp2(wp2),
    map(map)
{
11
    // Make sure this stick to the map
lm's avatar
lm committed
12
    //this->setFlag(QGraphicsItem::Item,true);
13
    setParentItem(map);
lm's avatar
lm committed
14

15
    // Set up the pen for this icon with the UAV color
lm's avatar
lm committed
16 17 18 19
    QPen pen(color);
    pen.setWidth(2);
    setPen(pen);

LM's avatar
LM committed
20 21 22
    point1 = wp1->Coord();
    point2 = wp2->Coord();

23 24 25 26
    // Pixel coordinates of the local points
    core::Point localPoint1 = map->FromLatLngToLocal(wp1->Coord());
    core::Point localPoint2 = map->FromLatLngToLocal(wp2->Coord());
    // Draw line
lm's avatar
lm committed
27 28
    setLine(localPoint1.X(), localPoint1.Y(), localPoint2.X(), localPoint2.Y());

29
    // Connect updates
lm's avatar
lm committed
30 31 32 33 34 35
    // Update line from both waypoints
    connect(wp1, SIGNAL(WPValuesChanged(WayPointItem*)), this, SLOT(updateWPValues(WayPointItem*)));
    connect(wp2, SIGNAL(WPValuesChanged(WayPointItem*)), this, SLOT(updateWPValues(WayPointItem*)));
    // Delete line if one of the waypoints get deleted
    connect(wp1, SIGNAL(destroyed()), this, SLOT(deleteLater()));
    connect(wp2, SIGNAL(destroyed()), this, SLOT(deleteLater()));
lm's avatar
lm committed
36 37

    // Map Zoom and move
LM's avatar
LM committed
38
    connect(map, SIGNAL(mapChanged()), this, SLOT(updateWPValues()));
lm's avatar
lm committed
39 40
}

41 42 43 44 45 46 47 48 49 50
void WaypointLineItem::RefreshPos()
{
    // Delete if either waypoint got deleted
    if (!wp1 || !wp2)
    {
        this->deleteLater();
    }
    else
    {
        // Set new pixel coordinates based on new global coordinates
LM's avatar
LM committed
51 52 53 54 55 56 57
        //QTimer::singleShot(0, this, SLOT(updateWPValues()));
        core::Point localPoint1 = map->FromLatLngToLocal(point1);
        core::Point localPoint2 = map->FromLatLngToLocal(point2);
        if (!localPoint1.IsEmpty() && !localPoint2.IsEmpty())
        {
            setLine(localPoint1.X(), localPoint1.Y(), localPoint2.X(), localPoint2.Y());
        }
58 59 60
    }
}

lm's avatar
lm committed
61 62 63
void WaypointLineItem::updateWPValues(WayPointItem* waypoint)
{
    Q_UNUSED(waypoint);
64
    // Delete if either waypoint got deleted
lm's avatar
lm committed
65 66 67 68 69 70
    if (!wp1 || !wp2)
    {
        this->deleteLater();
    }
    else
    {
71
        // Set new pixel coordinates based on new global coordinates
LM's avatar
LM committed
72 73
        point1 = wp1->Coord();
        point2 = wp2->Coord();
lm's avatar
lm committed
74 75 76 77 78 79 80
        core::Point localPoint1 = map->FromLatLngToLocal(wp1->Coord());
        core::Point localPoint2 = map->FromLatLngToLocal(wp2->Coord());

        setLine(localPoint1.X(), localPoint1.Y(), localPoint2.X(), localPoint2.Y());
    }
}

LM's avatar
LM committed
81 82 83 84 85
void WaypointLineItem::updateWPValues()
{
    updateWPValues(NULL);
}

lm's avatar
lm committed
86 87 88 89 90
int WaypointLineItem::type()const
{
    return Type;
}

91
}