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

#include <QDebug>
#include "UASManager.h"
7 8

#ifdef Q_OS_MAC
lm's avatar
lm committed
9 10 11
#include <QWebFrame>
#include <QWebPage>
#include "QGCWebPage.h"
12
#endif
13 14

#include "ui_QGCGoogleEarthView.h"
lm's avatar
lm committed
15
#include "QGCGoogleEarthView.h"
pixhawk's avatar
pixhawk committed
16 17

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

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

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

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

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

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

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

    // Parse for model links

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

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

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

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

}

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

}

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

103
void QGCGoogleEarthView::showEvent(QShowEvent* event)
pixhawk's avatar
pixhawk committed
104
{
105 106 107
    // React only to internal (pre-display)
    // events
    if (!event->spontaneous())
pixhawk's avatar
pixhawk committed
108
    {
109 110 111 112 113 114 115 116 117 118
        if (event->type() == QEvent::Hide)
        {
            // Disable widget
            updateTimer->stop();
        }
        else if (event->type() == QEvent::Show)
        {
            // Enable widget, initialize on first run
            if (!webViewInitialized)
            {
pixhawk's avatar
pixhawk committed
119
#if (defined Q_OS_MAC)
120 121 122
                webViewMac->setPage(new QGCWebPage(webViewMac));
                webViewMac->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
                webViewMac->load(QUrl("earth.html"));
pixhawk's avatar
pixhawk committed
123 124
#endif

125
#ifdef _MSC_VER
126 127
                webViewWin->dynamicCall("GoHome()");
                webViewWin->dynamicCall("Navigate(const QString&)", "http://pixhawk.ethz.ch");
pixhawk's avatar
pixhawk committed
128
#endif
129 130 131 132
                webViewInitialized = true;
            }
        }
        updateTimer->start();
pixhawk's avatar
pixhawk committed
133 134 135
    }
}

136 137
void QGCGoogleEarthView::updateState()
{
pixhawk's avatar
pixhawk committed
138 139
    if (isVisible())
    {
140
#ifdef Q_OS_MAC
lm's avatar
lm committed
141
        if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
142
        {
143 144 145
#endif
#ifdef _MSC_VER
            //        if (webViewMacWin->dynamicCall("Navigate(const QString&)","isInitialized();").toBool())
pixhawk's avatar
-  
pixhawk committed
146
            {
147
#endif
lm's avatar
lm committed
148 149 150
            static bool initialized = false;
            if (!initialized)
            {
151
#ifdef Q_OS_MAC
lm's avatar
lm committed
152
                webViewMac->page()->currentFrame()->evaluateJavaScript("setGCSHome(22.679833,8.549444, 470);");
153 154 155 156
#endif
#ifdef _MSC_VER
                //webViewMac->page()->currentFrame()->evaluateJavaScript("setGCSHome(22.679833,8.549444, 470);");
#endif
lm's avatar
lm committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
                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();
            }
178
            #ifdef Q_OS_MAC
lm's avatar
lm committed
179 180 181 182 183 184 185 186
            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));
187 188 189 190
#endif
#ifdef _MSC_VER

#endif
lm's avatar
lm committed
191 192 193

            if (followCamera)
            {
194
#ifdef Q_OS_MAC
lm's avatar
lm committed
195
                webViewMac->page()->currentFrame()->evaluateJavaScript(QString("updateFollowAircraft()"));
196 197 198
#endif
#ifdef _MSC_VER
#endif
lm's avatar
lm committed
199
            }
200
#if (defined Q_OS_MAC) || (defined _MSC_VER)
201
        }
lm's avatar
lm committed
202
#endif
203
    }
204
}
lm's avatar
lm committed
205

206

pixhawk's avatar
pixhawk committed
207 208 209 210 211 212 213 214 215 216 217
void QGCGoogleEarthView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}