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

pixhawk's avatar
pixhawk committed
10 11 12 13

#include <QList>
#include <QApplication>
#include <QTimer>
14
#include <QSettings>
15
#include <QtQml>
Don Gagne's avatar
Don Gagne committed
16

pixhawk's avatar
pixhawk committed
17
#include "UAS.h"
18
#include "UASInterface.h"
19
#include "HomePositionManager.h"
20
#include "QGC.h"
21
#include "QGCApplication.h"
22
#include "MultiVehicleManager.h"
pixhawk's avatar
pixhawk committed
23

LM's avatar
LM committed
24 25 26 27
#define PI 3.1415926535897932384626433832795
#define MEAN_EARTH_DIAMETER	12756274.0
#define UMR	0.017453292519943295769236907684886

28 29 30 31 32
const char* HomePositionManager::_settingsGroup =   "HomePositionManager";
const char* HomePositionManager::_latitudeKey =     "Latitude";
const char* HomePositionManager::_longitudeKey =    "Longitude";
const char* HomePositionManager::_altitudeKey =     "Altitude";

33 34
HomePositionManager::HomePositionManager(QGCApplication* app)
    : QGCTool(app)
35 36 37
    , homeLat(47.3769)
    , homeLon(8.549444)
    , homeAlt(470.0)
38
{
Don Gagne's avatar
Don Gagne committed
39
    qmlRegisterUncreatableType<HomePositionManager> ("QGroundControl", 1, 0, "HomePositionManager", "Reference only");
40 41
}

42
void HomePositionManager::setToolbox(QGCToolbox *toolbox)
43
{
44 45
    QGCTool::setToolbox(toolbox);

46

47
    _loadSettings();
48 49
}

50
void HomePositionManager::_storeSettings(void)
51 52
{
    QSettings settings;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    
    settings.remove(_settingsGroup);
    settings.beginGroup(_settingsGroup);
    
    for (int i=0; i<_homePositions.count(); i++) {
        HomePosition* homePos = qobject_cast<HomePosition*>(_homePositions[i]);
        
        qDebug() << "Saving" << homePos->name();
        
        settings.beginGroup(homePos->name());
        settings.setValue(_latitudeKey, homePos->coordinate().latitude());
        settings.setValue(_longitudeKey, homePos->coordinate().longitude());
        settings.setValue(_altitudeKey, homePos->coordinate().altitude());
        settings.endGroup();
    }
    
    settings.endGroup();
    
    // Deprecated settings for old editor
72 73 74 75 76 77 78
    settings.beginGroup("QGC_UASMANAGER");
    settings.setValue("HOMELAT", homeLat);
    settings.setValue("HOMELON", homeLon);
    settings.setValue("HOMEALT", homeAlt);
    settings.endGroup();
}

79
void HomePositionManager::_loadSettings(void)
80 81
{
    QSettings settings;
82 83 84 85 86
    
    _homePositions.clear();
    
    settings.beginGroup(_settingsGroup);
    
87
    foreach(const QString &name, settings.childGroups()) {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        QGeoCoordinate coordinate;
        
        qDebug() << "Load setting" << name;
        
        settings.beginGroup(name);
        coordinate.setLatitude(settings.value(_latitudeKey).toDouble());
        coordinate.setLongitude(settings.value(_longitudeKey).toDouble());
        coordinate.setAltitude(settings.value(_altitudeKey).toDouble());
        settings.endGroup();
        
        _homePositions.append(new HomePosition(name, coordinate, this));
    }
    
    settings.endGroup();
    
    if (_homePositions.count() == 0) {
104
        _homePositions.append(new HomePosition("ETH Campus", QGeoCoordinate(47.3769, 8.549444, 470.0), this));
Don Gagne's avatar
Don Gagne committed
105
    }    
106 107
}

108
void HomePositionManager::updateHomePosition(const QString& name, const QGeoCoordinate& coordinate)
109
{
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    HomePosition * homePos = NULL;
    
    for (int i=0; i<_homePositions.count(); i++) {
        homePos = qobject_cast<HomePosition*>(_homePositions[i]);
        if (homePos->name() == name) {
            break;
        }
        homePos = NULL;
    }
    
    if (homePos == NULL) {
        HomePosition* homePos = new HomePosition(name, coordinate, this);
        _homePositions.append(homePos);
    } else {
        homePos->setName(name);
        homePos->setCoordinate(coordinate);
    }
    
    _storeSettings();
129
}
LM's avatar
LM committed
130

131
void HomePositionManager::deleteHomePosition(const QString& name)
132
{
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    // Don't allow delete of last position
    if (_homePositions.count() == 1) {
        return;
    }
    
    qDebug() << "Attempting delete" << name;
    
    for (int i=0; i<_homePositions.count(); i++) {
        if (qobject_cast<HomePosition*>(_homePositions[i])->name() == name) {
            qDebug() << "Deleting" << name;
            _homePositions.removeAt(i);
            break;
        }
    }
    
    _storeSettings();
}
LM's avatar
LM committed
150

151
HomePosition::HomePosition(const QString& name, const QGeoCoordinate& coordinate, HomePositionManager* homePositionManager, QObject* parent)
152 153
    : QObject(parent)
    , _coordinate(coordinate)
154
    , _homePositionManager(homePositionManager)
155 156
{
    setObjectName(name);
157 158
}

159
HomePosition::~HomePosition()
160
{
161
    
162 163
}

164
QString HomePosition::name(void)
165
{
166
    return objectName();
167
}
LM's avatar
LM committed
168

169
void HomePosition::setName(const QString& name)
LM's avatar
LM committed
170
{
171
    setObjectName(name);
172
    _homePositionManager->_storeSettings();
173
    emit nameChanged(name);
LM's avatar
LM committed
174 175
}

176
QGeoCoordinate HomePosition::coordinate(void)
LM's avatar
LM committed
177
{
178
    return _coordinate;
LM's avatar
LM committed
179 180
}

181 182 183
void HomePosition::setCoordinate(const QGeoCoordinate& coordinate)
{
    _coordinate = coordinate;
184
    _homePositionManager->_storeSettings();
185 186
    emit coordinateChanged(coordinate);
}