QGCMapPolyline.h 5.44 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 37 38 39

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

    /// 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
58 59 60
    Q_INVOKABLE void beginReset (void);
    Q_INVOKABLE void endReset   (void);

61 62 63 64
    /// 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
65
    Q_INVOKABLE QGeoCoordinate vertexCoordinate(int vertex) const;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    /// 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
90 91
    bool            isValid     (void) const { return _polylineModel.count() >= 2; }
    bool            empty       (void) const { return _polylineModel.count() == 0; }
DoinLakeFlyer's avatar
DoinLakeFlyer committed
92
    bool            traceMode   (void) const { return _traceMode; }
93 94 95 96 97 98 99

    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
100
    void setTraceMode   (bool traceMode);
101 102 103 104 105 106 107 108 109

    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
110 111
    void isValidChanged     (void);
    void isEmptyChanged     (void);
112
    void traceModeChanged   (bool traceMode);
113 114 115 116 117 118

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

private:
Don Gagne's avatar
Don Gagne committed
119 120 121 122 123
    void            _init                   (void);
    QGeoCoordinate  _coordFromPointF        (const QPointF& point) const;
    QPointF         _pointFFromCoord        (const QGeoCoordinate& coordinate) const;
    void            _beginResetIfNotActive  (void);
    void            _endResetIfNotActive    (void);
124 125 126 127 128

    QVariantList        _polylinePath;
    QmlObjectListModel  _polylineModel;
    bool                _dirty;
    bool                _interactive;
Don Gagne's avatar
Don Gagne committed
129
    bool                _resetActive;
130
    bool                _traceMode = false;
131
};