Terrain.h 2.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/****************************************************************************
 *
 *   (c) 2017 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.
 *
 ****************************************************************************/

#pragma once

DonLakeFlyer's avatar
DonLakeFlyer committed
12 13
#include "QGCLoggingCategory.h"

14 15 16
#include <QObject>
#include <QGeoCoordinate>
#include <QNetworkAccessManager>
DonLakeFlyer's avatar
DonLakeFlyer committed
17
#include <QTimer>
18

DonLakeFlyer's avatar
DonLakeFlyer committed
19
Q_DECLARE_LOGGING_CATEGORY(ElevationProviderLog)
20

DonLakeFlyer's avatar
DonLakeFlyer committed
21
class ElevationProvider;
22

DonLakeFlyer's avatar
DonLakeFlyer committed
23 24
/// Used internally by ElevationProvider to batch requests together
class TerrainBatchManager : public QObject {
25 26
    Q_OBJECT

DonLakeFlyer's avatar
DonLakeFlyer committed
27 28
public:
    TerrainBatchManager(void);
29

DonLakeFlyer's avatar
DonLakeFlyer committed
30
    void addQuery(ElevationProvider* elevationProvider, const QList<QGeoCoordinate>& coordinates);
31 32

private slots:
33 34 35
    void _sendNextBatch                 (void);
    void _requestFinished               (void);
    void _elevationProviderDestroyed    (QObject* elevationProvider);
DonLakeFlyer's avatar
DonLakeFlyer committed
36

37
private:
DonLakeFlyer's avatar
DonLakeFlyer committed
38 39 40 41 42 43 44
    typedef struct {
        ElevationProvider*      elevationProvider;
        QList<QGeoCoordinate>   coordinates;
    } QueuedRequestInfo_t;

    typedef struct {
        ElevationProvider*      elevationProvider;
45
        bool                    providerDestroyed;
DonLakeFlyer's avatar
DonLakeFlyer committed
46 47 48
        int                     cCoord;
    } SentRequestInfo_t;

49 50 51 52 53 54

    enum class State {
        Idle,
        Downloading,
    };

DonLakeFlyer's avatar
DonLakeFlyer committed
55
    void _batchFailed(void);
56
    QString _stateToString(State state);
DonLakeFlyer's avatar
DonLakeFlyer committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    QList<QueuedRequestInfo_t>  _requestQueue;
    QList<SentRequestInfo_t>    _sentRequests;
    State                       _state = State::Idle;
    QNetworkAccessManager       _networkManager;
    const int                   _batchTimeout = 500;
    QTimer                      _batchTimer;
};

/// NOTE: ElevationProvider is not thread safe. All instances/calls to ElevationProvider must be on main thread.
class ElevationProvider : public QObject
{
    Q_OBJECT
public:
    ElevationProvider(QObject* parent = NULL);

     /// Async elevation query for a list of lon,lat coordinates. When the query is done, the terrainData() signal
     /// is emitted.
     ///    @param coordinates to query
    void queryTerrainData(const QList<QGeoCoordinate>& coordinates);

    // Internal method
    void _signalTerrainData(bool success, QList<float>& altitudes);

signals:
    void terrainData(bool success, QList<float> altitudes);
83
};