QGCMapPolygon.h 3.21 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
/// The QGCMapPolygon class provides a polygon which can be displayed on a map using a MapPolygon control.
/// It works in conjunction with the QGCMapPolygonControls control which provides the UI for drawing and
/// editing map polygons.
21 22 23 24 25 26 27 28 29
class QGCMapPolygon : public QObject
{
    Q_OBJECT

public:
    QGCMapPolygon(QObject* parent = NULL);

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

30 31 32 33
    QGeoCoordinate operator[](int index) const { return _polygonPath[index].value<QGeoCoordinate>(); }

    /// The polygon path to be bound to the MapPolygon.path property
    Q_PROPERTY(QVariantList path READ path WRITE setPath NOTIFY pathChanged)
34

35 36 37 38
    /// true: Polygon has changed since last time dirty was false
    Q_PROPERTY(bool dirty READ dirty WRITE setDirty NOTIFY dirtyChanged)

    /// Remove all points from polygon
39
    Q_INVOKABLE void clear(void);
40 41 42 43

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

46 47
    /// Returns the center point coordinate for the polygon
    Q_INVOKABLE QGeoCoordinate center(void) const;
48

49 50
    /// Returns true if the specified coordinate is within the polygon
    Q_INVOKABLE bool containsCoordinate(const QGeoCoordinate& coordinate) const;
51

52 53
    /// Returns the number of points in the polygon
    Q_INVOKABLE int count(void) const { return _polygonPath.count(); }
54

55 56
    /// Returns the path in a list of QGeoCoordinate's format
    QList<QGeoCoordinate> coordinateList(void) const;
57

58 59 60 61 62 63 64 65 66 67 68
    /// 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);

69 70 71 72 73 74 75 76 77
    // Property methods

    bool dirty(void) const { return _dirty; }
    void setDirty(bool dirty);

    QVariantList path(void) const { return _polygonPath; }
    void setPath(const QList<QGeoCoordinate>& path);
    void setPath(const QVariantList& path);

78 79 80 81 82
signals:
    void pathChanged(void);
    void dirtyChanged(bool dirty);

private:
83 84 85 86
    QPolygonF _toPolygonF(void) const;
    QGeoCoordinate _coordFromPointF(const QPointF& point) const;
    QPointF _pointFFromCoord(const QGeoCoordinate& coordinate) const;

87 88
    QVariantList    _polygonPath;
    bool            _dirty;
89 90

    static const char* _jsonPolygonKey;
91 92 93
};

#endif