WimaServiceArea.cc 3.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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
#include "WimaServiceArea.h"

#include "QGCLoggingCategory.h"
#include "QGCQGeoCoordinate.h"

QGC_LOGGING_CATEGORY(WimaServiceAreaLog, "WimaServiceAreaLog")

const char *WimaServiceArea::wimaServiceAreaName = "Service Area";
const char *WimaServiceArea::depotLatitudeName = "DepotLatitude";
const char *WimaServiceArea::depotLongitudeName = "DepotLongitude";
const char *WimaServiceArea::depotAltitudeName = "DepotAltitude";

WimaServiceArea::WimaServiceArea(QObject *parent) : WimaArea(parent) { init(); }

WimaServiceArea::WimaServiceArea(const WimaServiceArea &other, QObject *parent)
    : WimaArea(other, parent), _depot(other.depot()) {
  init();
}

/*!
 * \overload operator=()
 *
 * Calls the inherited operator WimaArea::operator=().
 */
WimaServiceArea &WimaServiceArea::operator=(const WimaServiceArea &other) {
  WimaArea::operator=(other);
  this->setDepot(other.depot());
  return *this;
}

const QGeoCoordinate &WimaServiceArea::depot() const { return _depot; }

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

bool WimaServiceArea::setDepot(const QGeoCoordinate &coordinate) {
  if (_depot.latitude() != coordinate.latitude() ||
      _depot.longitude() != coordinate.longitude()) {
    if (this->containsCoordinate(coordinate)) {
      _depot = coordinate;
      _depot.setAltitude(0);
      emit depotChanged();
      return true;
    }
  }
  return false;
}

void WimaServiceArea::saveToJson(QJsonObject &json) {
  this->WimaArea::saveToJson(json);
  json[areaTypeName] = wimaServiceAreaName;
  json[depotLatitudeName] = _depot.latitude();
  json[depotLongitudeName] = _depot.longitude();
  json[depotAltitudeName] = _depot.altitude();
}

bool WimaServiceArea::loadFromJson(const QJsonObject &json,
                                   QString &errorString) {
  bool retVal = false;
  if (this->WimaArea::loadFromJson(json, errorString)) {
    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;
  }
  return retVal;
}

void WimaServiceArea::init() {
  this->setObjectName(wimaServiceAreaName);
  connect(this, &WimaArea::pathChanged, [this] {
    if (!this->_depot.isValid() || !this->containsCoordinate(this->_depot)) {
      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();
        int minIndex = 0;
        for (int idx = 0; idx < this->pathModel().count(); ++idx) {
          const QObject *obj = this->pathModel()[idx];
          const auto *vertex = qobject_cast<const QGCQGeoCoordinate *>(obj);
          if (vertex != nullptr) {
            auto d = vertex->coordinate().distanceTo(this->_depot);
            if (d < minDist) {
              minDist = d;
              minIndex = idx;
            }
          } else {
            qCCritical(WimaServiceAreaLog) << "init(): nullptr catched!";
          }
        }
        this->setDepot(this->pathModel().value<QGCQGeoCoordinate *>(minIndex)->coordinate());
      } else if (this->pathModel().count() > 0) {
        // Use first coordinate.
        this->setDepot(this->pathModel().value<QGCQGeoCoordinate *>(0)->coordinate());
      }
    }
  });
}