FlightMapSettings.cc 4.22 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 "QGCApplication.h"
11
#include "FlightMapSettings.h"
Gus Grubba's avatar
Gus Grubba committed
12
#include "QGCMapEngine.h"
13 14
#include "AppSettings.h"
#include "SettingsManager.h"
15 16 17 18

#include <QQmlEngine>
#include <QtQml>

19
DECLARE_SETTINGGROUP(FlightMap, "FlightMap")
20 21 22 23 24
{
    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
25 26
    _savedMapTypeStrings = _nameToMetaDataMap[mapTypeName]->enumStrings();
    _savedMapTypeValues  = _nameToMetaDataMap[mapTypeName]->enumValues();
27 28

#ifdef QGC_NO_GOOGLE_MAPS
Gus Grubba's avatar
Gus Grubba committed
29 30
    //-- Remove Google
    _excludeProvider(mapProviderGoogle);
31
#endif
32
    if(qgcApp()->toolbox()->settingsManager()->appSettings()->mapboxToken()->rawValue().toString().isEmpty()) {
Gus Grubba's avatar
Gus Grubba committed
33
        _excludeProvider(mapProviderMapbox);
Gus Grubba's avatar
Gus Grubba committed
34
    }
35
    if(qgcApp()->toolbox()->settingsManager()->appSettings()->esriToken()->rawValue().toString().isEmpty()) {
Gus Grubba's avatar
Gus Grubba committed
36 37
        _excludeProvider(mapProviderEsri);
    }
38 39 40
    _newMapProvider(mapProvider()->rawValue());
}

41 42 43
DECLARE_SETTINGSFACT(FlightMapSettings, mapType)

DECLARE_SETTINGSFACT_NO_FUNC(FlightMapSettings, mapProvider)
44 45
{
    if (!_mapProviderFact) {
46
        _mapProviderFact = _createSettingsFact(mapProviderName);
47 48 49 50 51
        connect(_mapProviderFact, &Fact::rawValueChanged, this, &FlightMapSettings::_newMapProvider);
    }
    return _mapProviderFact;
}

Gus Grubba's avatar
Gus Grubba committed
52 53
void FlightMapSettings::_excludeProvider(MapProvider_t provider)
{
54
    FactMetaData* metaData = _nameToMetaDataMap[mapProviderName];
Gus Grubba's avatar
Gus Grubba committed
55 56 57 58 59 60
    QVariantList enumValues = metaData->enumValues();
    QStringList enumStrings = metaData->enumStrings();
    _removeEnumValue(provider, enumStrings, enumValues);
    metaData->setEnumInfo(enumStrings, enumValues);
}

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
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)
{
80
    FactMetaData* metaData = _nameToMetaDataMap[mapTypeName];
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    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;
97 98 99 100 101
    case mapProviderEniro:
        _removeEnumValue(mapTypeStreet, enumStrings, enumValues);
        _removeEnumValue(mapTypeSatellite, enumStrings, enumValues);
        _removeEnumValue(mapTypeHybrid, enumStrings, enumValues);
        break;
102 103 104
    case mapProviderEsri:
        _removeEnumValue(mapTypeHybrid, enumStrings, enumValues);
        break;
stmoon's avatar
stmoon committed
105 106 107
    case mapProviderVWorld:
        _removeEnumValue(mapTypeHybrid, enumStrings, enumValues);
        _removeEnumValue(mapTypeTerrain, enumStrings, enumValues);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    }
    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);
    }
}