Waypoint2DIcon.cc 11.7 KB
Newer Older
1 2
#include "Waypoint2DIcon.h"
#include <QPainter>
3
#include "opmapcontrol.h"
lm's avatar
lm committed
4
#include "QGC.h"
5

6 7 8 9 10 11 12 13
Waypoint2DIcon::Waypoint2DIcon(mapcontrol::MapGraphicItem* map, mapcontrol::OPMapWidget* parent, qreal latitude, qreal longitude, qreal altitude, int listindex, QString name, QString description, int radius)
    : mapcontrol::WayPointItem(internals::PointLatLng(latitude, longitude), altitude, description, map),
    parent(parent),
    waypoint(NULL),
    radius(radius),
    showAcceptanceRadius(true),
    showOrbit(false),
    color(Qt::red)
14
{
15 16 17 18
    SetHeading(0);
    SetNumber(listindex);
    this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
    picture = QPixmap(radius+1, radius+1);
lm's avatar
lm committed
19 20 21
    autoreachedEnabled = false; // In contrast to the use in OpenPilot, we don't
                                // want to let the map interfere with the actual mission logic
                                // wether a WP is reached depends solely on the UAV's state machine
22
    drawIcon();
23 24
}

25 26 27 28 29 30 31 32
Waypoint2DIcon::Waypoint2DIcon(mapcontrol::MapGraphicItem* map, mapcontrol::OPMapWidget* parent, Waypoint* wp, const QColor& color, int listindex, int radius)
    : mapcontrol::WayPointItem(internals::PointLatLng(wp->getLatitude(), wp->getLongitude()), wp->getAltitude(), wp->getDescription(), map),
    parent(parent),
    waypoint(wp),
    radius(radius),
    showAcceptanceRadius(true),
    showOrbit(false),
    color(color)
33
{
34 35 36 37
    SetHeading(wp->getYaw());
    SetNumber(listindex);
    this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
    picture = QPixmap(radius+1, radius+1);
lm's avatar
lm committed
38 39 40
    autoreachedEnabled = false; // In contrast to the use in OpenPilot, we don't
                                // want to let the map interfere with the actual mission logic
                                // wether a WP is reached depends solely on the UAV's state machine
41
    updateWaypoint();
42 43
}

44
Waypoint2DIcon::~Waypoint2DIcon()
45 46 47
{
}

48
void Waypoint2DIcon::SetHeading(float heading)
49
{
50 51
    mapcontrol::WayPointItem::SetHeading(heading);
    drawIcon();
52 53
}

54 55
void Waypoint2DIcon::updateWaypoint()
{
56
    if (waypoint) {
57 58 59
        // Store old size
        static QRectF oldSize;

60 61
        SetHeading(waypoint->getYaw());
        SetCoord(internals::PointLatLng(waypoint->getLatitude(), waypoint->getLongitude()));
62 63 64

        qDebug() << "UPDATING WP:" << waypoint->getId() << "LAT:" << waypoint->getLatitude() << "LON:" << waypoint->getLongitude();

65 66 67 68
        SetDescription(waypoint->getDescription());
        SetAltitude(waypoint->getAltitude());
        // FIXME Add SetNumber (currently needs a separate call)
        drawIcon();
69 70 71 72 73 74 75 76 77 78 79 80
        QRectF newSize = boundingRect();

        qDebug() << "WIDTH" << newSize.width() << "<" << oldSize.width();

        // If new size is smaller than old size, update surrounding
        if ((newSize.width() <= oldSize.width()) || (newSize.height() <= oldSize.height()))
        {
            // If the symbol size was reduced, enforce an update of the environment
//            update(oldSize);
            int oldWidth = oldSize.width() + 20;
            int oldHeight = oldSize.height() + 20;
            map->update(this->x()-10, this->y()-10, oldWidth, oldHeight);
81 82
            //qDebug() << "UPDATING DUE TO SMALLER SIZE";
            //qDebug() << "X:" << this->x()-1 << "Y:" << this->y()-1 << "WIDTH:" << oldWidth << "HEIGHT:" << oldHeight;
83 84 85 86 87 88 89
        }
        else
        {
            // Symbol size stayed constant or increased, use new size for update
            this->update();
        }
        oldSize = boundingRect();
90 91 92 93 94 95 96 97 98 99 100
    }
}

QRectF Waypoint2DIcon::boundingRect() const
{
    int loiter = 0;
    int acceptance = 0;
    internals::PointLatLng coord = (internals::PointLatLng)Coord();
    if (waypoint && showAcceptanceRadius && (waypoint->getAction() == (int)MAV_CMD_NAV_WAYPOINT))
    {
        acceptance = map->metersToPixels(waypoint->getAcceptanceRadius(), coord);
101
    }
102 103 104 105 106 107 108 109 110
    if (waypoint && ((waypoint->getAction() == (int)MAV_CMD_NAV_LOITER_UNLIM) || (waypoint->getAction() == (int)MAV_CMD_NAV_LOITER_TIME) || (waypoint->getAction() == (int)MAV_CMD_NAV_LOITER_TURNS)))
    {
        loiter = map->metersToPixels(waypoint->getLoiterOrbit(), coord);
    }

    int width = qMax(picture.width()/2, qMax(loiter, acceptance));
    int height = qMax(picture.height()/2, qMax(loiter, acceptance));

    return QRectF(-width,-height,2*width,2*height);
111 112
}

113
void Waypoint2DIcon::drawIcon()
114
{
115 116
    picture.fill(Qt::transparent);
    QPainter painter(&picture);
117 118 119
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true);

lm's avatar
lm committed
120 121 122 123 124 125 126 127 128 129 130
    QFont font("Bitstream Vera Sans");
    int fontSize = picture.height()*0.8f;
    font.setPixelSize(fontSize);

    QFontMetrics metrics = QFontMetrics(font);
    int border = qMax(4, metrics.leading());
    painter.setFont(font);
    painter.setRenderHint(QPainter::TextAntialiasing);



131 132 133 134 135
    QPen pen1(Qt::black);
    pen1.setWidth(4);
    QPen pen2(color);
    pen2.setWidth(2);
    painter.setBrush(Qt::NoBrush);
136

lm's avatar
lm committed
137 138
    int penWidth = pen1.width();

139
    // DRAW WAYPOINT
140
    QPointF p(picture.width()/2, picture.height()/2);
141 142 143

    QPolygonF poly(4);
    // Top point
lm's avatar
lm committed
144
    poly.replace(0, QPointF(p.x(), p.y()-picture.height()/2.0f+penWidth/2));
145
    // Right point
lm's avatar
lm committed
146
    poly.replace(1, QPointF(p.x()+picture.width()/2.0f-penWidth/2, p.y()));
147
    // Bottom point
lm's avatar
lm committed
148 149
    poly.replace(2, QPointF(p.x(), p.y() + picture.height()/2.0f-penWidth/2));
    poly.replace(3, QPointF(p.x() - picture.width()/2.0f+penWidth/2, p.y()));
150

151
    int waypointSize = qMin(picture.width(), picture.height());
152
    float rad = (waypointSize/2.0f) * 0.7f * (1/sqrt(2.0f));
153 154 155 156 157 158 159 160

    // If this is not a waypoint (only the default representation)
    // or it is a waypoint, but not one where direction has no meaning
    // then draw the heading indicator
    if (!waypoint || (waypoint && (
            (waypoint->getAction() != (int)MAV_CMD_NAV_TAKEOFF) &&
            (waypoint->getAction() != (int)MAV_CMD_NAV_LAND) &&
            (waypoint->getAction() != (int)MAV_CMD_NAV_LOITER_UNLIM) &&
161
            (waypoint->getAction() != (int)MAV_CMD_NAV_LOITER_TIME) &&
162 163
            (waypoint->getAction() != (int)MAV_CMD_NAV_LOITER_TURNS) &&
            (waypoint->getAction() != (int)MAV_CMD_NAV_RETURN_TO_LAUNCH)
164 165
            )))
    {
166 167 168 169
        painter.setPen(pen1);
        painter.drawLine(p.x(), p.y(), p.x()+sin(Heading()/180.0f*M_PI) * rad, p.y()-cos(Heading()/180.0f*M_PI) * rad);
        painter.setPen(pen2);
        painter.drawLine(p.x(), p.y(), p.x()+sin(Heading()/180.0f*M_PI) * rad, p.y()-cos(Heading()/180.0f*M_PI) * rad);
170 171 172 173 174
    }

    if ((waypoint != NULL) && (waypoint->getAction() == (int)MAV_CMD_NAV_TAKEOFF))
    {
        // Takeoff waypoint
lm's avatar
lm committed
175 176 177 178 179 180 181 182 183 184 185 186
        int width = picture.width()-penWidth;
        int height = picture.height()-penWidth;

        painter.setPen(pen1);
        painter.drawRect(penWidth/2, penWidth/2, width, height);
        painter.setPen(pen2);
        painter.drawRect(penWidth/2, penWidth/2, width, height);

        painter.setPen(pen1);
        painter.drawRect(width*0.3, height*0.3f, width*0.6f, height*0.6f);
        painter.setPen(pen2);
        painter.drawRect(width*0.3, height*0.3f, width*0.6f, height*0.6f);
187 188 189 190
    }
    else if ((waypoint != NULL) && (waypoint->getAction() == (int)MAV_CMD_NAV_LAND))
    {
        // Landing waypoint
lm's avatar
lm committed
191 192
        int width = (picture.width())/2-penWidth;
        int height = (picture.height())/2-penWidth;
193 194
        painter.setPen(pen1);
        painter.drawEllipse(p, width, height);
lm's avatar
lm committed
195
        painter.drawLine(p.x()-width/2, p.y()-height/2, 2*width, 2*height);
196
        painter.setPen(pen2);
197
        painter.drawEllipse(p, width, height);
lm's avatar
lm committed
198
        painter.drawLine(p.x()-width/2, p.y()-height/2, 2*width, 2*height);
199 200 201 202
    }
    else if ((waypoint != NULL) && ((waypoint->getAction() == (int)MAV_CMD_NAV_LOITER_UNLIM) || (waypoint->getAction() == (int)MAV_CMD_NAV_LOITER_TIME) || (waypoint->getAction() == (int)MAV_CMD_NAV_LOITER_TURNS)))
    {
        // Loiter waypoint
lm's avatar
lm committed
203 204
        int width = (picture.width()-penWidth)/2;
        int height = (picture.height()-penWidth)/2;
205 206 207 208
        painter.setPen(pen1);
        painter.drawEllipse(p, width, height);
        painter.drawPoint(p);
        painter.setPen(pen2);
209
        painter.drawEllipse(p, width, height);
210 211 212 213 214
        painter.drawPoint(p);
    }
    else if ((waypoint != NULL) && (waypoint->getAction() == (int)MAV_CMD_NAV_RETURN_TO_LAUNCH))
    {
        // Return to launch waypoint
lm's avatar
lm committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        int width = picture.width()-penWidth;
        int height = picture.height()-penWidth;
        painter.setPen(pen1);
        painter.drawRect(penWidth/2, penWidth/2, width, height);
        painter.setPen(pen2);
        painter.drawRect(penWidth/2, penWidth/2, width, height);

        QString text("R");

        painter.setPen(pen1);
        QRect rect = metrics.boundingRect(0, 0, width - 2*border, height, Qt::AlignLeft | Qt::TextWordWrap, text);
        painter.drawText(width/4, height/6, rect.width(), rect.height(),
                          Qt::AlignCenter | Qt::TextWordWrap, text);
        painter.setPen(pen2);

        font.setPixelSize(fontSize*0.85f);
        painter.setFont(font);
        painter.drawText(width/4, height/6, rect.width(), rect.height(), Qt::AlignCenter | Qt::TextWordWrap, text);
233 234 235 236
    }
    else
    {
        // Navigation waypoint
237 238 239
        painter.setPen(pen1);
        painter.drawPolygon(poly);
        painter.setPen(pen2);
240 241 242 243
        painter.drawPolygon(poly);
    }
}

lm's avatar
lm committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
void Waypoint2DIcon::SetShowNumber(const bool &value)
{
    shownumber=value;
    if((numberI==0) && value)
    {
        numberI=new QGraphicsSimpleTextItem(this);
        numberIBG=new QGraphicsRectItem(this);
        numberIBG->setBrush(Qt::black);
        numberIBG->setOpacity(0.5);
        numberI->setZValue(3);
        numberI->setPen(QPen(QGC::colorCyan));
        numberI->setPos(5,-picture.height());
        numberIBG->setPos(5,-picture.height());
        numberI->setText(QString::number(number));
        numberIBG->setRect(numberI->boundingRect().adjusted(-2,0,1,0));
    }
    else if (!value && numberI)
    {
        delete numberI;
        delete numberIBG;
    }
    this->update();
}

268 269 270 271
void Waypoint2DIcon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
272 273
    QPen pen = painter->pen();
    pen.setWidth(2);
274 275 276
    painter->drawPixmap(-picture.width()/2,-picture.height()/2,picture);
    if (this->isSelected())
    {
277
        pen.setColor(Qt::yellow);
278 279 280
        painter->drawRect(QRectF(-picture.width()/2,-picture.height()/2,picture.width()-1,picture.height()-1));
    }

281 282
    QPen penBlack(Qt::black);
    penBlack.setWidth(4);
283 284 285 286 287
    pen.setColor(color);

    if ((waypoint) && (waypoint->getAction() == (int)MAV_CMD_NAV_WAYPOINT) && showAcceptanceRadius)
    {
        QPen redPen = QPen(pen);
288
        redPen.setColor(Qt::yellow);
lm's avatar
lm committed
289
        redPen.setWidth(1);
290 291
        painter->setPen(redPen);
        const int acceptance = map->metersToPixels(waypoint->getAcceptanceRadius(), Coord());
292 293 294 295
        painter->setPen(penBlack);
        painter->drawEllipse(QPointF(0, 0), acceptance, acceptance);
        painter->setPen(redPen);
        painter->drawEllipse(QPointF(0, 0), acceptance, acceptance);
296 297 298
    }
    if ((waypoint) && ((waypoint->getAction() == (int)MAV_CMD_NAV_LOITER_UNLIM) || (waypoint->getAction() == (int)MAV_CMD_NAV_LOITER_TIME) || (waypoint->getAction() == (int)MAV_CMD_NAV_LOITER_TURNS)))
    {
299 300 301
        QPen penDash(color);
        penDash.setWidth(1);
        //penDash.setStyle(Qt::DotLine);
302
        const int loiter = map->metersToPixels(waypoint->getLoiterOrbit(), Coord());
303 304 305 306 307 308 309
        if (loiter > picture.width()/2)
        {
            painter->setPen(penBlack);
            painter->drawEllipse(QPointF(0, 0), loiter, loiter);
            painter->setPen(penDash);
            painter->drawEllipse(QPointF(0, 0), loiter, loiter);
        }
310
    }
311
}