]> git.mxchange.org Git - flightgear.git/blob - src/GUI/NavaidDiagram.cxx
Work on launcher diagrams.
[flightgear.git] / src / GUI / NavaidDiagram.cxx
1 // NavaidDiagram.cxx - part of GUI launcher using Qt5
2 //
3 // Written by James Turner, started October 2015.
4 //
5 // Copyright (C) 2014 James Turner <zakalawe@mac.com>
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 "NavaidDiagram.hxx"
22
23 #include <limits>
24
25 #include <QPainter>
26 #include <QDebug>
27 #include <QVector2D>
28 #include <QMouseEvent>
29
30 NavaidDiagram::NavaidDiagram(QWidget* pr) :
31     BaseDiagram(pr),
32     m_offsetEnabled(false),
33     m_offsetDistanceNm(5.0),
34     m_offsetBearingDeg(0),
35     m_headingDeg(0)
36 {
37
38 }
39
40 void NavaidDiagram::setNavaid(FGPositionedRef nav)
41 {
42     m_navaid = nav;
43     m_projectionCenter = nav ? nav->geod() : SGGeod();
44     m_geod = nav->geod();
45     recomputeBounds(true);
46 }
47
48 void NavaidDiagram::setGeod(const SGGeod &geod)
49 {
50     m_navaid.clear();
51     m_geod = geod;
52     m_projectionCenter = m_geod;
53     recomputeBounds(true);
54 }
55
56 void NavaidDiagram::setOffsetEnabled(bool offset)
57 {
58     if (m_offsetEnabled == offset)
59         return;
60     m_offsetEnabled = offset;
61     recomputeBounds(true);
62 }
63
64 void NavaidDiagram::setOffsetDistanceNm(double distanceNm)
65 {
66     m_offsetDistanceNm = distanceNm;
67     update();
68 }
69
70 void NavaidDiagram::setOffsetBearingDeg(int bearingDeg)
71 {
72     m_offsetBearingDeg = bearingDeg;
73     update();
74 }
75
76 void NavaidDiagram::paintContents(QPainter *painter)
77 {
78     QPointF base = project(m_geod);
79
80     QPointF airplaneIconPos = base;
81
82     if (m_offsetEnabled) {
83         double d = m_offsetDistanceNm * SG_NM_TO_METER;
84         SGGeod offsetGeod = SGGeodesy::direct(m_geod, m_offsetBearingDeg, d);
85         QPointF offset = project(offsetGeod);
86
87         QPen pen(Qt::green);
88         pen.setCosmetic(true);
89         painter->setPen(pen);
90         painter->drawLine(base, offset);
91
92         airplaneIconPos = offset;
93     }
94
95     QPixmap pix(":/airplane-icon");
96     airplaneIconPos = painter->transform().map(airplaneIconPos);
97     painter->resetTransform();
98     QRect airplaneIconRect = pix.rect();
99     airplaneIconRect.moveCenter(airplaneIconPos.toPoint());
100     painter->drawPixmap(airplaneIconRect, pix);
101 }
102
103 void NavaidDiagram::doComputeBounds()
104 {
105     extendBounds(project(m_geod));
106
107 // project three points around the base location at 40nm to give some
108 // coverage
109     for (int i=0; i<3; ++i) {
110         SGGeod pt = SGGeodesy::direct(m_geod, i * 120, SG_NM_TO_METER * 40.0);
111         extendBounds(project(pt));
112     }
113
114     if (m_offsetEnabled) {
115         double d = m_offsetDistanceNm * SG_NM_TO_METER;
116         SGGeod offsetPos = SGGeodesy::direct(m_geod, m_offsetBearingDeg, d);
117         extendBounds(project(offsetPos));
118     }
119
120 }