QGCMapPolyline.h 5.76 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#pragma once

#include <QObject>
#include <QGeoCoordinate>
#include <QVariantList>

#include "QmlObjectListModel.h"

class QGCMapPolyline : public QObject
{
    Q_OBJECT

public:
23 24
    QGCMapPolyline(QObject* parent = nullptr);
    QGCMapPolyline(const QGCMapPolyline& other, QObject* parent = nullptr);
25 26 27 28 29 30 31 32

    const QGCMapPolyline& operator=(const QGCMapPolyline& other);

    Q_PROPERTY(int                  count       READ count                                  NOTIFY countChanged)
    Q_PROPERTY(QVariantList         path        READ path                                   NOTIFY pathChanged)
    Q_PROPERTY(QmlObjectListModel*  pathModel   READ qmlPathModel                           CONSTANT)
    Q_PROPERTY(bool                 dirty       READ dirty          WRITE setDirty          NOTIFY dirtyChanged)
    Q_PROPERTY(bool                 interactive READ interactive    WRITE setInteractive    NOTIFY interactiveChanged)
DonLakeFlyer's avatar
DonLakeFlyer committed
33 34
    Q_PROPERTY(bool                 isValid     READ isValid                                NOTIFY isValidChanged)
    Q_PROPERTY(bool                 empty       READ empty                                  NOTIFY isEmptyChanged)
DoinLakeFlyer's avatar
DoinLakeFlyer committed
35
    Q_PROPERTY(bool                 traceMode   READ traceMode      WRITE setTraceMode      NOTIFY traceModeChanged)
36
    Q_PROPERTY(int              selectedVertex  READ selectedVertex WRITE selectVertex      NOTIFY selectedVertexChanged)
37 38 39 40

    Q_INVOKABLE void clear(void);
    Q_INVOKABLE void appendVertex(const QGeoCoordinate& coordinate);
    Q_INVOKABLE void removeVertex(int vertexIndex);
DonLakeFlyer's avatar
DonLakeFlyer committed
41
    Q_INVOKABLE void appendVertices(const QList<QGeoCoordinate>& coordinates);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

    /// Adjust the value for the specified coordinate
    ///     @param vertexIndex Polygon point index to modify (0-based)
    ///     @param coordinate New coordinate for point
    Q_INVOKABLE void adjustVertex(int vertexIndex, const QGeoCoordinate coordinate);

    /// Splits the line segment comprised of vertexIndex -> vertexIndex + 1
    Q_INVOKABLE void splitSegment(int vertexIndex);

    /// Offsets the current polyline edges by the specified distance in meters
    /// @return Offset set of vertices
    QList<QGeoCoordinate> offsetPolyline(double distance);

    /// Loads a polyline from a KML file
    /// @return true: success
    Q_INVOKABLE bool loadKMLFile(const QString& kmlFile);

Don Gagne's avatar
Don Gagne committed
59 60 61
    Q_INVOKABLE void beginReset (void);
    Q_INVOKABLE void endReset   (void);

62 63 64 65
    /// Returns the path in a list of QGeoCoordinate's format
    QList<QGeoCoordinate> coordinateList(void) const;

    /// Returns the QGeoCoordinate for the vertex specified
Don Gagne's avatar
Don Gagne committed
66
    Q_INVOKABLE QGeoCoordinate vertexCoordinate(int vertex) const;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

    /// Saves the polyline to the json object.
    ///     @param json Json object to save to
    void saveToJson(QJsonObject& json);

    /// Load a polyline from json
    ///     @param json Json object to load from
    ///     @param required true: no polygon in object will generate error
    ///     @param errorString Error string if return is false
    /// @return true: success, false: failure (errorString set)
    bool loadFromJson(const QJsonObject& json, bool required, QString& errorString);

    /// Convert polyline to NED and return (D is ignored)
    QList<QPointF> nedPolyline(void);

    /// Returns the length of the polyline in meters
    double length(void) const;

    // Property methods
    int             count       (void) const { return _polylinePath.count(); }
    bool            dirty       (void) const { return _dirty; }
    void            setDirty    (bool dirty);
    bool            interactive (void) const { return _interactive; }
    QVariantList    path        (void) const { return _polylinePath; }
Don Gagne's avatar
Don Gagne committed
91 92
    bool            isValid     (void) const { return _polylineModel.count() >= 2; }
    bool            empty       (void) const { return _polylineModel.count() == 0; }
DoinLakeFlyer's avatar
DoinLakeFlyer committed
93
    bool            traceMode   (void) const { return _traceMode; }
94
    int             selectedVertex()   const { return _selectedVertexIndex; }
95 96 97 98 99 100 101

    QmlObjectListModel* qmlPathModel(void) { return &_polylineModel; }
    QmlObjectListModel& pathModel   (void) { return _polylineModel; }

    void setPath        (const QList<QGeoCoordinate>& path);
    void setPath        (const QVariantList& path);
    void setInteractive (bool interactive);
DoinLakeFlyer's avatar
DoinLakeFlyer committed
102
    void setTraceMode   (bool traceMode);
103
    void selectVertex   (int index);
104 105 106 107 108 109 110 111 112

    static const char* jsonPolylineKey;

signals:
    void countChanged       (int count);
    void pathChanged        (void);
    void dirtyChanged       (bool dirty);
    void cleared            (void);
    void interactiveChanged (bool interactive);
DonLakeFlyer's avatar
DonLakeFlyer committed
113 114
    void isValidChanged     (void);
    void isEmptyChanged     (void);
115
    void traceModeChanged   (bool traceMode);
116
    void selectedVertexChanged(int index);
117 118 119 120 121 122

private slots:
    void _polylineModelCountChanged(int count);
    void _polylineModelDirtyChanged(bool dirty);

private:
Don Gagne's avatar
Don Gagne committed
123 124 125 126 127
    void            _init                   (void);
    QGeoCoordinate  _coordFromPointF        (const QPointF& point) const;
    QPointF         _pointFFromCoord        (const QGeoCoordinate& coordinate) const;
    void            _beginResetIfNotActive  (void);
    void            _endResetIfNotActive    (void);
128 129 130 131 132

    QVariantList        _polylinePath;
    QmlObjectListModel  _polylineModel;
    bool                _dirty;
    bool                _interactive;
Don Gagne's avatar
Don Gagne committed
133
    bool                _resetActive;
134
    bool                _traceMode = false;
135
    int                 _selectedVertexIndex = -1;
136
};