GeoArea.h 2.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#pragma once

#include <QObject>

#include "QGCMapPolygon.h"

class GeoArea : public QGCMapPolygon {
  Q_OBJECT
public:
  GeoArea(QObject *parent = nullptr);
  GeoArea(const GeoArea &other, QObject *parent = nullptr);
  GeoArea &operator=(const GeoArea &other);

  Q_PROPERTY(QString mapVisualQML READ mapVisualQML CONSTANT)
  Q_PROPERTY(QString editorQML READ editorQML CONSTANT)
16
  Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged)
17 18 19 20

  virtual QString mapVisualQML(void) const = 0;
  virtual QString editorQML(void) const = 0;

21
  virtual bool saveToJson(QJsonObject &jsonObject);
22 23 24 25 26
  virtual bool loadFromJson(const QJsonObject &jsonObject,
                            QString &errorString);

  virtual GeoArea *clone(QObject *parent = nullptr) const = 0;

27 28
  Q_INVOKABLE virtual bool isCorrect();
  Q_INVOKABLE QString errorString() const;
29

30 31 32
  //!
  //! \brief covers Checks if GeoArea covers c.
  //! \param c
33 34
  //! \return  Returns true if c is inside, or on the border of GeoArea, false
  //! either.
35 36 37
  //!
  Q_INVOKABLE bool covers(const QGeoCoordinate &c);

38
  // static Members
39
  static const char *nameString;
40
  static const char *areaTypeKey;
41 42
  static const char *settingsGroup;

43 44 45 46 47 48 49
signals:
  void errorStringChanged();

protected:
  void setErrorString(const QString &str);
  void setErrorString(const std::string &str);

50 51
private:
  void init();
52 53

  QString _errorString;
54 55 56 57 58
};

// Example usage:
// QmlObjecListModel list;
// .... add areas ....
59 60 61 62 63 64 65 66
// auto area = getGeoArea<MeasurementArea *>(list); // returns the first
// MeasurementArea or nullptr
//! \example
//!
//! QmlObjecListModel list;
//! // .... add areas ....
//! auto area = getGeoArea<MeasurementArea *>(list); // returns the first
//! MeasurementArea or nullptr
67 68 69 70 71 72 73 74 75 76 77 78 79 80
template <class AreaPtr, class QObjectList>
inline AreaPtr getGeoArea(QObjectList &list) {
  static_assert(std::is_pointer<AreaPtr>::value,
                "AreaPtr must be a pointer type.");
  for (int i = 0; i < list.count(); ++i) {
    auto obj = list[i];
    auto area = qobject_cast<AreaPtr>(obj);
    if (area != nullptr) {
      return area;
    }
  }
  return nullptr;
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
//! \example
//!
//! QmlObjecListModel list;
//! // .... add areas ....
//! auto areaArray = getGeoAreaArray<MeasurementArea *>(list); // returns a
//! QList<MeasurementArea*>
//! // containing all MeasurementArea's inside list
template <class AreaPtr, template <class> class Container = QList,
          class QObjectList>
inline Container<AreaPtr> getGeoAreaList(QObjectList &list) {
  static_assert(std::is_pointer<AreaPtr>::value,
                "AreaPtr must be a pointer type.");
  Container<AreaPtr> out;
  for (int i = 0; i < list.count(); ++i) {
    auto obj = list[i];
    auto area = qobject_cast<AreaPtr>(obj);
    if (area != nullptr) {
      out.append(area);
    }
  }
  return out;
}

104 105
bool copyAreaList(const QmlObjectListModel &from, QmlObjectListModel &to,
                  QObject *parent);