WimaServiceArea.cc 2.92 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
bool WimaServiceArea::setDepot(const QGeoCoordinate &coordinate) {
27
  if (_depot != coordinate) {
28 29 30 31 32
    if (this->containsCoordinate(coordinate)) {
      _depot = coordinate;
      emit depotChanged();
      return true;
    }
33
  }
34
  return false;
35 36
}

37 38 39
void WimaServiceArea::saveToJson(QJsonObject &json) {
  this->WimaArea::saveToJson(json);
  json[areaTypeName] = wimaServiceAreaName;
40 41 42
  json[depotLatitudeName] = _depot.latitude();
  json[depotLongitudeName] = _depot.longitude();
  json[depotAltitudeName] = _depot.altitude();
43 44
}

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

77 78 79 80
void print(const WimaServiceArea &area) {
  QString message;
  print(area, message);
  qWarning() << message;
81 82
}

83 84 85 86
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)));
87 88
}

89 90
void WimaServiceArea::init() {
  this->setObjectName(wimaServiceAreaName);
91 92
  connect(this, &WimaArea::pathChanged, [this] {
    if (!this->_depot.isValid() || !this->containsCoordinate(this->_depot)) {
93 94 95
      this->setDepot(this->center());
    }
  });
96
}