QGCGoogleEarthView.cc 8.91 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 "ui_QGCGoogleEarthView.h"
lm's avatar
lm committed
16
#include "QGCGoogleEarthView.h"
pixhawk's avatar
pixhawk committed
17

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

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

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

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

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

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

pixhawk's avatar
pixhawk committed
64 65 66 67 68 69 70 71
    // 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
72 73 74 75 76 77 78 79 80 81 82 83
    // 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;
}
84

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

pixhawk's avatar
pixhawk committed
94 95
        // 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
96 97
}

pixhawk's avatar
pixhawk committed
98
void QGCGoogleEarthView::setActiveUAS(UASInterface* uas)
pixhawk's avatar
pixhawk committed
99
{
pixhawk's avatar
pixhawk committed
100 101 102 103 104 105 106 107 108 109 110 111 112
    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
113 114
}

pixhawk's avatar
pixhawk committed
115
void QGCGoogleEarthView::updateGlobalPosition(UASInterface* uas, double lat, double lon, double alt, quint64 usec)
116
{
pixhawk's avatar
pixhawk committed
117 118 119 120 121 122 123
    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));
#endif
#ifdef _MSC_VER
        //if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
#endif
124 125
}

pixhawk's avatar
pixhawk committed
126
void QGCGoogleEarthView::showTrail(bool state)
127
{
pixhawk's avatar
pixhawk committed
128
    ui->trailCheckbox->setChecked(state);
129 130
}

pixhawk's avatar
pixhawk committed
131
void QGCGoogleEarthView::showWaypoints(bool state)
132 133 134 135 136 137
{

}

void QGCGoogleEarthView::follow(bool follow)
{
pixhawk's avatar
pixhawk committed
138
    ui->followAirplaneCheckbox->setChecked(follow);
pixhawk's avatar
pixhawk committed
139
    followCamera = follow;
140 141
}

pixhawk's avatar
pixhawk committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
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
}

166
void QGCGoogleEarthView::showEvent(QShowEvent* event)
pixhawk's avatar
pixhawk committed
167
{
168 169 170
    // React only to internal (pre-display)
    // events
    if (!event->spontaneous())
pixhawk's avatar
pixhawk committed
171
    {
172 173 174 175
        if (event->type() == QEvent::Hide)
        {
            // Disable widget
            updateTimer->stop();
pixhawk's avatar
pixhawk committed
176
            qDebug() << "STOPPED GOOGLE EARTH UPDATES";
177 178 179 180 181 182
        }
        else if (event->type() == QEvent::Show)
        {
            // Enable widget, initialize on first run
            if (!webViewInitialized)
            {
pixhawk's avatar
pixhawk committed
183
#if (defined Q_OS_MAC)
184 185 186
                webViewMac->setPage(new QGCWebPage(webViewMac));
                webViewMac->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
                webViewMac->load(QUrl("earth.html"));
pixhawk's avatar
pixhawk committed
187 188
#endif

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

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

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

pixhawk's avatar
pixhawk committed
205
void QGCGoogleEarthView::initializeGoogleEarth()
206
{
pixhawk's avatar
pixhawk committed
207
    if (!gEarthInitialized)
pixhawk's avatar
pixhawk committed
208
    {
209
#ifdef Q_OS_MAC
pixhawk's avatar
pixhawk committed
210
        if (!webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
211 212
#endif
#ifdef _MSC_VER
pixhawk's avatar
pixhawk committed
213
            //if (!webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
214
#endif
pixhawk's avatar
pixhawk committed
215 216 217 218 219 220 221 222 223 224
        {
            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
225

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

pixhawk's avatar
pixhawk committed
229 230 231
            // Add all MAVs
            QList<UASInterface*> mavs = UASManager::instance()->getUASList();
            foreach (UASInterface* mav, mavs)
lm's avatar
lm committed
232
            {
pixhawk's avatar
pixhawk committed
233
                addUAS(mav);
lm's avatar
lm committed
234
            }
pixhawk's avatar
pixhawk committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

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

            gEarthInitialized = true;
        }
    }
}

void QGCGoogleEarthView::updateState()
{
    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
270 271 272 273 274 275 276 277
            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));
278 279 280 281
#endif
#ifdef _MSC_VER

#endif
pixhawk's avatar
pixhawk committed
282
        }
lm's avatar
lm committed
283

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

295

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