1 // BaseDiagram.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 "BaseDiagram.hxx"
28 #include <QMouseEvent>
30 #include <Navaids/navrecord.hxx>
31 #include <Navaids/positioned.hxx>
32 #include <Airports/airport.hxx>
33 #include <Navaids/PolyLine.hxx>
35 #include "QtLauncher_fwd.hxx"
37 /* equatorial and polar earth radius */
38 const float rec = 6378137; // earth radius, equator (?)
39 const float rpol = 6356752.314f; // earth radius, polar (?)
41 //Returns Earth radius at a given latitude (Ellipsoide equation with two equal axis)
42 static float earth_radius_lat( float lat )
44 double a = cos(lat)/rec;
45 double b = sin(lat)/rpol;
46 return 1.0f / sqrt( a * a + b * b );
49 BaseDiagram::BaseDiagram(QWidget* pr) :
52 m_wheelAngleDeltaAccumulator(0)
54 setSizePolicy(QSizePolicy::MinimumExpanding,
55 QSizePolicy::MinimumExpanding);
56 setMinimumSize(100, 100);
59 QTransform BaseDiagram::transform() const
62 t.translate(width() / 2, height() / 2); // center projection origin in the widget
63 t.scale(m_scale, m_scale);
65 // apply any pan offset that exists
66 t.translate(m_panOffset.x(), m_panOffset.y());
67 // center the bounding box (may not be at the origin)
68 t.translate(-m_bounds.center().x(), -m_bounds.center().y());
72 void BaseDiagram::clearIgnoredNavaids()
77 void BaseDiagram::addIgnoredNavaid(FGPositionedRef pos)
79 if (isNavaidIgnored(pos))
81 m_ignored.push_back(pos);
84 void BaseDiagram::extendRect(QRectF &r, const QPointF &p)
86 if (p.x() < r.left()) {
88 } else if (p.x() > r.right()) {
92 if (p.y() < r.top()) {
94 } else if (p.y() > r.bottom()) {
99 void BaseDiagram::paintEvent(QPaintEvent* pe)
102 p.setRenderHints(QPainter::Antialiasing);
103 p.fillRect(rect(), QColor(0x3f, 0x3f, 0x3f));
105 if (m_autoScalePan) {
106 // fit bounds within our available space, allowing for a margin
107 const int MARGIN = 32; // pixels
108 double ratioInX = (width() - MARGIN * 2) / m_bounds.width();
109 double ratioInY = (height() - MARGIN * 2) / m_bounds.height();
110 m_scale = std::min(ratioInX, ratioInY);
113 QTransform t(transform());
123 void BaseDiagram::paintAirplaneIcon(QPainter* painter, const SGGeod& geod, int headingDeg)
125 QPointF pos = project(geod);
126 QPixmap pix(":/airplane-icon");
127 pos = painter->transform().map(pos);
128 painter->resetTransform();
129 painter->translate(pos.x(), pos.y());
130 painter->rotate(headingDeg);
132 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
133 QRect airplaneIconRect = pix.rect();
134 airplaneIconRect.moveCenter(QPoint(0,0));
135 painter->drawPixmap(airplaneIconRect, pix);
138 void BaseDiagram::paintCoastlines(QPainter* painter)
140 QTransform xf = painter->transform();
141 QTransform invT = xf.inverted();
143 SGGeod topLeft = unproject(invT.map(rect().topLeft()), m_projectionCenter);
144 SGGeod viewCenter = unproject(invT.map(rect().center()), m_projectionCenter);
145 SGGeod bottomRight = unproject(invT.map(rect().bottomRight()), m_projectionCenter);
147 double drawRangeNm = std::max(SGGeodesy::distanceNm(viewCenter, topLeft),
148 SGGeodesy::distanceNm(viewCenter, bottomRight));
150 flightgear::PolyLineList lines(flightgear::PolyLine::linesNearPos(viewCenter, drawRangeNm,
151 flightgear::PolyLine::COASTLINE));
153 QPen waterPen(QColor(64, 64, 255), 1);
154 waterPen.setCosmetic(true);
155 painter->setPen(waterPen);
156 flightgear::PolyLineList::const_iterator it;
157 for (it=lines.begin(); it != lines.end(); ++it) {
158 paintGeodVec(painter, (*it)->points());
161 lines = flightgear::PolyLine::linesNearPos(viewCenter, drawRangeNm,
162 flightgear::PolyLine::URBAN);
163 for (it=lines.begin(); it != lines.end(); ++it) {
164 fillClosedGeodVec(painter, QColor(192, 192, 96), (*it)->points());
167 lines = flightgear::PolyLine::linesNearPos(viewCenter, drawRangeNm,
168 flightgear::PolyLine::RIVER);
170 painter->setPen(waterPen);
171 for (it=lines.begin(); it != lines.end(); ++it) {
172 paintGeodVec(painter, (*it)->points());
176 lines = flightgear::PolyLine::linesNearPos(viewCenter, drawRangeNm,
177 flightgear::PolyLine::LAKE);
179 for (it=lines.begin(); it != lines.end(); ++it) {
180 fillClosedGeodVec(painter, QColor(128, 128, 255),
187 void BaseDiagram::paintGeodVec(QPainter* painter, const flightgear::SGGeodVec& vec)
189 QVector<QPointF> projected;
190 projected.reserve(vec.size());
191 flightgear::SGGeodVec::const_iterator it;
192 for (it=vec.begin(); it != vec.end(); ++it) {
193 projected.append(project(*it));
196 painter->drawPolyline(projected.data(), projected.size());
199 void BaseDiagram::fillClosedGeodVec(QPainter* painter, const QColor& color, const flightgear::SGGeodVec& vec)
201 QVector<QPointF> projected;
202 projected.reserve(vec.size());
203 flightgear::SGGeodVec::const_iterator it;
204 for (it=vec.begin(); it != vec.end(); ++it) {
205 projected.append(project(*it));
208 painter->setPen(Qt::NoPen);
209 painter->setBrush(color);
210 painter->drawPolygon(projected.data(), projected.size());
213 class MapFilter : public FGPositioned::TypeFilter
217 MapFilter(LauncherAircraftType aircraft)
219 // addType(FGPositioned::FIX);
220 addType(FGPositioned::NDB);
221 addType(FGPositioned::VOR);
223 if (aircraft == Helicopter) {
224 addType(FGPositioned::HELIPAD);
227 if (aircraft == Seaplane) {
228 addType(FGPositioned::SEAPORT);
230 addType(FGPositioned::AIRPORT);
234 virtual bool pass(FGPositioned* aPos) const
236 bool ok = TypeFilter::pass(aPos);
237 // fix-filtering code disabled since fixed are entirely disabled
239 if (ok && (aPos->type() == FGPositioned::FIX)) {
240 // ignore fixes which end in digits
241 if (aPos->ident().length() > 4 && isdigit(aPos->ident()[3]) && isdigit(aPos->ident()[4])) {
250 void BaseDiagram::splitItems(const FGPositionedList& in, FGPositionedList& navaids,
251 FGPositionedList& ports)
253 FGPositionedList::const_iterator it = in.begin();
254 for (; it != in.end(); ++it) {
255 if (FGAirport::isAirportType(it->ptr())) {
256 ports.push_back(*it);
258 navaids.push_back(*it);
263 bool orderAirportsByRunwayLength(const FGPositionedRef& a,
264 const FGPositionedRef& b)
266 FGAirport* aptA = static_cast<FGAirport*>(a.ptr());
267 FGAirport* aptB = static_cast<FGAirport*>(b.ptr());
269 return aptA->longestRunway()->lengthFt() > aptB->longestRunway()->lengthFt();
272 void BaseDiagram::paintNavaids(QPainter* painter)
274 QTransform xf = painter->transform();
275 painter->setTransform(QTransform()); // reset to identity
276 QTransform invT = xf.inverted();
279 SGGeod topLeft = unproject(invT.map(rect().topLeft()), m_projectionCenter);
280 SGGeod viewCenter = unproject(invT.map(rect().center()), m_projectionCenter);
281 SGGeod bottomRight = unproject(invT.map(rect().bottomRight()), m_projectionCenter);
283 double drawRangeNm = std::max(SGGeodesy::distanceNm(viewCenter, topLeft),
284 SGGeodesy::distanceNm(viewCenter, bottomRight));
286 MapFilter f(m_aircraftType);
287 FGPositionedList items = FGPositioned::findWithinRange(viewCenter, drawRangeNm, &f);
289 FGPositionedList navaids, ports;
290 splitItems(items, navaids, ports);
292 if (ports.size() >= 40) {
293 FGPositionedList::iterator middle = ports.begin() + 40;
294 std::partial_sort(ports.begin(), middle, ports.end(),
295 orderAirportsByRunwayLength);
299 m_labelRects.clear();
300 m_labelRects.reserve(items.size());
302 FGPositionedList::const_iterator it;
303 for (it = ports.begin(); it != ports.end(); ++it) {
304 paintNavaid(painter, xf, *it);
307 for (it = navaids.begin(); it != navaids.end(); ++it) {
308 paintNavaid(painter, xf, *it);
313 painter->setTransform(xf);
316 QRect boundsOfLines(const QVector<QLineF>& lines)
319 Q_FOREACH(const QLineF& l, lines) {
320 r = r.united(QRectF(l.p1(), l.p2()).toRect());
326 void BaseDiagram::paintNavaid(QPainter* painter, const QTransform& t, const FGPositionedRef &pos)
328 if (isNavaidIgnored(pos))
331 bool drawAsIcon = true;
332 const double minRunwayLengthFt = (16 / m_scale) * SG_METER_TO_FEET;
333 const FGPositioned::Type ty(pos->type());
334 const bool isNDB = (ty == FGPositioned::NDB);
337 if (ty == FGPositioned::AIRPORT) {
338 FGAirport* apt = static_cast<FGAirport*>(pos.ptr());
339 if (apt->hasHardRunwayOfLengthFt(minRunwayLengthFt)) {
342 painter->setTransform(t);
343 QVector<QLineF> lines = projectAirportRuwaysWithCenter(apt, m_projectionCenter);
345 QPen pen(QColor(0x03, 0x83, 0xbf), 8);
346 pen.setCosmetic(true);
347 painter->setPen(pen);
348 painter->drawLines(lines);
350 QPen linePen(Qt::white, 2);
351 linePen.setCosmetic(true);
352 painter->setPen(linePen);
353 painter->drawLines(lines);
355 painter->resetTransform();
357 iconRect = t.mapRect(boundsOfLines(lines));
362 QPixmap pm = iconForPositioned(pos);
363 QPointF loc = t.map(project(pos->geod()));
364 iconRect = pm.rect();
365 iconRect.moveCenter(loc.toPoint());
366 painter->drawPixmap(iconRect, pm);
369 // compute label text so we can measure it
371 if (FGAirport::isAirportType(pos.ptr())) {
372 label = QString::fromStdString(pos->name());
373 label = fixNavaidName(label);
375 label = QString::fromStdString(pos->ident());
378 if (ty == FGPositioned::NDB) {
379 FGNavRecord* nav = static_cast<FGNavRecord*>(pos.ptr());
380 label.append("\n").append(QString::number(nav->get_freq() / 100));
381 } else if (ty == FGPositioned::VOR) {
382 FGNavRecord* nav = static_cast<FGNavRecord*>(pos.ptr());
383 label.append("\n").append(QString::number(nav->get_freq() / 100.0, 'f', 1));
386 QRect textBounds = painter->boundingRect(QRect(0, 0, 100, 100),
387 Qt::TextWordWrap, label);
389 textBounds = rectAndFlagsForLabel(pos->guid(), iconRect,
393 painter->setPen(isNDB ? QColor(0x9b, 0x5d, 0xa2) : QColor(0x03, 0x83, 0xbf));
394 painter->drawText(textBounds, textFlags, label);
397 bool BaseDiagram::isNavaidIgnored(const FGPositionedRef &pos) const
399 return m_ignored.contains(pos);
402 bool BaseDiagram::isLabelRectAvailable(const QRect &r) const
404 Q_FOREACH(const QRect& lr, m_labelRects) {
405 if (lr.intersects(r))
412 int BaseDiagram::textFlagsForLabelPosition(LabelPosition pos)
416 case LABEL_RIGHT: return Qt::AlignLeft | Qt::AlignVCenter;
417 case LABEL_ABOVE: return Qt::AlignHCenter | Qt::A
423 QRect BaseDiagram::rectAndFlagsForLabel(PositionedID guid, const QRect& item,
427 m_labelRects.append(item);
428 int pos = m_labelPositions.value(guid, LABEL_RIGHT);
429 bool firstAttempt = true;
430 flags = Qt::TextWordWrap;
432 while (pos < LAST_POSITION) {
433 QRect r = labelPositioned(item, bounds, static_cast<LabelPosition>(pos));
434 if (isLabelRectAvailable(r)) {
435 m_labelRects.append(r);
436 m_labelPositions[guid] = static_cast<LabelPosition>(pos);
437 flags |= textFlagsForLabelPosition(static_cast<LabelPosition>(pos));
439 } else if (firstAttempt && (pos != LABEL_RIGHT)) {
445 firstAttempt = false;
448 return QRect(item.x(), item.y(), bounds.width(), bounds.height());
451 QRect BaseDiagram::labelPositioned(const QRect& itemRect,
453 LabelPosition lp) const
455 const int SHORT_MARGIN = 4;
456 const int DIAGONAL_MARGIN = 12;
458 QPoint topLeft = itemRect.topLeft();
461 // cardinal compass points are short (close in)
463 topLeft = QPoint(itemRect.right() + SHORT_MARGIN,
464 itemRect.center().y() - bounds.height() / 2);
467 topLeft = QPoint(itemRect.center().x() - (bounds.width() / 2),
468 itemRect.top() - (SHORT_MARGIN + bounds.height()));
471 topLeft = QPoint(itemRect.center().x() - (bounds.width() / 2),
472 itemRect.bottom() + SHORT_MARGIN);
475 topLeft = QPoint(itemRect.left() - (SHORT_MARGIN + bounds.width()),
476 itemRect.center().y() - bounds.height() / 2);
479 // first diagonals are further out (to hopefully have a better chance
480 // of finding clear space
483 topLeft = QPoint(itemRect.right() + DIAGONAL_MARGIN,
484 itemRect.top() - (DIAGONAL_MARGIN + bounds.height()));
488 topLeft = QPoint(itemRect.left() - (DIAGONAL_MARGIN + bounds.width()),
489 itemRect.top() - (DIAGONAL_MARGIN + bounds.height()));
493 topLeft = QPoint(itemRect.right() + DIAGONAL_MARGIN,
494 itemRect.bottom() + DIAGONAL_MARGIN);
498 topLeft = QPoint(itemRect.left() - (DIAGONAL_MARGIN + bounds.width()),
499 itemRect.bottom() + DIAGONAL_MARGIN);
502 qWarning() << Q_FUNC_INFO << "Implement me";
506 return QRect(topLeft, bounds);
509 void BaseDiagram::mousePressEvent(QMouseEvent *me)
511 m_lastMousePos = me->pos();
515 void BaseDiagram::mouseMoveEvent(QMouseEvent *me)
517 m_autoScalePan = false;
519 QPointF delta = me->pos() - m_lastMousePos;
520 m_lastMousePos = me->pos();
522 // offset is stored in metres so we don't have to modify it when
524 m_panOffset += (delta / m_scale);
532 return (v == 0) ? 0 : (v < 0) ? -1 : 1;
535 void BaseDiagram::wheelEvent(QWheelEvent *we)
537 m_autoScalePan = false;
539 int delta = we->angleDelta().y();
543 if (intSign(m_wheelAngleDeltaAccumulator) != intSign(delta)) {
544 m_wheelAngleDeltaAccumulator = 0;
547 m_wheelAngleDeltaAccumulator += delta;
548 if (m_wheelAngleDeltaAccumulator > 120) {
549 m_wheelAngleDeltaAccumulator = 0;
551 } else if (m_wheelAngleDeltaAccumulator < -120) {
552 m_wheelAngleDeltaAccumulator = 0;
559 void BaseDiagram::paintContents(QPainter* painter)
563 void BaseDiagram::recomputeBounds(bool resetZoom)
569 m_autoScalePan = true;
571 m_panOffset = QPointF();
577 void BaseDiagram::doComputeBounds()
579 // no-op in the base class
582 void BaseDiagram::extendBounds(const QPointF& p)
584 extendRect(m_bounds, p);
587 QPointF BaseDiagram::project(const SGGeod& geod, const SGGeod& center)
589 double r = earth_radius_lat(geod.getLatitudeRad());
590 double ref_lat = center.getLatitudeRad(),
591 ref_lon = center.getLongitudeRad(),
592 lat = geod.getLatitudeRad(),
593 lon = geod.getLongitudeRad(),
594 lonDiff = lon - ref_lon;
596 double c = acos( sin(ref_lat) * sin(lat) + cos(ref_lat) * cos(lat) * cos(lonDiff) );
598 // angular distance from center is 0
599 return QPointF(0.0, 0.0);
602 double k = c / sin(c);
604 if (ref_lat == (90 * SG_DEGREES_TO_RADIANS))
606 x = (SGD_PI / 2 - lat) * sin(lonDiff);
607 y = -(SGD_PI / 2 - lat) * cos(lonDiff);
609 else if (ref_lat == -(90 * SG_DEGREES_TO_RADIANS))
611 x = (SGD_PI / 2 + lat) * sin(lonDiff);
612 y = (SGD_PI / 2 + lat) * cos(lonDiff);
616 x = k * cos(lat) * sin(lonDiff);
617 y = k * ( cos(ref_lat) * sin(lat) - sin(ref_lat) * cos(lat) * cos(lonDiff) );
620 // flip for top-left origin
621 return QPointF(x, -y) * r;
624 SGGeod BaseDiagram::unproject(const QPointF& xy, const SGGeod& center)
626 double r = earth_radius_lat(center.getLatitudeRad());
629 ref_lat = center.getLatitudeRad(),
630 ref_lon = center.getLongitudeRad(),
631 rho = QVector2D(xy).length(),
638 // invert y to balance the equivalent in project()
641 lat = asin( cos(c) * sin(ref_lat) + (y * sin(c) * cos(ref_lat)) / rho);
643 if (ref_lat == (90 * SG_DEGREES_TO_RADIANS)) // north pole
645 lon = ref_lon + atan(-x/y);
647 else if (ref_lat == -(90 * SG_DEGREES_TO_RADIANS)) // south pole
649 lon = ref_lon + atan(x/y);
653 lon = ref_lon + atan(x* sin(c) / (rho * cos(ref_lat) * cos(c) - y * sin(ref_lat) * sin(c)));
656 return SGGeod::fromRad(lon, lat);
659 QPointF BaseDiagram::project(const SGGeod& geod) const
661 return project(geod, m_projectionCenter);
664 QPixmap BaseDiagram::iconForPositioned(const FGPositionedRef& pos,
665 const IconOptions& options)
667 // if airport type, check towered or untowered
668 bool small = options.testFlag(SmallIcons);
670 bool isTowered = false;
671 if (FGAirport::isAirportType(pos)) {
672 FGAirport* apt = static_cast<FGAirport*>(pos.ptr());
673 isTowered = apt->hasTower();
676 switch (pos->type()) {
677 case FGPositioned::VOR:
678 if (static_cast<FGNavRecord*>(pos.ptr())->isVORTAC())
679 return QPixmap(":/vortac-icon");
681 if (static_cast<FGNavRecord*>(pos.ptr())->hasDME())
682 return QPixmap(":/vor-dme-icon");
684 return QPixmap(":/vor-icon");
686 case FGPositioned::AIRPORT:
687 return iconForAirport(static_cast<FGAirport*>(pos.ptr()), options);
689 case FGPositioned::HELIPORT:
690 return QPixmap(":/heliport-icon");
691 case FGPositioned::SEAPORT:
692 return QPixmap(isTowered ? ":/seaport-tower-icon" : ":/seaport-icon");
693 case FGPositioned::NDB:
694 return QPixmap(small ? ":/ndb-small-icon" : ":/ndb-icon");
695 case FGPositioned::FIX:
696 return QPixmap(":/waypoint-icon");
705 QPixmap BaseDiagram::iconForAirport(FGAirport* apt, const IconOptions& options)
707 if (apt->isClosed()) {
708 return QPixmap(":/airport-closed-icon");
711 if (!apt->hasHardRunwayOfLengthFt(1500)) {
712 return QPixmap(apt->hasTower() ? ":/airport-tower-icon" : ":/airport-icon");
715 if (options.testFlag(LargeAirportPlans) && apt->hasHardRunwayOfLengthFt(8500)) {
716 QPixmap result(32, 32);
717 result.fill(Qt::transparent);
720 p.setRenderHint(QPainter::Antialiasing, true);
721 QRectF b = result.rect().adjusted(4, 4, -4, -4);
722 QVector<QLineF> lines = projectAirportRuwaysIntoRect(apt, b);
724 p.setPen(QPen(QColor(0x03, 0x83, 0xbf), 8));
727 p.setPen(QPen(Qt::white, 2));
733 QPixmap result(25, 25);
734 result.fill(Qt::transparent);
738 p.setRenderHint(QPainter::Antialiasing, true);
741 p.setBrush(apt->hasTower() ? QColor(0x03, 0x83, 0xbf) :
742 QColor(0x9b, 0x5d, 0xa2));
743 p.drawEllipse(QPointF(13, 13), 10, 10);
745 FGRunwayRef r = apt->longestRunway();
747 p.setPen(QPen(Qt::white, 2));
749 p.rotate(r->headingDeg());
750 p.drawLine(0, -8, 0, 8);
756 QVector<QLineF> BaseDiagram::projectAirportRuwaysWithCenter(FGAirportRef apt, const SGGeod& c)
760 const FGRunwayList& runways(apt->getRunwaysWithoutReciprocals());
761 FGRunwayList::const_iterator it;
763 for (it = runways.begin(); it != runways.end(); ++it) {
764 FGRunwayRef rwy = *it;
765 QPointF p1 = project(rwy->geod(), c);
766 QPointF p2 = project(rwy->end(), c);
767 r.append(QLineF(p1, p2));
773 void BaseDiagram::setAircraftType(LauncherAircraftType type)
775 m_aircraftType = type;
779 QVector<QLineF> BaseDiagram::projectAirportRuwaysIntoRect(FGAirportRef apt, const QRectF &bounds)
781 QVector<QLineF> r = projectAirportRuwaysWithCenter(apt, apt->geod());
784 Q_FOREACH(const QLineF& l, r) {
785 extendRect(extent, l.p1());
786 extendRect(extent, l.p2());
789 // find constraining scale factor
790 double ratioInX = bounds.width() / extent.width();
791 double ratioInY = bounds.height() / extent.height();
794 t.translate(bounds.left(), bounds.top());
795 t.scale(std::min(ratioInX, ratioInY),
796 std::min(ratioInX, ratioInY));
797 t.translate(-extent.left(), -extent.top()); // move unscaled to 0,0
799 for (int i=0; i<r.size(); ++i) {