WimaServiceArea.cc 3.95 KB
Newer Older
1 2
#include "WimaServiceArea.h"

3
#include "QGCLoggingCategory.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
4
#include "QGCQGeoCoordinate.h"
5 6 7

QGC_LOGGING_CATEGORY(WimaServiceAreaLog, "WimaServiceAreaLog")

8
const char *WimaServiceArea::wimaServiceAreaName = "Service Area";
9 10 11
const char *WimaServiceArea::depotLatitudeName = "DepotLatitude";
const char *WimaServiceArea::depotLongitudeName = "DepotLongitude";
const char *WimaServiceArea::depotAltitudeName = "DepotAltitude";
12

13
WimaServiceArea::WimaServiceArea(QObject *parent) : WimaArea(parent) { init(); }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
14

15
WimaServiceArea::WimaServiceArea(const WimaServiceArea &other, QObject *parent)
16
    : WimaArea(other, parent), _depot(other.depot()) {
17
  init();
18 19
}

20 21 22 23 24
/*!
 * \overload operator=()
 *
 * Calls the inherited operator WimaArea::operator=().
 */
25 26
WimaServiceArea &WimaServiceArea::operator=(const WimaServiceArea &other) {
  WimaArea::operator=(other);
27
  this->setDepot(other.depot());
28
  return *this;
29 30
}

31 32 33 34
const QGeoCoordinate &WimaServiceArea::depot() const { return _depot; }

QGeoCoordinate WimaServiceArea::depotQml() const { return _depot; }

35
bool WimaServiceArea::setDepot(const QGeoCoordinate &coordinate) {
36 37
  if (_depot.latitude() != coordinate.latitude() ||
      _depot.longitude() != coordinate.longitude()) {
38 39
    if (this->containsCoordinate(coordinate)) {
      _depot = coordinate;
40
      _depot.setAltitude(0);
41 42 43
      emit depotChanged();
      return true;
    }
44
  }
45
  return false;
46 47
}

48 49 50
void WimaServiceArea::saveToJson(QJsonObject &json) {
  this->WimaArea::saveToJson(json);
  json[areaTypeName] = wimaServiceAreaName;
51 52 53
  json[depotLatitudeName] = _depot.latitude();
  json[depotLongitudeName] = _depot.longitude();
  json[depotAltitudeName] = _depot.altitude();
54 55
}

56 57
bool WimaServiceArea::loadFromJson(const QJsonObject &json,
                                   QString &errorString) {
58
  bool retVal = false;
59
  if (this->WimaArea::loadFromJson(json, errorString)) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    double lat = 0;
    if (json.contains(depotLatitudeName) &&
        json[depotLatitudeName].isDouble()) {
      lat = json[depotLatitudeName].toDouble();
      double lon = 0;
      if (json.contains(depotLongitudeName) &&
          json[depotLongitudeName].isDouble()) {
        lon = json[depotLongitudeName].toDouble();
        double alt = 0;
        if (json.contains(depotAltitudeName) &&
            json[depotAltitudeName].isDouble()) {
          alt = json[depotAltitudeName].toDouble();
          this->setDepot(QGeoCoordinate(lat, lon, alt));
          retVal = true;
        } else {
          errorString = "Not able to load depot altitude.";
        }
      } else {
        errorString = "Not able to load depot longitude.";
      }
    } else {
      errorString = "Not able to load depot latitude.";
    }
    retVal = true;
84
  }
85
  return retVal;
86 87
}

88 89
void WimaServiceArea::init() {
  this->setObjectName(wimaServiceAreaName);
90 91
  connect(this, &WimaArea::pathChanged, [this] {
    if (!this->_depot.isValid() || !this->containsCoordinate(this->_depot)) {
92 93 94 95 96 97
      if (this->containsCoordinate(this->center())) {
        // Use center.
        this->setDepot(this->center());
      } else if (this->_depot.isValid()) {
        // Use nearest coordinate.
        auto minDist = std::numeric_limits<double>::infinity();
98 99 100
        int minIndex = 0;
        for (int idx = 0; idx < this->pathModel().count(); ++idx) {
          const QObject *obj = this->pathModel()[idx];
Valentin Platzgummer's avatar
Valentin Platzgummer committed
101
          const auto *vertex = qobject_cast<const QGCQGeoCoordinate *>(obj);
102
          if (vertex != nullptr) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
103
            auto d = vertex->coordinate().distanceTo(this->_depot);
104 105 106 107 108 109
            if (d < minDist) {
              minDist = d;
              minIndex = idx;
            }
          } else {
            qCCritical(WimaServiceAreaLog) << "init(): nullptr catched!";
110 111
          }
        }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
112
        this->setDepot(this->pathModel().value<QGCQGeoCoordinate *>(minIndex)->coordinate());
113
      } else if (this->pathModel().count() > 0) {
114
        // Use first coordinate.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
115
        this->setDepot(this->pathModel().value<QGCQGeoCoordinate *>(0)->coordinate());
116
      }
117 118
    }
  });
119
}