QGCMapPolygon.h 7.71 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
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#ifndef QGCMapPolygon_H
#define QGCMapPolygon_H

#include <QObject>
#include <QGeoCoordinate>
#include <QVariantList>
16
#include <QPolygon>
17

18
#include "QmlObjectListModel.h"
19
#include "KMLDomDocument.h"
20 21 22

/// The QGCMapPolygon class provides a polygon which can be displayed on a map using a map visuals control.
/// It maintains a representation of the polygon on QVariantList and QmlObjectListModel format.
23 24 25 26 27
class QGCMapPolygon : public QObject
{
    Q_OBJECT

public:
28 29
    QGCMapPolygon(QObject* parent = nullptr);
    QGCMapPolygon(const QGCMapPolygon& other, QObject* parent = nullptr);
30

31 32
    const QGCMapPolygon& operator=(const QGCMapPolygon& other);

33 34
    Q_PROPERTY(int                  count           READ count                                  NOTIFY countChanged)
    Q_PROPERTY(QVariantList         path            READ path                                   NOTIFY pathChanged)
35
    Q_PROPERTY(double               area            READ area                                   NOTIFY pathChanged)
36 37 38 39 40 41 42 43 44
    Q_PROPERTY(QmlObjectListModel*  pathModel       READ qmlPathModel                           CONSTANT)
    Q_PROPERTY(bool                 dirty           READ dirty          WRITE setDirty          NOTIFY dirtyChanged)
    Q_PROPERTY(QGeoCoordinate       center          READ center         WRITE setCenter         NOTIFY centerChanged)
    Q_PROPERTY(bool                 centerDrag      READ centerDrag     WRITE setCenterDrag     NOTIFY centerDragChanged)
    Q_PROPERTY(bool                 interactive     READ interactive    WRITE setInteractive    NOTIFY interactiveChanged)
    Q_PROPERTY(bool                 isValid         READ isValid                                NOTIFY isValidChanged)
    Q_PROPERTY(bool                 empty           READ empty                                  NOTIFY isEmptyChanged)
    Q_PROPERTY(bool                 traceMode       READ traceMode      WRITE setTraceMode      NOTIFY traceModeChanged)
    Q_PROPERTY(bool                 showAltColor    READ showAltColor   WRITE setShowAltColor   NOTIFY showAltColorChanged)
45
    Q_PROPERTY(int                  selectedVertex  READ selectedVertex WRITE selectVertex      NOTIFY selectedVertexChanged)
46

47
    Q_INVOKABLE void clear(void);
48 49
    Q_INVOKABLE void appendVertex(const QGeoCoordinate& coordinate);
    Q_INVOKABLE void removeVertex(int vertexIndex);
50 51 52
    Q_INVOKABLE void appendVertices(const QVariantList& varCoords);

    void appendVertices(const QList<QGeoCoordinate>& coordinates);
53 54 55 56

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

    /// Splits the segment comprised of vertextIndex -> vertexIndex + 1
    Q_INVOKABLE void splitPolygonSegment(int vertexIndex);
61

62 63
    /// Returns true if the specified coordinate is within the polygon
    Q_INVOKABLE bool containsCoordinate(const QGeoCoordinate& coordinate) const;
64

65 66 67
    /// Offsets the current polygon edges by the specified distance in meters
    Q_INVOKABLE void offset(double distance);

68
    /// Loads a polygon from a KML/SH{ file
DonLakeFlyer's avatar
DonLakeFlyer committed
69
    /// @return true: success
70
    Q_INVOKABLE bool loadKMLOrSHPFile(const QString& file);
DonLakeFlyer's avatar
DonLakeFlyer committed
71

72 73
    /// Returns the path in a list of QGeoCoordinate's format
    QList<QGeoCoordinate> coordinateList(void) const;
74

75
    /// Returns the QGeoCoordinate for the vertex specified
76
    Q_INVOKABLE QGeoCoordinate vertexCoordinate(int vertex) const;
77

78 79 80
    /// Adjust polygon winding order to be clockwise (if needed)
    Q_INVOKABLE void verifyClockwiseWinding(void);

Don Gagne's avatar
Don Gagne committed
81 82 83
    Q_INVOKABLE void beginReset (void);
    Q_INVOKABLE void endReset   (void);

84 85 86 87 88 89 90 91 92 93 94
    /// Saves the polygon to the json object.
    ///     @param json Json object to save to
    void saveToJson(QJsonObject& json);

    /// Load a polygon 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);

95
    /// Convert polygon to NED and return (D is ignored)
96 97 98 99
    QList<QPointF> nedPolygon(void) const;

    /// Returns the area of the polygon in meters squared
    double area(void) const;
100

101 102
    QDomElement kmlPolygonElement(KMLDomDocument& domDocument);

103 104
    // Property methods

DonLakeFlyer's avatar
DonLakeFlyer committed
105 106 107 108 109
    int             count       (void) const { return _polygonPath.count(); }
    bool            dirty       (void) const { return _dirty; }
    void            setDirty    (bool dirty);
    QGeoCoordinate  center      (void) const { return _center; }
    bool            centerDrag  (void) const { return _centerDrag; }
110
    bool            interactive (void) const { return _interactive; }
Don Gagne's avatar
Don Gagne committed
111 112
    bool            isValid     (void) const { return _polygonModel.count() >= 3; }
    bool            empty       (void) const { return _polygonModel.count() == 0; }
DoinLakeFlyer's avatar
DoinLakeFlyer committed
113
    bool            traceMode   (void) const { return _traceMode; }
114
    bool            showAltColor(void) const { return _showAltColor; }
115
    int             selectedVertex()   const { return _selectedVertexIndex; }
116 117 118 119

    QVariantList        path        (void) const { return _polygonPath; }
    QmlObjectListModel* qmlPathModel(void) { return &_polygonModel; }
    QmlObjectListModel& pathModel   (void) { return _polygonModel; }
120

DonLakeFlyer's avatar
DonLakeFlyer committed
121 122 123 124
    void setPath        (const QList<QGeoCoordinate>& path);
    void setPath        (const QVariantList& path);
    void setCenter      (QGeoCoordinate newCenter);
    void setCenterDrag  (bool centerDrag);
125
    void setInteractive (bool interactive);
DoinLakeFlyer's avatar
DoinLakeFlyer committed
126
    void setTraceMode   (bool traceMode);
127
    void setShowAltColor(bool showAltColor);
128
    void selectVertex   (int index);
129 130

    static const char* jsonPolygonKey;
131

132
signals:
DonLakeFlyer's avatar
DonLakeFlyer committed
133 134 135 136 137 138
    void countChanged       (int count);
    void pathChanged        (void);
    void dirtyChanged       (bool dirty);
    void cleared            (void);
    void centerChanged      (QGeoCoordinate center);
    void centerDragChanged  (bool centerDrag);
139
    void interactiveChanged (bool interactive);
DonLakeFlyer's avatar
DonLakeFlyer committed
140 141
    bool isValidChanged     (void);
    bool isEmptyChanged     (void);
142
    void traceModeChanged   (bool traceMode);
143
    void showAltColorChanged(bool showAltColor);
144
    void selectedVertexChanged(int index);
145

146 147 148
private slots:
    void _polygonModelCountChanged(int count);
    void _polygonModelDirtyChanged(bool dirty);
149
    void _updateCenter(void);
150

151
private:
Don Gagne's avatar
Don Gagne committed
152 153 154 155 156 157
    void            _init                   (void);
    QPolygonF       _toPolygonF             (void) const;
    QGeoCoordinate  _coordFromPointF        (const QPointF& point) const;
    QPointF         _pointFFromCoord        (const QGeoCoordinate& coordinate) const;
    void            _beginResetIfNotActive  (void);
    void            _endResetIfNotActive    (void);
158

159 160
    QVariantList        _polygonPath;
    QmlObjectListModel  _polygonModel;
161
    bool                _dirty =                false;
162
    QGeoCoordinate      _center;
163 164 165 166 167 168
    bool                _centerDrag =           false;
    bool                _ignoreCenterUpdates =  false;
    bool                _interactive =          false;
    bool                _resetActive =          false;
    bool                _traceMode =            false;
    bool                _showAltColor =         false;
169
    int                 _selectedVertexIndex =  -1;
170 171 172
};

#endif