AreaData.cc 5.97 KB
Newer Older
1
#include "AreaData.h"
2

3 4
#include "geometry/MeasurementArea.h"
#include "geometry/SafeArea.h"
5

6 7
#include "QGCLoggingCategory.h"
#include "QGCQGeoCoordinate.h"
8

9
QGC_LOGGING_CATEGORY(AreaDataLog, "AreaDataLog")
10

11
AreaData::AreaData(QObject *parent) : QObject(parent) {}
12

13
AreaData::~AreaData() {}
14

15 16 17 18 19
AreaData::AreaData(const AreaData &other, QObject *parent) : QObject(parent) {
  if (!copyAreaList(other._areaList, _areaList, this)) {
    qCWarning(AreaDataLog) << "AreaData(): not able to copy other._areaList";
  } else {
    _origin = other._origin;
20 21 22
  }
}

23 24 25 26 27
AreaData &AreaData::operator=(const AreaData &other) {
  if (!copyAreaList(other._areaList, _areaList, this)) {
    qCWarning(AreaDataLog) << "operator=(): not able to copy other._areaList";
  } else {
    _origin = other._origin;
28
  }
29
  return *this;
30 31
}

32
bool AreaData::insert(GeoArea *areaData) {
33 34 35 36 37
  if (areaData != nullptr) {
    if (Q_LIKELY(!this->_areaList.contains(areaData))) {
      _areaList.append(areaData);
      emit areaList();
      return true;
38
    }
39 40
  }

41
  return false;
42 43
}

44 45 46 47
void AreaData::remove(GeoArea *areaData) {
  int index = _areaList.indexOf(areaData);
  if (index >= 0) {
    QObject *obj = _areaList.removeAt(index);
48

49
    _setOrigin(_newOrigin());
50

51 52
    if (obj->parent() == nullptr) {
      obj->deleteLater();
53 54
    }

55 56
    emit areaListChanged();
  }
57 58
}

59 60 61 62 63 64 65
void AreaData::clear() {
  if (_areaList.count() > 0) {
    for (int i = 0; i < _areaList.count(); ++i) {
      remove(_areaList.value<GeoArea *>(i));
    }
    emit areaListChanged();
  }
66 67
}

68
QmlObjectListModel *AreaData::areaList() { return &_areaList; }
69

70
const QmlObjectListModel *AreaData::areaList() const { return &_areaList; }
71

72
const QGeoCoordinate &AreaData::origin() const { return _origin; }
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
bool AreaData::isValid() const {
  qWarning("AreaData::isValid(): impl. incomplete.");
  auto *measurementArea = getGeoArea<const MeasurementArea *>(_areaList);
  auto *safeArea = getGeoArea<const SafeArea *>(_areaList);
  return measurementArea != nullptr && safeArea != nullptr &&
         measurementArea->count() >= 3 && safeArea->count() >= 3 &&
         _origin.isValid();
}

bool AreaData::tryMakeValid() {
  qWarning("AreaData::tryMakeValid(): impl. missing.");
  return true;
}

bool AreaData::initialize(const QGeoCoordinate &bottomLeft,
                          const QGeoCoordinate &topRight) {
  // bottomLeft and topRight define the bounding box.
  if (bottomLeft.isValid() && topRight.isValid() && bottomLeft != topRight) {
    auto *measurementArea = getGeoArea<MeasurementArea *>(_areaList);
    auto *safeArea = getGeoArea<SafeArea *>(_areaList);

    if (safeArea == nullptr) {
96 97 98
      safeArea = new SafeArea(this);
      if (!insert(safeArea)) {
        safeArea->deleteLater();
99 100 101 102 103
        qCCritical(AreaDataLog)
            << "initialize(): safeArea == nullptr, but insert() failed.";
        return false;
      }
    }
104

105
    if (measurementArea == nullptr) {
106 107 108
      measurementArea = new MeasurementArea(this);
      if (!insert(measurementArea)) {
        measurementArea->deleteLater();
109 110 111 112 113
        qCCritical(AreaDataLog) << "initialize(): measurementArea == nullptr, "
                                   "but insert() failed.";
        return false;
      }
    }
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    // Fit safe area to bounding box.
    safeArea->clear();
    safeArea->appendVertex(bottomLeft);
    safeArea->appendVertex(
        QGeoCoordinate(topRight.latitude(), bottomLeft.longitude()));
    safeArea->appendVertex(topRight);
    safeArea->appendVertex(
        QGeoCoordinate(bottomLeft.latitude(), topRight.longitude()));

    // Put measurement area inside safeArea;
    measurementArea->clear();
    measurementArea->appendVertex(QGeoCoordinate(
        0.8 * bottomLeft.latitude() + 0.2 * topRight.latitude(),
        0.8 * bottomLeft.longitude() + 0.2 * topRight.longitude()));
    measurementArea->appendVertex(QGeoCoordinate(
        0.2 * bottomLeft.latitude() + 0.8 * topRight.latitude(),
        0.8 * bottomLeft.longitude() + 0.2 * topRight.longitude()));
    measurementArea->appendVertex(QGeoCoordinate(
        0.2 * bottomLeft.latitude() + 0.8 * topRight.latitude(),
        0.2 * bottomLeft.longitude() + 0.8 * topRight.longitude()));
    measurementArea->appendVertex(QGeoCoordinate(
        0.8 * bottomLeft.latitude() + 0.2 * topRight.latitude(),
        0.2 * bottomLeft.longitude() + 0.8 * topRight.longitude()));
    return true;
  } else {
    qCWarning(AreaDataLog)
        << "initialize(): bounding box invaldid (bottomLeft, topRight) "
        << bottomLeft << "," << topRight;
    return false;
  }
}
146

147 148 149 150 151
bool AreaData::initialized() {
  auto *measurementArea = getGeoArea<MeasurementArea *>(_areaList);
  auto *safeArea = getGeoArea<SafeArea *>(_areaList);
  return measurementArea != nullptr && safeArea != nullptr &&
         measurementArea->count() >= 3 && safeArea->count() >= 3;
152 153 154
}

bool AreaData::operator==(const AreaData &other) const {
155 156 157 158 159 160 161 162 163 164
  if (_areaList.count() == other._areaList.count()) {
    for (int i = 0; i < _areaList.count(); ++i) {
      if (_areaList[i] != other._areaList[i]) {
        return false;
      }
    }
    return true;
  } else {
    return false;
  }
165 166 167 168 169
}
bool AreaData::operator!=(const AreaData &other) const {
  return !(*this == other);
}

170
void AreaData::_setOrigin(const QGeoCoordinate &origin) {
171 172 173 174 175
  if (this->_origin != origin) {
    this->_origin = origin;
    emit originChanged();
  }
}
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

QGeoCoordinate AreaData::_newOrigin() {
  auto *measurementArea = getGeoArea<MeasurementArea *>(_areaList);
  auto *safeArea = getGeoArea<SafeArea *>(_areaList);
  if (measurementArea != nullptr && measurementArea->pathModel().count() > 0) {
    QGCQGeoCoordinate *ori =
        measurementArea->pathModel().value<QGCQGeoCoordinate *>(0);
    if (ori != nullptr && ori->coordinate().isValid()) {
      return ori->coordinate();
    }
  }

  if (safeArea != nullptr && safeArea->pathModel().count() > 0) {
    QGCQGeoCoordinate *ori =
        measurementArea->pathModel().value<QGCQGeoCoordinate *>(0);
    if (ori != nullptr && ori->coordinate().isValid()) {
      return ori->coordinate();
    }
  }

  return QGeoCoordinate();
}