AreaData.cc 6.21 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 33 34 35 36 37 38 39 40 41 42
bool AreaData::insert(GeoArea *areaData) {
  {
    SafeArea *area = qobject_cast<SafeArea *>(areaData);
    if (area != nullptr) {
      if (Q_LIKELY(!this->_areaList.contains(area))) {
        _areaList.append(area);
        emit areaList();
        return true;
      } else {
        return false;
      }
43 44 45
    }
  }

46 47 48 49 50 51 52 53 54 55 56
  {
    MeasurementArea *area = qobject_cast<MeasurementArea *>(areaData);
    if (area != nullptr) {
      if (Q_LIKELY(!this->_areaList.contains(area))) {
        _areaList.append(area);
        emit areaList();
        return true;
      } else {
        return false;
      }
    }
57 58
  }

59
  return false;
60 61
}

62 63 64 65
void AreaData::remove(GeoArea *areaData) {
  int index = _areaList.indexOf(areaData);
  if (index >= 0) {
    QObject *obj = _areaList.removeAt(index);
66

67
    _setOrigin(_newOrigin());
68

69 70
    if (obj->parent() == nullptr) {
      obj->deleteLater();
71 72
    }

73 74
    emit areaListChanged();
  }
75 76
}

77 78 79 80 81 82 83
void AreaData::clear() {
  if (_areaList.count() > 0) {
    for (int i = 0; i < _areaList.count(); ++i) {
      remove(_areaList.value<GeoArea *>(i));
    }
    emit areaListChanged();
  }
84 85
}

86
QmlObjectListModel *AreaData::areaList() { return &_areaList; }
87

88
const QmlObjectListModel *AreaData::areaList() const { return &_areaList; }
89

90
const QGeoCoordinate &AreaData::origin() const { return _origin; }
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
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) {
      if (!insert(new SafeArea())) {
        qCCritical(AreaDataLog)
            << "initialize(): safeArea == nullptr, but insert() failed.";
        return false;
      }
    }
120

121 122 123 124 125 126 127
    if (measurementArea == nullptr) {
      if (!insert(new MeasurementArea())) {
        qCCritical(AreaDataLog) << "initialize(): measurementArea == nullptr, "
                                   "but insert() failed.";
        return false;
      }
    }
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    // 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;
  }
}
160

161 162 163 164 165
bool AreaData::initialized() {
  auto *measurementArea = getGeoArea<MeasurementArea *>(_areaList);
  auto *safeArea = getGeoArea<SafeArea *>(_areaList);
  return measurementArea != nullptr && safeArea != nullptr &&
         measurementArea->count() >= 3 && safeArea->count() >= 3;
166 167 168
}

bool AreaData::operator==(const AreaData &other) const {
169 170 171 172 173 174 175 176 177 178
  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;
  }
179 180 181 182 183
}
bool AreaData::operator!=(const AreaData &other) const {
  return !(*this == other);
}

184
void AreaData::_setOrigin(const QGeoCoordinate &origin) {
185 186 187 188 189
  if (this->_origin != origin) {
    this->_origin = origin;
    emit originChanged();
  }
}
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

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();
}