QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
Viewer3DManager.cc
Go to the documentation of this file.
1#include "Viewer3DManager.h"
2
4#include "OsmParser.h"
6#include "Fact.h"
7#include "SettingsManager.h"
8#include "Vehicle.h"
10#include "Viewer3DSettings.h"
11
12#include <QtCore/QVariant>
13
14QGC_LOGGING_CATEGORY(Viewer3DManagerLog, "Viewer3d.Viewer3DManager")
15
17 : QObject(parent)
18 , _mapProvider(new OsmParser(this))
19{
20 _onActiveVehicleChanged(MultiVehicleManager::instance()->activeVehicle());
21
22 connect(_mapProvider, &Viewer3DMapProvider::gpsRefChanged, this, &Viewer3DManager::_onGpsRefChanged);
23 connect(_mapProvider, &Viewer3DMapProvider::mapChanged, this, &Viewer3DManager::_updateVehicleOutsideMapRegion);
24 connect(MultiVehicleManager::instance(), &MultiVehicleManager::activeVehicleChanged, this, &Viewer3DManager::_onActiveVehicleChanged);
25 connect(SettingsManager::instance()->viewer3DSettings()->enabled(), &Fact::rawValueChanged, this, &Viewer3DManager::_onEnabledChanged);
26}
27
29{
30 if (_displayMode == mode) {
31 return;
32 }
33
34 if (mode == DisplayMode::View3D) {
35 if (!SettingsManager::instance()->viewer3DSettings()->enabled()->rawValue().toBool()) {
36 return;
37 }
38 }
39
40 _displayMode = mode;
41 qCDebug(Viewer3DManagerLog) << "Display mode changed to" << (mode == DisplayMode::View3D ? "View3D" : "Map");
42 emit displayModeChanged();
43}
44
45void Viewer3DManager::_onActiveVehicleChanged(Vehicle *vehicle)
46{
47 if (_activeVehicle) {
48 disconnect(_activeVehicle, &Vehicle::coordinateChanged, this, &Viewer3DManager::_onActiveVehicleCoordinateChanged);
49 }
50
51 _activeVehicle = vehicle;
52 if (!_activeVehicle) {
53 if (_gpsRefSource == GpsRefSource::Vehicle) {
54 _gpsRefSource = GpsRefSource::None;
55 _gpsRef = QGeoCoordinate();
56 emit gpsRefChanged();
57 }
58 } else {
59 _onActiveVehicleCoordinateChanged(_activeVehicle->coordinate());
60 connect(_activeVehicle, &Vehicle::coordinateChanged, this, &Viewer3DManager::_onActiveVehicleCoordinateChanged);
61 }
62
63 _updateVehicleOutsideMapRegion();
64}
65
66void Viewer3DManager::_onActiveVehicleCoordinateChanged(const QGeoCoordinate &newCoordinate)
67{
68 if (_gpsRefSource == GpsRefSource::None) {
69 if (newCoordinate.isValid()) {
70 _gpsRef = newCoordinate;
71 _gpsRef.setAltitude(0);
72 _gpsRefSource = GpsRefSource::Vehicle;
73 emit gpsRefChanged();
74
75 qCDebug(Viewer3DManagerLog) << "GPS ref set by vehicle:" << _gpsRef.latitude() << _gpsRef.longitude() << _gpsRef.altitude();
76 }
77 }
78
79 _updateVehicleOutsideMapRegion();
80}
81
82void Viewer3DManager::_onGpsRefChanged(const QGeoCoordinate &newGpsRef, bool isRefSet)
83{
84 if (isRefSet) {
85 _gpsRef = newGpsRef;
86 _gpsRefSource = GpsRefSource::Map;
87 emit gpsRefChanged();
88 qCDebug(Viewer3DManagerLog) << "GPS ref set by map provider:" << _gpsRef.latitude() << _gpsRef.longitude() << _gpsRef.altitude();
89 } else {
90 if (_gpsRefSource != GpsRefSource::Map) {
91 return;
92 }
93
94 _gpsRefSource = GpsRefSource::None;
95 if (_activeVehicle && _activeVehicle->coordinate().isValid()) {
96 _gpsRef = _activeVehicle->coordinate();
97 _gpsRef.setAltitude(0);
98 _gpsRefSource = GpsRefSource::Vehicle;
99 } else {
100 _gpsRef = QGeoCoordinate();
101 }
102 emit gpsRefChanged();
103 }
104}
105
106void Viewer3DManager::_onEnabledChanged(const QVariant &value)
107{
108 if (!value.toBool()) {
110 }
111}
112
113bool Viewer3DManager::coordinateWithinRegion(const QGeoCoordinate &coordinate, const QGeoCoordinate &bbMin, const QGeoCoordinate &bbMax)
114{
115 if (!coordinate.isValid() || !bbMin.isValid() || !bbMax.isValid()) {
116 return false;
117 }
118
119 return (coordinate.latitude() >= bbMin.latitude()) && (coordinate.latitude() <= bbMax.latitude())
120 && (coordinate.longitude() >= bbMin.longitude()) && (coordinate.longitude() <= bbMax.longitude());
121}
122
123void Viewer3DManager::_updateVehicleOutsideMapRegion()
124{
125 bool outside = false;
126
127 if (_mapProvider && _mapProvider->mapLoaded() && _activeVehicle && _activeVehicle->coordinate().isValid()) {
128 const auto [bbMin, bbMax] = _mapProvider->mapBoundingBox();
129 outside = !coordinateWithinRegion(_activeVehicle->coordinate(), bbMin, bbMax);
130 }
131
132 if (outside != _vehicleOutsideMapRegion) {
133 _vehicleOutsideMapRegion = outside;
135
136 if (outside) {
137 qCWarning(Viewer3DManagerLog) << "Vehicle is outside the loaded OSM map region";
138 }
139 }
140}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void rawValueChanged(const QVariant &value)
static MultiVehicleManager * instance()
void activeVehicleChanged(Vehicle *activeVehicle)
static SettingsManager * instance()
void coordinateChanged(QGeoCoordinate coordinate)
QGeoCoordinate coordinate()
Definition Vehicle.h:413
static bool coordinateWithinRegion(const QGeoCoordinate &coordinate, const QGeoCoordinate &bbMin, const QGeoCoordinate &bbMax)
Returns true if the coordinate lies within the lat/lon bounding box. Invalid inputs return false.
void gpsRefChanged()
void vehicleOutsideMapRegionChanged()
void displayModeChanged()
Q_INVOKABLE void setDisplayMode(DisplayMode mode)
virtual std::pair< QGeoCoordinate, QGeoCoordinate > mapBoundingBox() const =0
virtual bool mapLoaded() const =0
void gpsRefChanged(QGeoCoordinate newGpsRef, bool isRefSet)