AirMapRulesetsManager.cc 9.31 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 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.
 *
 ****************************************************************************/

10
#include "AirMapRulesetsManager.h"
Gus Grubba's avatar
Gus Grubba committed
11
#include "AirMapManager.h"
12

Gus Grubba's avatar
Gus Grubba committed
13 14
using namespace airmap;

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
//-----------------------------------------------------------------------------
AirMapRuleFeature::AirMapRuleFeature(QObject* parent)
    : AirspaceRuleFeature(parent)
{
}

//-----------------------------------------------------------------------------
AirMapRuleFeature::AirMapRuleFeature(airmap::RuleSet::Feature feature, QObject* parent)
    : AirspaceRuleFeature(parent)
    , _feature(feature)
{
    //-- TODO: Read possible saved value from previous runs
}

//-----------------------------------------------------------------------------
AirspaceRuleFeature::Type
AirMapRuleFeature::type()
{
    return AirspaceRuleFeature::Unknown;
}

//-----------------------------------------------------------------------------
AirspaceRuleFeature::Unit
AirMapRuleFeature::unit()
{
    return AirspaceRuleFeature::UnknownUnit;
}

//-----------------------------------------------------------------------------
AirspaceRuleFeature::Measurement
AirMapRuleFeature::measurement()
{
    return AirspaceRuleFeature::UnknownMeasurement;
}

Gus Grubba's avatar
Gus Grubba committed
50 51 52
//-----------------------------------------------------------------------------
AirMapRule::AirMapRule(QObject* parent)
    : AirspaceRule(parent)
53 54 55
{
}

56 57 58 59 60 61 62
//-----------------------------------------------------------------------------
AirMapRule::AirMapRule(const airmap::RuleSet::Rule& rule, QObject* parent)
    : AirspaceRule(parent)
    , _rule(rule)
{
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
//-----------------------------------------------------------------------------
AirspaceRule::Status
AirMapRule::status()
{
    switch(_rule.status) {
    case RuleSet::Rule::Status::conflicting:
        return AirspaceRule::Conflicting;
    case RuleSet::Rule::Status::not_conflicting:
        return AirspaceRule::NotConflicting;
    case RuleSet::Rule::Status::missing_info:
        return AirspaceRule::MissingInfo;
    case RuleSet::Rule::Status::unknown:
    default:
        return AirspaceRule::Unknown;
    }
}

//-----------------------------------------------------------------------------
AirMapRuleSet::AirMapRuleSet(QObject* parent)
    : AirspaceRuleSet(parent)
Gus Grubba's avatar
Gus Grubba committed
83
    , _isDefault(false)
Gus Grubba's avatar
Gus Grubba committed
84
    , _selected(false)
85
    , _selectionType(AirspaceRuleSet::Optional)
Gus Grubba's avatar
Gus Grubba committed
86 87 88
{
}

89 90 91 92 93 94
//-----------------------------------------------------------------------------
AirMapRuleSet::~AirMapRuleSet()
{
    _rules.deleteListAndContents();
}

95 96 97 98 99 100
//-----------------------------------------------------------------------------
AirMapRulesetsManager::AirMapRulesetsManager(AirMapSharedState& shared)
    : _shared(shared)
{
}

101 102 103 104 105 106 107 108 109 110
//-----------------------------------------------------------------------------
static bool
rules_sort(QObject* a, QObject* b)
{
    AirMapRule* aa = qobject_cast<AirMapRule*>(a);
    AirMapRule* bb = qobject_cast<AirMapRule*>(b);
    if(!aa || !bb) return false;
    return (int)aa->order() > (int)bb->order();
}

111 112 113 114 115 116 117 118
//-----------------------------------------------------------------------------
void AirMapRulesetsManager::setROI(const QGeoCoordinate& center)
{
    if (!_shared.client()) {
        qCDebug(AirMapManagerLog) << "No AirMap client instance. Not updating Airspace";
        return;
    }
    if (_state != State::Idle) {
119
        qCWarning(AirMapManagerLog) << "AirMapRulesetsManager::updateROI: state not idle";
120 121 122
        return;
    }
    qCDebug(AirMapManagerLog) << "Setting ROI for Rulesets";
123
    _valid = false;
124
    _ruleSets.clearAndDeleteContents();
125 126 127 128 129 130 131 132 133
    _state = State::RetrieveItems;
    RuleSets::Search::Parameters params;
    params.geometry = Geometry::point(center.latitude(), center.longitude());
    std::weak_ptr<LifetimeChecker> isAlive(_instance);
    _shared.client()->rulesets().search(params,
            [this, isAlive](const RuleSets::Search::Result& result) {
        if (!isAlive.lock()) return;
        if (_state != State::RetrieveItems) return;
        if (result) {
134 135 136 137 138 139 140 141 142 143
            const std::vector<RuleSet> rulesets = result.value();
            qCDebug(AirMapManagerLog) << "Successful rulesets search. Items:" << rulesets.size();
            for (const auto& ruleset : rulesets) {
                AirMapRuleSet* pRuleSet = new AirMapRuleSet(this);
                connect(pRuleSet, &AirspaceRuleSet::selectedChanged, this, &AirMapRulesetsManager::_selectedChanged);
                pRuleSet->_id          = QString::fromStdString(ruleset.id);
                pRuleSet->_name        = QString::fromStdString(ruleset.name);
                pRuleSet->_shortName   = QString::fromStdString(ruleset.short_name);
                pRuleSet->_description = QString::fromStdString(ruleset.description);
                pRuleSet->_isDefault   = ruleset.is_default;
144
                //-- TODO: This should be persistent and if the new incoming set has the
145 146 147
                //   same, previosuly selected rulesets, it should "remember".
                if(pRuleSet->_isDefault) {
                    pRuleSet->_selected = true;
148 149 150
                }
                switch(ruleset.selection_type) {
                case RuleSet::SelectionType::pickone:
151
                    pRuleSet->_selectionType = AirspaceRuleSet::Pickone;
152 153
                    break;
                case RuleSet::SelectionType::required:
154 155
                    pRuleSet->_selectionType = AirspaceRuleSet::Required;
                    pRuleSet->_selected = true;
156 157 158
                    break;
                default:
                case RuleSet::SelectionType::optional:
159
                    pRuleSet->_selectionType = AirspaceRuleSet::Optional;
160 161
                    break;
                }
162 163 164 165 166 167 168
                //-- Iterate Rules
                for (const auto& rule : ruleset.rules) {
                    AirMapRule* pRule = new AirMapRule(rule, this);
                    pRuleSet->_rules.append(pRule);
                }
                //-- Sort rules by display order
                std::sort(pRuleSet->_rules.objectList()->begin(), pRuleSet->_rules.objectList()->end(), rules_sort);
169 170
                _ruleSets.append(pRuleSet);
                qCDebug(AirMapManagerLog) << "Adding ruleset" << pRuleSet->name();
Gus Grubba's avatar
Gus Grubba committed
171
                /*
172 173 174 175 176 177 178 179 180 181 182
                qDebug() << "------------------------------------------";
                qDebug() << "Jurisdiction:" << ruleset.jurisdiction.name.data() << (int)ruleset.jurisdiction.region;
                qDebug() << "Name:        " << ruleset.name.data();
                qDebug() << "Short Name:  " << ruleset.short_name.data();
                qDebug() << "Description: " << ruleset.description.data();
                qDebug() << "Is default:  " << ruleset.is_default;
                qDebug() << "Applicable to these airspace types:";
                for (const auto& airspaceType : ruleset.airspace_types) {
                    qDebug() << airspaceType.data();
                }
                qDebug() << "Rules:";
183
                for (const auto& rule : ruleset.rules) {
184 185 186 187 188 189 190 191 192 193 194 195
                    qDebug() << "    --------------------------------------";
                    qDebug() << "    " << rule.short_text.data();
                    qDebug() << "    " << rule.description.data();
                    qDebug() << "    " << rule.display_order;
                    qDebug() << "    " << (int)rule.status;
                    qDebug() << "     Features:";
                    for (const auto& feature : rule.features) {
                        qDebug() << "        " << feature.name.data();
                        qDebug() << "        " << feature.description.data();
                        qDebug() << "        " << (int)feature.status;
                    }
                }
Gus Grubba's avatar
Gus Grubba committed
196
                */
197
            }
198
            _valid = true;
199 200
        } else {
            QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
201
            emit error("Failed to retrieve RuleSets", QString::fromStdString(result.error().message()), description);
202 203
        }
        _state = State::Idle;
204 205
        emit ruleSetsChanged();
        emit selectedRuleSetsChanged();
206 207
    });
}
208 209 210

//-----------------------------------------------------------------------------
QString
211
AirMapRulesetsManager::selectedRuleSets()
212
{
213
    QString selection;
214 215
    for(int i = 0; i < _ruleSets.count(); i++) {
        AirMapRuleSet* rule = qobject_cast<AirMapRuleSet*>(_ruleSets.get(i));
216 217
        if(rule && rule->selected()) {
            selection += rule->shortName() + ", ";
218 219
        }
    }
220 221 222 223 224
    int idx = selection.lastIndexOf(", ");
    if(idx >= 0) {
        selection = selection.left(idx);
    }
    return selection;
225 226 227 228
}

//-----------------------------------------------------------------------------
void
229
AirMapRulesetsManager::_selectedChanged()
230
{
231
    emit selectedRuleSetsChanged();
232 233
    //-- TODO: Do whatever it is you do to select a rule
}
234 235 236 237 238 239

//-----------------------------------------------------------------------------
QStringList
AirMapRulesetsManager::rulesetsIDs()
{
    QStringList list;
240 241
    for(int i = 0; i < _ruleSets.count(); i++) {
        AirMapRuleSet* rule = qobject_cast<AirMapRuleSet*>(_ruleSets.get(i));
242 243 244 245 246 247
        if(rule && rule->selected()) {
            list << rule->id();
        }
    }
    return list;
}