QGCMapPolygon.h 6.07 KB
Newer Older
1
/****************************************************************************
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * 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 19 20 21
#include "QmlObjectListModel.h"

/// 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.
22 23 24 25 26
class QGCMapPolygon : public QObject
{
    Q_OBJECT

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

30 31 32 33 34 35 36 37 38
    const QGCMapPolygon& operator=(const QGCMapPolygon& 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(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)
39
    Q_PROPERTY(double               area        READ area                                   NOTIFY areaChanged)
40

41
    Q_INVOKABLE void clear(void);
42 43
    Q_INVOKABLE void appendVertex(const QGeoCoordinate& coordinate);
    Q_INVOKABLE void removeVertex(int vertexIndex);
44
    Q_INVOKABLE void appendVertices(const QList<QGeoCoordinate>& coordinates);
45 46 47 48

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

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

54 55
    /// Returns true if the specified coordinate is within the polygon
    Q_INVOKABLE bool containsCoordinate(const QGeoCoordinate& coordinate) const;
56

57 58 59
    /// Offsets the current polygon edges by the specified distance in meters
    Q_INVOKABLE void offset(double distance);

60
    /// Loads a polygon from a KML/SH{ file
DonLakeFlyer's avatar
DonLakeFlyer committed
61
    /// @return true: success
62
    Q_INVOKABLE bool loadKMLOrSHPFile(const QString& file);
DonLakeFlyer's avatar
DonLakeFlyer committed
63

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

67
    /// Returns the QGeoCoordinate for the vertex specified
68
    Q_INVOKABLE QGeoCoordinate vertexCoordinate(int vertex) const;
69

70 71 72
    /// Adjust polygon winding order to be clockwise (if needed)
    Q_INVOKABLE void verifyClockwiseWinding(void);

73 74 75 76 77 78 79 80 81 82 83
    /// 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);

84
    /// Convert polygon to NED and return (D is ignored)
85 86 87 88
    QList<QPointF> nedPolygon(void) const;

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

90 91
    // Property methods

DonLakeFlyer's avatar
DonLakeFlyer committed
92 93 94 95 96
    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; }
97
    bool            interactive (void) const { return _interactive; }
98 99 100 101

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

103 104 105 106 107
    // Friends
    friend void print(const QGCMapPolygon& poly, QString& outputString);
    friend void print(const QGCMapPolygon& poly);

    // static Variables
108
    static const char* jsonPolygonKey;
109

110
signals:
DonLakeFlyer's avatar
DonLakeFlyer committed
111 112 113 114 115 116
    void countChanged       (int count);
    void pathChanged        (void);
    void dirtyChanged       (bool dirty);
    void cleared            (void);
    void centerChanged      (QGeoCoordinate center);
    void centerDragChanged  (bool centerDrag);
117
    void interactiveChanged (bool interactive);
118
    void areaChanged        (void);
119

120 121
public slots:
    void setPath                    (const QList<QGeoCoordinate>& path);
122
    void setPath                    (const QVector<QGeoCoordinate>& path);
123 124 125 126 127
    void setPath                    (const QVariantList& path);
    void setCenter                  (QGeoCoordinate newCenter);
    void setCenterDrag              (bool centerDrag);
    void setInteractive             (bool interactive);

128
private slots:
129 130 131
    void _polygonModelCountChanged  (int count);
    void _polygonModelDirtyChanged  (bool dirty);
    void _updateCenter              (void);
132

133
private:
134
    void _init(void);
135 136 137 138
    QPolygonF _toPolygonF(void) const;
    QGeoCoordinate _coordFromPointF(const QPointF& point) const;
    QPointF _pointFFromCoord(const QGeoCoordinate& coordinate) const;

139 140 141
    QVariantList        _polygonPath;
    QmlObjectListModel  _polygonModel;
    bool                _dirty;
142
    QGeoCoordinate      _center;
DonLakeFlyer's avatar
DonLakeFlyer committed
143
    bool                _centerDrag;
144
    bool                _ignoreCenterUpdates;
145
    bool                _interactive;
146 147 148
};

#endif