MAV2DIcon.cc 10.2 KB
Newer Older
1 2 3
#include "MAV2DIcon.h"
#include <QPainter>

4 5
#include <qmath.h>

6 7
MAV2DIcon::MAV2DIcon(UASInterface* uas, int radius, int type, const QColor& color, QString name, Alignment alignment, QPen* pen)
    : Point(uas->getLatitude(), uas->getLongitude(), name, alignment),
8 9 10
    yaw(0.0f),
    radius(radius),
    type(type),
11
    airframe(uas->getAirframe()),
12 13 14
    iconColor(color),
    selected(uas->getSelected()),
    uasid(uas->getUASID())
15
{
16
    //connect
17 18
    size = QSize(radius, radius);
    drawIcon(pen);
19 20
}

21
MAV2DIcon::MAV2DIcon(qreal x, qreal y, QString name, Alignment alignment, QPen* pen)
22 23 24
    : Point(x, y, name, alignment),
    radius(20),
    type(0),
25
    airframe(0),
26 27 28
    iconColor(Qt::yellow),
    selected(false),
    uasid(0)
29 30
{
    size = QSize(radius, radius);
31
    drawIcon(pen);
32 33 34 35 36 37 38 39
}

MAV2DIcon::~MAV2DIcon()
{
    delete mypixmap;
}

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

45 46 47 48 49 50
void MAV2DIcon::setSelectedUAS(bool selected)
{
    this->selected = selected;
    drawIcon(mypen);
}

51
/**
52 53
 * Yaw changes of less than ±15 degrees are ignored.
 *
54
 * @param yaw in radians, 0 = north, positive = clockwise
55
 */
56
void MAV2DIcon::setYaw(float yaw)
57
{
pixhawk's avatar
pixhawk committed
58
    //qDebug() << "MAV2Icon" << yaw;
59
    float diff = fabs(yaw - this->yaw);
60
    while (diff > (float)M_PI)
61
    {
62
        diff -= (float)M_PI;
63 64
    }

65
    if (diff > 0.1f)
66
    {
67 68
        this->yaw = yaw;
        drawIcon(mypen);
69
    }
70 71 72 73
}

void MAV2DIcon::drawIcon(QPen* pen)
{
pixhawk's avatar
pixhawk committed
74
    Q_UNUSED(pen);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    if (!mypixmap) mypixmap = new QPixmap(radius+1, radius+1);
    mypixmap->fill(Qt::transparent);
    QPainter painter(mypixmap);
    painter.setRenderHint(QPainter::TextAntialiasing);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::HighQualityAntialiasing);

    // Rotate by yaw
    painter.translate(radius/2, radius/2);

    // Draw selected indicator
    if (selected)
    {
//        qDebug() << "SYSTEM IS NOW SELECTED";
//        QColor color(Qt::yellow);
//        color.setAlpha(0.3f);
        painter.setBrush(Qt::NoBrush);
//        QPen selPen(color);
//        int width = 5;
//        selPen.setWidth(width);
95 96 97
        QPen pen(Qt::yellow);
        pen.setWidth(2);
        painter.setPen(pen);
98 99 100 101
        painter.drawEllipse(QPoint(0, 0), radius/2-1, radius/2-1);
        //qDebug() << "Painting ellipse" << radius/2-width << width;
        //selPen->deleteLater();
    }
102 103
    drawAirframePolygon(airframe, painter, radius, iconColor, yaw);
}
104

105 106
void MAV2DIcon::drawAirframePolygon(int airframe, QPainter& painter, int radius, QColor& iconColor, float yaw)
{
107
    switch (airframe)
108
    {
109 110
    case UASInterface::QGC_AIRFRAME_PREDATOR:
    case UASInterface::QGC_AIRFRAME_REAPER:
111 112
        {
            // DRAW AIRPLANE
113

114 115 116 117 118
            // Rotate 180 degs more since the icon is oriented downwards
            float yawRotate = (yaw/(float)M_PI)*180.0f + 180.0f+180.0f;

            painter.rotate(yawRotate);

119
            //qDebug() << "ICON SIZE:" << radius;
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
            float iconSize = radius*0.9f;
            QPolygonF poly(24);
            poly.replace(0, QPointF(0.000000f*iconSize, -0.312500f*iconSize));
            poly.replace(1, QPointF(0.025000f*iconSize, -0.287500f*iconSize));
            poly.replace(2, QPointF(0.037500f*iconSize, -0.237500f*iconSize));
            poly.replace(3, QPointF(0.031250f*iconSize, -0.187500f*iconSize));
            poly.replace(4, QPointF(0.025000f*iconSize, -0.043750f*iconSize));
            poly.replace(5, QPointF(0.500000f*iconSize, -0.025000f*iconSize));
            poly.replace(6, QPointF(0.500000f*iconSize, 0.025000f*iconSize));
            poly.replace(7, QPointF(0.025000f*iconSize, 0.043750f*iconSize));
            poly.replace(8, QPointF(0.025000f*iconSize, 0.162500f*iconSize));
            poly.replace(9, QPointF(0.137500f*iconSize, 0.181250f*iconSize));
            poly.replace(10, QPointF(0.137500f*iconSize, 0.218750f*iconSize));
            poly.replace(11, QPointF(0.025000f*iconSize, 0.206250f*iconSize));
            poly.replace(12, QPointF(-0.025000f*iconSize, 0.206250f*iconSize));
            poly.replace(13, QPointF(-0.137500f*iconSize, 0.218750f*iconSize));
            poly.replace(14, QPointF(-0.137500f*iconSize, 0.181250f*iconSize));
            poly.replace(15, QPointF(-0.025000f*iconSize, 0.162500f*iconSize));
            poly.replace(16, QPointF(-0.025000f*iconSize, 0.043750f*iconSize));
            poly.replace(17, QPointF(-0.500000f*iconSize, 0.025000f*iconSize));
            poly.replace(18, QPointF(-0.500000f*iconSize, -0.025000f*iconSize));
            poly.replace(19, QPointF(-0.025000f*iconSize, -0.043750f*iconSize));
            poly.replace(20, QPointF(-0.031250f*iconSize, -0.187500f*iconSize));
            poly.replace(21, QPointF(-0.037500f*iconSize, -0.237500f*iconSize));
            poly.replace(22, QPointF(-0.031250f*iconSize, -0.262500f*iconSize));
            poly.replace(23, QPointF(0.000000f*iconSize, -0.312500f*iconSize));
147

148 149 150 151 152 153
            painter.setBrush(QBrush(iconColor));
            QPen iconPen(Qt::black);
            iconPen.setWidthF(1.0f);
            painter.setPen(iconPen);

            painter.drawPolygon(poly);
154
            painter.rotate(-yawRotate);
155 156
        }
        break;
157 158
    case UASInterface::QGC_AIRFRAME_MIKROKOPTER:
    case UASInterface::QGC_AIRFRAME_CHEETAH:
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        {
            // QUADROTOR
            float iconSize = radius*0.9f;
            float yawRotate = (yaw/(float)M_PI)*180.0f + 180.0f;

            painter.rotate(yawRotate);

            //qDebug() << "ICON SIZE:" << radius;

            QPointF front(0, 0.2);
            front = front *iconSize;
            QPointF left(-0.2, 0);
            left = left * iconSize;
            QPointF right(0.2, 0.0);
            right *= iconSize;
            QPointF back(0, -0.2);
            back *= iconSize;

            QPolygonF poly(0);



            painter.setBrush(QBrush(iconColor));
            QPen iconPen(Qt::black);
            iconPen.setWidthF(1.0f);
            painter.setPen(iconPen);

            painter.drawPolygon(poly);

            painter.drawEllipse(left, radius/4/2, radius/4/2);
            painter.drawEllipse(right, radius/4/2, radius/4/2);
            painter.drawEllipse(back, radius/4/2, radius/4/2);

            painter.setBrush(Qt::red);
            painter.drawEllipse(front, radius/4/2, radius/4/2);
194
            painter.rotate(-yawRotate);
195 196
        }
        break;
197 198 199
    case UASInterface::QGC_AIRFRAME_EASYSTAR:
    case UASInterface::QGC_AIRFRAME_MERLIN:
    case UASInterface::QGC_AIRFRAME_TWINSTAR:
200
    case UASInterface::QGC_AIRFRAME_PTERYX:
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        {
            // DRAW AIRPLANE

            float yawRotate = (yaw/(float)M_PI)*180.0f + 180.0f;

            painter.rotate(yawRotate);

            //qDebug() << "ICON SIZE:" << radius;

            float iconSize = radius*0.7f;

            QPolygonF poly(32);
            poly.replace(0, QPointF(0.000000f*iconSize, 0.362319f*iconSize));
            poly.replace(1, QPointF(0.018116f*iconSize, 0.340580f*iconSize));
            poly.replace(2, QPointF(0.028986f*iconSize, 0.318841f*iconSize));
            poly.replace(3, QPointF(0.036232f*iconSize, 0.166667f*iconSize));
            poly.replace(4, QPointF(0.326087f*iconSize, 0.170290f*iconSize));
            poly.replace(5, QPointF(0.420290f*iconSize, 0.130435f*iconSize));
            poly.replace(6, QPointF(0.456522f*iconSize, 0.108696f*iconSize));
            poly.replace(7, QPointF(0.500000f*iconSize, 0.000000f*iconSize));
            poly.replace(8, QPointF(0.456522f*iconSize, 0.021739f*iconSize));
            poly.replace(9, QPointF(0.391304f*iconSize, 0.021739f*iconSize));
            poly.replace(10, QPointF(0.028986f*iconSize, 0.021739f*iconSize));
            poly.replace(11, QPointF(0.021739f*iconSize, -0.239130f*iconSize));
            poly.replace(12, QPointF(0.094203f*iconSize, -0.246377f*iconSize));
            poly.replace(13, QPointF(0.123188f*iconSize, -0.268116f*iconSize));
            poly.replace(14, QPointF(0.144928f*iconSize, -0.304348f*iconSize));
            poly.replace(15, QPointF(0.000000f*iconSize, -0.307971f*iconSize));
            poly.replace(16, QPointF(0.000000f*iconSize, -0.307971f*iconSize));
            poly.replace(17, QPointF(-0.144928f*iconSize, -0.304348f*iconSize));
            poly.replace(18, QPointF(-0.123188f*iconSize, -0.268116f*iconSize));
            poly.replace(19, QPointF(-0.094203f*iconSize, -0.246377f*iconSize));
            poly.replace(20, QPointF(-0.021739f*iconSize, -0.239130f*iconSize));
            poly.replace(21, QPointF(-0.028986f*iconSize, 0.021739f*iconSize));
            poly.replace(22, QPointF(-0.391304f*iconSize, 0.021739f*iconSize));
            poly.replace(23, QPointF(-0.456522f*iconSize, 0.021739f*iconSize));
            poly.replace(24, QPointF(-0.500000f*iconSize, 0.000000f*iconSize));
            poly.replace(25, QPointF(-0.456522f*iconSize, 0.108696f*iconSize));
            poly.replace(26, QPointF(-0.420290f*iconSize, 0.130435f*iconSize));
            poly.replace(27, QPointF(-0.326087f*iconSize, 0.170290f*iconSize));
            poly.replace(28, QPointF(-0.036232f*iconSize, 0.166667f*iconSize));
            poly.replace(29, QPointF(-0.028986f*iconSize, 0.318841f*iconSize));
            poly.replace(30, QPointF(-0.018116f*iconSize, 0.340580f*iconSize));
            poly.replace(31, QPointF(-0.000000f*iconSize, 0.362319f*iconSize));

            painter.setBrush(QBrush(iconColor));
            QPen iconPen(Qt::black);
            iconPen.setWidthF(1.0f);
            painter.setPen(iconPen);

            painter.drawPolygon(poly);
252
            painter.rotate(-yawRotate);
253 254 255
        }
        break;
        case UASInterface::QGC_AIRFRAME_GENERIC:
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    default:
        {
            // GENERIC

            float yawRotate = (yaw/(float)M_PI)*180.0f + 180.0f;

            painter.rotate(yawRotate);

            //qDebug() << "ICON SIZE:" << radius;

            float iconSize = radius*0.9f;
            QPolygonF poly(3);
            poly.replace(0, QPointF(0.0f*iconSize, 0.3f*iconSize));
            poly.replace(1, QPointF(-0.2f*iconSize, -0.2f*iconSize));
            poly.replace(2, QPointF(0.2f*iconSize, -0.2f*iconSize));

272
            painter.setBrush(QBrush(iconColor));
273 274 275
            QPen iconPen(Qt::black);
            iconPen.setWidthF(1.0f);
            painter.setPen(iconPen);
276 277

            painter.drawPolygon(poly);
278
            painter.rotate(-yawRotate);
279 280 281
        }
        break;
    }
282
}