FlightGear next
RouteDiagram.cxx
Go to the documentation of this file.
1// RouteDiagram.cxx - GUI diagram of a route
2//
3// Written by James Turner, started August 2018.
4//
5// Copyright (C) 2018 James Turner <james@flightgear.org>
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
30
32
33using namespace flightgear;
34
36 BaseDiagram(pr)
37{
38}
39
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
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
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
95void 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
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
138void 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}
#define p(x)
std::vector< SGGeod > SGGeodVec
BaseDiagram(QQuickItem *pr=nullptr)
SGGeod m_projectionCenter
QPointF project(const SGGeod &geod) const
void recomputeBounds(bool resetZoom)
flightgear::FlightPlanRef flightplan() const
RouteDiagram(QQuickItem *pr=nullptr)
void setFlightplan(FlightPlanController *fp)
void paintContents(QPainter *) override
void setActiveLegIndex(int activeLegIndex)
void flightplanChanged(FlightPlanController *flightplan)
void legIndexChanged(int activeLegIndex)
void doComputeBounds() override
FlightPlan.hxx - defines a full flight-plan object, including departure, cruise, arrival information ...
Definition Addon.cxx:53
SGSharedPtr< FlightPlan > FlightPlanRef