AirMapRulesetsManager.cc 6.07 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;

Gus Grubba's avatar
Gus Grubba committed
15 16 17 18
//-----------------------------------------------------------------------------
AirMapRule::AirMapRule(QObject* parent)
    : AirspaceRule(parent)
    , _isDefault(false)
Gus Grubba's avatar
Gus Grubba committed
19 20
    , _selected(false)
    , _selectionType(AirspaceRule::Optional)
Gus Grubba's avatar
Gus Grubba committed
21 22 23
{
}

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//-----------------------------------------------------------------------------
AirMapRulesetsManager::AirMapRulesetsManager(AirMapSharedState& shared)
    : _shared(shared)
{
}

//-----------------------------------------------------------------------------
void AirMapRulesetsManager::setROI(const QGeoCoordinate& center)
{
    if (!_shared.client()) {
        qCDebug(AirMapManagerLog) << "No AirMap client instance. Not updating Airspace";
        return;
    }
    if (_state != State::Idle) {
        qCWarning(AirMapManagerLog) << "AirMapRestrictionManager::updateROI: state not idle";
        return;
    }
    qCDebug(AirMapManagerLog) << "Setting ROI for Rulesets";
42
    _valid = false;
Gus Grubba's avatar
Gus Grubba committed
43
    _rules.clearAndDeleteContents();
44 45 46 47 48 49 50 51 52
    _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) {
Gus Grubba's avatar
Gus Grubba committed
53
            const std::vector<RuleSet> rules = result.value();
54
            qCDebug(AirMapManagerLog) << "Successful rulesets search. Items:" << rules.size();
Gus Grubba's avatar
Gus Grubba committed
55 56
            for (const auto& ruleset : rules) {
                AirMapRule* pRule = new AirMapRule(this);
57
                connect(pRule, &AirspaceRule::selectedChanged, this, &AirMapRulesetsManager::_selectedChanged);
Gus Grubba's avatar
Gus Grubba committed
58
                pRule->_id          = QString::fromStdString(ruleset.id);
Gus Grubba's avatar
Gus Grubba committed
59 60
                pRule->_name        = QString::fromStdString(ruleset.name);
                pRule->_shortName   = QString::fromStdString(ruleset.short_name);
Gus Grubba's avatar
Gus Grubba committed
61 62
                pRule->_description = QString::fromStdString(ruleset.description);
                pRule->_isDefault   = ruleset.is_default;
63 64
                //-- TODO: This should be persistent and if the new incoming set has the
                //   same, previosuly selected rules, it should "remember".
65
                if(pRule->_isDefault) {
66
                    pRule->_selected = true;
67 68 69
                }
                switch(ruleset.selection_type) {
                case RuleSet::SelectionType::pickone:
Gus Grubba's avatar
Gus Grubba committed
70
                    pRule->_selectionType = AirspaceRule::Pickone;
71 72
                    break;
                case RuleSet::SelectionType::required:
Gus Grubba's avatar
Gus Grubba committed
73
                    pRule->_selectionType = AirspaceRule::Required;
74 75 76
                    break;
                default:
                case RuleSet::SelectionType::optional:
Gus Grubba's avatar
Gus Grubba committed
77
                    pRule->_selectionType = AirspaceRule::Optional;
78 79
                    break;
                }
Gus Grubba's avatar
Gus Grubba committed
80
                _rules.append(pRule);
81
                qCDebug(AirMapManagerLog) << "Adding rule" << pRule->name();
Gus Grubba's avatar
Gus Grubba committed
82
                /*
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
                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:";
                for (const auto& rule : ruleset.rules) {
                    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
107
                */
108
            }
109
            _valid = true;
110 111
        } else {
            QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
112
            emit error("Failed to retrieve RuleSets", QString::fromStdString(result.error().message()), description);
113 114
        }
        _state = State::Idle;
Gus Grubba's avatar
Gus Grubba committed
115
        emit rulesChanged();
116
        emit selectedRulesChanged();
117 118
    });
}
119 120 121

//-----------------------------------------------------------------------------
QString
122
AirMapRulesetsManager::selectedRules()
123
{
124 125 126 127 128
    QString selection;
    for(int i = 0; i < _rules.count(); i++) {
        AirMapRule* rule = qobject_cast<AirMapRule*>(_rules.get(i));
        if(rule && rule->selected()) {
            selection += rule->shortName() + ", ";
129 130
        }
    }
131 132 133 134 135
    int idx = selection.lastIndexOf(", ");
    if(idx >= 0) {
        selection = selection.left(idx);
    }
    return selection;
136 137 138 139
}

//-----------------------------------------------------------------------------
void
140
AirMapRulesetsManager::_selectedChanged()
141
{
142
    emit selectedRulesChanged();
143 144
    //-- TODO: Do whatever it is you do to select a rule
}