AirspaceManager.cc 6.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/****************************************************************************
 *
 *   (c) 2017 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * 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
AirspaceManager::AirspaceManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
28
    , _airspaceVisible(false)
29
{
Gus Grubba's avatar
Gus Grubba committed
30 31
    _ruleUpdateTimer.setInterval(2000);
    _ruleUpdateTimer.setSingleShot(true);
32 33
    _updateTimer.setInterval(1000);
    _updateTimer.setSingleShot(true);
Gus Grubba's avatar
Gus Grubba committed
34 35
    connect(&_ruleUpdateTimer, &QTimer::timeout, this, &AirspaceManager::_updateRulesTimeout);
    connect(&_updateTimer,     &QTimer::timeout, this, &AirspaceManager::_updateTimeout);
36
    qmlRegisterUncreatableType<AirspaceAdvisoryProvider>    ("QGroundControl.Airspace",      1, 0, "AirspaceAdvisoryProvider",       "Reference only");
Gus Grubba's avatar
Gus Grubba committed
37
    qmlRegisterUncreatableType<AirspaceFlightPlanProvider>  ("QGroundControl.Airspace",      1, 0, "AirspaceFlightPlanProvider",     "Reference only");
38 39 40 41 42 43 44
    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
45 46
    qmlRegisterUncreatableType<AirspaceFlightAuthorization> ("QGroundControl.Airspace",      1, 0, "AirspaceFlightAuthorization",    "Reference only");
    qmlRegisterUncreatableType<AirspaceFlightInfo>          ("QGroundControl.Airspace",      1, 0, "AirspaceFlightInfo",             "Reference only");
47 48
}

49
//-----------------------------------------------------------------------------
50 51
AirspaceManager::~AirspaceManager()
{
Gus Grubba's avatar
Gus Grubba committed
52 53 54 55 56
    if(_advisories) {
        delete _advisories;
    }
    if(_weatherProvider) {
        delete _weatherProvider;
57
    }
58 59
    if(_ruleSetsProvider) {
        delete _ruleSetsProvider;
Gus Grubba's avatar
Gus Grubba committed
60
    }
61 62
    if(_airspaces) {
        delete _airspaces;
Gus Grubba's avatar
Gus Grubba committed
63
    }
Gus Grubba's avatar
Gus Grubba committed
64 65 66
    if(_flightPlan) {
        delete _flightPlan;
    }
67 68
}

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

86 87 88
//-----------------------------------------------------------------------------
void
AirspaceManager::setROI(const QGeoCoordinate& pointNW, const QGeoCoordinate& pointSE, bool planView, bool reset)
89
{
90 91 92 93 94
    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) {
95 96 97 98 99 100
                if(reset) {
                    _roi = *_flightPlan->missionArea();
                    _updateToROI(true);
                } else {
                    _setROI(*_flightPlan->missionArea());
                }
101 102 103 104 105
                return;
            }
        }
    }
    //-- Use screen coordinates (what you see is what you get)
106 107 108 109 110 111 112 113 114 115 116
    if(reset) {
        _roi = QGCGeoBoundingCube(pointNW, pointSE);
        _updateToROI(true);
    } else {
        _setROI(QGCGeoBoundingCube(pointNW, pointSE));
    }
}

//-----------------------------------------------------------------------------
void
AirspaceManager::_setROI(const QGCGeoBoundingCube& roi)
117
{
118 119
    if(_roi != roi) {
        _roi = roi;
120
        _updateTimer.start();
121
    }
122 123
}

124 125 126
//-----------------------------------------------------------------------------
void
AirspaceManager::_updateToROI(bool reset)
127
{
128
    if(_airspaces) {
129
        _airspaces->setROI(_roi, reset);
130
    }
131
    if(_ruleSetsProvider) {
132
        _ruleSetsProvider->setROI(_roi, reset);
Gus Grubba's avatar
Gus Grubba committed
133
    }
134
    if(_weatherProvider) {
135
        _weatherProvider->setROI(_roi, reset);
136
    }
137
}
138

Gus Grubba's avatar
Gus Grubba committed
139

140 141
//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
142
AirspaceManager::_updateTimeout()
143 144 145 146 147 148
{
    _updateToROI(false);
}

//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
149
AirspaceManager::_rulesChanged()
150
{
Gus Grubba's avatar
Gus Grubba committed
151 152 153 154 155 156 157 158 159 160
    _ruleUpdateTimer.start();
}

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