QGCGoogleEarthView.cc 4.47 KB
Newer Older
1 2 3 4 5
#include <QWebFrame>
#include <QWebPage>

#include <QDebug>

pixhawk's avatar
pixhawk committed
6
#include "QGCGoogleEarthView.h"
7 8
#include "QGCWebPage.h"
#include "UASManager.h"
9
#include "ui_QGCGoogleEarthControls.h"
10
#if (defined Q_OS_WIN) && !(defined __MINGW32__)
11
#include "ui_QGCGoogleEarthViewWin.h"
12
#else
pixhawk's avatar
pixhawk committed
13
#include "ui_QGCGoogleEarthView.h"
14
#endif
pixhawk's avatar
pixhawk committed
15 16

QGCGoogleEarthView::QGCGoogleEarthView(QWidget *parent) :
17 18
        QWidget(parent),
        updateTimer(new QTimer(this)),
pixhawk's avatar
pixhawk committed
19
        refreshRateMs(200),
20 21
        mav(NULL),
        followCamera(true),
pixhawk's avatar
pixhawk committed
22
        trailEnabled(true),
pixhawk's avatar
pixhawk committed
23
        webViewInitialized(false),
24 25 26 27
#if (defined Q_OS_MAC)
        webViewMac(new QWebView(this)),
#endif
#if (defined Q_OS_WIN) & !(defined __MINGW32__)
28 29 30 31
        webViewWin(new QGCWebAxWidget(this)),
#else
        ui(new Ui::QGCGoogleEarthView)
#endif
pixhawk's avatar
pixhawk committed
32
{
33
#if (defined Q_OS_WIN) & !(defined __MINGW32__)
34 35
    // Create layout and attach webViewWin
#else
36 37
#endif

pixhawk's avatar
pixhawk committed
38
    ui->setupUi(this);
39 40 41 42 43
#if (defined Q_OS_MAC)
    ui->webViewLayout->addWidget(webViewMac);
#endif

#if ((defined Q_OS_MAC) | ((defined Q_OS_WIN) & !(defined __MINGW32__)))
44 45
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
    connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateState()));
pixhawk's avatar
pixhawk committed
46
    updateTimer->start(refreshRateMs);
47 48
#endif

pixhawk's avatar
pixhawk committed
49 50 51 52 53 54 55 56
    // Follow checkbox
    ui->followAirplaneCheckbox->setChecked(followCamera);
    connect(ui->followAirplaneCheckbox, SIGNAL(toggled(bool)), this, SLOT(follow(bool)));

    // Trail checkbox
    ui->trailCheckbox->setChecked(trailEnabled);
    connect(ui->trailCheckbox, SIGNAL(toggled(bool)), this, SLOT(showTrail(bool)));

57 58 59 60 61 62 63
    // Get list of available 3D models

    // Load HTML file

    // Parse for model links

    // Populate model list
pixhawk's avatar
pixhawk committed
64 65 66 67 68 69 70
}

QGCGoogleEarthView::~QGCGoogleEarthView()
{
    delete ui;
}

71 72 73 74 75
void QGCGoogleEarthView::setActiveUAS(UASInterface* uas)
{
    mav = uas;
}

pixhawk's avatar
pixhawk committed
76
void QGCGoogleEarthView::showTrail(bool state)
77 78 79 80
{

}

pixhawk's avatar
pixhawk committed
81
void QGCGoogleEarthView::showWaypoints(bool state)
82 83 84 85 86 87
{

}

void QGCGoogleEarthView::follow(bool follow)
{
pixhawk's avatar
pixhawk committed
88
    followCamera = follow;
89 90
}

pixhawk's avatar
pixhawk committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
void QGCGoogleEarthView::hide()
{
    updateTimer->stop();
    QWidget::hide();
}

void QGCGoogleEarthView::show()
{
    if (!webViewInitialized)
    {
#if (defined Q_OS_MAC)
    webViewMac->setPage(new QGCWebPage(webViewMac));
    webViewMac->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
    webViewMac->load(QUrl("earth.html"));
#endif

#if (defined Q_OS_WIN) & !(defined __MINGW32__)
    webViewWin->load(QUrl("earth.html"));
#endif
    webViewInitialized = true;
    }
    updateTimer->start();
    QWidget::show();
}

116 117
void QGCGoogleEarthView::updateState()
{
lm's avatar
lm committed
118
#ifdef Q_OS_MAC
pixhawk's avatar
pixhawk committed
119 120
    if (isVisible())
    {
121
    if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
122 123 124 125
    {
        static bool initialized = false;
        if (!initialized)
        {
126
            webViewMac->page()->currentFrame()->evaluateJavaScript("setGCSHome(22.679833,8.549444, 470);");
127 128 129 130 131 132 133 134 135 136 137 138 139 140
            initialized = true;
        }
        int uasId = 0;
        double lat = 22.679833;
        double lon = 8.549444;
        double alt = 470.0;

        float roll = 0.0f;
        float pitch = 0.0f;
        float yaw = 0.0f;

        if (mav)
        {
            uasId = mav->getUASID();
pixhawk's avatar
pixhawk committed
141 142 143
            lat = mav->getLatitude();
            lon = mav->getLongitude();
            alt = mav->getAltitude();
144 145 146 147
            roll = mav->getRoll();
            pitch = mav->getPitch();
            yaw = mav->getYaw();
        }
148
        webViewMac->page()->currentFrame()->evaluateJavaScript(QString("setAircraftPositionAttitude(%1, %2, %3, %4, %6, %7, %8);")
pixhawk's avatar
pixhawk committed
149 150 151 152 153 154 155
                                                                .arg(uasId)
                                                                .arg(lat)
                                                                .arg(lon)
                                                                .arg(alt+500)
                                                                .arg(roll)
                                                                .arg(pitch)
                                                                .arg(yaw));
156 157 158

        if (followCamera)
        {
159
             webViewMac->page()->currentFrame()->evaluateJavaScript(QString("updateFollowAircraft()"));
160 161
        }
    }
lm's avatar
lm committed
162
#endif
163
}
pixhawk's avatar
pixhawk committed
164
}
165

pixhawk's avatar
pixhawk committed
166 167 168 169 170 171 172 173 174 175 176
void QGCGoogleEarthView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}