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

3
const char *WimaServiceArea::wimaServiceAreaName = "Service Area";
4 5 6
const char *WimaServiceArea::depotLatitudeName = "DepotLatitude";
const char *WimaServiceArea::depotLongitudeName = "DepotLongitude";
const char *WimaServiceArea::depotAltitudeName = "DepotAltitude";
7

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

10
WimaServiceArea::WimaServiceArea(const WimaServiceArea &other, QObject *parent)
11
    : WimaArea(other, parent), _depot(other.depot()) {
12
  init();
13 14
}

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

26 27 28 29
const QGeoCoordinate &WimaServiceArea::depot() const { return _depot; }

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

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

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

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

83 84 85 86
void print(const WimaServiceArea &area) {
  QString message;
  print(area, message);
  qWarning() << message;
87 88
}

89 90 91 92
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)));
93 94
}

95 96
void WimaServiceArea::init() {
  this->setObjectName(wimaServiceAreaName);
97 98
  connect(this, &WimaArea::pathChanged, [this] {
    if (!this->_depot.isValid() || !this->containsCoordinate(this->_depot)) {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
      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();
        auto minIt = this->pathReference().begin();
        for (auto it = this->pathReference().begin();
             it < this->pathReference().end(); ++it) {
          const auto vertex = it->value<QGeoCoordinate>();
          auto d = vertex.distanceTo(this->_depot);
          if (d < minDist) {
            minDist = d;
            minIt = it;
          }
        }
        this->setDepot(minIt->value<QGeoCoordinate>());
      } else if (this->pathReference().size() > 0) {
        // Use first coordinate.
        this->setDepot(this->pathReference().value(0).value<QGeoCoordinate>());
      }
120 121
    }
  });
122
}