| 1 | // QmlPositioned.cxx - Expose NavData to Qml |
| 2 | // |
| 3 | // Written by James Turner, started April 2018. |
| 4 | // |
| 5 | // Copyright (C) 2018 James Turner <[email protected]> |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #include "QmlPositioned.hxx" |
| 22 | |
| 23 | #include <QDebug> |
| 24 | |
| 25 | #include <simgear/misc/strutils.hxx> |
| 26 | |
| 27 | #include <Navaids/NavDataCache.hxx> |
| 28 | #include <Navaids/navrecord.hxx> |
| 29 | #include <Airports/airport.hxx> |
| 30 | #include <Airports/groundnetwork.hxx> |
| 31 | #include <Airports/runways.hxx> |
| 32 | |
| 33 | using namespace flightgear; |
| 34 | using namespace simgear::strutils; |
| 35 | |
| 36 | QmlGeod::QmlGeod() : |
| 37 | m_data(SGGeod::fromDeg(-9999.0, -9999.0)) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | QmlGeod::QmlGeod(const SGGeod &geod) : |
| 42 | m_data(geod) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | double QmlGeod::latitudeDeg() const |
| 47 | { |
| 48 | return m_data.getLatitudeDeg(); |
| 49 | } |
| 50 | |
| 51 | double QmlGeod::longitudeDeg() const |
| 52 | { |
| 53 | return m_data.getLongitudeDeg(); |
| 54 | } |
| 55 | |
| 56 | double QmlGeod::latitudeRad() const |
| 57 | { |
| 58 | return m_data.getLatitudeRad(); |
| 59 | } |
| 60 | |
| 61 | double QmlGeod::longitudeRad() const |
| 62 | { |
| 63 | return m_data.getLongitudeRad(); |
| 64 | } |
| 65 | |
| 66 | double QmlGeod::elevationFt() const |
| 67 | { |
| 68 | return m_data.getElevationFt(); |
| 69 | } |
| 70 | |
| 71 | bool QmlGeod::valid() const |
| 72 | { |
| 73 | return m_data.isValid(); |
| 74 | } |
| 75 | |
| 76 | QString QmlGeod::toString(QmlGeod::Format fmt) const |
| 77 | { |
| 78 | if (!m_data.isValid()) |
| 79 | return "<invalid coordinate>"; |
| 80 | |
| 81 | LatLonFormat internalFormat = LatLonFormat::DECIMAL_DEGREES; |
| 82 | switch (fmt) { |
| 83 | case DecimalDegrees: internalFormat = LatLonFormat::DECIMAL_DEGREES; break; |
| 84 | case SignedDecimalDegrees: internalFormat = LatLonFormat::SIGNED_DECIMAL_DEGREES; break; |
| 85 | } |
| 86 | |
| 87 | const auto s = formatGeodAsString(m_data, internalFormat, DegreeSymbol::UTF8_DEGREE); |
| 88 | return QString::fromStdString(s); |
| 89 | } |
| 90 | |
| 91 | double QmlGeod::elevationM() const |
| 92 | { |
| 93 | return m_data.getElevationM(); |
| 94 | } |
| 95 | |
| 96 | void QmlGeod::setLatitudeDeg(double latitudeDeg) |
| 97 | { |
| 98 | if (qFuzzyCompare(m_data.getLatitudeDeg(), latitudeDeg)) |
| 99 | return; |
| 100 | |
| 101 | m_data.setLatitudeDeg(latitudeDeg); |
| 102 | // emit latitudeChanged(); |
| 103 | } |
| 104 | |
| 105 | void QmlGeod::setLongitudeDeg(double longitudeDeg) |
| 106 | { |
| 107 | if (qFuzzyCompare(m_data.getLongitudeDeg(), longitudeDeg)) |
| 108 | return; |
| 109 | |
| 110 | m_data.setLongitudeDeg(longitudeDeg); |
| 111 | // emit longitudeChanged(); |
| 112 | } |
| 113 | |
| 114 | void QmlGeod::setLatitudeRad(double latitudeRad) |
| 115 | { |
| 116 | if (qFuzzyCompare(m_data.getLatitudeRad(), latitudeRad)) |
| 117 | return; |
| 118 | |
| 119 | m_data.setLatitudeRad(latitudeRad); |
| 120 | // emit latitudeChanged(); |
| 121 | } |
| 122 | |
| 123 | void QmlGeod::setLongitudeRad(double longitudeRad) |
| 124 | { |
| 125 | if (qFuzzyCompare(m_data.getLongitudeRad(), longitudeRad)) |
| 126 | return; |
| 127 | |
| 128 | m_data.setLongitudeRad(longitudeRad); |
| 129 | // emit longitudeChanged(); |
| 130 | } |
| 131 | |
| 132 | void QmlGeod::setElevationM(double elevationM) |
| 133 | { |
| 134 | if (qFuzzyCompare(m_data.getElevationM(), elevationM)) |
| 135 | return; |
| 136 | |
| 137 | m_data.setElevationM(elevationM); |
| 138 | // emit elevationChanged(); |
| 139 | } |
| 140 | |
| 141 | void QmlGeod::setElevationFt(double elevationFt) |
| 142 | { |
| 143 | if (qFuzzyCompare(m_data.getElevationFt(), elevationFt)) |
| 144 | return; |
| 145 | |
| 146 | m_data.setElevationFt(elevationFt); |
| 147 | // emit elevationChanged(); |
| 148 | } |
| 149 | |
| 150 | ///////////////////////////////////////////////////////////////////////////////// |
| 151 | |
| 152 | QmlPositioned::QmlPositioned(QObject *parent) : QObject(parent) |
| 153 | { |
| 154 | |
| 155 | } |
| 156 | |
| 157 | QmlPositioned::QmlPositioned(FGPositionedRef p) : |
| 158 | m_pos(p) |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | void QmlPositioned::setInner(FGPositionedRef p) |
| 163 | { |
| 164 | if (!p) { |
| 165 | m_pos.clear(); |
| 166 | } else { |
| 167 | m_pos = p; |
| 168 | } |
| 169 | |
| 170 | emit infoChanged(); |
| 171 | } |
| 172 | |
| 173 | FGPositionedRef QmlPositioned::inner() const |
| 174 | { |
| 175 | return m_pos; |
| 176 | } |
| 177 | |
| 178 | bool QmlPositioned::valid() const |
| 179 | { |
| 180 | return m_pos.valid(); |
| 181 | } |
| 182 | |
| 183 | QString QmlPositioned::ident() const |
| 184 | { |
| 185 | if (!m_pos) |
| 186 | return {}; |
| 187 | |
| 188 | return QString::fromStdString(m_pos->ident()); |
| 189 | } |
| 190 | |
| 191 | QString QmlPositioned::name() const |
| 192 | { |
| 193 | if (!m_pos) |
| 194 | return {}; |
| 195 | |
| 196 | return QString::fromStdString(m_pos->name()); |
| 197 | } |
| 198 | |
| 199 | qlonglong QmlPositioned::guid() const |
| 200 | { |
| 201 | if (!m_pos) |
| 202 | return 0; |
| 203 | |
| 204 | return m_pos->guid(); |
| 205 | } |
| 206 | |
| 207 | QmlPositioned::Type QmlPositioned::type() const |
| 208 | { |
| 209 | if (!m_pos) |
| 210 | return Invalid; |
| 211 | |
| 212 | return static_cast<Type>(m_pos->type()); |
| 213 | } |
| 214 | |
| 215 | void QmlPositioned::setGuid(qlonglong guid) |
| 216 | { |
| 217 | if (m_pos && (m_pos->guid()) == guid) |
| 218 | return; |
| 219 | |
| 220 | m_pos = NavDataCache::instance()->loadById(guid); |
| 221 | |
| 222 | emit guidChanged(); |
| 223 | emit infoChanged(); |
| 224 | } |
| 225 | |
| 226 | bool QmlPositioned::isAirportType() const |
| 227 | { |
| 228 | return FGPositioned::isAirportType(m_pos.get()); |
| 229 | } |
| 230 | |
| 231 | bool QmlPositioned::isRunwayType() const |
| 232 | { |
| 233 | return FGPositioned::isRunwayType(m_pos.get()); |
| 234 | } |
| 235 | |
| 236 | bool QmlPositioned::isNavaidType() const |
| 237 | { |
| 238 | return FGPositioned::isNavaidType(m_pos.get()); |
| 239 | } |
| 240 | |
| 241 | QmlGeod* QmlPositioned::geod() const |
| 242 | { |
| 243 | if (!m_pos) |
| 244 | return nullptr; |
| 245 | |
| 246 | return new QmlGeod(m_pos->geod()); |
| 247 | } |
| 248 | |
| 249 | double QmlPositioned::navaidFrequencyMHz() const |
| 250 | { |
| 251 | FGNavRecord* nav = fgpositioned_cast<FGNavRecord>(m_pos); |
| 252 | if (!nav) |
| 253 | return 0.0; |
| 254 | |
| 255 | qWarning() << Q_FUNC_INFO << "check me!"; |
| 256 | return nav->get_freq() / 1000.0; |
| 257 | } |
| 258 | |
| 259 | double QmlPositioned::navaidRangeNm() const |
| 260 | { |
| 261 | FGNavRecord* nav = fgpositioned_cast<FGNavRecord>(m_pos); |
| 262 | if (!nav) |
| 263 | return 0.0; |
| 264 | |
| 265 | return nav->get_range(); |
| 266 | } |
| 267 | |
| 268 | QmlPositioned *QmlPositioned::navaidRunway() const |
| 269 | { |
| 270 | FGNavRecord* nav = fgpositioned_cast<FGNavRecord>(m_pos); |
| 271 | if (!nav || !nav->runway()) |
| 272 | return nullptr; |
| 273 | |
| 274 | return new QmlPositioned(nav->runway()); |
| 275 | } |
| 276 | |
| 277 | QmlPositioned *QmlPositioned::colocatedDME() const |
| 278 | { |
| 279 | FGNavRecord* nav = fgpositioned_cast<FGNavRecord>(m_pos); |
| 280 | if (!nav || !nav->hasDME()) |
| 281 | return nullptr; |
| 282 | |
| 283 | return new QmlPositioned(flightgear::NavDataCache::instance()->loadById(nav->colocatedDME())); |
| 284 | } |
| 285 | |
| 286 | QmlPositioned *QmlPositioned::owningAirport() const |
| 287 | { |
| 288 | FGRunway* runway = fgpositioned_cast<FGRunway>(m_pos); |
| 289 | if (runway) { |
| 290 | return new QmlPositioned(runway->airport()); |
| 291 | } |
| 292 | |
| 293 | return nullptr; |
| 294 | } |
| 295 | |
| 296 | double QmlPositioned::runwayHeadingDeg() const |
| 297 | { |
| 298 | FGRunway* runway = fgpositioned_cast<FGRunway>(m_pos); |
| 299 | if (runway) { |
| 300 | return runway->headingDeg(); |
| 301 | } |
| 302 | |
| 303 | return 0.0; |
| 304 | } |
| 305 | |
| 306 | double QmlPositioned::runwayLengthFt() const |
| 307 | { |
| 308 | FGRunway* runway = fgpositioned_cast<FGRunway>(m_pos); |
| 309 | if (runway) { |
| 310 | return runway->lengthFt(); |
| 311 | } |
| 312 | |
| 313 | return 0.0; |
| 314 | } |
| 315 | |
| 316 | bool QmlPositioned::equals(QmlPositioned *other) const |
| 317 | { |
| 318 | return (other && (other->inner() == inner())); |
| 319 | } |
| 320 | |
| 321 | bool QmlPositioned::airportHasParkings() const |
| 322 | { |
| 323 | if (!isAirportType()) |
| 324 | return false; |
| 325 | |
| 326 | FGAirport* apt = fgpositioned_cast<FGAirport>(m_pos); |
| 327 | if (!apt) |
| 328 | return false; |
| 329 | |
| 330 | return !apt->groundNetwork()->allParkings().empty(); |
| 331 | } |
| 332 | |
| 333 | bool operator==(const QmlPositioned& p1, const QmlPositioned& p2) |
| 334 | { |
| 335 | return p1.inner() == p2.inner(); |
| 336 | } |
| 337 |