QGCGoogleEarthView.cc 4.82 KB
Newer Older
1 2
#include <QApplication>
#include <QDir>
3 4 5

#include <QDebug>
#include "UASManager.h"
6 7
#ifdef _MSC_VER
#include "ui_QGCGoogleEarthView.h"
8
#else
lm's avatar
lm committed
9 10 11
#include <QWebFrame>
#include <QWebPage>
#include "QGCWebPage.h"
pixhawk's avatar
pixhawk committed
12
#include "ui_QGCGoogleEarthView.h"
13
#endif
lm's avatar
lm committed
14
#include "QGCGoogleEarthView.h"
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
#if (defined Q_OS_MAC)
        webViewMac(new QWebView(this)),
#endif
27
#ifdef _MSC_VER
28
        webViewWin(new QGCWebAxWidget(this)),
29 30 31
#endif
#if (defined _MSC_VER)
		ui(new Ui::QGCGoogleEarthView)
32 33 34
#else
        ui(new Ui::QGCGoogleEarthView)
#endif
pixhawk's avatar
pixhawk committed
35
{
36
#ifdef _MSC_VER
37 38
    // Create layout and attach webViewWin
#else
39 40
#endif

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

46 47 48 49
#ifdef _MSC_VER
	ui->webViewLayout->addWidget(webViewWin);
#endif

50
#if ((defined Q_OS_MAC) | (defined _MSC_VER))
51 52
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
    connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateState()));
pixhawk's avatar
pixhawk committed
53
    updateTimer->start(refreshRateMs);
54 55
#endif

pixhawk's avatar
pixhawk committed
56 57 58 59 60 61 62 63
    // 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)));

64 65 66
    // Get list of available 3D models

    // Load HTML file
67 68 69 70
	#ifdef _MSC_VER
	webViewWin->dynamicCall("GoHome()");
	webViewWin->dynamicCall("Navigate(const QString&)", QApplication::applicationDirPath() + "/earth.html");
#endif
71 72 73 74

    // Parse for model links

    // Populate model list
pixhawk's avatar
pixhawk committed
75 76 77 78 79 80 81
}

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

82 83 84 85 86
void QGCGoogleEarthView::setActiveUAS(UASInterface* uas)
{
    mav = uas;
}

pixhawk's avatar
pixhawk committed
87
void QGCGoogleEarthView::showTrail(bool state)
88 89 90 91
{

}

pixhawk's avatar
pixhawk committed
92
void QGCGoogleEarthView::showWaypoints(bool state)
93 94 95 96 97 98
{

}

void QGCGoogleEarthView::follow(bool follow)
{
pixhawk's avatar
pixhawk committed
99
    followCamera = follow;
100 101
}

pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
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

118
#ifdef _MSC_VER
119 120
	webViewWin->dynamicCall("GoHome()");
	webViewWin->dynamicCall("Navigate(const QString&)", "http://pixhawk.ethz.ch");
pixhawk's avatar
pixhawk committed
121 122 123 124 125 126 127
#endif
    webViewInitialized = true;
    }
    updateTimer->start();
    QWidget::show();
}

128 129
void QGCGoogleEarthView::updateState()
{
lm's avatar
lm committed
130
#ifdef Q_OS_MAC
pixhawk's avatar
pixhawk committed
131 132
    if (isVisible())
    {
lm's avatar
lm committed
133
        if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
134
        {
lm's avatar
lm committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
            static bool initialized = false;
            if (!initialized)
            {
                webViewMac->page()->currentFrame()->evaluateJavaScript("setGCSHome(22.679833,8.549444, 470);");
                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();
                lat = mav->getLatitude();
                lon = mav->getLongitude();
                alt = mav->getAltitude();
                roll = mav->getRoll();
                pitch = mav->getPitch();
                yaw = mav->getYaw();
            }
            webViewMac->page()->currentFrame()->evaluateJavaScript(QString("setAircraftPositionAttitude(%1, %2, %3, %4, %6, %7, %8);")
                                                                   .arg(uasId)
                                                                   .arg(lat)
                                                                   .arg(lon)
                                                                   .arg(alt+500)
                                                                   .arg(roll)
                                                                   .arg(pitch)
                                                                   .arg(yaw));

            if (followCamera)
            {
                webViewMac->page()->currentFrame()->evaluateJavaScript(QString("updateFollowAircraft()"));
            }
173 174
        }
    }
lm's avatar
lm committed
175
#endif
176
}
lm's avatar
lm committed
177

178

pixhawk's avatar
pixhawk committed
179 180 181 182 183 184 185 186 187 188 189
void QGCGoogleEarthView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}