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