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

3 4 5 6
#include "QGCLoggingCategory.h"

QGC_LOGGING_CATEGORY(WimaServiceAreaLog, "WimaServiceAreaLog")

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

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

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

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

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

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

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

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

55 56
bool WimaServiceArea::loadFromJson(const QJsonObject &json,
                                   QString &errorString) {
57
  bool retVal = false;
58
  if (this->WimaArea::loadFromJson(json, errorString)) {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    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;
83
  }
84
  return retVal;
85 86
}

87 88 89 90
void print(const WimaServiceArea &area) {
  QString message;
  print(area, message);
  qWarning() << message;
91 92
}

93 94 95 96
void print(const WimaServiceArea &area, QString &outputStr) {
  print(static_cast<const WimaArea &>(area), outputStr);
  outputStr.append(QString("Depot Position: %s\n")
                       .arg(area._depot.toString(QGeoCoordinate::Degrees)));
97 98
}

99 100
void WimaServiceArea::init() {
  this->setObjectName(wimaServiceAreaName);
101 102
  connect(this, &WimaArea::pathChanged, [this] {
    if (!this->_depot.isValid() || !this->containsCoordinate(this->_depot)) {
103 104 105 106 107 108
      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();
109 110 111 112 113 114 115 116 117 118 119 120
        int minIndex = 0;
        for (int idx = 0; idx < this->pathModel().count(); ++idx) {
          const QObject *obj = this->pathModel()[idx];
          const auto *vertex = qobject_cast<const QGeoCoordinate *>(obj);
          if (vertex != nullptr) {
            auto d = vertex->distanceTo(this->_depot);
            if (d < minDist) {
              minDist = d;
              minIndex = idx;
            }
          } else {
            qCCritical(WimaServiceAreaLog) << "init(): nullptr catched!";
121 122
          }
        }
123 124
        this->setDepot(*this->pathModel().value<QGeoCoordinate *>(minIndex));
      } else if (this->pathModel().count() > 0) {
125
        // Use first coordinate.
126
        this->setDepot(*this->pathModel().value<QGeoCoordinate *>(0));
127
      }
128 129
    }
  });
130
}