Debian Codegraph @

src/GUI/LocationController.cxx

raw symbols SCIP
1 // LocationController.cxx - GUI launcher dialog using Qt5
2 //
3 // Written by James Turner, started October 2015.
4 //
5 // Copyright (C) 2015 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 "LocationController.hxx"
22
23 #include <QSettings>
24 #include <QAbstractListModel>
25 #include <QTimer>
26 #include <QDebug>
27 #include <QQmlComponent>
28 #include <QQmlEngine>
29
30 #include <simgear/misc/strutils.hxx>
31 #include <simgear/structure/exception.hxx>
32
33 #include "AirportDiagram.hxx"
34 #include "CarrierDiagram.hxx"
35 #include "CarriersLocationModel.hxx"
36 #include "DefaultAircraftLocator.hxx"
37 #include "LaunchConfig.hxx"
38 #include "NavaidDiagram.hxx"
39 #include "NavaidSearchModel.hxx"
40 #include "QmlPositionedModel.hxx"
41 #include "SettingsWrapper.hxx"
42
43 #include <Airports/airport.hxx>
44 #include <Airports/groundnetwork.hxx>
45
46 #include <Main/globals.hxx>
47 #include <Navaids/NavDataCache.hxx>
48 #include <Navaids/navrecord.hxx>
49 #include <Main/options.hxx>
50 #include <Main/fg_init.hxx>
51 #include <Main/fg_props.hxx> // for fgSetDouble
52
53 using namespace flightgear;
54
55 const unsigned int MAX_RECENT_LOCATIONS = 20;
56
57 QVariant savePositionList(const FGPositionedList& posList)
58 {
59 QVariantList vl;
60 for (const auto& pos : posList) {
61 QVariantMap vm;
62 vm.insert("ident", QString::fromStdString(pos->ident()));
63 vm.insert("type", pos->type());
64 vm.insert("lat", pos->geod().getLatitudeDeg());
65 vm.insert("lon", pos->geod().getLongitudeDeg());
66 vl.append(vm);
67 }
68 return vl;
69 }
70
71 FGPositionedList loadPositionedList(QVariant v)
72 {
73 QVariantList vl = v.toList();
74 FGPositionedList result;
75 result.reserve(static_cast<size_t>(vl.size()));
76 NavDataCache* cache = NavDataCache::instance();
77
78 Q_FOREACH(QVariant v, vl) {
79 QVariantMap vm = v.toMap();
80 std::string ident(vm.value("ident").toString().toStdString());
81 double lat = vm.value("lat").toDouble();
82 double lon = vm.value("lon").toDouble();
83 FGPositioned::Type ty(static_cast<FGPositioned::Type>(vm.value("type").toInt()));
84 FGPositioned::TypeFilter filter(ty);
85 FGPositionedRef pos = cache->findClosestWithIdent(ident,
86 SGGeod::fromDeg(lon, lat),
87 &filter);
88 if (pos)
89 result.push_back(pos);
90 }
91
92 return result;
93 }
94
95 LocationController::LocationController(QObject *parent) :
96 QObject(parent)
97 {
98 qmlRegisterUncreatableType<QmlPositionedModel>("FlightGear.Launcher", 1, 0, "QmlPositionedModel", "no");
99
100 m_searchModel = new NavaidSearchModel(this);
101 m_detailQml = new QmlPositioned(this);
102 m_baseQml = new QmlPositioned(this);
103 m_carriersModel = new CarriersLocationModel(this);
104 m_runwaysModel = new QmlPositionedModel(this);
105 m_helipadsModel = new QmlPositionedModel(this);
106 m_parkingsModel = new QmlPositionedModel(this);
107
108 m_defaultAltitude = QuantityValue{Units::FeetMSL, 6000};
109 m_defaultAirspeed = QuantityValue{Units::Knots, 120};
110 m_defaultHeading = QuantityValue{Units::DegreesTrue, 0};
111 m_defaultOffsetDistance = QuantityValue{Units::NauticalMiles, 1.0};
112 m_defaultOffsetRadial = QuantityValue{Units::DegreesTrue, 90};
113
114 // chain location and offset updated to description
115 connect(this, &LocationController::baseLocationChanged,
116 this, &LocationController::descriptionChanged);
117 connect(this, &LocationController::configChanged,
118 this, &LocationController::descriptionChanged);
119 connect(this, &LocationController::offsetChanged,
120 this, &LocationController::descriptionChanged);
121 }
122
123 LocationController::~LocationController() = default;
124
125 void LocationController::setLaunchConfig(LaunchConfig *config)
126 {
127 m_config = config;
128 connect(m_config, &LaunchConfig::collect, this, &LocationController::onCollectConfig);
129 connect(m_config, &LaunchConfig::save, this, &LocationController::onSaveCurrentLocation);
130 connect(m_config, &LaunchConfig::restore, this, &LocationController::onRestoreCurrentLocation);
131 }
132
133 void LocationController::restoreSearchHistory()
134 {
135 auto settings = flightgear::getQSettings();
136 m_recentLocations = loadPositionedList(settings.value("recent-locations"));
137 }
138
139 void LocationController::onRestoreCurrentLocation()
140 {
141 QVariantMap vm = m_config->getValueForKey("", "current-location", QVariantMap()).toMap();
142 if (vm.empty())
143 return;
144
145 restoreLocation(vm);
146 }
147
148 void LocationController::onSaveCurrentLocation()
149 {
150 m_config->setValueForKey("", "current-location", saveLocation());
151 }
152
153 bool LocationController::isParkedLocation() const
154 {
155 if (m_airportLocation) {
156 if (m_useAvailableParking)
157 return true;
158
159 if (m_detailLocation && (m_detailLocation->type() == FGPositioned::PARKING)) {
160 return true;
161 }
162 }
163
164 // treat all other ground starts as taxi or on runway, i.e engines
165 // running if possible
166 return false;
167 }
168
169 bool LocationController::isAirborneLocation() const
170 {
171 const bool altIsPositive = (m_altitude.value > 0);
172
173 if (m_locationIsLatLon) {
174 return m_altitudeEnabled && altIsPositive;
175 }
176
177 if (m_airportLocation) {
178 const bool onRunway =
179 (m_detailLocation && (m_detailLocation->type() == FGPositioned::RUNWAY)) ||
180 m_useActiveRunway;
181
182 if (onRunway && m_onFinal) {
183 // in this case no altitude might be set, but we assume
184 // it's still an airborne position
185 return true;
186 }
187
188 return false;
189 }
190
191 // relative to a navaid or fix - base off altitude.
192 return m_altitudeEnabled && altIsPositive;
193 }
194
195 QuantityValue LocationController::offsetRadial() const
196 {
197 return m_offsetRadial;
198 }
199
200 void LocationController::setBaseGeod(QmlGeod geod)
201 {
202 if (m_locationIsLatLon && (m_geodLocation == geod.geod()))
203 return;
204
205 clearLocation();
206 m_locationIsLatLon = true;
207 m_geodLocation = geod.geod();
208 emit baseLocationChanged();
209 }
210
211 QString LocationController::carrierName() const
212 {
213 return m_carrierName;
214 }
215
216 void LocationController::setCarrierLocation(QString name)
217 {
218 const auto cIndex = m_carriersModel->indexOf(name);
219 clearLocation();
220 if (cIndex < 0) {
221 qWarning() << "invalid carrier name:" << name;
222 return;
223 }
224
225 m_locationIsCarrier = true;
226 m_carrierName = name;
227 m_geodLocation = m_carriersModel->geodForIndex(cIndex);
228 m_carrierParkings = m_carriersModel->parkingsForIndex(cIndex);
229
230 emit baseLocationChanged();
231 }
232
233 void LocationController::clearLocation()
234 {
235 m_locationIsLatLon = false;
236 m_locationIsCarrier = false;
237 m_abeam = false;
238 m_location.clear();
239 m_carrierName.clear();
240 m_airportLocation.clear();
241 m_detailLocation.clear();
242 m_detailQml->setGuid(0);
243 m_baseQml->setGuid(0);
244 m_carrierParkings.clear();
245 m_carrierParking.clear();
246 m_runwaysModel->clear();
247 m_helipadsModel->clear();
248 m_parkingsModel->clear();
249 emit baseLocationChanged();
250 }
251
252 void LocationController::setBaseLocation(QmlPositioned* pos)
253 {
254 if (!pos) {
255 clearLocation();
256 return;
257 }
258
259 if (pos->inner() == m_location)
260 return;
261
262 clearLocation();
263 m_location = pos->inner();
264 m_baseQml->setGuid(pos->guid());
265
266 if (FGPositioned::isAirportType(m_location.ptr())) {
267 m_airportLocation = static_cast<FGAirport*>(m_location.ptr());
268 // disable offset when selecting a heliport
269 if (m_airportLocation->isHeliport()) {
270 m_onFinal = false;
271 m_useActiveRunway = false;
272 }
273
274 updateAirportModels();
275 } else {
276 m_airportLocation.clear();
277 }
278
279 emit offsetChanged();
280 emit baseLocationChanged();
281 }
282
283 void LocationController::updateAirportModels()
284 {
285 m_runwaysModel->setValues(m_airportLocation->getRunways());
286
287 FGPositionedList helipads;
288 for (unsigned int r = 0; r < m_airportLocation->numHelipads(); ++r) {
289 helipads.push_back(m_airportLocation->getHelipadByIndex(r));
290 }
291
292 m_helipadsModel->setValues(helipads);
293 m_parkingsModel->setValues(m_airportLocation->groundNetwork()->allParkings());
294 }
295
296 void LocationController::setDetailLocation(QmlPositioned* pos)
297 {
298 if (pos && (pos->inner() == m_detailLocation))
299 return;
300
301 if (!pos) {
302 m_detailLocation.clear();
303 m_detailQml->setInner({});
304 } else {
305 m_detailLocation = pos->inner();
306 m_useActiveRunway = false;
307 m_useAvailableParking = false;
308 m_detailQml->setInner(pos->inner());
309 }
310
311 emit configChanged();
312 }
313
314 QmlGeod LocationController::baseGeod() const
315 {
316 if (m_locationIsLatLon || m_locationIsCarrier)
317 return m_geodLocation;
318
319 if (m_location)
320 return QmlGeod(m_location->geod());
321
322 return {};
323 }
324
325 bool LocationController::isAirportLocation() const
326 {
327 return m_airportLocation;
328 }
329
330 void LocationController::setUseActiveRunway(bool b)
331 {
332 if (b == m_useActiveRunway)
333 return;
334
335 m_useActiveRunway = b;
336 if (m_useActiveRunway) {
337 m_detailLocation.clear(); // clear any specific runway
338 m_useAvailableParking = false;
339 }
340 emit configChanged();
341 }
342
343 void LocationController::addToRecent(QmlPositioned* pos)
344 {
345 addToRecent(pos->inner());
346 }
347
348 void LocationController::showHistoryInSearchModel()
349 {
350 // prepend the default location and tutorial airport
351
352 FGPositionedList locs = m_recentLocations;
353 const std::string defaultICAO = flightgear::defaultAirportICAO();
354 const std::string tutorialICAO = "PHTO"; // C172P tutorial aiurport
355
356 // remove them from the recent locations
357 auto it = std::remove_if(locs.begin(), locs.end(),
358 [defaultICAO, tutorialICAO](FGPositionedRef pos)
359 {
360 return (pos->ident() == defaultICAO) || (pos->ident() == tutorialICAO);
361 });
362 locs.erase(it, locs.end());
363
364 // prepend them
365 FGAirportRef apt = FGAirport::findByIdent(tutorialICAO);
366 if (apt) {
367 locs.insert(locs.begin(), apt);
368 } else {
369 qWarning() << Q_FUNC_INFO << "couldn't find tutorial airport for:" << QString::fromStdString(tutorialICAO);
370 }
371
372 apt = FGAirport::findByIdent(defaultICAO);
373 if (apt) {
374 locs.insert(locs.begin(), apt);
375 } else {
376 qWarning() << Q_FUNC_INFO << "couldn't find default airport for:" << QString::fromStdString(defaultICAO);
377 }
378
379 // Sentry FLIGHTGEAR-1BM indicates we are somtimes passing null pointers
380 // in this list; checks above are an attempt to diagnose this.
381 m_searchModel->setItems(locs);
382 }
383
384 QmlGeod LocationController::parseStringAsGeod(QString string) const
385 {
386 SGGeod g;
387 if (!simgear::strutils::parseStringAsGeod(string.toStdString(), &g)) {
388 return {};
389 }
390
391 return QmlGeod(g);
392 }
393
394 QString LocationController::carrierParking() const
395 {
396 if (!m_locationIsCarrier)
397 return {};
398 return m_carrierParking;
399 }
400
401 void LocationController::setCarrierParking(QString name)
402 {
403 if (!m_locationIsCarrier) {
404 qWarning() << "active location is not a carrier";
405 return;
406 }
407
408 if (m_carrierParking == name)
409 return;
410
411 if (!m_carrierParkings.contains(name)) {
412 qWarning() << "parking '" << name << "' not found in carrier parking list";
413 return;
414 }
415
416 m_carrierParking = name;
417 m_useCarrierFLOLS = false;
418 emit configChanged();
419 }
420
421 QmlPositioned *LocationController::detail() const
422 {
423 return m_detailQml;
424 }
425
426 QmlPositioned *LocationController::baseLocation() const
427 {
428 return m_baseQml;
429 }
430
431 QStringList LocationController::carrierParkings() const
432 {
433 return m_carrierParkings;
434 }
435
436 void LocationController::setOffsetRadial(QuantityValue offsetRadial)
437 {
438 if (m_offsetRadial == offsetRadial)
439 return;
440
441 m_offsetRadial = offsetRadial;
442 emit offsetChanged();
443 }
444
445 void LocationController::setOffsetDistance(QuantityValue d)
446 {
447 if (m_offsetDistance == d)
448 return;
449
450 m_offsetDistance = d;
451 emit offsetChanged();
452 }
453
454 void LocationController::setOffsetEnabled(bool offsetEnabled)
455 {
456 if (m_offsetEnabled == offsetEnabled)
457 return;
458
459 m_offsetEnabled = offsetEnabled;
460 emit offsetChanged();
461 }
462
463 void LocationController::setOnFinal(bool onFinal)
464 {
465 if (m_onFinal == onFinal)
466 return;
467
468 m_onFinal = onFinal;
469 emit configChanged();
470 }
471
472 void LocationController::setTuneNAV1(bool tuneNAV1)
473 {
474 if (m_tuneNAV1 == tuneNAV1)
475 return;
476
477 m_tuneNAV1 = tuneNAV1;
478 emit configChanged();
479 }
480
481 void LocationController::setUseAvailableParking(bool useAvailableParking)
482 {
483 if (m_useAvailableParking == useAvailableParking)
484 return;
485
486 m_useAvailableParking = useAvailableParking;
487 if (m_useAvailableParking) {
488 m_detailLocation.clear(); // clear any specific runway
489 m_useActiveRunway = false;
490 }
491 emit configChanged();
492 }
493
494 void LocationController::setUseCarrierFLOLS(bool useCarrierFLOLS)
495 {
496 if (!m_locationIsCarrier) {
497 qWarning() << "location is not a carrier";
498 return;
499 }
500
501 if (m_useCarrierFLOLS == useCarrierFLOLS)
502 return;
503
504 m_useCarrierFLOLS = useCarrierFLOLS;
505 m_carrierParking.clear();
506 emit configChanged();
507 }
508
509 void LocationController::setAbeam(bool abeam)
510 {
511 m_abeam = abeam;
512 emit configChanged();
513 }
514
515 void LocationController::restoreLocation(QVariantMap l)
516 {
517 clearLocation();
518
519 try {
520 m_altitudeEnabled = l.contains("altitude");
521 m_speedEnabled = l.contains("speed");
522 m_headingEnabled = l.contains("heading");
523
524 m_altitude = l.value("altitude", QVariant::fromValue(m_defaultAltitude)).value<QuantityValue>();
525 m_airspeed = l.value("speed", QVariant::fromValue(m_defaultAirspeed)).value<QuantityValue>();
526 m_heading = l.value("heading", QVariant::fromValue(m_defaultHeading)).value<QuantityValue>();
527
528 m_offsetEnabled = l.value("offset-enabled").toBool();
529 m_offsetRadial = l.value("offset-bearing", QVariant::fromValue(m_defaultOffsetRadial)).value<QuantityValue>();
530 m_offsetDistance = l.value("offset-distance", QVariant::fromValue(m_defaultOffsetDistance)).value<QuantityValue>();
531 m_tuneNAV1 = l.value("tune-nav1-radio").toBool();
532
533 if (l.contains("carrier")) {
534 setCarrierLocation(l.value("carrier").toString());
535 if (l.contains("carrier-flols")) {
536 setUseCarrierFLOLS(l.value("carrier-flols").toBool());
537 setAbeam(l.value("abeam").toBool());
538 // overwrite value form above, intentionally
539 m_offsetDistance = l.value("location-carrier-flols-distance", QVariant::fromValue(m_defaultOffsetDistance)).value<QuantityValue>();
540 } else if (l.contains("carrier-parking")) {
541 setCarrierParking(l.value("carrier-parking").toString());
542 }
543 } else if (l.contains("location-apt")) {
544 const auto icao = l.value("location-apt").toString().toStdString();
545 m_airportLocation = FGAirport::findByIdent(icao);
546 m_location = m_airportLocation;
547 m_baseQml->setInner(m_location);
548 } else if (l.contains("location-navaid")) {
549 const auto ident = l.value("location-navaid").toString().toStdString();
550
551 // we need lat/lon to disambiguate globally
552 const SGGeod vicinity = SGGeod::fromDeg(l.value("vicinity-lon").toDouble(),
553 l.value("vicinity-lat").toDouble());
554
555 // special-case TACANs so we don't get an ambiguity on VORTACs, where
556 // there would be a double hit on any ident
557 const auto isTacan = l.value("location-is-tacan").toBool();
558 if (isTacan) {
559 FGPositioned::TypeFilter filter(FGPositioned::Type::DME);
560 m_location = FGPositioned::findClosestWithIdent(ident, vicinity, &filter);
561 } else {
562 FGPositioned::TypeFilter filter({FGPositioned::Type::NDB, FGPositioned::Type::VOR,
563 FGPositioned::Type::FIX, FGPositioned::Type::WAYPOINT});
564 m_location = FGPositioned::findClosestWithIdent(ident, vicinity, &filter);
565 }
566
567 m_baseQml->setInner(m_location);
568 } else if (l.contains("location-lat")) {
569 m_locationIsLatLon = true;
570 m_geodLocation = SGGeod::fromDeg(l.value("location-lon").toDouble(),
571 l.value("location-lat").toDouble());
572 }
573
574 if (m_airportLocation) {
575 m_useActiveRunway = false;
576 m_useAvailableParking = false;
577
578 if (l.contains("location-apt-runway")) {
579 QString runway = l.value("location-apt-runway").toString().toUpper();
580 const auto runwayStr = runway.toStdString();
581 if (runway == QStringLiteral("ACTIVE")) {
582 m_useActiveRunway = true;
583 } else if (m_airportLocation->isHeliport()) {
584 m_detailLocation = m_airportLocation->getHelipadByIdent(runwayStr);
585 } else {
586 // could be a helipad at a regular airport
587 if (m_airportLocation->hasHelipadWithIdent(runwayStr)) {
588 m_detailLocation = m_airportLocation->getHelipadByIdent(runwayStr);
589 } else {
590 m_detailLocation = m_airportLocation->getRunwayByIdent(runwayStr);
591 }
592 }
593 } else if (l.contains("location-apt-parking")) {
594 QString parking = l.value("location-apt-parking").toString();
595 if (parking == QStringLiteral("AVAILABLE")) {
596 m_useAvailableParking = true;
597 } else {
598 m_detailLocation = m_airportLocation->groundNetwork()->findParkingByName(parking.toStdString());
599 }
600 }
601
602 if (m_detailLocation) {
603 m_detailQml->setInner(m_detailLocation);
604 }
605
606 m_onFinal = l.value("location-on-final").toBool();
607 setAbeam(l.value("abeam").toBool());
608 m_offsetDistance = l.value("location-apt-final-distance", QVariant::fromValue(m_defaultOffsetDistance)).value<QuantityValue>();
609 updateAirportModels();
610
611 } // of location is an airport
612 } catch (const sg_exception&) {
613 qWarning() << "Errors restoring saved location, clearing";
614 clearLocation();
615 }
616
617 baseLocationChanged();
618 configChanged();
619 offsetChanged();
620 }
621
622 bool LocationController::shouldStartPaused() const
623 {
624 if (m_useCarrierFLOLS) {
625 return true;
626 }
627
628 if (!m_location) {
629 return false; // defaults to on-ground at the default airport
630 }
631
632 if (m_airportLocation) {
633 return m_onFinal;
634 } else {
635 // navaid, start paused
636 return true;
637 }
638 }
639
640 QVariantMap LocationController::saveLocation() const
641 {
642 QVariantMap locationSet;
643 if (m_locationIsLatLon) {
644 locationSet.insert("location-lat", m_geodLocation.getLatitudeDeg());
645 locationSet.insert("location-lon", m_geodLocation.getLongitudeDeg());
646 } else if (m_locationIsCarrier) {
647 locationSet.insert("carrier", m_carrierName);
648 if (m_useCarrierFLOLS) {
649 locationSet.insert("carrier-flols", true);
650 locationSet.insert("location-carrier-flols-distance", QVariant::fromValue(m_offsetDistance));
651 locationSet.insert("abeam", m_abeam);
652 } else if (!m_carrierParking.isEmpty()) {
653 locationSet.insert("carrier-parking", m_carrierParking);
654 }
655 } else if (m_location) {
656 if (m_airportLocation) {
657 locationSet.insert("location-apt", QString::fromStdString(m_airportLocation->ident()));
658 locationSet.insert("location-on-final", m_onFinal);
659 locationSet.insert("location-apt-final-distance", QVariant::fromValue(m_offsetDistance));
660 locationSet.insert("abeam", m_abeam);
661 if (m_useActiveRunway) {
662 locationSet.insert("location-apt-runway", "ACTIVE");
663 } else if (m_useAvailableParking) {
664 locationSet.insert("location-apt-parking", "AVAILABLE");
665 } else if (m_detailLocation) {
666 const auto detailType = m_detailLocation->type();
667 if ((detailType == FGPositioned::RUNWAY) || (detailType == FGPositioned::HELIPAD)) {
668 locationSet.insert("location-apt-runway", QString::fromStdString(m_detailLocation->ident()));
669 } else if (detailType == FGPositioned::PARKING) {
670 locationSet.insert("location-apt-parking", QString::fromStdString(m_detailLocation->ident()));
671 }
672 }
673
674 } else { // not an aiport, must be a navaid
675 locationSet.insert("location-navaid", QString::fromStdString(m_location->ident()));
676 if (m_location->type() == FGPositioned::DME) {
677 // so we don't get ambiguous on VORTACs, explicity mark TACANs
678 // otherwise every VORTAC would be ambiguous
679 locationSet.insert("location-is-tacan", true);
680 }
681 locationSet.insert("vicinity-lat", m_location->geod().getLatitudeDeg());
682 locationSet.insert("vicinity-lon", m_location->geod().getLongitudeDeg());
683
684 } // of m_location is valid
685 }
686
687 if (m_altitudeEnabled) {
688 locationSet.insert("altitude", QVariant::fromValue(m_altitude));
689 }
690
691 if (m_speedEnabled) {
692 locationSet.insert("speed", QVariant::fromValue(m_airspeed));
693 }
694
695 if (m_headingEnabled) {
696 locationSet.insert("heading", QVariant::fromValue(m_heading));
697 }
698
699 if (m_offsetEnabled) {
700 locationSet.insert("offset-enabled", m_offsetEnabled);
701 locationSet.insert("offset-bearing", QVariant::fromValue(m_offsetRadial));
702 locationSet.insert("offset-distance", QVariant::fromValue(m_offsetDistance));
703 }
704
705 locationSet.insert("text", description());
706 locationSet.insert("tune-nav1-radio", m_tuneNAV1);
707
708 return locationSet;
709 }
710
711 void LocationController::setLocationProperties()
712 {
713 SGPropertyNode_ptr presets = fgGetNode("/sim/presets", true);
714
715 QStringList props = QStringList() << "vor-id"
716 << "fix"
717 << "ndb-id"
718 << "tacan-id"
719 << "runway-requested"
720 << "navaid-id"
721 << "offset-azimuth-deg"
722 << "offset-distance-nm"
723 << "glideslope-deg"
724 << "speed-set"
725 << "on-ground"
726 << "airspeed-kt"
727 << "airport-id"
728 << "runway"
729 << "parkpos"
730 << "carrier"
731 << "carrier-position";
732
733 Q_FOREACH(QString s, props) {
734 SGPropertyNode* c = presets->getChild(s.toStdString());
735 if (c) {
736 c->clearValue();
737 }
738 }
739
740 if (m_locationIsLatLon) {
741 fgSetDouble("/sim/presets/latitude-deg", m_geodLocation.getLatitudeDeg());
742 fgSetDouble("/position/latitude-deg", m_geodLocation.getLatitudeDeg());
743 fgSetDouble("/sim/presets/longitude-deg", m_geodLocation.getLongitudeDeg());
744 fgSetDouble("/position/longitude-deg", m_geodLocation.getLongitudeDeg());
745
746 applyPositionOffset();
747 applyAltitude();
748 applyAirspeed();
749 return;
750 }
751
752 fgSetDouble("/sim/presets/latitude-deg", 9999.0);
753 fgSetDouble("/sim/presets/longitude-deg", 9999.0);
754 fgSetDouble("/sim/presets/altitude-ft", -9999.0);
755 fgSetDouble("/sim/presets/heading-deg", 9999.0);
756
757 if (m_locationIsCarrier) {
758 fgSetString("/sim/presets/carrier", m_carrierName.toStdString());
759
760 if (m_useCarrierFLOLS) {
761 if (m_abeam) {
762 fgSetString("/sim/presets/carrier-position", "abeam");
763 } else {
764 fgSetString("/sim/presets/carrier-position", "FLOLS");
765 }
766 fgSetDouble("/sim/presets/offset-distance-nm", m_offsetDistance.convertToUnit(Units::NauticalMiles).value);
767 applyAltitude();
768 applyAirspeed();
769 } else if (!m_carrierParking.isEmpty()) {
770 fgSetString("/sim/presets/carrier-position", m_carrierParking.toStdString());
771 }
772
773 if (m_tuneNAV1) {
774 // tune TACAN to the carrier
775 qInfo() << "Implement TACAN tuning";
776 }
777 return;
778 }
779
780 if (!m_location) {
781 return;
782 }
783
784 if (m_airportLocation) {
785 fgSetString("/sim/presets/airport-id", m_airportLocation->ident());
786 fgSetBool("/sim/presets/on-ground", true);
787 fgSetBool("/sim/presets/airport-requested", true);
788 fgSetBool("/sim/presets/abeam", m_abeam);
789
790 const bool onRunway = (m_detailLocation && (m_detailLocation->type() == FGPositioned::RUNWAY));
791 const bool atParking = (m_detailLocation && (m_detailLocation->type() == FGPositioned::PARKING));
792 if (m_useActiveRunway) {
793 // automatic runway choice
794 // we can't set navaid here
795 } else if (m_useAvailableParking) {
796 fgSetString("/sim/presets/parkpos", "AVAILABLE");
797 } else if (onRunway) {
798 if (m_airportLocation->type() == FGPositioned::AIRPORT) {
799 // explicit runway choice (this also works for helipads)
800 fgSetString("/sim/presets/runway", m_detailLocation->ident() );
801 fgSetBool("/sim/presets/runway-requested", true );
802
803 // set nav-radio 1 based on selected runway
804 FGRunway* runway = static_cast<FGRunway*>(m_detailLocation.ptr());
805 if (m_tuneNAV1 && runway->ILS()) {
806 double mhz = runway->ILS()->get_freq() / 100.0;
807 fgSetDouble("/instrumentation/nav[0]/radials/selected-deg", runway->headingDeg());
808 fgSetDouble("/instrumentation/nav[0]/frequencies/selected-mhz", mhz);
809 }
810
811 if (m_onFinal) {
812 fgSetDouble("/sim/presets/glideslope-deg", 3.0);
813 fgSetDouble("/sim/presets/offset-distance-nm", m_offsetDistance.convertToUnit(Units::NauticalMiles).value);
814 fgSetBool("/sim/presets/on-ground", false);
815 }
816 } else if (m_airportLocation->type() == FGPositioned::HELIPORT) {
817 // explicit pad choice
818 fgSetString("/sim/presets/runway", m_detailLocation->ident() );
819 fgSetBool("/sim/presets/runway-requested", true );
820 }
821 } else if (atParking) {
822 // parking selection
823 fgSetString("/sim/presets/parkpos", m_detailLocation->ident());
824 }
825
826 updateAirportModels();
827 // of location is an airport
828 } else {
829 fgSetString("/sim/presets/airport-id", "");
830
831 // location is a navaid
832 // note setting the ident here is ambigious, we really only need and
833 // want the 'navaid-id' property. However setting the 'real' option
834 // gives a better UI experience (eg existing Position in Air dialog)
835 FGPositioned::Type ty = m_location->type();
836 switch (ty) {
837 case FGPositioned::VOR:
838 fgSetString("/sim/presets/vor-id", m_location->ident());
839 setNavRadioOption();
840 break;
841
842 case FGPositioned::NDB:
843 fgSetString("/sim/presets/ndb-id", m_location->ident());
844 setNavRadioOption();
845 break;
846
847 case FGPositioned::FIX:
848 fgSetString("/sim/presets/fix", m_location->ident());
849 break;
850
851 // assume if a DME was selected, it was actually a TACAN
852 case FGPositioned::DME:
853 fgSetString("/sim/presets/tacan-id", m_location->ident());
854 break;
855
856 default:
857 break;
858 }
859
860 // set disambiguation property
861 globals->get_props()->setIntValue("/sim/presets/navaid-id",
862 static_cast<int>(m_location->guid()));
863
864 applyPositionOffset();
865 applyAltitude();
866 applyAirspeed();
867 } // of navaid location
868 }
869
870 void LocationController::applyAirspeed()
871 {
872 if (m_speedEnabled && (m_airspeed.unit != Units::NoUnits)) {
873 if (m_airspeed.unit == Units::Knots) {
874 m_config->setArg("vc", QString::number(m_airspeed.value));
875 } else if (m_airspeed.unit == Units::KilometersPerHour) {
876 const double vc = m_airspeed.convertToUnit(Units::Knots).value;
877 m_config->setArg("vc", QString::number(vc));
878 } else if (m_airspeed.unit == Units::Mach) {
879 m_config->setArg("mach", QString::number(m_airspeed.value));
880 } else {
881 qWarning() << Q_FUNC_INFO << "unsupported airpseed unit" << m_airspeed.unit;
882 }
883 }
884 }
885
886 void LocationController::applyPositionOffset()
887 {
888 if (m_headingEnabled && (m_heading.unit != Units::NoUnits)) {
889 if (m_heading.unit == Units::DegreesTrue) {
890 m_config->setArg("heading", QString::number(m_heading.value));
891 } else {
892 qWarning() << Q_FUNC_INFO << "unsupported heading unit" << m_heading.unit;
893 }
894 }
895
896 if (m_offsetEnabled) {
897 // flip direction of azimuth to balance the flip done in fgApplyStartOffset
898 // I don't know why that flip exists but changing it there will break
899 // command-line compatability so compensating here instead
900 int offsetAzimuth = static_cast<int>(m_offsetRadial.value) - 180;
901 m_config->setArg("offset-azimuth", QString::number(offsetAzimuth));
902 const double offsetNm = m_offsetDistance.convertToUnit(Units::NauticalMiles).value;
903 m_config->setArg("offset-distance", QString::number(offsetNm));
904 }
905 }
906
907 void LocationController::applyAltitude()
908 {
909 if (!m_altitudeEnabled)
910 return;
911
912 switch (m_altitude.unit) {
913 default:
914 qWarning() << Q_FUNC_INFO << "unsupported altitude unit";
915 break;
916 case Units::FeetMSL:
917 m_config->setArg("altitude", QString::number(m_altitude.value));
918 break;
919
920 case Units::FeetAGL:
921 // fixme - allow the sim to accpet AGL start position
922 m_config->setArg("altitude", QString::number(m_altitude.value));
923 break;
924
925 case Units::FlightLevel:
926 // FIXME - allow the sim to accept real FlightLevel arguments
927 m_config->setArg("altitude", QString::number(m_altitude.value * 100));
928 break;
929
930 case Units::FeetAboveFieldElevation:
931 m_config->setArg("altitude", QString::number(m_altitude.value));
932 break;
933
934 case Units::MetersMSL:
935 const double ftMSL = m_altitude.convertToUnit(Units::FeetMSL).value;
936 m_config->setArg("altitude", QString::number(ftMSL));
937 break;
938 }
939 }
940
941 void LocationController::applyOnFinal()
942 {
943 if (m_onFinal) {
944 if (!m_altitudeEnabled) {
945 m_config->setArg("glideslope", std::string("3.0"));
946 }
947
948 const double offsetNm = m_offsetDistance.convertToUnit(Units::NauticalMiles).value;
949 m_config->setArg("offset-distance", QString::number(offsetNm));
950 m_config->setArg("in-air", std::string(""));
951
952 applyAirspeed();
953 applyAltitude();
954 }
955 }
956
957 void LocationController::onCollectConfig()
958 {
959 if (m_skipFromArgs) {
960 qWarning() << Q_FUNC_INFO << "skipping argument collection";
961 return;
962 }
963
964 if (m_locationIsLatLon) {
965 m_config->setArg("lat", QString::number(m_geodLocation.getLatitudeDeg(), 'f', 8));
966 m_config->setArg("lon", QString::number(m_geodLocation.getLongitudeDeg(), 'f', 8));
967 applyPositionOffset();
968 applyAltitude();
969 applyAirspeed();
970 return;
971 }
972
973 if (m_locationIsCarrier) {
974 m_config->setArg("carrier", m_carrierName);
975
976 if (!m_carrierParking.isEmpty()) {
977 m_config->setArg("carrier-position", m_carrierParking);
978 } else if (m_useCarrierFLOLS) {
979 if (m_abeam) {
980 m_config->setArg("carrier-position", QStringLiteral("abeam"));
981 } else {
982 m_config->setArg("carrier-position", QStringLiteral("FLOLS"));
983 }
984 const double offsetNm = m_offsetDistance.convertToUnit(Units::NauticalMiles).value;
985 m_config->setArg("offset-distance", QString::number(offsetNm));
986
987 applyAltitude();
988 applyAirspeed();
989 }
990
991 return;
992 }
993
994 if (!m_location) {
995 return;
996 }
997
998 if (m_airportLocation) {
999 m_config->setArg("airport", QString::fromStdString(m_airportLocation->ident()));
1000 const auto ty = m_detailLocation ? m_detailLocation->type() : FGPositioned::INVALID;
1001 const bool onRunway = (ty == FGPositioned::RUNWAY) || (ty == FGPositioned::HELIPAD);
1002 const bool atParking = ty == FGPositioned::PARKING;
1003
1004 if (m_useActiveRunway) {
1005 // pick by default
1006 applyOnFinal();
1007 } else if (m_useAvailableParking) {
1008 m_config->setArg("parking-id", QStringLiteral("AVAILABLE"));
1009 } else if (onRunway) {
1010 if (m_airportLocation->type() == FGPositioned::AIRPORT) {
1011 m_config->setArg("runway", QString::fromStdString(m_detailLocation->ident()));
1012
1013 if (ty == FGPositioned::RUNWAY) {
1014 // set nav-radio 1 based on selected runway
1015 auto runway = fgpositioned_cast<FGRunway>(m_detailLocation);
1016 if (runway->ILS()) {
1017 double mhz = runway->ILS()->get_freq() / 100.0;
1018 m_config->setArg("nav1", QString("%1:%2").arg(runway->headingDeg()).arg(mhz));
1019 }
1020
1021 applyOnFinal();
1022 }
1023
1024 } else if (m_airportLocation->type() == FGPositioned::HELIPORT) {
1025 m_config->setArg("runway", QString::fromStdString(m_detailLocation->ident()));
1026 }
1027 } else if (atParking) {
1028 // parking selection
1029 m_config->setArg("parking-id", QString::fromStdString(m_detailLocation->ident()));
1030 }
1031 // of location is an airport
1032 } else {
1033 // location is a navaid
1034 // note setting the ident here is ambigious, we really only need and
1035 // want the 'navaid-id' property. However setting the 'real' option
1036 // gives a better UI experience (eg existing Position in Air dialog)
1037 FGPositioned::Type ty = m_location->type();
1038 switch (ty) {
1039 case FGPositioned::VOR:
1040 m_config->setArg("vor", m_location->ident());
1041 setNavRadioOption();
1042 break;
1043
1044 case FGPositioned::NDB:
1045 m_config->setArg("ndb", m_location->ident());
1046 setNavRadioOption();
1047 break;
1048
1049 case FGPositioned::FIX:
1050 m_config->setArg("fix", m_location->ident());
1051 break;
1052
1053 case FGPositioned::DME:
1054 m_config->setArg("tacan", m_location->ident());
1055 break;
1056
1057 default:
1058 break;
1059 }
1060
1061 // set disambiguation property
1062 m_config->setProperty("/sim/presets/navaid-id", QString::number(m_location->guid()));
1063 applyPositionOffset();
1064 applyAltitude();
1065 applyAirspeed();
1066 } // of navaid location
1067 }
1068
1069 void LocationController::setNavRadioOption()
1070 {
1071 if (!m_tuneNAV1)
1072 return;
1073
1074 if (m_location->type() == FGPositioned::VOR) {
1075 FGNavRecordRef nav(static_cast<FGNavRecord*>(m_location.ptr()));
1076 double mhz = nav->get_freq() / 100.0;
1077 int heading = 0; // add heading support
1078 QString navOpt = QString("%1:%2").arg(heading).arg(mhz);
1079 m_config->setArg("nav1", navOpt);
1080 } else {
1081 FGNavRecordRef nav(static_cast<FGNavRecord*>(m_location.ptr()));
1082 int khz = nav->get_freq() / 100;
1083 int heading = 0;
1084 QString adfOpt = QString("%1:%2").arg(heading).arg(khz);
1085 m_config->setArg("adf1", adfOpt);
1086 }
1087 }
1088
1089 QString LocationController::compassPointFromHeading(int heading) const
1090 {
1091 const int labelArc = 360 / 8;
1092 heading += (labelArc >> 1);
1093 SG_NORMALIZE_RANGE(heading, 0, 359);
1094
1095 //
1096 switch (heading / labelArc) {
1097 case 0: return tr("N");
1098 case 1: return tr("NE");
1099 case 2: return tr("E");
1100 case 3: return tr("SE");
1101 case 4: return tr("S");
1102 case 5: return tr("SW");
1103 case 6: return tr("W");
1104 case 7: return tr("NW");
1105 }
1106
1107 return {};
1108 }
1109
1110 QString LocationController::description() const
1111 {
1112 const double offsetNm = m_offsetDistance.convertToUnit(Units::NauticalMiles).value;
1113
1114 if (!m_location) {
1115 if (m_locationIsLatLon) {
1116 const auto s = simgear::strutils::formatGeodAsString(m_geodLocation,
1117 simgear::strutils::LatLonFormat::DECIMAL_DEGREES,
1118 simgear::strutils::DegreeSymbol::UTF8_DEGREE);
1119 return tr("at position %1").arg(QString::fromStdString(s));
1120 }
1121
1122 if (m_locationIsCarrier) {
1123 QString pennant = m_carriersModel->pennantForIndex(m_carriersModel->indexOf(m_carrierName));
1124 QString locationToCarrier;
1125 if (m_abeam) {
1126 locationToCarrier = tr("%1nm abeam").arg(offsetNm);
1127 } else if (m_useCarrierFLOLS) {
1128 locationToCarrier = tr("on %1nm final to").arg(offsetNm);
1129 } else {
1130 locationToCarrier = tr("on deck at %1 on").arg(m_carrierParking);
1131 }
1132 return tr("%1 carrier %2 (%3)").arg(locationToCarrier).arg(m_carrierName).arg(pennant);
1133 }
1134
1135 return tr("No location selected");
1136 }
1137
1138 QString ident = QString::fromStdString(m_location->ident()),
1139 name = QString::fromStdString(m_location->name());
1140
1141 name = fixNavaidName(name);
1142
1143 if (m_airportLocation) {
1144 const auto ty = m_detailLocation ? m_detailLocation->type() : FGPositioned::INVALID;
1145 const bool onRunway = ty == FGPositioned::RUNWAY;
1146 const bool onPad = ty == FGPositioned::HELIPAD;
1147 const bool atParking = ty == FGPositioned::PARKING;
1148
1149 QString locationOnAirport;
1150
1151 if (m_useActiveRunway) {
1152 if (m_onFinal) {
1153 locationOnAirport = tr("on %1-mile final to active runway").arg(offsetNm);
1154 } else {
1155 locationOnAirport = tr("on active runway");
1156 }
1157 } else if (m_useAvailableParking) {
1158 locationOnAirport = tr("at an available parking position");
1159 } else if (onRunway) {
1160 QString runwayName = tr("runway %1").arg(QString::fromStdString(m_detailLocation->ident()));
1161
1162 if (m_onFinal) {
1163 locationOnAirport = tr("on %2-mile final to %1").arg(runwayName).arg(offsetNm);
1164 } else {
1165 locationOnAirport = tr("on %1").arg(runwayName);
1166 }
1167 } else if (onPad) {
1168 locationOnAirport = tr("on pad %1").arg(QString::fromStdString(m_detailLocation->ident()));
1169 } else if (atParking) {
1170 locationOnAirport = tr("at parking position %1").arg(QString::fromStdString(m_detailLocation->ident()));
1171 }
1172
1173 return tr("%2 (%1): %3").arg(ident).arg(name).arg(locationOnAirport);
1174 } else {
1175 QString offsetDesc = tr("at");
1176 if (m_offsetEnabled) {
1177 offsetDesc = tr("%1nm %2 of").
1178 arg(offsetNm, 0, 'f', 1).
1179 arg(compassPointFromHeading(static_cast<int>(m_offsetRadial.value)));
1180 }
1181
1182 QString navaidType;
1183 switch (m_location->type()) {
1184 case FGPositioned::VOR:
1185 navaidType = QString("VOR"); break;
1186 case FGPositioned::NDB:
1187 navaidType = QString("NDB"); break;
1188 case FGPositioned::DME:
1189 navaidType = QString("TACAN");
1190 break;
1191 case FGPositioned::FIX:
1192 return tr("%2 waypoint %1").arg(ident).arg(offsetDesc);
1193 default:
1194 // unsupported type
1195 break;
1196 }
1197
1198 return tr("%4 %1 %2 (%3)").arg(navaidType).arg(ident).arg(name).arg(offsetDesc);
1199 }
1200 }
1201
1202
1203 void LocationController::addToRecent(FGPositionedRef pos)
1204 {
1205 if (!pos) {
1206 // ensure null positiones don't get added, could cause
1207 // Sentry FLIGHTGEAR-1BM
1208 return;
1209 }
1210
1211 auto it = std::find(m_recentLocations.begin(),
1212 m_recentLocations.end(), pos);
1213 if (it != m_recentLocations.end()) {
1214 m_recentLocations.erase(it);
1215 }
1216
1217 if (m_recentLocations.size() >= MAX_RECENT_LOCATIONS) {
1218 m_recentLocations.pop_back();
1219 }
1220
1221 m_recentLocations.insert(m_recentLocations.begin(), pos);
1222 auto settings = flightgear::getQSettings();
1223 settings.setValue("recent-locations", savePositionList(m_recentLocations));
1224 }