QGroundControlQmlGlobal.cc 12.1 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/
9

10 11 12 13 14

/// @file
///     @author Don Gagne <don@thegagnes.com>

#include "QGroundControlQmlGlobal.h"
15 16

#include <QSettings>
Don Gagne's avatar
Don Gagne committed
17 18
#include <QLineF>
#include <QPointF>
19

Don Gagne's avatar
Don Gagne committed
20
static const char* kQmlGlobalKeyName = "QGCQml";
21

22 23 24 25 26 27
SettingsFact* QGroundControlQmlGlobal::_distanceUnitsFact =                         NULL;
FactMetaData* QGroundControlQmlGlobal::_distanceUnitsMetaData =                     NULL;
SettingsFact* QGroundControlQmlGlobal::_areaUnitsFact =                             NULL;
FactMetaData* QGroundControlQmlGlobal::_areaUnitsMetaData =                         NULL;
SettingsFact* QGroundControlQmlGlobal::_speedUnitsFact =                            NULL;
FactMetaData* QGroundControlQmlGlobal::_speedUnitsMetaData =                        NULL;
28 29 30 31
SettingsFact* QGroundControlQmlGlobal::_offlineEditingFirmwareTypeFact =            NULL;
SettingsFact* QGroundControlQmlGlobal::_offlineEditingVehicleTypeFact =             NULL;
SettingsFact* QGroundControlQmlGlobal::_offlineEditingCruiseSpeedFact =             NULL;
SettingsFact* QGroundControlQmlGlobal::_offlineEditingHoverSpeedFact =              NULL;
32
SettingsFact* QGroundControlQmlGlobal::_batteryPercentRemainingAnnounceFact =       NULL;
33

34
const char* QGroundControlQmlGlobal::_virtualTabletJoystickKey  = "VirtualTabletJoystick";
35
const char* QGroundControlQmlGlobal::_baseFontPointSizeKey      = "BaseDeviceFontPointSize";
36

37 38 39 40 41 42
QGroundControlQmlGlobal::QGroundControlQmlGlobal(QGCApplication* app)
    : QGCTool(app)
    , _flightMapSettings(NULL)
    , _homePositionManager(NULL)
    , _linkManager(NULL)
    , _multiVehicleManager(NULL)
dogmaphobic's avatar
dogmaphobic committed
43
    , _mapEngineManager(NULL)
44 45
    , _qgcPositionManager(NULL)
    , _missionCommandTree(NULL)
46
    , _videoManager(NULL)
Gus Grubba's avatar
Gus Grubba committed
47
    , _mavlinkLogManager(NULL)
48
    , _corePlugin(NULL)
49
    , _virtualTabletJoystick(false)
50
    , _baseFontPointSize(0.0)
51
{
52
    QSettings settings;
53 54
    _virtualTabletJoystick  = settings.value(_virtualTabletJoystickKey, false).toBool();
    _baseFontPointSize      = settings.value(_baseFontPointSizeKey, 0.0).toDouble();
55

56 57
    // We clear the parent on this object since we run into shutdown problems caused by hybrid qml app. Instead we let it leak on shutdown.
    setParent(NULL);
58 59 60 61 62
}

QGroundControlQmlGlobal::~QGroundControlQmlGlobal()
{

63
}
64

65 66 67
void QGroundControlQmlGlobal::setToolbox(QGCToolbox* toolbox)
{
    QGCTool::setToolbox(toolbox);
dogmaphobic's avatar
dogmaphobic committed
68 69 70 71
    _flightMapSettings      = toolbox->flightMapSettings();
    _homePositionManager    = toolbox->homePositionManager();
    _linkManager            = toolbox->linkManager();
    _multiVehicleManager    = toolbox->multiVehicleManager();
Jimmy Johnson's avatar
Jimmy Johnson committed
72
    _mapEngineManager       = toolbox->mapEngineManager();
73 74
    _qgcPositionManager     = toolbox->qgcPositionManager();
    _missionCommandTree     = toolbox->missionCommandTree();
75
    _videoManager           = toolbox->videoManager();
Gus Grubba's avatar
Gus Grubba committed
76
    _mavlinkLogManager      = toolbox->mavlinkLogManager();
77
    _corePlugin             = toolbox->corePlugin();
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
void QGroundControlQmlGlobal::saveGlobalSetting (const QString& key, const QString& value)
{
    QSettings settings;
    settings.beginGroup(kQmlGlobalKeyName);
    settings.setValue(key, value);
}

QString QGroundControlQmlGlobal::loadGlobalSetting (const QString& key, const QString& defaultValue)
{
    QSettings settings;
    settings.beginGroup(kQmlGlobalKeyName);
    return settings.value(key, defaultValue).toString();
}

void QGroundControlQmlGlobal::saveBoolGlobalSetting (const QString& key, bool value)
{
    QSettings settings;
    settings.beginGroup(kQmlGlobalKeyName);
    settings.setValue(key, value);
}

bool QGroundControlQmlGlobal::loadBoolGlobalSetting (const QString& key, bool defaultValue)
{
    QSettings settings;
    settings.beginGroup(kQmlGlobalKeyName);
    return settings.value(key, defaultValue).toBool();
}
107 108 109 110

void QGroundControlQmlGlobal::startPX4MockLink(bool sendStatusText)
{
#ifdef QT_DEBUG
111
    MockLink::startPX4MockLink(sendStatusText);
Don Gagne's avatar
Don Gagne committed
112 113
#else
    Q_UNUSED(sendStatusText);
114 115 116 117 118 119
#endif
}

void QGroundControlQmlGlobal::startGenericMockLink(bool sendStatusText)
{
#ifdef QT_DEBUG
120
    MockLink::startGenericMockLink(sendStatusText);
Don Gagne's avatar
Don Gagne committed
121 122
#else
    Q_UNUSED(sendStatusText);
123 124 125 126 127 128
#endif
}

void QGroundControlQmlGlobal::startAPMArduCopterMockLink(bool sendStatusText)
{
#ifdef QT_DEBUG
129
    MockLink::startAPMArduCopterMockLink(sendStatusText);
Don Gagne's avatar
Don Gagne committed
130 131
#else
    Q_UNUSED(sendStatusText);
132 133 134 135 136 137
#endif
}

void QGroundControlQmlGlobal::startAPMArduPlaneMockLink(bool sendStatusText)
{
#ifdef QT_DEBUG
138
    MockLink::startAPMArduPlaneMockLink(sendStatusText);
Don Gagne's avatar
Don Gagne committed
139 140
#else
    Q_UNUSED(sendStatusText);
141 142 143
#endif
}

144 145 146 147 148 149 150 151 152
void QGroundControlQmlGlobal::startAPMArduSubMockLink(bool sendStatusText)
{
#ifdef QT_DEBUG
    MockLink::startAPMArduSubMockLink(sendStatusText);
#else
    Q_UNUSED(sendStatusText);
#endif
}

153 154 155 156 157
void QGroundControlQmlGlobal::stopAllMockLinks(void)
{
#ifdef QT_DEBUG
    LinkManager* linkManager = qgcApp()->toolbox()->linkManager();

Don Gagne's avatar
Don Gagne committed
158 159
    for (int i=0; i<linkManager->links()->count(); i++) {
        LinkInterface* link = linkManager->links()->value<LinkInterface*>(i);
160
        MockLink* mockLink = qobject_cast<MockLink*>(link);
Don Gagne's avatar
Don Gagne committed
161

162
        if (mockLink) {
Don Gagne's avatar
Don Gagne committed
163
            linkManager->disconnectLink(mockLink);
164 165 166 167
        }
    }
#endif
}
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

void QGroundControlQmlGlobal::setIsDarkStyle(bool dark)
{
    qgcApp()->setStyle(dark);
    emit isDarkStyleChanged(dark);
}

void QGroundControlQmlGlobal::setIsAudioMuted(bool muted)
{
    qgcApp()->toolbox()->audioOutput()->mute(muted);
    emit isAudioMutedChanged(muted);
}

void QGroundControlQmlGlobal::setIsSaveLogPrompt(bool prompt)
{
    qgcApp()->setPromptFlightDataSave(prompt);
    emit isSaveLogPromptChanged(prompt);
}

void QGroundControlQmlGlobal::setIsSaveLogPromptNotArmed(bool prompt)
{
    qgcApp()->setPromptFlightDataSaveNotArmed(prompt);
    emit isSaveLogPromptNotArmedChanged(prompt);
}

Don Gagne's avatar
Don Gagne committed
193 194 195 196 197 198
void QGroundControlQmlGlobal::setIsVersionCheckEnabled(bool enable)
{
    qgcApp()->toolbox()->mavlinkProtocol()->enableVersionCheck(enable);
    emit isVersionCheckEnabledChanged(enable);
}

199 200 201 202 203 204
void QGroundControlQmlGlobal::setMavlinkSystemID(int id)
{
    qgcApp()->toolbox()->mavlinkProtocol()->setSystemId(id);
    emit mavlinkSystemIDChanged(id);
}

205 206 207 208 209 210 211 212 213
void QGroundControlQmlGlobal::setVirtualTabletJoystick(bool enabled)
{
    if (_virtualTabletJoystick != enabled) {
        QSettings settings;
        settings.setValue(_virtualTabletJoystickKey, enabled);
        _virtualTabletJoystick = enabled;
        emit virtualTabletJoystickChanged(enabled);
    }
}
214

215 216 217 218 219 220 221 222 223 224
void QGroundControlQmlGlobal::setBaseFontPointSize(qreal size)
{
    if (size >= 6.0 && size <= 48.0) {
        QSettings settings;
        settings.setValue(_baseFontPointSizeKey, size);
        _baseFontPointSize = size;
        emit baseFontPointSizeChanged(size);
    }
}

225
SettingsFact* QGroundControlQmlGlobal::_createSettingsFact(const QString& name)
226
{
227 228
    SettingsFact*   fact;
    FactMetaData*   metaData = nameToMetaDataMap()[name];
229

230 231
    fact = new SettingsFact(QString(), name, metaData->type(), metaData->rawDefaultValue());
    fact->setMetaData(metaData);
232

233 234
    return fact;
}
235

236 237 238 239
Fact* QGroundControlQmlGlobal::offlineEditingFirmwareType(void)
{
    if (!_offlineEditingFirmwareTypeFact) {
        _offlineEditingFirmwareTypeFact = _createSettingsFact(QStringLiteral("OfflineEditingFirmwareType"));
240 241 242 243 244
    }

    return _offlineEditingFirmwareTypeFact;
}

245 246 247
Fact* QGroundControlQmlGlobal::offlineEditingVehicleType(void)
{
    if (!_offlineEditingVehicleTypeFact) {
248
        _offlineEditingVehicleTypeFact = _createSettingsFact(QStringLiteral("OfflineEditingVehicleType"));
249 250 251 252 253 254 255 256
    }

    return _offlineEditingVehicleTypeFact;
}

Fact* QGroundControlQmlGlobal::offlineEditingCruiseSpeed(void)
{
    if (!_offlineEditingCruiseSpeedFact) {
257
        _offlineEditingCruiseSpeedFact = _createSettingsFact(QStringLiteral("OfflineEditingCruiseSpeed"));
258 259 260 261 262 263 264
    }
    return _offlineEditingCruiseSpeedFact;
}

Fact* QGroundControlQmlGlobal::offlineEditingHoverSpeed(void)
{
    if (!_offlineEditingHoverSpeedFact) {
265
        _offlineEditingHoverSpeedFact = _createSettingsFact(QStringLiteral("OfflineEditingHoverSpeed"));
266 267 268 269
    }
    return _offlineEditingHoverSpeedFact;
}

270 271 272
Fact* QGroundControlQmlGlobal::distanceUnits(void)
{
    if (!_distanceUnitsFact) {
273
        // Distance/Area/Speed units settings can't be loaded from json since it creates an infinite loop of meta data loading.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        QStringList     enumStrings;
        QVariantList    enumValues;

        _distanceUnitsFact = new SettingsFact(QString(), "DistanceUnits", FactMetaData::valueTypeUint32, DistanceUnitsMeters);
        _distanceUnitsMetaData = new FactMetaData(FactMetaData::valueTypeUint32);

        enumStrings << "Feet" << "Meters";
        enumValues << QVariant::fromValue((uint32_t)DistanceUnitsFeet) << QVariant::fromValue((uint32_t)DistanceUnitsMeters);

        _distanceUnitsMetaData->setEnumInfo(enumStrings, enumValues);
        _distanceUnitsFact->setMetaData(_distanceUnitsMetaData);
    }

    return _distanceUnitsFact;

}

291 292 293
Fact* QGroundControlQmlGlobal::areaUnits(void)
{
    if (!_areaUnitsFact) {
294
        // Distance/Area/Speed units settings can't be loaded from json since it creates an infinite loop of meta data loading.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
        QStringList     enumStrings;
        QVariantList    enumValues;

        _areaUnitsFact = new SettingsFact(QString(), "AreaUnits", FactMetaData::valueTypeUint32, AreaUnitsSquareMeters);
        _areaUnitsMetaData = new FactMetaData(FactMetaData::valueTypeUint32);

        enumStrings << "SquareFeet" << "SquareMeters" << "SquareKilometers" << "Hectares" << "Acres" << "SquareMiles";
        enumValues << QVariant::fromValue((uint32_t)AreaUnitsSquareFeet) << QVariant::fromValue((uint32_t)AreaUnitsSquareMeters) << QVariant::fromValue((uint32_t)AreaUnitsSquareKilometers) << QVariant::fromValue((uint32_t)AreaUnitsHectares) << QVariant::fromValue((uint32_t)AreaUnitsAcres) << QVariant::fromValue((uint32_t)AreaUnitsSquareMiles);

        _areaUnitsMetaData->setEnumInfo(enumStrings, enumValues);
        _areaUnitsFact->setMetaData(_areaUnitsMetaData);
    }

    return _areaUnitsFact;

}

312 313 314
Fact* QGroundControlQmlGlobal::speedUnits(void)
{
    if (!_speedUnitsFact) {
315
        // Distance/Area/Speed units settings can't be loaded from json since it creates an infinite loop of meta data loading.
316 317 318 319 320 321
        QStringList     enumStrings;
        QVariantList    enumValues;

        _speedUnitsFact = new SettingsFact(QString(), "SpeedUnits", FactMetaData::valueTypeUint32, SpeedUnitsMetersPerSecond);
        _speedUnitsMetaData = new FactMetaData(FactMetaData::valueTypeUint32);

322
        enumStrings << "Feet/second" << "Meters/second" << "Miles/hour" << "Kilometers/hour" << "Knots";
323 324 325 326 327 328 329 330
        enumValues << QVariant::fromValue((uint32_t)SpeedUnitsFeetPerSecond) << QVariant::fromValue((uint32_t)SpeedUnitsMetersPerSecond) << QVariant::fromValue((uint32_t)SpeedUnitsMilesPerHour) << QVariant::fromValue((uint32_t)SpeedUnitsKilometersPerHour) << QVariant::fromValue((uint32_t)SpeedUnitsKnots);

        _speedUnitsMetaData->setEnumInfo(enumStrings, enumValues);
        _speedUnitsFact->setMetaData(_speedUnitsMetaData);
    }

    return _speedUnitsFact;
}
331

332 333 334
Fact* QGroundControlQmlGlobal::batteryPercentRemainingAnnounce(void)
{
    if (!_batteryPercentRemainingAnnounceFact) {
335
        _batteryPercentRemainingAnnounceFact = _createSettingsFact(QStringLiteral("batteryPercentRemainingAnnounce"));
336 337 338 339 340
    }

    return _batteryPercentRemainingAnnounceFact;
}

341 342 343 344 345 346 347
bool QGroundControlQmlGlobal::linesIntersect(QPointF line1A, QPointF line1B, QPointF line2A, QPointF line2B)
{
    QPointF intersectPoint;

    return QLineF(line1A, line1B).intersect(QLineF(line2A, line2B), &intersectPoint) == QLineF::BoundedIntersection &&
            intersectPoint != line1A && intersectPoint != line1B;
}
348 349 350 351 352 353 354 355 356 357

QMap<QString, FactMetaData*>& QGroundControlQmlGlobal::nameToMetaDataMap(void) {
    static QMap<QString, FactMetaData*> map;

    if (map.isEmpty()) {
        map = FactMetaData::createMapFromJsonFile(":/json/QGroundControlQmlGlobal.json", NULL);
    }

    return map;
}