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

#include "EditPositionDialogController.h"
#include "QGCGeo.h"
12
#include "QGCApplication.h"
13 14 15 16 17 18 19

const char*  EditPositionDialogController::_latitudeFactName =      "Latitude";
const char*  EditPositionDialogController::_longitudeFactName =     "Longitude";
const char*  EditPositionDialogController::_zoneFactName =          "Zone";
const char*  EditPositionDialogController::_hemisphereFactName =    "Hemisphere";
const char*  EditPositionDialogController::_eastingFactName =       "Easting";
const char*  EditPositionDialogController::_northingFactName =      "Northing";
20
const char*  EditPositionDialogController::_mgrsFactName =          "MGRS";
21 22 23 24 25 26 27 28 29 30

QMap<QString, FactMetaData*> EditPositionDialogController::_metaDataMap;

EditPositionDialogController::EditPositionDialogController(void)
    : _latitudeFact     (0, _latitudeFactName,      FactMetaData::valueTypeDouble)
    , _longitudeFact    (0, _longitudeFactName,     FactMetaData::valueTypeDouble)
    , _zoneFact         (0, _zoneFactName,          FactMetaData::valueTypeUint8)
    , _hemisphereFact   (0, _hemisphereFactName,    FactMetaData::valueTypeUint8)
    , _eastingFact      (0, _eastingFactName,       FactMetaData::valueTypeDouble)
    , _northingFact     (0, _northingFactName,      FactMetaData::valueTypeDouble)
31
    , _mgrsFact         (0, _mgrsFactName,          FactMetaData::valueTypeString)
32 33
{
    if (_metaDataMap.isEmpty()) {
34
        _metaDataMap = FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/EditPositionDialog.FactMetaData.json"), nullptr /* QObject parent */);
35 36 37 38 39 40 41 42
    }

    _latitudeFact.setMetaData   (_metaDataMap[_latitudeFactName]);
    _longitudeFact.setMetaData  (_metaDataMap[_longitudeFactName]);
    _zoneFact.setMetaData       (_metaDataMap[_zoneFactName]);
    _hemisphereFact.setMetaData (_metaDataMap[_hemisphereFactName]);
    _eastingFact.setMetaData    (_metaDataMap[_eastingFactName]);
    _northingFact.setMetaData   (_metaDataMap[_northingFactName]);
43
    _mgrsFact.setMetaData       (_metaDataMap[_mgrsFactName]);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
}

void EditPositionDialogController::setCoordinate(QGeoCoordinate coordinate)
{
    if (coordinate != _coordinate) {
        _coordinate = coordinate;
        emit coordinateChanged(coordinate);
    }
}

void EditPositionDialogController::initValues(void)
{
    _latitudeFact.setRawValue(_coordinate.latitude());
    _longitudeFact.setRawValue(_coordinate.longitude());

    double easting, northing;
    int zone = convertGeoToUTM(_coordinate, easting, northing);
61 62 63 64 65 66 67 68 69 70
    if (zone >= 1 && zone <= 60) {
        _zoneFact.setRawValue(zone);
        _hemisphereFact.setRawValue(_coordinate.latitude() < 0);
        _eastingFact.setRawValue(easting);
        _northingFact.setRawValue(northing);
    }
    QString mgrs = convertGeoToMGRS(_coordinate);
    if (!mgrs.isEmpty()) {
        _mgrsFact.setRawValue(mgrs);
    }
71 72 73 74 75 76 77 78 79 80 81 82
}

void EditPositionDialogController::setFromGeo(void)
{
    _coordinate.setLatitude(_latitudeFact.rawValue().toDouble());
    _coordinate.setLongitude(_longitudeFact.rawValue().toDouble());
    emit coordinateChanged(_coordinate);
}

void EditPositionDialogController::setFromUTM(void)
{
    qDebug() << _eastingFact.rawValue().toDouble() << _northingFact.rawValue().toDouble() << _zoneFact.rawValue().toInt() << (_hemisphereFact.rawValue().toInt() == 1);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    if (convertUTMToGeo(_eastingFact.rawValue().toDouble(), _northingFact.rawValue().toDouble(), _zoneFact.rawValue().toInt(), _hemisphereFact.rawValue().toInt() == 1, _coordinate)) {
        qDebug() << _eastingFact.rawValue().toDouble() << _northingFact.rawValue().toDouble() << _zoneFact.rawValue().toInt() << (_hemisphereFact.rawValue().toInt() == 1) << _coordinate;
        emit coordinateChanged(_coordinate);
    } else {
        initValues();
    }
}

void EditPositionDialogController::setFromMGRS(void)
{
    if (convertMGRSToGeo(_mgrsFact.rawValue().toString(), _coordinate)) {
        emit coordinateChanged(_coordinate);
    } else {
        initValues();
    }
98
}
99 100 101 102 103 104 105

void EditPositionDialogController::setFromVehicle(void)
{
    _coordinate = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()->coordinate();
    emit coordinateChanged(_coordinate);
}