Waypoint2DIcon.cc 2.59 KB
Newer Older
1 2 3
#include "Waypoint2DIcon.h"
#include <QPainter>

4 5 6 7 8
Waypoint2DIcon::Waypoint2DIcon(qreal x, qreal y, int radius, QString name, Alignment alignment, QPen* pen)
    : Point(x, y, name, alignment),
    yaw(0.0f),
    radius(radius)
{
9 10 11 12 13 14 15 16 17 18
    waypoint = NULL;
    size = QSize(radius, radius);
    drawIcon(pen);
}

Waypoint2DIcon::Waypoint2DIcon(Waypoint* wp, int listIndex, int radius, Alignment alignment, QPen* pen)
    : Point(wp->getX(), wp->getY(), QString("%1").arg(listIndex), alignment)
{
    waypoint = wp;
    this->yaw = wp->getYaw();
19 20 21
    size = QSize(radius, radius);
    drawIcon(pen);
}
22

23 24
Waypoint2DIcon::Waypoint2DIcon(qreal x, qreal y, QString name, Alignment alignment, QPen* pen)
    : Point(x, y, name, alignment)
25
{
26 27
    waypoint = NULL;
    int radius = 20;
28 29
    size = QSize(radius, radius);
    drawIcon(pen);
30 31
}

32
Waypoint2DIcon::~Waypoint2DIcon()
33
{
34 35 36 37 38 39 40
    delete mypixmap;
}

void Waypoint2DIcon::setPen(QPen* pen)
{
    mypen = pen;
    drawIcon(pen);
41 42 43
}

/**
44
 * @param yaw in radians, 0 = north, positive = clockwise
45
 */
46
void Waypoint2DIcon::setYaw(float yaw)
47
{
48
    this->yaw = yaw;
49
    drawIcon(mypen);
50 51
}

52 53 54 55 56 57 58 59 60 61 62 63
void Waypoint2DIcon::updateWaypoint()
{
    if (waypoint)
    {
        if (waypoint->getYaw() != yaw)
        {
            yaw = waypoint->getYaw();
            drawIcon(mypen);
        }
    }
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
void Waypoint2DIcon::drawIcon(QPen* pen)
{
    mypixmap = new QPixmap(radius+1, radius+1);
    mypixmap->fill(Qt::transparent);
    QPainter painter(mypixmap);

    // DRAW WAYPOINT
    QPointF p(radius/2, radius/2);

    float waypointSize = radius;
    QPolygonF poly(4);
    // Top point
    poly.replace(0, QPointF(p.x(), p.y()-waypointSize/2.0f));
    // Right point
    poly.replace(1, QPointF(p.x()+waypointSize/2.0f, p.y()));
    // Bottom point
    poly.replace(2, QPointF(p.x(), p.y() + waypointSize/2.0f));
    poly.replace(3, QPointF(p.x() - waypointSize/2.0f, p.y()));

//    // Select color based on if this is the current waypoint
//    if (list.at(i)->getCurrent())
//    {
//        color = QGC::colorCyan;//uas->getColor();
//        pen.setWidthF(refLineWidthToPen(0.8f));
//    }
//    else
//    {
//        color = uas->getColor();
//        pen.setWidthF(refLineWidthToPen(0.4f));
//    }

    //pen.setColor(color);
    if (pen)
    {
        pen->setWidthF(2);
        painter.setPen(*pen);
    }
    else
    {
        QPen pen2(Qt::red);
        pen2.setWidth(2);
        painter.setPen(pen2);
    }
    painter.setBrush(Qt::NoBrush);

    float rad = (waypointSize/2.0f) * 0.8 * (1/sqrt(2.0f));
    painter.drawLine(p.x(), p.y(), p.x()+sin(yaw) * radius, p.y()-cos(yaw) * rad);
    painter.drawPolygon(poly);
112
}