AirspaceManager.cc 6.13 KB
Newer Older
1 2
/****************************************************************************
 *
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/


Gus Grubba's avatar
Gus Grubba committed
11
#include "AirspaceAdvisoryProvider.h"
Gus Grubba's avatar
Gus Grubba committed
12 13
#include "AirspaceFlightPlanProvider.h"
#include "AirspaceManager.h"
Gus Grubba's avatar
Gus Grubba committed
14
#include "AirspaceRestriction.h"
15
#include "AirspaceRestrictionProvider.h"
Gus Grubba's avatar
Gus Grubba committed
16
#include "AirspaceRulesetsProvider.h"
Gus Grubba's avatar
Gus Grubba committed
17
#include "AirspaceVehicleManager.h"
Gus Grubba's avatar
Gus Grubba committed
18
#include "AirspaceWeatherInfoProvider.h"
Gus Grubba's avatar
Gus Grubba committed
19 20 21

#include "Vehicle.h"
#include "QGCApplication.h"
22 23 24

QGC_LOGGING_CATEGORY(AirspaceManagementLog, "AirspaceManagementLog")

25
//-----------------------------------------------------------------------------
26 27 28
AirspaceManager::AirspaceManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
{
Gus Grubba's avatar
Gus Grubba committed
29 30
    _ruleUpdateTimer.setInterval(2000);
    _ruleUpdateTimer.setSingleShot(true);
31 32
    _updateTimer.setInterval(1000);
    _updateTimer.setSingleShot(true);
Gus Grubba's avatar
Gus Grubba committed
33 34
    connect(&_ruleUpdateTimer, &QTimer::timeout, this, &AirspaceManager::_updateRulesTimeout);
    connect(&_updateTimer,     &QTimer::timeout, this, &AirspaceManager::_updateTimeout);
35
    qmlRegisterUncreatableType<AirspaceAdvisoryProvider>    ("QGroundControl.Airspace",      1, 0, "AirspaceAdvisoryProvider",       "Reference only");
Gus Grubba's avatar
Gus Grubba committed
36
    qmlRegisterUncreatableType<AirspaceFlightPlanProvider>  ("QGroundControl.Airspace",      1, 0, "AirspaceFlightPlanProvider",     "Reference only");
37 38 39 40 41 42 43
    qmlRegisterUncreatableType<AirspaceManager>             ("QGroundControl.Airspace",      1, 0, "AirspaceManager",                "Reference only");
    qmlRegisterUncreatableType<AirspaceRestrictionProvider> ("QGroundControl.Airspace",      1, 0, "AirspaceRestrictionProvider",    "Reference only");
    qmlRegisterUncreatableType<AirspaceRule>                ("QGroundControl.Airspace",      1, 0, "AirspaceRule",                   "Reference only");
    qmlRegisterUncreatableType<AirspaceRuleFeature>         ("QGroundControl.Airspace",      1, 0, "AirspaceRuleFeature",            "Reference only");
    qmlRegisterUncreatableType<AirspaceRuleSet>             ("QGroundControl.Airspace",      1, 0, "AirspaceRuleSet",                "Reference only");
    qmlRegisterUncreatableType<AirspaceRulesetsProvider>    ("QGroundControl.Airspace",      1, 0, "AirspaceRulesetsProvider",       "Reference only");
    qmlRegisterUncreatableType<AirspaceWeatherInfoProvider> ("QGroundControl.Airspace",      1, 0, "AirspaceWeatherInfoProvider",    "Reference only");
Gus Grubba's avatar
Gus Grubba committed
44 45
    qmlRegisterUncreatableType<AirspaceFlightAuthorization> ("QGroundControl.Airspace",      1, 0, "AirspaceFlightAuthorization",    "Reference only");
    qmlRegisterUncreatableType<AirspaceFlightInfo>          ("QGroundControl.Airspace",      1, 0, "AirspaceFlightInfo",             "Reference only");
46 47
}

48
//-----------------------------------------------------------------------------
49 50
AirspaceManager::~AirspaceManager()
{
51 52 53 54 55 56 57 58 59 60
    delete _advisories;
    _advisories = nullptr;
    delete _weatherProvider;
    _weatherProvider = nullptr;
    delete _ruleSetsProvider;
    _ruleSetsProvider = nullptr;
    delete _airspaces;
    _airspaces = nullptr;
    delete _flightPlan;
    _flightPlan = nullptr;
61 62
}

63 64 65
//-----------------------------------------------------------------------------
void
AirspaceManager::setToolbox(QGCToolbox* toolbox)
66 67
{
    QGCTool::setToolbox(toolbox);
68
    // We should not call virtual methods in the constructor, so we instantiate the restriction provider here
69 70 71 72
    _ruleSetsProvider   = _instantiateRulesetsProvider();
    _weatherProvider    = _instatiateAirspaceWeatherInfoProvider();
    _advisories         = _instatiateAirspaceAdvisoryProvider();
    _airspaces          = _instantiateAirspaceRestrictionProvider();
Gus Grubba's avatar
Gus Grubba committed
73
    _flightPlan         = _instantiateAirspaceFlightPlanProvider();
Gus Grubba's avatar
Gus Grubba committed
74 75 76 77
    //-- Keep track of rule changes
    if(_ruleSetsProvider) {
        connect(_ruleSetsProvider, &AirspaceRulesetsProvider::selectedRuleSetsChanged, this, &AirspaceManager::_rulesChanged);
    }
78 79
}

80 81 82
//-----------------------------------------------------------------------------
void
AirspaceManager::setROI(const QGeoCoordinate& pointNW, const QGeoCoordinate& pointSE, bool planView, bool reset)
83
{
84 85 86 87 88
    if(planView) {
        //-- Is there a mission?
        if(_flightPlan->flightPermitStatus() != AirspaceFlightPlanProvider::PermitNone) {
            //-- Is there a polygon to work with?
            if(_flightPlan->missionArea()->isValid() && _flightPlan->missionArea()->area() > 0.0) {
89 90 91 92 93 94
                if(reset) {
                    _roi = *_flightPlan->missionArea();
                    _updateToROI(true);
                } else {
                    _setROI(*_flightPlan->missionArea());
                }
95 96 97 98 99
                return;
            }
        }
    }
    //-- Use screen coordinates (what you see is what you get)
100 101 102 103 104 105 106 107 108 109 110
    if(reset) {
        _roi = QGCGeoBoundingCube(pointNW, pointSE);
        _updateToROI(true);
    } else {
        _setROI(QGCGeoBoundingCube(pointNW, pointSE));
    }
}

//-----------------------------------------------------------------------------
void
AirspaceManager::_setROI(const QGCGeoBoundingCube& roi)
111
{
112 113
    if(_roi != roi) {
        _roi = roi;
114
        _updateTimer.start();
115
    }
116 117
}

118 119 120
//-----------------------------------------------------------------------------
void
AirspaceManager::_updateToROI(bool reset)
121
{
122
    if(_airspaces) {
123
        _airspaces->setROI(_roi, reset);
124
    }
125
    if(_ruleSetsProvider) {
126
        _ruleSetsProvider->setROI(_roi, reset);
Gus Grubba's avatar
Gus Grubba committed
127
    }
128
    if(_weatherProvider) {
129
        _weatherProvider->setROI(_roi, reset);
130
    }
131
}
132

Gus Grubba's avatar
Gus Grubba committed
133

134 135
//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
136
AirspaceManager::_updateTimeout()
137 138 139 140 141 142
{
    _updateToROI(false);
}

//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
143
AirspaceManager::_rulesChanged()
144
{
Gus Grubba's avatar
Gus Grubba committed
145 146 147 148 149 150 151 152 153 154
    _ruleUpdateTimer.start();
}

//-----------------------------------------------------------------------------
void
AirspaceManager::_updateRulesTimeout()
{
    if (_advisories) {
        _advisories->setROI(_roi, true);
    }
155
}