WimaAreaData.cc 3.43 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 120 121 122 123 124 125 126 127
#include "WimaAreaData.h"

WimaAreaData::WimaAreaData(QObject *parent) : QObject(parent) {}

WimaAreaData::WimaAreaData(const WimaAreaData &other, QObject *parent)
    : QObject(parent), _path(other._path), _list(other._list),
      _center(other._center) {}

WimaAreaData::WimaAreaData(const WimaArea &otherData, QObject *parent)
    : QObject(parent), _center(otherData.center()) {
  _setPathImpl(otherData.path());
}

bool WimaAreaData::operator==(const WimaAreaData &data) const {
  return this->_path == data._path && this->_center == data._center;
}

bool WimaAreaData::operator!=(const WimaAreaData &data) const {
  return !this->operator==(data);
}

QVariantList WimaAreaData::path() const { return _path; }

QGeoCoordinate WimaAreaData::center() const { return _center; }

const QList<QGeoCoordinate> &WimaAreaData::coordinateList() const {
  return _list;
}

bool WimaAreaData::containsCoordinate(const QGeoCoordinate &coordinate) const {
  using namespace PlanimetryCalculus;
  using namespace PolygonCalculus;
  using namespace GeoUtilities;

  if (this->coordinateList().size() > 2) {
    QPolygonF polygon;
    toCartesianList(this->coordinateList(), coordinate, polygon);
    return PlanimetryCalculus::contains(polygon, QPointF(0, 0));
  } else
    return false;
}

void WimaAreaData::append(const QGeoCoordinate &c) {
  _list.append(c);
  _path.push_back(QVariant::fromValue(c));
  emit pathChanged();
}

void WimaAreaData::push_back(const QGeoCoordinate &c) { append(c); }

void WimaAreaData::clear() {
  if (_list.size() > 0 || _path.size() > 0) {
    _list.clear();
    _path.clear();
    emit pathChanged();
  }
}

void WimaAreaData::setPath(const QVariantList &coordinateList) {
  if (_path != coordinateList) {
    _setPathImpl(coordinateList);
    emit pathChanged();
  }
}

void WimaAreaData::setCenter(const QGeoCoordinate &center) {
  if (_center != center) {
    _center = center;
    emit centerChanged();
  }
}

WimaAreaData &WimaAreaData::operator=(const WimaAreaData &otherData) {
  setPath(otherData._list);
  setCenter(otherData._center);
  return *this;
}

WimaAreaData &WimaAreaData::operator=(const WimaArea &otherData) {
  setPath(otherData.path());
  setCenter(otherData.center());
  return *this;
}

void WimaAreaData::_setPathImpl(const QList<QGeoCoordinate> &coordinateList) {
  _list = coordinateList;
  _path.clear();
  // copy all coordinates to _path
  for (const auto &vertex : coordinateList) {
    _path.append(QVariant::fromValue(vertex));
  }
}

void WimaAreaData::_setPathImpl(const QVariantList &coordinateList) {
  _path = coordinateList;
  _list.clear();
  for (const auto &variant : coordinateList) {
    _list.push_back(variant.value<QGeoCoordinate>());
  }
}

/*!
 * \fn void WimaAreaData::setPath(const QList<QGeoCoordinate> &coordinateList)
 *
 * Sets the path member to \a coordinateList by copying all entries of \a
 * coordinateList. Emits the \c pathChanged() signal.
 */
void WimaAreaData::setPath(const QList<QGeoCoordinate> &coordinateList) {
  if (_list != coordinateList) {
    _setPathImpl(coordinateList);
    emit pathChanged();
  }
}

bool operator==(const WimaAreaData &m1, const WimaArea &m2) {
  return m1.path() == m2.path() && m1.center() == m2.center();
}

bool operator!=(const WimaAreaData &m1, const WimaArea &m2) {
  return !operator==(m1, m2);
}

bool operator==(const WimaArea &m1, const WimaAreaData &m2) { return m2 == m1; }

bool operator!=(const WimaArea &m1, const WimaAreaData &m2) {
  return !operator==(m2, m1);
}