WimaTrackerPolyline.cc 4.77 KB
Newer Older
1 2
#include "WimaTrackerPolyline.h"

3 4
WimaTrackerPolyline::WimaTrackerPolyline(QObject *parent)
    : WimaTrackerPolyline (nullptr, parent)
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
}

WimaTrackerPolyline::WimaTrackerPolyline(QGCMapPolyline *other, QObject *parent)
    :QGCMapPolyline (other, parent)
    , _boundArea  (nullptr)
    , _startVertexIndex(0)
    , _endVertexIndex(0)
{
}

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

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

36
void WimaTrackerPolyline::swapEndPoints()
37 38 39 40
{
    int storage         = _startVertexIndex;
    _startVertexIndex   = _endVertexIndex;
    _endVertexIndex     = storage;
41
    recalcPolyline();
42 43 44 45 46 47 48 49 50 51 52 53 54
}


void WimaTrackerPolyline::setStartVertexIndex(int PolygonVertexIndex)
{
    if(PolygonVertexIndex >= 0 && PolygonVertexIndex < _boundArea->count()){
        _startVertexIndex = PolygonVertexIndex;
        emit startVertexIndexChanged();
        recalcPolyline();
    }else{
        qWarning("WimaTrackerPolyline::setStartVertexIndex(): Index out of bounds!");
    }
}
55

56 57 58 59 60 61 62 63 64
void WimaTrackerPolyline::setEndVertexIndex(int PolygonVertexIndex)
{
    if(PolygonVertexIndex >= 0 && PolygonVertexIndex < _boundArea->count()){
        _endVertexIndex = PolygonVertexIndex;
        emit endVertexIndexChanged();
        recalcPolyline();
    }else{
        qWarning("WimaTrackerPolyline::setEndVertexIndex(): Index out of bounds!");
    }
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 112 113 114 115 116 117 118

void WimaTrackerPolyline::snapVertex(int polylineVertexIndex)
{
    int polylineVertexCount = this->count();
    if (polylineVertexIndex >= 0 && polylineVertexIndex < polylineVertexCount){
        if (polylineVertexIndex == 0){
            snapStartVertex();
        }else if (polylineVertexIndex == polylineVertexCount-1) {
            snapEndVertex();
        }else {
            int polygonVertexIndex = _startVertexIndex + polylineVertexIndex;
            int polygonVertexCount = _boundArea->count();
            if (polygonVertexIndex >= polygonVertexCount){
                polygonVertexIndex = polygonVertexIndex - polygonVertexCount;
                if (polygonVertexCount > _endVertexIndex) {
                    // This branch sould actually not be reachable, just in case...
                    qWarning("WimaTrackerPolyline::snapVertex(): polylineVertexIndex out of bounds!");
                    return;
                }
            }
            this->adjustVertex(polylineVertexIndex, _boundArea->vertexCoordinate(polygonVertexIndex));
        }
    }else {
        qWarning("WimaTrackerPolyline::snapVertex(): Index out of bounds!");
    }
}

void WimaTrackerPolyline::snapEndVertex()
{
    if (_boundArea != nullptr && _boundArea->count() >= 3){
        int currentVertexIndex = this->count()-1;
        int closestVertexIndex          = _boundArea->getClosestVertexIndex(this->vertexCoordinate(currentVertexIndex));
        QGeoCoordinate closestVertex    = _boundArea->vertexCoordinate(closestVertexIndex);
        _endVertexIndex                 = closestVertexIndex;
        this->adjustVertex(currentVertexIndex, closestVertex);
        this->recalcPolyline();
    }
}

void WimaTrackerPolyline::snapStartVertex()
{
    if (_boundArea != nullptr && _boundArea->count() >= 3){
        int currentVertexIndex = 0;
        int closestVertexIndex          = _boundArea->getClosestVertexIndex(this->vertexCoordinate(currentVertexIndex));
        QGeoCoordinate closestVertex    = _boundArea->vertexCoordinate(closestVertexIndex);
        _startVertexIndex               = closestVertexIndex;
        this->adjustVertex(currentVertexIndex, closestVertex);
        this->recalcPolyline();
    }
}

void WimaTrackerPolyline::recalcPolyline()
{
119
    //qWarning("WimaTrackerPolyline::recalcPolyline()");
120 121 122 123 124 125 126 127
    if (_boundArea != nullptr && _boundArea->count() > 0){
        if (_startVertexIndex >= _boundArea->count()){
            _startVertexIndex = 0;
        }
        if (_endVertexIndex >= _boundArea->count()){
            _endVertexIndex = _boundArea->count()-1;
        }

128
        this->clear();
129 130
        int i = _startVertexIndex;
        while(1){
131
            this->appendVertex(_boundArea->vertexCoordinate(i));
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
            if (i == _boundArea->count()-1 && i != _endVertexIndex){
                i = 0;
            }else if (i == _endVertexIndex) {
                break;
            }else {
                i++;
            }

        }
    }else{
        qWarning("WimaTrackerPolyline::recalcPolyline(): No object bound!");
    }
}