PositionManager.cpp 2.56 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

#include "PositionManager.h"
11 12
#include "QGCApplication.h"
#include "QGCCorePlugin.h"
Jimmy Johnson's avatar
Jimmy Johnson committed
13

14 15 16 17
QGCPositionManager::QGCPositionManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
    , _updateInterval(0)
    , _currentSource(nullptr)
Jimmy Johnson's avatar
Jimmy Johnson committed
18 19 20 21 22 23 24 25 26
{

}

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

27
void QGCPositionManager::setToolbox(QGCToolbox *toolbox)
Jimmy Johnson's avatar
Jimmy Johnson committed
28
{
29 30 31 32 33 34 35 36
   QGCTool::setToolbox(toolbox);
   //-- First see if plugin provides a position source
   _defaultSource = toolbox->corePlugin()->createPositionSource(this);
   if(!_defaultSource) {
       //-- Otherwise, create a default one
       _defaultSource = QGeoPositionInfoSource::createDefaultSource(this);
   }
   _simulatedSource = new SimulatedPosition();
Jimmy Johnson's avatar
Jimmy Johnson committed
37

38 39 40 41 42 43 44
   // Enable this to get a simulated target on desktop
   // if (_defaultSource == nullptr) {
   //     _defaultSource = _simulatedSource;
   // }

   setPositionSource(QGCPositionSource::GPS);
}
Jimmy Johnson's avatar
Jimmy Johnson committed
45

46 47 48
void QGCPositionManager::positionUpdated(const QGeoPositionInfo &update)
{
    emit lastPositionUpdated(update.isValid(), QVariant::fromValue(update.coordinate()));
Jimmy Johnson's avatar
Jimmy Johnson committed
49 50 51 52 53 54 55 56 57 58
    emit positionInfoUpdated(update);
}

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

void QGCPositionManager::setPositionSource(QGCPositionManager::QGCPositionSource source)
{
59
    if (_currentSource != nullptr) {
Jimmy Johnson's avatar
Jimmy Johnson committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        _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;
    }

76 77 78 79 80
    if (_currentSource != nullptr) {
        _updateInterval = _currentSource->minimumUpdateInterval();
        _currentSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
        _currentSource->setUpdateInterval(_updateInterval);
        connect(_currentSource, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
81
        _currentSource->startUpdates();
82
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
83 84
}