AirspaceManager.cc 6.15 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 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()
{
Gus Grubba's avatar
Gus Grubba committed
51 52 53 54 55
    if(_advisories) {
        delete _advisories;
    }
    if(_weatherProvider) {
        delete _weatherProvider;
56
    }
57 58
    if(_ruleSetsProvider) {
        delete _ruleSetsProvider;
Gus Grubba's avatar
Gus Grubba committed
59
    }
60 61
    if(_airspaces) {
        delete _airspaces;
Gus Grubba's avatar
Gus Grubba committed
62
    }
Gus Grubba's avatar
Gus Grubba committed
63 64 65
    if(_flightPlan) {
        delete _flightPlan;
    }
66 67
}

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

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

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

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

Gus Grubba's avatar
Gus Grubba committed
138

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

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

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