FlightMapSettings.cc 3.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 107 108 109 110 111 112 113 114 115 116
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/

#include "FlightMapSettings.h"

#include <QQmlEngine>
#include <QtQml>

const char* FlightMapSettings::flightMapSettingsGroupName =  "FlightMap";
const char* FlightMapSettings::mapProviderSettingsName =     "MapProvider";
const char* FlightMapSettings::mapTypeSettingsName =         "MapType";
const char* FlightMapSettings::_settingsGroupName =          "FlightMap";

FlightMapSettings::FlightMapSettings(QObject* parent)
    : SettingsGroup(flightMapSettingsGroupName, QString(_settingsGroupName) /* root settings group */, parent)
    , _mapProviderFact(NULL)
    , _mapTypeFact(NULL)
{
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
    qmlRegisterUncreatableType<FlightMapSettings>("QGroundControl.SettingsManager", 1, 0, "FlightMapSettings", "Reference only");

    // Save the original version since we modify based on map provider
    _savedMapTypeStrings = _nameToMetaDataMap[mapTypeSettingsName]->enumStrings();
    _savedMapTypeValues = _nameToMetaDataMap[mapTypeSettingsName]->enumValues();

#ifdef QGC_NO_GOOGLE_MAPS
    // Find google in the list and remove it
    FactMetaData* metaData = _nameToMetaDataMap[mapProviderSettingsName];
    QVariantList enumValues = metaData->enumValues();
    QStringList enumStrings = metaData->enumStrings();
    _removeEnumValue(mapProviderGoogle, enumStrings, enumValues);
    metaData->setEnumInfo(enumStrings, enumValues);
#endif

    _newMapProvider(mapProvider()->rawValue());
}

Fact* FlightMapSettings::mapProvider(void)
{
    if (!_mapProviderFact) {
        _mapProviderFact = _createSettingsFact(mapProviderSettingsName);
        connect(_mapProviderFact, &Fact::rawValueChanged, this, &FlightMapSettings::_newMapProvider);
    }

    return _mapProviderFact;
}

Fact* FlightMapSettings::mapType(void)
{
    if (!_mapTypeFact) {
        _mapTypeFact = _createSettingsFact(mapTypeSettingsName);
    }

    return _mapTypeFact;
}

void FlightMapSettings::_removeEnumValue(int value, QStringList& enumStrings, QVariantList& enumValues)
{
    bool found = false;
    int removeIndex;
    for (removeIndex=0; removeIndex<enumValues.count(); removeIndex++) {
        if (enumValues[removeIndex].toInt() == value) {
            found = true;
            break;
        }
    }

    if (found) {
        enumValues.removeAt(removeIndex);
        enumStrings.removeAt(removeIndex);
    }
}

void FlightMapSettings::_newMapProvider(QVariant value)
{
    FactMetaData* metaData = _nameToMetaDataMap[mapTypeSettingsName];

    QStringList enumStrings = _savedMapTypeStrings;
    QVariantList enumValues = _savedMapTypeValues;

    switch (value.toInt()) {
    case mapProviderBing:
        _removeEnumValue(mapTypeTerrain, enumStrings, enumValues);
        break;
    case mapProviderGoogle:
        _removeEnumValue(mapTypeHybrid, enumStrings, enumValues);
        break;
    case mapProviderStarkart:
        _removeEnumValue(mapTypeStreet, enumStrings, enumValues);
        _removeEnumValue(mapTypeSatellite, enumStrings, enumValues);
        _removeEnumValue(mapTypeHybrid, enumStrings, enumValues);
        break;
    }
    metaData->setEnumInfo(enumStrings, enumValues);
    emit mapTypeChanged();

    // Check that map type is still valid for this new map provider

    bool found = false;
    int currentMapType = mapType()->rawValue().toInt();
    for (int i=0; i<enumValues.count(); i++) {
        if (currentMapType == enumValues[i].toInt()) {
            found = true;
            break;
        }
    }
    if (!found) {
        mapType()->setRawValue(0);
    }
}