PositionManager.cpp 2.28 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.
 *
 ****************************************************************************/
Jimmy Johnson's avatar
Jimmy Johnson committed
9 10 11 12 13 14 15 16 17 18 19

#include "PositionManager.h"

QGCPositionManager::QGCPositionManager(QGCApplication* app) :
    QGCTool(app),
    _updateInterval(0),
    _currentSource(nullptr)
{
    _defaultSource = QGeoPositionInfoSource::createDefaultSource(this);
    _simulatedSource = new SimulatedPosition();

20
    // Enable this to get a simulated target on desktop
Jimmy Johnson's avatar
Jimmy Johnson committed
21

22 23 24
    // if (_defaultSource == nullptr) {
    //     _defaultSource = _simulatedSource;
    // }
Jimmy Johnson's avatar
Jimmy Johnson committed
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

    setPositionSource(QGCPositionSource::GPS);
}

QGCPositionManager::~QGCPositionManager()
{
    delete(_simulatedSource);
}

void QGCPositionManager::positionUpdated(const QGeoPositionInfo &update)
{

    QGeoCoordinate position(update.coordinate().latitude(), update.coordinate().longitude());

    emit lastPositionUpdated(update.isValid(), QVariant::fromValue(position));
    emit positionInfoUpdated(update);
}

int QGCPositionManager::updateInterval() const
{
    return _updateInterval;
}

void QGCPositionManager::setPositionSource(QGCPositionManager::QGCPositionSource source)
{
50
    if (_currentSource != nullptr) {
Jimmy Johnson's avatar
Jimmy Johnson committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        _currentSource->stopUpdates();
        disconnect(_currentSource, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
    }

    switch(source) {
    case QGCPositionManager::Log:
        break;
    case QGCPositionManager::Simulated:
        _currentSource = _simulatedSource;
        break;
    case QGCPositionManager::GPS:
    default:        
        _currentSource = _defaultSource;
        break;
    }

67 68 69 70 71
    if (_currentSource != nullptr) {
        _updateInterval = _currentSource->minimumUpdateInterval();
        _currentSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
        _currentSource->setUpdateInterval(_updateInterval);
        _currentSource->startUpdates();
Jimmy Johnson's avatar
Jimmy Johnson committed
72

73 74
        connect(_currentSource, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
75 76
}