1 // AirportDiagram.cxx - part of GUI launcher using Qt5
3 // Written by James Turner, started December 2014.
5 // Copyright (C) 2014 James Turner <zakalawe@mac.com>
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.
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.
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.
21 #include "AirportDiagram.hxx"
25 #include <simgear/sg_inlines.h>
30 #include <QMouseEvent>
32 #include <Airports/airport.hxx>
33 #include <Airports/runways.hxx>
34 #include <Airports/parking.hxx>
35 #include <Airports/pavement.hxx>
37 #include <Navaids/navrecord.hxx>
39 static double distanceToLineSegment(const QVector2D& p, const QVector2D& a,
40 const QVector2D& b, double* outT = NULL)
45 // Squared length, to avoid a sqrt
46 const qreal len2 = ab.lengthSquared();
48 // Line null, the projection can't exist, we return the first point
53 return (p - a).length();
56 // Parametric value of the projection on the line
57 const qreal t = (ac.x() * ab.x() + ac.y() * ab.y()) / len2;
60 // Point is before the first point
64 return (p - a).length();
66 // Point is after the second point
70 return (p - b).length();
76 const QVector2D proj = a + t * ab;
77 return (proj - p).length();
83 AirportDiagram::AirportDiagram(QWidget* pr) :
85 m_approachDistanceNm(-1.0)
87 m_parkingIconPath.moveTo(0,0);
88 m_parkingIconPath.lineTo(-16, -16);
89 m_parkingIconPath.lineTo(-64, -16);
90 m_parkingIconPath.lineTo(-64, 16);
91 m_parkingIconPath.lineTo(-16, 16);
92 m_parkingIconPath.lineTo(0, 0);
94 m_parkingIconLeftPath.moveTo(0,0);
95 m_parkingIconLeftPath.lineTo(16, -16);
96 m_parkingIconLeftPath.lineTo(64, -16);
97 m_parkingIconLeftPath.lineTo(64, 16);
98 m_parkingIconLeftPath.lineTo(16, 16);
99 m_parkingIconLeftPath.lineTo(0, 0);
102 AirportDiagram::~AirportDiagram()
107 void AirportDiagram::setAirport(FGAirportRef apt)
110 m_projectionCenter = apt ? apt->geod() : SGGeod();
112 m_approachDistanceNm = -1.0;
120 clearIgnoredNavaids();
121 addIgnoredNavaid(apt);
123 recomputeBounds(true);
127 FGRunwayRef AirportDiagram::selectedRunway() const
129 return m_selectedRunway;
132 void AirportDiagram::setSelectedRunway(FGRunwayRef r)
134 if (r == m_selectedRunway) {
138 m_selectedRunway = r;
142 void AirportDiagram::setApproachExtensionDistance(double distanceNm)
144 m_approachDistanceNm = distanceNm;
145 recomputeBounds(true);
149 void AirportDiagram::addRunway(FGRunwayRef rwy)
151 Q_FOREACH(RunwayData rd, m_runways) {
152 if (rd.runway == rwy->reciprocalRunway()) {
153 return; // only add one end of reciprocal runways
158 r.p1 = project(rwy->geod());
159 r.p2 = project(rwy->end());
160 r.widthM = qRound(rwy->widthM());
164 recomputeBounds(false);
168 void AirportDiagram::doComputeBounds()
170 Q_FOREACH(const RunwayData& r, m_runways) {
175 Q_FOREACH(const TaxiwayData& t, m_taxiways) {
180 Q_FOREACH(const ParkingData& p, m_parking) {
184 if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
185 double d = SG_NM_TO_METER * m_approachDistanceNm;
186 QPointF pt = project(m_selectedRunway->pointOnCenterline(-d));
191 void AirportDiagram::addParking(FGParkingRef park)
193 ParkingData pd = { project(park->geod()), park };
194 m_parking.push_back(pd);
195 recomputeBounds(false);
200 void AirportDiagram::paintContents(QPainter* p)
202 QTransform t = p->transform();
205 QBrush brush(QColor(0x9f, 0x9f, 0x9f));
206 Q_FOREACH(const QPainterPath& path, m_pavements) {
211 Q_FOREACH(const TaxiwayData& t, m_taxiways) {
212 QPen pen(QColor(0x9f, 0x9f, 0x9f));
213 pen.setWidth(t.widthM);
215 p->drawLine(t.p1, t.p2);
226 // draw ILS first so underneath all runways
227 QPen pen(QColor(0x5f, 0x5f, 0x5f));
229 pen.setCosmetic(true);
232 Q_FOREACH(const RunwayData& r, m_runways) {
233 drawILS(p, r.runway);
234 drawILS(p, r.runway->reciprocalRunway());
237 bool drawAircraft = false;
241 // now draw the runways for real
242 Q_FOREACH(const RunwayData& r, m_runways) {
244 QColor color(Qt::magenta);
245 if ((r.runway == m_selectedRunway) || (r.runway->reciprocalRunway() == m_selectedRunway)) {
252 pen.setWidth(r.widthM);
255 p->drawLine(r.p1, r.p2);
258 QString ident = QString::fromStdString(r.runway->ident());
261 p->rotate(r.runway->headingDeg());
262 // invert scaling factor so we can use screen pixel sizes here
263 p->scale(1.0 / m_scale, 1.0/ m_scale);
265 p->setPen((r.runway == m_selectedRunway) ? Qt::yellow : Qt::magenta);
266 p->drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
268 FGRunway* recip = r.runway->reciprocalRunway();
269 QString recipIdent = QString::fromStdString(recip->ident());
273 p->rotate(recip->headingDeg());
274 p->scale(1.0 / m_scale, 1.0/ m_scale);
276 p->setPen((r.runway->reciprocalRunway() == m_selectedRunway) ? Qt::yellow : Qt::magenta);
277 p->drawText(QRect(-100, 5, 200, 200), recipIdent, Qt::AlignHCenter | Qt::AlignTop);
280 if (m_selectedRunway) {
282 aircraftPos = m_selectedRunway->geod();
283 headingDeg = m_selectedRunway->headingDeg();
286 if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
288 // draw approach extension point
289 double d = SG_NM_TO_METER * m_approachDistanceNm;
290 QPointF pt = project(m_selectedRunway->pointOnCenterline(-d));
291 QPointF pt2 = project(m_selectedRunway->geod());
292 QPen pen(Qt::yellow);
293 pen.setWidth(2.0 / m_scale);
295 p->drawLine(pt, pt2);
297 aircraftPos = m_selectedRunway->pointOnCenterline(-d);
302 paintAirplaneIcon(p, aircraftPos, headingDeg);
307 void AirportDiagram::drawParkings(QPainter* painter)
309 QTransform t = painter->transform();
312 QFont f = painter->font();
316 Q_FOREACH(const ParkingData& p, m_parking) {
317 painter->setTransform(t);
318 painter->translate(p.pt);
320 double hdg = p.parking->getHeading();
321 bool useLeftIcon = false;
322 QRect labelRect(-62, -14, 40, 28);
327 labelRect = QRect(22, -14, 40, 28);
332 painter->rotate(hdg);
334 painter->setBrush(QColor(255, 196, 196)); // kind of pink
335 painter->drawPath(useLeftIcon ? m_parkingIconLeftPath : m_parkingIconPath);
337 painter->fillRect(labelRect, Qt::white);
340 painter->setPen(Qt::black);
341 painter->drawText(labelRect,
342 Qt::AlignVCenter | Qt::AlignHCenter,
343 QString::fromStdString(p.parking->name()));
346 painter->setTransform(t);
349 void AirportDiagram::drawILS(QPainter* painter, FGRunwayRef runway) const
354 FGNavRecord* loc = runway->ILS();
358 double halfBeamWidth = loc->localizerWidth() * 0.5;
359 QPointF threshold = project(runway->threshold());
360 double rangeM = loc->get_range() * SG_NM_TO_METER;
361 double radial = loc->get_multiuse();
362 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
364 // compute the three end points at the wide end of the arrow
365 QPointF endCentre = project(SGGeodesy::direct(loc->geod(), radial, -rangeM));
366 QPointF endR = project(SGGeodesy::direct(loc->geod(), radial + halfBeamWidth, -rangeM * 1.1));
367 QPointF endL = project(SGGeodesy::direct(loc->geod(), radial - halfBeamWidth, -rangeM * 1.1));
369 painter->drawLine(threshold, endCentre);
370 painter->drawLine(threshold, endL);
371 painter->drawLine(threshold, endR);
372 painter->drawLine(endL, endCentre);
373 painter->drawLine(endR, endCentre);
376 void AirportDiagram::mouseReleaseEvent(QMouseEvent* me)
379 return; // ignore panning drag+release ops here
381 QTransform t(transform());
382 double minDist = std::numeric_limits<double>::max();
383 FGRunwayRef bestRunway;
385 Q_FOREACH(const RunwayData& r, m_runways) {
386 QPointF p1(t.map(r.p1)), p2(t.map(r.p2));
388 double d = distanceToLineSegment(QVector2D(me->pos()),
393 bestRunway = r.runway->reciprocalRunway();
395 bestRunway = r.runway;
401 if (minDist < 16.0) {
402 emit clickedRunway(bestRunway);
406 void AirportDiagram::buildTaxiways()
409 for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
410 FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
413 td.p1 = project(tx->geod());
414 td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
416 td.widthM = tx->widthM();
417 m_taxiways.append(td);
421 void AirportDiagram::buildPavements()
424 for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
425 FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
426 if (pave->getNodeList().empty()) {
433 QPointF p0 = project(pave->getNodeList().front()->mPos);
435 FGPavement::NodeList::const_iterator it;
436 for (it = pave->getNodeList().begin(); it != pave->getNodeList().end(); ) {
437 const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>(it->get());
438 bool close = (*it)->mClose;
440 // increment iterator so we can look at the next point
442 QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
445 QPointF control = project(bn->mControl);
446 QPointF endPoint = close ? startPoint : nextPoint;
447 pp.quadTo(control, endPoint);
449 // straight line segment
461 startPoint = QPointF();
465 } // of nodes iteration
471 m_pavements.append(pp);
472 } // of pavements iteration