AppSettings.cc 9.68 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
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "AppSettings.h"
11 12
#include "QGCPalette.h"
#include "QGCApplication.h"
13
#include "ParameterManager.h"
14 15

#include <QQmlEngine>
Don Gagne's avatar
Don Gagne committed
16
#include <QtQml>
17
#include <QStandardPaths>
18

19
const char* AppSettings::parameterFileExtension =   "params";
20
const char* AppSettings::planFileExtension =        "plan";
21
const char* AppSettings::missionFileExtension =     "mission";
22
const char* AppSettings::waypointsFileExtension =   "waypoints";
23 24 25
const char* AppSettings::fenceFileExtension =       "fence";
const char* AppSettings::rallyPointFileExtension =  "rally";
const char* AppSettings::telemetryFileExtension =   "tlog";
26
const char* AppSettings::kmlFileExtension =         "kml";
27
const char* AppSettings::shpFileExtension =         "shp";
28
const char* AppSettings::logFileExtension =         "ulg";
29

30 31 32 33
const char* AppSettings::parameterDirectory =       "Parameters";
const char* AppSettings::telemetryDirectory =       "Telemetry";
const char* AppSettings::missionDirectory =         "Missions";
const char* AppSettings::logDirectory =             "Logs";
34
const char* AppSettings::videoDirectory =           "Video";
35
const char* AppSettings::crashDirectory =           "CrashLogs";
36

37
DECLARE_SETTINGGROUP(App, "")
38
{
39
    qmlRegisterUncreatableType<AppSettings>("QGroundControl.SettingsManager", 1, 0, "AppSettings", "Reference only");
Don Gagne's avatar
Don Gagne committed
40
    QGCPalette::setGlobalTheme(indoorPalette()->rawValue().toBool() ? QGCPalette::Dark : QGCPalette::Light);
41

42 43 44 45 46 47 48 49 50
    // virtualJoystickCentralized -> virtualJoystickAutoCenterThrottle
    QSettings settings;
    settings.beginGroup(_settingsGroup);
    QString deprecatedVirtualJoystickCentralizedKey("virtualJoystickCentralized");
    if (settings.contains(deprecatedVirtualJoystickCentralizedKey)) {
        settings.setValue(virtualJoystickAutoCenterThrottleName, settings.value(deprecatedVirtualJoystickCentralizedKey));
        settings.remove(deprecatedVirtualJoystickCentralizedKey);
    }

51 52
    // Instantiate savePath so we can check for override and setup default path if needed

DonLakeFlyer's avatar
DonLakeFlyer committed
53
    SettingsFact* savePathFact = qobject_cast<SettingsFact*>(savePath());
54
    QString appName = qgcApp()->applicationName();
55
#ifdef __mobile__
56 57
    // Mobile builds always use the runtime generated location for savePath.
    bool userHasModifiedSavePath = false;
58
#else
59
    bool userHasModifiedSavePath = !savePathFact->rawValue().toString().isEmpty() || !_nameToMetaDataMap[savePathName]->rawDefaultValue().toString().isEmpty();
60
#endif
61 62

    if (!userHasModifiedSavePath) {
63
#ifdef __mobile__
64 65 66 67 68
    #ifdef __ios__
        // This will expose the directories directly to the File iOs app
        QDir rootDir = QDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
        savePathFact->setRawValue(rootDir.absolutePath());
    #else
69
        QDir rootDir = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
70 71
        savePathFact->setRawValue(rootDir.filePath(appName));
    #endif
72 73 74 75
        savePathFact->setVisible(false);
#else
        QDir rootDir = QDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
        savePathFact->setRawValue(rootDir.filePath(appName));
76
#endif
77 78 79 80
    }

    connect(savePathFact, &Fact::rawValueChanged, this, &AppSettings::savePathsChanged);
    connect(savePathFact, &Fact::rawValueChanged, this, &AppSettings::_checkSavePathDirectories);
81

82
    _checkSavePathDirectories();
83
    //-- Keep track of language changes
84 85
    SettingsFact* languageFact = qobject_cast<SettingsFact*>(language());
    connect(languageFact, &Fact::rawValueChanged, this, &AppSettings::_languageChanged);
86 87
}

88 89 90 91 92 93 94 95 96 97 98
DECLARE_SETTINGSFACT(AppSettings, offlineEditingFirmwareType)
DECLARE_SETTINGSFACT(AppSettings, offlineEditingVehicleType)
DECLARE_SETTINGSFACT(AppSettings, offlineEditingCruiseSpeed)
DECLARE_SETTINGSFACT(AppSettings, offlineEditingHoverSpeed)
DECLARE_SETTINGSFACT(AppSettings, offlineEditingAscentSpeed)
DECLARE_SETTINGSFACT(AppSettings, offlineEditingDescentSpeed)
DECLARE_SETTINGSFACT(AppSettings, batteryPercentRemainingAnnounce)
DECLARE_SETTINGSFACT(AppSettings, defaultMissionItemAltitude)
DECLARE_SETTINGSFACT(AppSettings, telemetrySave)
DECLARE_SETTINGSFACT(AppSettings, telemetrySaveNotArmed)
DECLARE_SETTINGSFACT(AppSettings, audioMuted)
99
DECLARE_SETTINGSFACT(AppSettings, checkInternet)
100
DECLARE_SETTINGSFACT(AppSettings, virtualJoystick)
101
DECLARE_SETTINGSFACT(AppSettings, virtualJoystickAutoCenterThrottle)
102 103 104 105 106
DECLARE_SETTINGSFACT(AppSettings, appFontPointSize)
DECLARE_SETTINGSFACT(AppSettings, showLargeCompass)
DECLARE_SETTINGSFACT(AppSettings, savePath)
DECLARE_SETTINGSFACT(AppSettings, autoLoadMissions)
DECLARE_SETTINGSFACT(AppSettings, useChecklist)
107
DECLARE_SETTINGSFACT(AppSettings, enforceChecklist)
108 109 110 111 112
DECLARE_SETTINGSFACT(AppSettings, mapboxToken)
DECLARE_SETTINGSFACT(AppSettings, esriToken)
DECLARE_SETTINGSFACT(AppSettings, defaultFirmwareType)
DECLARE_SETTINGSFACT(AppSettings, gstDebugLevel)
DECLARE_SETTINGSFACT(AppSettings, followTarget)
113
DECLARE_SETTINGSFACT(AppSettings, apmStartMavlinkStreams)
114 115
DECLARE_SETTINGSFACT(AppSettings, enableTaisync)
DECLARE_SETTINGSFACT(AppSettings, enableTaisyncVideo)
116
DECLARE_SETTINGSFACT(AppSettings, enableMicrohard)
117
DECLARE_SETTINGSFACT(AppSettings, language)
118
DECLARE_SETTINGSFACT(AppSettings, disableAllPersistence)
Gus Grubba's avatar
Gus Grubba committed
119
DECLARE_SETTINGSFACT(AppSettings, usePairing)
120
DECLARE_SETTINGSFACT(AppSettings, saveCsvTelemetry)
121
DECLARE_SETTINGSFACT(AppSettings, firstRunPromptIdsShown)
122 123 124 125 126 127 128 129 130 131

DECLARE_SETTINGSFACT_NO_FUNC(AppSettings, indoorPalette)
{
    if (!_indoorPaletteFact) {
        _indoorPaletteFact = _createSettingsFact(indoorPaletteName);
        connect(_indoorPaletteFact, &Fact::rawValueChanged, this, &AppSettings::_indoorPaletteChanged);
    }
    return _indoorPaletteFact;
}

132 133 134 135 136
void AppSettings::_languageChanged()
{
    qgcApp()->setLanguage();
}

137 138 139 140 141 142 143 144 145 146
void AppSettings::_checkSavePathDirectories(void)
{
    QDir savePathDir(savePath()->rawValue().toString());
    if (!savePathDir.exists()) {
        QDir().mkpath(savePathDir.absolutePath());
    }
    if (savePathDir.exists()) {
        savePathDir.mkdir(parameterDirectory);
        savePathDir.mkdir(telemetryDirectory);
        savePathDir.mkdir(missionDirectory);
147
        savePathDir.mkdir(logDirectory);
148
        savePathDir.mkdir(videoDirectory);
149
        savePathDir.mkdir(crashDirectory);
150
    }
151 152
}

153 154
void AppSettings::_indoorPaletteChanged(void)
{
Don Gagne's avatar
Don Gagne committed
155
    QGCPalette::setGlobalTheme(indoorPalette()->rawValue().toBool() ? QGCPalette::Dark : QGCPalette::Light);
156
}
157

158 159 160 161 162 163 164
QString AppSettings::missionSavePath(void)
{
    QString path = savePath()->rawValue().toString();
    if (!path.isEmpty() && QDir(path).exists()) {
        QDir dir(path);
        return dir.filePath(missionDirectory);
    }
165
    return QString();
166 167 168 169 170 171 172 173 174
}

QString AppSettings::parameterSavePath(void)
{
    QString path = savePath()->rawValue().toString();
    if (!path.isEmpty() && QDir(path).exists()) {
        QDir dir(path);
        return dir.filePath(parameterDirectory);
    }
175
    return QString();
176 177 178 179 180 181 182 183 184
}

QString AppSettings::telemetrySavePath(void)
{
    QString path = savePath()->rawValue().toString();
    if (!path.isEmpty() && QDir(path).exists()) {
        QDir dir(path);
        return dir.filePath(telemetryDirectory);
    }
185
    return QString();
186 187
}

188 189 190 191 192 193 194
QString AppSettings::logSavePath(void)
{
    QString path = savePath()->rawValue().toString();
    if (!path.isEmpty() && QDir(path).exists()) {
        QDir dir(path);
        return dir.filePath(logDirectory);
    }
195
    return QString();
196 197
}

198 199 200 201 202 203 204
QString AppSettings::videoSavePath(void)
{
    QString path = savePath()->rawValue().toString();
    if (!path.isEmpty() && QDir(path).exists()) {
        QDir dir(path);
        return dir.filePath(videoDirectory);
    }
205 206 207 208 209 210 211 212 213 214 215
    return QString();
}

QString AppSettings::crashSavePath(void)
{
    QString path = savePath()->rawValue().toString();
    if (!path.isEmpty() && QDir(path).exists()) {
        QDir dir(path);
        return dir.filePath(crashDirectory);
    }
    return QString();
216 217
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
MAV_AUTOPILOT AppSettings::offlineEditingFirmwareTypeFromFirmwareType(MAV_AUTOPILOT firmwareType)
{
    if (firmwareType != MAV_AUTOPILOT_PX4 && firmwareType != MAV_AUTOPILOT_ARDUPILOTMEGA) {
        firmwareType = MAV_AUTOPILOT_GENERIC;
    }
    return firmwareType;
}

MAV_TYPE AppSettings::offlineEditingVehicleTypeFromVehicleType(MAV_TYPE vehicleType)
{
    if (QGCMAVLink::isRover(vehicleType)) {
        return MAV_TYPE_GROUND_ROVER;
    } else if (QGCMAVLink::isSub(vehicleType)) {
        return MAV_TYPE_SUBMARINE;
    } else if (QGCMAVLink::isVTOL(vehicleType)) {
        return MAV_TYPE_VTOL_QUADROTOR;
    } else if (QGCMAVLink::isFixedWing(vehicleType)) {
        return MAV_TYPE_FIXED_WING;
    } else {
        return MAV_TYPE_QUADROTOR;
    }
}
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

QList<int> AppSettings::firstRunPromptsIdsVariantToList(const QVariant& firstRunPromptIds)
{
    QList<int> rgIds;
    QStringList strIdList = firstRunPromptIds.toString().split(",", QString::SkipEmptyParts);
    for (const QString& strId: strIdList) {
        rgIds.append(strId.toInt());
    }
    return rgIds;
}

QVariant AppSettings::firstRunPromptsIdsListToVariant(const QList<int>& rgIds)
{
    QStringList strList;
    for (int id: rgIds) {
        strList.append(QString::number(id));
    }
    return QVariant(strList.join(","));
}

void AppSettings::firstRunPromptIdsMarkIdAsShown(int id)
{
    QList<int> rgIds = firstRunPromptsIdsVariantToList(firstRunPromptIdsShown()->rawValue());
    if (!rgIds.contains(id)) {
        rgIds.append(id);
        firstRunPromptIdsShown()->setRawValue(firstRunPromptsIdsListToVariant(rgIds));
    }
}