QGCGoogleEarthView.cc 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
#include <QApplication>
#include <QDir>
#include <QShowEvent>
#include <QSettings>
#include <QAxObject>
#include <QUuid>

#include <QDebug>
#include <QFile>
#include <QTextStream>
#include "UASManager.h"

#ifdef Q_OS_MAC
#include <QWebFrame>
#include <QWebPage>
#include "QGCWebPage.h"
#endif

#include "ui_QGCGoogleEarthView.h"
#include "QGCGoogleEarthView.h"

#define QGCGOOGLEEARTHVIEWSETTINGS QString("GoogleEarthViewSettings_")

QGCGoogleEarthView::QGCGoogleEarthView(QWidget *parent) :
        QWidget(parent),
        updateTimer(new QTimer(this)),
        refreshRateMs(80),
        mav(NULL),
        followCamera(true),
        trailEnabled(true),
        webViewInitialized(false),
        gEarthInitialized(false),
#if (defined Q_OS_MAC)
        webViewMac(new QWebView(this)),
#endif
#ifdef _MSC_VER
        webViewWin(new QGCWebAxWidget(this)),
#endif
#if (defined _MSC_VER)
        ui(new Ui::QGCGoogleEarthView)
#else
        ui(new Ui::QGCGoogleEarthView)
#endif
{
#ifdef _MSC_VER
    // Create layout and attach webViewWin

	/*
	QFile file("doc.html");
     if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
		 qDebug() << __FILE__ << __LINE__ << "Could not open log file";

     QTextStream out(&file);
     out << webViewWin->generateDocumentation();
	 out.flush();
	 file.flush();
	 file.close();*/


#else
#endif

    // Load settings
    QSettings settings;
    followCamera = settings.value(QGCGOOGLEEARTHVIEWSETTINGS + "follow", followCamera).toBool();
    trailEnabled = settings.value(QGCGOOGLEEARTHVIEWSETTINGS + "trail", trailEnabled).toBool();

    ui->setupUi(this);
#if (defined Q_OS_MAC)
    ui->webViewLayout->addWidget(webViewMac);
    connect(webViewMac, SIGNAL(loadFinished(bool)), this, SLOT(initializeGoogleEarth(bool)));
#endif

#ifdef _MSC_VER
    ui->webViewLayout->addWidget(webViewWin);
#endif

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

    // 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)));

    // 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;
}

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
#ifdef _MSC_VER
        //if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
#endif

        // Automatically receive further position updates
        connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64)));
}

void QGCGoogleEarthView::setActiveUAS(UASInterface* uas)
{
    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
    }
}

void QGCGoogleEarthView::updateGlobalPosition(UASInterface* uas, double lat, double lon, double alt, quint64 usec)
{
    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
}

void QGCGoogleEarthView::showTrail(bool state)
{
    ui->trailCheckbox->setChecked(state);
}

void QGCGoogleEarthView::showWaypoints(bool state)
{

}

void QGCGoogleEarthView::follow(bool follow)
{
    ui->followAirplaneCheckbox->setChecked(follow);
    followCamera = follow;
}

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)).toStdString().c_str());
#endif
}

void QGCGoogleEarthView::showEvent(QShowEvent* event)
{
    // React only to internal (pre-display)
    // events
    if (!event->spontaneous())
    {
        if (event->type() == QEvent::Hide)
        {
            // Disable widget
            updateTimer->stop();
            qDebug() << "STOPPED GOOGLE EARTH UPDATES";
        }
        else if (event->type() == QEvent::Show)
        {
            // Enable widget, initialize on first run
            if (!webViewInitialized)
            {
#if (defined Q_OS_MAC)
                webViewMac->setPage(new QGCWebPage(webViewMac));
                webViewMac->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
                webViewMac->load(QUrl("earth.html"));
#endif

#ifdef _MSC_VER
                //webViewWin->dynamicCall("GoHome()");
                webViewWin->dynamicCall("Navigate(const QString&)", QApplication::applicationDirPath() + "/earth.html");
				/*
				
				Sleep(4000);
					
				
				QAxObject* doc = webViewWin->querySubObject("Document()");
				IUnknown* winDoc = NULL;
				doc->queryInterface(QUuid("{25336920-03F9-11CF-8FD0-00AA00686F13}"), (void**)(&winDoc));
				//if (winDoc)
				{
					doc = new QAxObject(winDoc, webViewWin);
				}

		QFile file("ie-doc.html");
     if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
		 qDebug() << __FILE__ << __LINE__ << "Could not open log file";

     QTextStream out(&file);
     out << doc->generateDocumentation();
	 out.flush();
	 file.flush();
	 file.close();
	 while(1);
	 */

#endif

                webViewInitialized = true;
                // Reloading the webpage, this resets Google Earth
                gEarthInitialized = false;

                QTimer::singleShot(1000, this, SLOT(initializeGoogleEarth()));
                updateTimer->start(refreshRateMs);
            }
        }
    }
}

void QGCGoogleEarthView::initializeGoogleEarth()
{
    if (!gEarthInitialized)
    {
#ifdef Q_OS_MAC
        if (!webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
#endif
#ifdef _MSC_VER
		if (!webViewWin->dynamicCall("InvokeScript(const QString&)", QString("isInitialized")).toBool())
#endif
        {
            QTimer::singleShot(200, this, SLOT(initializeGoogleEarth()));
        }
        else
        {
            // Set home location
            setHome(47.3769, 8.549444, 500);

            // Move to home location
            goHome();

            // Set current UAS
            setActiveUAS(mav);

            // Add all MAVs
            QList<UASInterface*> mavs = UASManager::instance()->getUASList();
            foreach (UASInterface* mav, mavs)
            {
                addUAS(mav);
            }

            // 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
            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));
#endif
#ifdef _MSC_VER
webViewWin->dynamicCall((QString("InvokeScript(\"setAircraftPositionAttitude\", %1, %2, %3, %4, %6, %7, %8);")
                                                                   .arg(uasId)
                                                                   .arg(lat)
                                                                   .arg(lon)
                                                                   .arg(alt+500)
                                                                   .arg(roll)
                                                                   .arg(pitch)
																   .arg(yaw)).toStdString().c_str());
#endif
        }

        if (followCamera)
        {
#ifdef Q_OS_MAC
            webViewMac->page()->currentFrame()->evaluateJavaScript(QString("updateFollowAircraft()"));
#endif
#ifdef _MSC_VER
			webViewWin->dynamicCall("InvokeScript(\"updateFollowAircraft\");");
#endif
        }
    }
}


void QGCGoogleEarthView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}