UnitsSettings.cc 9.85 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10 11 12
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "UnitsSettings.h"

#include <QQmlEngine>
Don Gagne's avatar
Don Gagne committed
13
#include <QtQml>
14

15
DECLARE_SETTINGGROUP(Units, "Units")
16 17 18 19
{
    qmlRegisterUncreatableType<UnitsSettings>("QGroundControl.SettingsManager", 1, 0, "UnitsSettings", "Reference only");
}

Remek Zajac's avatar
Remek Zajac committed
20
DECLARE_SETTINGSFACT_NO_FUNC(UnitsSettings, horizontalDistanceUnits)
21
{
Remek Zajac's avatar
Remek Zajac committed
22
    if (!_horizontalDistanceUnitsFact) {
23 24 25 26
        // Distance/Area/Speed units settings can't be loaded from json since it creates an infinite loop of meta data loading.
        QStringList     enumStrings;
        QVariantList    enumValues;
        enumStrings << "Feet" << "Meters";
Remek Zajac's avatar
Remek Zajac committed
27 28
        enumValues << QVariant::fromValue(static_cast<uint32_t>(HorizontalDistanceUnitsFeet))
                   << QVariant::fromValue(static_cast<uint32_t>(HorizontalDistanceUnitsMeters));
29
        FactMetaData* metaData = new FactMetaData(FactMetaData::valueTypeUint32, this);
Remek Zajac's avatar
Remek Zajac committed
30
        metaData->setName(horizontalDistanceUnitsName);
31
        metaData->setShortDescription("Distance units");
32
        metaData->setEnumInfo(enumStrings, enumValues);
33

Remek Zajac's avatar
Remek Zajac committed
34
        HorizontalDistanceUnits defaultHorizontalDistanceUnit = HorizontalDistanceUnitsMeters;
35 36
        switch(QLocale::system().measurementSystem()) {
            case QLocale::MetricSystem: {
Remek Zajac's avatar
Remek Zajac committed
37
                defaultHorizontalDistanceUnit = HorizontalDistanceUnitsMeters;
38 39 40
            } break;
            case QLocale::ImperialUSSystem:
            case QLocale::ImperialUKSystem:
Remek Zajac's avatar
Remek Zajac committed
41
                defaultHorizontalDistanceUnit = HorizontalDistanceUnitsFeet;
42 43
                break;
        }
Remek Zajac's avatar
Remek Zajac committed
44
        metaData->setRawDefaultValue(defaultHorizontalDistanceUnit);
45
        metaData->setQGCRebootRequired(true);
Remek Zajac's avatar
Remek Zajac committed
46
        _horizontalDistanceUnitsFact = new SettingsFact(_settingsGroup, metaData, this);
47
    }
Remek Zajac's avatar
Remek Zajac committed
48
    return _horizontalDistanceUnitsFact;
49 50
}

Remek Zajac's avatar
Remek Zajac committed
51
DECLARE_SETTINGSFACT_NO_FUNC(UnitsSettings, verticalDistanceUnits)
52
{
Remek Zajac's avatar
Remek Zajac committed
53
    if (!_verticalDistanceUnitsFact) {
54 55 56 57
        // Distance/Area/Speed units settings can't be loaded from json since it creates an infinite loop of meta data loading.
        QStringList     enumStrings;
        QVariantList    enumValues;
        enumStrings << "Feet" << "Meters";
Remek Zajac's avatar
Remek Zajac committed
58 59
        enumValues << QVariant::fromValue(static_cast<uint32_t>(VerticalDistanceUnitsFeet))
                   << QVariant::fromValue(static_cast<uint32_t>(VerticalDistanceUnitsMeters));
60
        FactMetaData* metaData = new FactMetaData(FactMetaData::valueTypeUint32, this);
Remek Zajac's avatar
Remek Zajac committed
61
        metaData->setName(verticalDistanceUnitsName);
62 63
        metaData->setShortDescription("Altitude units");
        metaData->setEnumInfo(enumStrings, enumValues);
Remek Zajac's avatar
Remek Zajac committed
64
        VerticalDistanceUnits defaultVerticalAltitudeUnit = VerticalDistanceUnitsMeters;
65 66
        switch(QLocale::system().measurementSystem()) {
            case QLocale::MetricSystem: {
Remek Zajac's avatar
Remek Zajac committed
67
                defaultVerticalAltitudeUnit = VerticalDistanceUnitsMeters;
68 69 70
            } break;
            case QLocale::ImperialUSSystem:
            case QLocale::ImperialUKSystem:
Remek Zajac's avatar
Remek Zajac committed
71
                defaultVerticalAltitudeUnit = VerticalDistanceUnitsFeet;
72 73
                break;
        }
Remek Zajac's avatar
Remek Zajac committed
74
        metaData->setRawDefaultValue(defaultVerticalAltitudeUnit);
75
        metaData->setQGCRebootRequired(true);
Remek Zajac's avatar
Remek Zajac committed
76
        _verticalDistanceUnitsFact = new SettingsFact(_settingsGroup, metaData, this);
77
    }
Remek Zajac's avatar
Remek Zajac committed
78
    return _verticalDistanceUnitsFact;
79 80
}

81
DECLARE_SETTINGSFACT_NO_FUNC(UnitsSettings, areaUnits)
82 83 84 85 86 87
{
    if (!_areaUnitsFact) {
        // Distance/Area/Speed units settings can't be loaded from json since it creates an infinite loop of meta data loading.
        QStringList     enumStrings;
        QVariantList    enumValues;
        enumStrings << "SquareFeet" << "SquareMeters" << "SquareKilometers" << "Hectares" << "Acres" << "SquareMiles";
88 89 90 91 92 93 94
        enumValues <<
            QVariant::fromValue(static_cast<uint32_t>(AreaUnitsSquareFeet)) <<
            QVariant::fromValue(static_cast<uint32_t>(AreaUnitsSquareMeters)) <<
            QVariant::fromValue(static_cast<uint32_t>(AreaUnitsSquareKilometers)) <<
            QVariant::fromValue(static_cast<uint32_t>(AreaUnitsHectares)) <<
            QVariant::fromValue(static_cast<uint32_t>(AreaUnitsAcres)) <<
            QVariant::fromValue(static_cast<uint32_t>(AreaUnitsSquareMiles));
95
        FactMetaData* metaData = new FactMetaData(FactMetaData::valueTypeUint32, this);
96
        metaData->setName(areaUnitsName);
97
        metaData->setShortDescription("Area units");
98
        metaData->setEnumInfo(enumStrings, enumValues);
99 100 101 102 103 104 105 106 107 108 109 110

        AreaUnits defaultAreaUnit = AreaUnitsSquareMeters;
        switch(QLocale::system().measurementSystem()) {
            case QLocale::MetricSystem: {
                defaultAreaUnit = AreaUnitsSquareMeters;
            } break;
            case QLocale::ImperialUSSystem:
            case QLocale::ImperialUKSystem:
                defaultAreaUnit = AreaUnitsSquareMiles;
                break;
        }
        metaData->setRawDefaultValue(defaultAreaUnit);
111
        metaData->setQGCRebootRequired(true);
112
        _areaUnitsFact = new SettingsFact(_settingsGroup, metaData, this);
113 114 115 116
    }
    return _areaUnitsFact;
}

117
DECLARE_SETTINGSFACT_NO_FUNC(UnitsSettings, speedUnits)
118 119 120 121 122 123
{
    if (!_speedUnitsFact) {
        // Distance/Area/Speed units settings can't be loaded from json since it creates an infinite loop of meta data loading.
        QStringList     enumStrings;
        QVariantList    enumValues;
        enumStrings << "Feet/second" << "Meters/second" << "Miles/hour" << "Kilometers/hour" << "Knots";
124 125 126 127 128 129
        enumValues <<
            QVariant::fromValue(static_cast<uint32_t>(SpeedUnitsFeetPerSecond)) <<
            QVariant::fromValue(static_cast<uint32_t>(SpeedUnitsMetersPerSecond)) <<
            QVariant::fromValue(static_cast<uint32_t>(SpeedUnitsMilesPerHour)) <<
            QVariant::fromValue(static_cast<uint32_t>(SpeedUnitsKilometersPerHour)) <<
            QVariant::fromValue(static_cast<uint32_t>(SpeedUnitsKnots));
130
        FactMetaData* metaData = new FactMetaData(FactMetaData::valueTypeUint32, this);
131
        metaData->setName(speedUnitsName);
132
        metaData->setShortDescription("Speed units");
133
        metaData->setEnumInfo(enumStrings, enumValues);
134 135 136 137 138 139 140 141 142 143 144 145

        SpeedUnits defaultSpeedUnit = SpeedUnitsMetersPerSecond;
        switch(QLocale::system().measurementSystem()) {
            case QLocale::MetricSystem: {
                defaultSpeedUnit = SpeedUnitsMetersPerSecond;
            } break;
            case QLocale::ImperialUSSystem:
            case QLocale::ImperialUKSystem:
                defaultSpeedUnit = SpeedUnitsMilesPerHour;
                break;
        }
        metaData->setRawDefaultValue(defaultSpeedUnit);
146
        metaData->setQGCRebootRequired(true);
147
        _speedUnitsFact = new SettingsFact(_settingsGroup, metaData, this);
148 149 150
    }
    return _speedUnitsFact;
}
151

152
DECLARE_SETTINGSFACT_NO_FUNC(UnitsSettings, temperatureUnits)
153 154 155 156 157 158
{
    if (!_temperatureUnitsFact) {
        // Units settings can't be loaded from json since it creates an infinite loop of meta data loading.
        QStringList     enumStrings;
        QVariantList    enumValues;
        enumStrings << "Celsius" << "Farenheit";
159
        enumValues << QVariant::fromValue(static_cast<uint32_t>(TemperatureUnitsCelsius)) << QVariant::fromValue(static_cast<uint32_t>(TemperatureUnitsFarenheit));
160
        FactMetaData* metaData = new FactMetaData(FactMetaData::valueTypeUint32, this);
161
        metaData->setName(temperatureUnitsName);
162
        metaData->setShortDescription("Temperature units");
163
        metaData->setEnumInfo(enumStrings, enumValues);
164 165 166 167 168 169 170 171 172 173 174 175

        TemperatureUnits defaultTemperatureUnit = TemperatureUnitsCelsius;
        switch(QLocale::system().measurementSystem()) {
            case QLocale::MetricSystem: {
                defaultTemperatureUnit = TemperatureUnitsCelsius;
            } break;
            case QLocale::ImperialUSSystem:
            case QLocale::ImperialUKSystem:
                defaultTemperatureUnit = TemperatureUnitsFarenheit;
                break;
        }
        metaData->setRawDefaultValue(defaultTemperatureUnit);
176
        metaData->setQGCRebootRequired(true);
177
        _temperatureUnitsFact = new SettingsFact(_settingsGroup, metaData, this);
178 179 180
    }
    return _temperatureUnitsFact;
}
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

DECLARE_SETTINGSFACT_NO_FUNC(UnitsSettings, weightUnits)
{
    if (!_weightUnitsFact) {
        // Units settings can't be loaded from json since it creates an infinite loop of meta data loading.
        QStringList     enumStrings;
        QVariantList    enumValues;
        enumStrings << "Grams" << "Kilograms" << "Ounces" << "Pounds";
        enumValues
            << QVariant::fromValue(static_cast<uint32_t>(WeightUnitsGrams))
            << QVariant::fromValue(static_cast<uint32_t>(WeightUnitsKg))
            << QVariant::fromValue(static_cast<uint32_t>(WeightUnitsOz))
            << QVariant::fromValue(static_cast<uint32_t>(WeightUnitsLbs));
        FactMetaData* metaData = new FactMetaData(FactMetaData::valueTypeUint32, this);
        metaData->setName(weightUnitsName);
        metaData->setShortDescription(tr("Weight units"));
        metaData->setEnumInfo(enumStrings, enumValues);
        WeightUnits defaultWeightUnit = WeightUnitsGrams;
        switch(QLocale::system().measurementSystem()) {
            case QLocale::MetricSystem:
            case QLocale::ImperialUKSystem: {
                defaultWeightUnit = WeightUnitsGrams;
            } break;
            case QLocale::ImperialUSSystem:
                defaultWeightUnit = WeightUnitsOz;
                break;
        }
        metaData->setRawDefaultValue(defaultWeightUnit);
        metaData->setQGCRebootRequired(true);
        _weightUnitsFact = new SettingsFact(_settingsGroup, metaData, this);
    }
    return _weightUnitsFact;
}