WimaPolyline.cc 2.46 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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
#include "WimaPolyline.h"

WimaPolyline::WimaPolyline(QObject *parent)
    : QObject       (parent)
    , _boundPolygon  (nullptr)
    , _startVertexIndex(0)
    , _endVertexIndex(1)
{
    connect(&_polyline, &QGCMapPolyline::interactiveChanged, this, &WimaPolyline::interactiveChanged);
}

WimaPolyline::WimaPolyline(QGCMapPolyline *polyline, QObject *parent)
    :WimaPolyline(parent)
{
    _polyline = *polyline;
}

void WimaPolyline::bindPolygon(QGCMapPolygon *polygon)
{
    if(polygon != nullptr){
        _boundPolygon = polygon;
        connect(polygon, &QGCMapPolygon::pathChanged, this, &WimaPolyline::recalcPolyline);
    }else{
        qWarning("Invalid Object!");
    }
}

void WimaPolyline::unbindPolygon()
{
    if(_boundPolygon != nullptr){
        disconnect(_boundPolygon, &QGCMapPolygon::pathChanged, this, &WimaPolyline::recalcPolyline);
        _boundPolygon = nullptr;
    }else{
        qWarning("No Object bound!");
    }
}

void WimaPolyline::swapLimits()
{
    int storage         = _startVertexIndex;
    _startVertexIndex   = _endVertexIndex;
    _endVertexIndex     = storage;
}

void WimaPolyline::setInteractive(bool interactive)
{
    _polyline.setInteractive(interactive);
}
void WimaPolyline::setStartVertexIndex(int PolygonVertexIndex)
{
    if(PolygonVertexIndex >= 0 && PolygonVertexIndex < _boundPolygon->count()){
        _startVertexIndex = PolygonVertexIndex;
        recalcPolyline();
    }else{
        qWarning("Index out of bounds!");
    }
}

void WimaPolyline::setEndVertexIndex(int PolygonVertexIndex)
{
    if(PolygonVertexIndex >= 0 && PolygonVertexIndex < _boundPolygon->count()){
        _endVertexIndex = PolygonVertexIndex;
        recalcPolyline();
    }else{
        qWarning("Index out of bounds!");
    }
}

void WimaPolyline::recalcPolyline()
{
    if (_boundPolygon != nullptr && _boundPolygon->count() > 0){
        if (_startVertexIndex >= _boundPolygon->count()){
            _startVertexIndex = 0;
        }
        if (_endVertexIndex >= _boundPolygon->count()){
            _startVertexIndex = _boundPolygon->count()-1;
        }

        _polyline.clear();
        int i = _startVertexIndex;
        while(1){
            _polyline.appendVertex(_boundPolygon->vertexCoordinate(i));
            if (i == _boundPolygon->count()-1){
                i = 0;
            }else if (i == _endVertexIndex) {
                break;
            }
            i++;
        }

        emit polylineChanged();
    }else{
        qWarning("No object bound!");
    }
}