]> git.mxchange.org Git - flightgear.git/blob - src/GUI/BaseDiagram.hxx
Code cleanups, code updates and fix at least on (possible) devide-by-zero
[flightgear.git] / src / GUI / BaseDiagram.hxx
1 // BaseDiagram.hxx - 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 #ifndef GUI_BASEDIAGRAM_HXX
22 #define GUI_BASEDIAGRAM_HXX
23
24 #include <QWidget>
25 #include <QPainterPath>
26 #include <QHash>
27
28 #include <simgear/math/sg_geodesy.hxx>
29
30 #include <Navaids/positioned.hxx>
31 #include <Airports/airport.hxx>
32 #include <Navaids/PolyLine.hxx>
33
34 #include "QtLauncher_fwd.hxx"
35
36 class BaseDiagram : public QWidget
37 {
38     Q_OBJECT
39 public:
40     BaseDiagram(QWidget* pr);
41
42     enum IconOption
43     {
44         NoOptions = 0,
45         SmallIcons = 0x1,
46         LargeAirportPlans = 0x2
47     };
48
49     Q_DECLARE_FLAGS(IconOptions, IconOption)
50
51     static QPixmap iconForPositioned(const FGPositionedRef &pos, const IconOptions& options = NoOptions);
52     static QPixmap iconForAirport(FGAirport *apt, const IconOptions& options = NoOptions);
53
54     static QVector<QLineF> projectAirportRuwaysIntoRect(FGAirportRef apt, const QRectF& bounds);
55     static QVector<QLineF> projectAirportRuwaysWithCenter(FGAirportRef apt, const SGGeod &c);
56
57     void setAircraftType(LauncherAircraftType type);
58 protected:
59     virtual void paintEvent(QPaintEvent* pe);
60
61     virtual void mousePressEvent(QMouseEvent* me);
62     virtual void mouseMoveEvent(QMouseEvent* me);
63
64     virtual void wheelEvent(QWheelEvent* we);
65
66     virtual void paintContents(QPainter*);
67
68
69 protected:
70     void recomputeBounds(bool resetZoom);
71
72     virtual void doComputeBounds();
73
74     void extendBounds(const QPointF& p);
75     QPointF project(const SGGeod& geod) const;
76     QTransform transform() const;
77     
78     void clearIgnoredNavaids();
79     void addIgnoredNavaid(FGPositionedRef pos);
80
81     SGGeod m_projectionCenter;
82     double m_scale;
83     QRectF m_bounds;
84     bool m_autoScalePan;
85     QPointF m_panOffset, m_lastMousePos;
86     int m_wheelAngleDeltaAccumulator;
87     bool m_didPan;
88     LauncherAircraftType m_aircraftType;
89
90     static void extendRect(QRectF& r, const QPointF& p);
91
92     static QPointF project(const SGGeod &geod, const SGGeod &center);
93
94     static SGGeod unproject(const QPointF &xy, const SGGeod &center);
95
96     void paintAirplaneIcon(QPainter *painter, const SGGeod &geod, int headingDeg);
97 private:
98     enum LabelPosition
99     {
100         LABEL_RIGHT = 0,
101         LABEL_ABOVE,
102         LABEL_BELOW,
103         LABEL_LEFT,
104         LABEL_NE,
105         LABEL_SE,
106         LABEL_SW,
107         LABEL_NW,
108         LAST_POSITION // marker value
109     };
110
111     void paintNavaids(QPainter *p);
112
113     bool isNavaidIgnored(const FGPositionedRef& pos) const;
114
115     bool isLabelRectAvailable(const QRect& r) const;
116     QRect rectAndFlagsForLabel(PositionedID guid, const QRect &item,
117                                const QSize &bounds,
118                                int & flags /* out parameter */) const;
119     QRect labelPositioned(const QRect &itemRect, const QSize &bounds, LabelPosition lp) const;
120
121     QVector<FGPositionedRef> m_ignored;
122
123     mutable QHash<PositionedID, LabelPosition> m_labelPositions;
124     mutable QVector<QRect> m_labelRects;
125
126     static int textFlagsForLabelPosition(LabelPosition pos);
127
128     void splitItems(const FGPositionedList &in, FGPositionedList &navaids, FGPositionedList &ports);
129     void paintNavaid(QPainter *painter,
130                      const QTransform& t,
131                      const FGPositionedRef &pos);
132     void paintPolygonData(QPainter *painter);
133     void paintGeodVec(QPainter *painter, const flightgear::SGGeodVec &vec);
134     void fillClosedGeodVec(QPainter *painter, const QColor &color, const flightgear::SGGeodVec &vec);
135 };
136
137 Q_DECLARE_OPERATORS_FOR_FLAGS(BaseDiagram::IconOptions)
138
139 #endif // of GUI_BASEDIAGRAM_HXX