QGCGoogleEarthView.cc 9.08 KB
Newer Older
1 2
#include <QApplication>
#include <QDir>
3
#include <QShowEvent>
pixhawk's avatar
pixhawk committed
4
#include <QSettings>
5 6 7

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

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

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

pixhawk's avatar
pixhawk committed
19 20
#define QGCGOOGLEEARTHVIEWSETTINGS QString("GoogleEarthViewSettings_")

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

pixhawk's avatar
pixhawk committed
47 48 49 50 51
    // Load settings
    QSettings settings;
    followCamera = settings.value(QGCGOOGLEEARTHVIEWSETTINGS + "follow", followCamera).toBool();
    trailEnabled = settings.value(QGCGOOGLEEARTHVIEWSETTINGS + "trail", trailEnabled).toBool();

pixhawk's avatar
pixhawk committed
52
    ui->setupUi(this);
53 54
#if (defined Q_OS_MAC)
    ui->webViewLayout->addWidget(webViewMac);
pixhawk's avatar
pixhawk committed
55
    connect(webViewMac, SIGNAL(loadFinished(bool)), this, SLOT(initializeGoogleEarth(bool)));
56 57
#endif

58
#ifdef _MSC_VER
59
    ui->webViewLayout->addWidget(webViewWin);
60 61
#endif

62 63 64
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
    connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateState()));

pixhawk's avatar
pixhawk committed
65 66 67 68 69 70 71 72
    // 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)));

pixhawk's avatar
pixhawk committed
73 74 75 76 77 78 79 80 81 82 83 84
    // Go home
    connect(ui->goHomeButton, SIGNAL(clicked()), this, SLOT(goHome()));
}

QGCGoogleEarthView::~QGCGoogleEarthView()
{
    QSettings settings;
    settings.setValue(QGCGOOGLEEARTHVIEWSETTINGS + "follow", followCamera);
    settings.setValue(QGCGOOGLEEARTHVIEWSETTINGS + "trail", trailEnabled);
    settings.sync();
    delete ui;
}
85

pixhawk's avatar
pixhawk committed
86 87 88
void QGCGoogleEarthView::addUAS(UASInterface* uas)
{
#ifdef Q_OS_MAC
89 90
    // uasid, type, color (in aarrbbgg format)
    webViewMac->page()->currentFrame()->evaluateJavaScript(QString("createAircraft(%1, %2, %3);").arg(uas->getUASID()).arg(uas->getSystemType()).arg(uas->getColor().name().remove(0, 1).prepend("50")));
pixhawk's avatar
pixhawk committed
91
#endif
92
#ifdef _MSC_VER
pixhawk's avatar
pixhawk committed
93
        //if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
94
#endif
95

pixhawk's avatar
pixhawk committed
96 97
        // Automatically receive further position updates
        connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64)));
pixhawk's avatar
pixhawk committed
98 99
}

pixhawk's avatar
pixhawk committed
100
void QGCGoogleEarthView::setActiveUAS(UASInterface* uas)
pixhawk's avatar
pixhawk committed
101
{
pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108 109 110 111 112 113 114
    if (uas)
    {
        mav = uas;
#ifdef Q_OS_MAC
        if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
        {
            webViewMac->page()->currentFrame()->evaluateJavaScript(QString("setCurrAircraft(%1);").arg(uas->getUASID()));
        }
#endif
#ifdef _MSC_VER
        //if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
#endif
    }
pixhawk's avatar
pixhawk committed
115 116
}

pixhawk's avatar
pixhawk committed
117
void QGCGoogleEarthView::updateGlobalPosition(UASInterface* uas, double lat, double lon, double alt, quint64 usec)
118
{
pixhawk's avatar
pixhawk committed
119 120 121
    Q_UNUSED(usec);
#ifdef Q_OS_MAC
        webViewMac->page()->currentFrame()->evaluateJavaScript(QString("addTrailPosition(%1, %2, %3, %4);").arg(uas->getUASID()).arg(lat, 0, 'f', 15).arg(lon, 0, 'f', 15).arg(alt, 0, 'f', 15));
122 123 124

        //qDebug() << QString("addTrailPosition(%1, %2, %3, %4);").arg(uas->getUASID()).arg(lat, 0, 'f', 15).arg(lon, 0, 'f', 15).arg(alt, 0, 'f', 15);

pixhawk's avatar
pixhawk committed
125 126 127 128
#endif
#ifdef _MSC_VER
        //if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
#endif
129 130
}

pixhawk's avatar
pixhawk committed
131
void QGCGoogleEarthView::showTrail(bool state)
132
{
pixhawk's avatar
pixhawk committed
133
    ui->trailCheckbox->setChecked(state);
134 135
}

pixhawk's avatar
pixhawk committed
136
void QGCGoogleEarthView::showWaypoints(bool state)
137 138 139 140 141 142
{

}

void QGCGoogleEarthView::follow(bool follow)
{
pixhawk's avatar
pixhawk committed
143
    ui->followAirplaneCheckbox->setChecked(follow);
pixhawk's avatar
pixhawk committed
144
    followCamera = follow;
145 146
}

pixhawk's avatar
pixhawk committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
void QGCGoogleEarthView::goHome()
{
    // Disable follow and update
    follow(false);
    updateState();
    // Go to home location
#ifdef Q_OS_MAC
    webViewMac->page()->currentFrame()->evaluateJavaScript("goHome();");
#endif
#ifdef _MSC_VER
    webViewWin.dynamicCall("InvokeScript(\"goHome\");");
#endif
}

void QGCGoogleEarthView::setHome(double lat, double lon, double alt)
{
#ifdef Q_OS_MAC
    webViewMac->page()->currentFrame()->evaluateJavaScript(QString("setGCSHome(%1,%2,%3);").arg(lat, 0, 'f', 15).arg(lon, 0, 'f', 15).arg(alt, 0, 'f', 15));
#endif
#ifdef _MSC_VER
    webViewWin.dynamicCall(QString("InvokeScript(\"setGCSHome\", %1, %2, %3);").arg(lat, 0, 'f', 15).arg(lon, 0, 'f', 15).arg(alt, 0, 'f', 15));
#endif
}

171 172 173 174 175
void QGCGoogleEarthView::hideEvent(QHideEvent* event)
{
    Q_UNUSED(event) updateTimer->stop();
}

176
void QGCGoogleEarthView::showEvent(QShowEvent* event)
pixhawk's avatar
pixhawk committed
177
{
178 179
    // React only to internal (pre-display)
    // events
180
    Q_UNUSED(event)
pixhawk's avatar
pixhawk committed
181
    {
182 183 184
            // Enable widget, initialize on first run
            if (!webViewInitialized)
            {
pixhawk's avatar
pixhawk committed
185
#if (defined Q_OS_MAC)
186 187 188
                webViewMac->setPage(new QGCWebPage(webViewMac));
                webViewMac->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
                webViewMac->load(QUrl("earth.html"));
pixhawk's avatar
pixhawk committed
189 190
#endif

191
#ifdef _MSC_VER
pixhawk's avatar
pixhawk committed
192 193
                //webViewWin->dynamicCall("GoHome()");
                webViewWin->dynamicCall("Navigate(const QString&)", QApplication::applicationDirPath() + "/earth.html");
pixhawk's avatar
pixhawk committed
194
#endif
pixhawk's avatar
pixhawk committed
195

196
                webViewInitialized = true;
pixhawk's avatar
pixhawk committed
197 198 199 200 201
                // Reloading the webpage, this resets Google Earth
                gEarthInitialized = false;

                QTimer::singleShot(1000, this, SLOT(initializeGoogleEarth()));
                updateTimer->start(refreshRateMs);
202
            }
pixhawk's avatar
pixhawk committed
203 204 205
    }
}

pixhawk's avatar
pixhawk committed
206
void QGCGoogleEarthView::initializeGoogleEarth()
207
{
pixhawk's avatar
pixhawk committed
208
    if (!gEarthInitialized)
pixhawk's avatar
pixhawk committed
209
    {
210
#ifdef Q_OS_MAC
pixhawk's avatar
pixhawk committed
211
        if (!webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
212 213
#endif
#ifdef _MSC_VER
pixhawk's avatar
pixhawk committed
214
            //if (!webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
215
#endif
pixhawk's avatar
pixhawk committed
216 217 218 219 220 221 222 223 224 225
        {
            QTimer::singleShot(200, this, SLOT(initializeGoogleEarth()));
        }
        else
        {
            // Set home location
            setHome(47.3769, 8.549444, 500);

            // Move to home location
            goHome();
lm's avatar
lm committed
226

pixhawk's avatar
pixhawk committed
227 228
            // Set current UAS
            setActiveUAS(mav);
lm's avatar
lm committed
229

pixhawk's avatar
pixhawk committed
230 231 232
            // Add all MAVs
            QList<UASInterface*> mavs = UASManager::instance()->getUASList();
            foreach (UASInterface* mav, mavs)
lm's avatar
lm committed
233
            {
pixhawk's avatar
pixhawk committed
234
                addUAS(mav);
lm's avatar
lm committed
235
            }
pixhawk's avatar
pixhawk committed
236 237 238 239 240 241 242 243 244 245 246

            // Add any further MAV automatically
            connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)), this, SLOT(addUAS(UASInterface*)));

            gEarthInitialized = true;
        }
    }
}

void QGCGoogleEarthView::updateState()
{
247 248 249
#if (QGC_EVENTLOOP_DEBUG)
    qDebug() << "EVENTLOOP:" << __FILE__ << __LINE__;
#endif
pixhawk's avatar
pixhawk committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    if (gEarthInitialized)
    {
        int uasId = 0;
        double lat = 47.3769;
        double lon = 8.549444;
        double alt = 470.0;

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

        // Update all MAVs
        QList<UASInterface*> mavs = UASManager::instance()->getUASList();
        foreach (UASInterface* mav, mavs)
        {
            uasId = mav->getUASID();
            lat = mav->getLatitude();
            lon = mav->getLongitude();
            alt = mav->getAltitude();
            roll = mav->getRoll();
            pitch = mav->getPitch();
            yaw = mav->getYaw();

#ifdef Q_OS_MAC
lm's avatar
lm committed
274 275 276 277 278 279 280 281
            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));
282 283 284 285
#endif
#ifdef _MSC_VER

#endif
pixhawk's avatar
pixhawk committed
286
        }
lm's avatar
lm committed
287

pixhawk's avatar
pixhawk committed
288 289
        if (followCamera)
        {
290
#ifdef Q_OS_MAC
pixhawk's avatar
pixhawk committed
291
            webViewMac->page()->currentFrame()->evaluateJavaScript(QString("updateFollowAircraft()"));
292 293 294
#endif
#ifdef _MSC_VER
#endif
295
        }
296
    }
297
}
lm's avatar
lm committed
298

299

pixhawk's avatar
pixhawk committed
300 301 302 303 304 305 306 307 308 309 310
void QGCGoogleEarthView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}