| 1 | // RouteDiagram.cxx - GUI diagram of a route |
| 2 | // |
| 3 | // Written by James Turner, started August 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 "RouteDiagram.hxx" |
| 22 | |
| 23 | |
| 24 | #include <QPainter> |
| 25 | #include <QDebug> |
| 26 | #include <QVector2D> |
| 27 | #include <QMouseEvent> |
| 28 | |
| 29 | #include <Navaids/NavDataCache.hxx> |
| 30 | |
| 31 | #include "FlightPlanController.hxx" |
| 32 | |
| 33 | using namespace flightgear; |
| 34 | |
| 35 | RouteDiagram::RouteDiagram(QQuickItem* pr) : |
| 36 | BaseDiagram(pr) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void RouteDiagram::setFlightplan(FlightPlanController *fp) |
| 41 | { |
| 42 | if (fp == m_flightplan) |
| 43 | return; |
| 44 | |
| 45 | if (m_flightplan) { |
| 46 | // disconnect from old signal |
| 47 | disconnect(m_flightplan, nullptr, this, nullptr); |
| 48 | } |
| 49 | |
| 50 | m_flightplan = fp; |
| 51 | m_activeLegIndex = 0; |
| 52 | |
| 53 | emit flightplanChanged(fp); |
| 54 | |
| 55 | if (fp) { |
| 56 | connect(fp, &FlightPlanController::infoChanged, this, &RouteDiagram::fpChanged); |
| 57 | connect(fp, &FlightPlanController::waypointsChanged, this, &RouteDiagram::fpChanged); |
| 58 | } |
| 59 | |
| 60 | fpChanged(); |
| 61 | update(); |
| 62 | } |
| 63 | |
| 64 | int RouteDiagram::numLegs() const |
| 65 | { |
| 66 | if (!m_flightplan) |
| 67 | return 0; |
| 68 | |
| 69 | FlightPlanRef fp = m_flightplan->flightplan(); |
| 70 | if (!fp) |
| 71 | return 0; |
| 72 | |
| 73 | return fp->numLegs(); |
| 74 | } |
| 75 | |
| 76 | void RouteDiagram::setActiveLegIndex(int activeLegIndex) |
| 77 | { |
| 78 | if (m_activeLegIndex == activeLegIndex) |
| 79 | return; |
| 80 | |
| 81 | if ((activeLegIndex < 0) || (activeLegIndex >= numLegs())) { |
| 82 | qWarning() << Q_FUNC_INFO << "invalid leg index" << activeLegIndex; |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | m_activeLegIndex = activeLegIndex; |
| 87 | emit legIndexChanged(m_activeLegIndex); |
| 88 | |
| 89 | const double halfLegDistance = m_path->distanceForIndex(m_activeLegIndex) * 0.5; |
| 90 | m_projectionCenter = m_path->positionForDistanceFrom(m_activeLegIndex, halfLegDistance); |
| 91 | recomputeBounds(true); |
| 92 | update(); |
| 93 | } |
| 94 | |
| 95 | void RouteDiagram::paintContents(QPainter *painter) |
| 96 | { |
| 97 | if (!m_flightplan) |
| 98 | return; |
| 99 | |
| 100 | FlightPlanRef fp = m_flightplan->flightplan(); |
| 101 | QVector<QLineF> lines; |
| 102 | QVector<QLineF> activeLines; |
| 103 | for (int l=0; l < fp->numLegs(); ++l) { |
| 104 | QPointF previous; |
| 105 | bool isFirst = true; |
| 106 | for (auto g : m_path->pathForIndex(l)) { |
| 107 | QPointF p = project(g); |
| 108 | if (isFirst) { |
| 109 | isFirst = false; |
| 110 | } else if (l == m_activeLegIndex) { |
| 111 | activeLines.append(QLineF(previous, p)); |
| 112 | } else { |
| 113 | lines.append(QLineF(previous, p)); |
| 114 | } |
| 115 | previous = p; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | QPen linePen(Qt::magenta, 2); |
| 120 | linePen.setCosmetic(true); |
| 121 | painter->setPen(linePen); |
| 122 | painter->drawLines(lines); |
| 123 | |
| 124 | linePen.setColor(Qt::yellow); |
| 125 | painter->setPen(linePen); |
| 126 | painter->drawLines(activeLines); |
| 127 | } |
| 128 | |
| 129 | void RouteDiagram::doComputeBounds() |
| 130 | { |
| 131 | FlightPlanRef fp = m_flightplan->flightplan(); |
| 132 | const SGGeodVec gv(m_path->pathForIndex(m_activeLegIndex)); |
| 133 | std::for_each(gv.begin(), gv.end(), [this](const SGGeod& g) |
| 134 | {this->extendBounds(this->project(g)); } |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | void RouteDiagram::fpChanged() |
| 139 | { |
| 140 | FlightPlanRef fp = m_flightplan->flightplan(); |
| 141 | m_path.reset(new RoutePath(fp)); |
| 142 | m_activeLegIndex = 0; |
| 143 | |
| 144 | if (fp && (fp->numLegs() > 0)) { |
| 145 | const double halfLegDistance = m_path->distanceForIndex(m_activeLegIndex) * 0.5; |
| 146 | m_projectionCenter = m_path->positionForDistanceFrom(m_activeLegIndex, halfLegDistance); |
| 147 | } |
| 148 | recomputeBounds(true); |
| 149 | update(); |
| 150 | } |