Commit f182d292 authored by lm's avatar lm

YEEHAY! First working version of Google Earth direct interface -...

YEEHAY! First working version of Google Earth direct interface - initialization is not perfect yet, current the user first has to ignore a number of script errors.
parent 1f2b0334
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html> <html>
<head> <head>
...@@ -56,7 +56,8 @@ function isInitialized() ...@@ -56,7 +56,8 @@ function isInitialized()
} }
function init() { function init()
{
google.earth.createInstance("map3d", initCallback, failureCallback); google.earth.createInstance("map3d", initCallback, failureCallback);
} }
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#include <QAxObject> #include <QAxObject>
#include <QUuid> #include <QUuid>
#include <mshtml.h>
#endif #endif
#include "QGC.h" #include "QGC.h"
...@@ -33,6 +34,7 @@ QGCGoogleEarthView::QGCGoogleEarthView(QWidget *parent) : ...@@ -33,6 +34,7 @@ QGCGoogleEarthView::QGCGoogleEarthView(QWidget *parent) :
followCamera(true), followCamera(true),
trailEnabled(true), trailEnabled(true),
webViewInitialized(false), webViewInitialized(false),
jScriptInitialized(false),
gEarthInitialized(false), gEarthInitialized(false),
#if (defined Q_OS_MAC) #if (defined Q_OS_MAC)
webViewMac(new QWebView(this)), webViewMac(new QWebView(this)),
...@@ -49,7 +51,7 @@ QGCGoogleEarthView::QGCGoogleEarthView(QWidget *parent) : ...@@ -49,7 +51,7 @@ QGCGoogleEarthView::QGCGoogleEarthView(QWidget *parent) :
#ifdef _MSC_VER #ifdef _MSC_VER
// Create layout and attach webViewWin // Create layout and attach webViewWin
/*
QFile file("doc.html"); QFile file("doc.html");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
qDebug() << __FILE__ << __LINE__ << "Could not open log file"; qDebug() << __FILE__ << __LINE__ << "Could not open log file";
...@@ -58,7 +60,7 @@ QGCGoogleEarthView::QGCGoogleEarthView(QWidget *parent) : ...@@ -58,7 +60,7 @@ QGCGoogleEarthView::QGCGoogleEarthView(QWidget *parent) :
out << webViewWin->generateDocumentation(); out << webViewWin->generateDocumentation();
out.flush(); out.flush();
file.flush(); file.flush();
file.close();*/ file.close();
#else #else
...@@ -105,14 +107,8 @@ QGCGoogleEarthView::~QGCGoogleEarthView() ...@@ -105,14 +107,8 @@ QGCGoogleEarthView::~QGCGoogleEarthView()
void QGCGoogleEarthView::addUAS(UASInterface* uas) void QGCGoogleEarthView::addUAS(UASInterface* uas)
{ {
#ifdef Q_OS_MAC
// uasid, type, color (in aarrbbgg format) // 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"))); javaScript(QString("createAircraft(%1, %2, %3);").arg(uas->getUASID()).arg(uas->getSystemType()).arg(uas->getColor().name().remove(0, 1).prepend("50")));
#endif
#ifdef _MSC_VER
//if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
#endif
// Automatically receive further position updates // Automatically receive further position updates
connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64))); connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64)));
} }
...@@ -122,30 +118,16 @@ void QGCGoogleEarthView::setActiveUAS(UASInterface* uas) ...@@ -122,30 +118,16 @@ void QGCGoogleEarthView::setActiveUAS(UASInterface* uas)
if (uas) if (uas)
{ {
mav = uas; mav = uas;
#ifdef Q_OS_MAC javaScript(QString("setCurrAircraft(%1);").arg(uas->getUASID()));
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) void QGCGoogleEarthView::updateGlobalPosition(UASInterface* uas, double lat, double lon, double alt, quint64 usec)
{ {
Q_UNUSED(usec); Q_UNUSED(usec);
#ifdef Q_OS_MAC javaScript(QString("addTrailPosition(%1, %2, %3, %4);").arg(uas->getUASID()).arg(lat, 0, 'f', 15).arg(lon, 0, 'f', 15).arg(alt, 0, 'f', 15));
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));
//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); //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);
#endif
#ifdef _MSC_VER
//if (webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
#endif
} }
void QGCGoogleEarthView::showTrail(bool state) void QGCGoogleEarthView::showTrail(bool state)
...@@ -170,22 +152,12 @@ void QGCGoogleEarthView::goHome() ...@@ -170,22 +152,12 @@ void QGCGoogleEarthView::goHome()
follow(false); follow(false);
updateState(); updateState();
// Go to home location // Go to home location
#ifdef Q_OS_MAC javaScript("goHome();");
webViewMac->page()->currentFrame()->evaluateJavaScript("goHome();");
#endif
#ifdef _MSC_VER
webViewWin->dynamicCall("InvokeScript(\"goHome\");");
#endif
} }
void QGCGoogleEarthView::setHome(double lat, double lon, double alt) void QGCGoogleEarthView::setHome(double lat, double lon, double alt)
{ {
#ifdef Q_OS_MAC javaScript(QString("setGCSHome(%1,%2,%3);").arg(lat, 0, 'f', 15).arg(lon, 0, 'f', 15).arg(alt, 0, 'f', 15));
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::hideEvent(QHideEvent* event) void QGCGoogleEarthView::hideEvent(QHideEvent* event)
...@@ -211,31 +183,6 @@ void QGCGoogleEarthView::showEvent(QShowEvent* event) ...@@ -211,31 +183,6 @@ void QGCGoogleEarthView::showEvent(QShowEvent* event)
#ifdef _MSC_VER #ifdef _MSC_VER
//webViewWin->dynamicCall("GoHome()"); //webViewWin->dynamicCall("GoHome()");
webViewWin->dynamicCall("Navigate(const QString&)", QApplication::applicationDirPath() + "/earth.html"); 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 #endif
webViewInitialized = true; webViewInitialized = true;
...@@ -248,6 +195,33 @@ void QGCGoogleEarthView::showEvent(QShowEvent* event) ...@@ -248,6 +195,33 @@ void QGCGoogleEarthView::showEvent(QShowEvent* event)
} }
} }
void QGCGoogleEarthView::printWinException(int no, QString str1, QString str2, QString str3)
{
qDebug() << no << str1 << str2 << str3;
}
QVariant QGCGoogleEarthView::javaScript(QString javaScript)
{
#ifdef Q_OS_MAC
return webViewMac->page()->currentFrame()->evaluateJavaScript(javaScript);
#endif
#ifdef _MSC_VER
if(!jScriptInitialized)
{
// Inititalize and drop call
initializeGoogleEarth();
return QVariant(false);
}
else
{
QVariantList params;
params.append(javaScript);
params.append("JScript");
return jScriptWin->dynamicCall("execScript(QString, QString)", params);
}
#endif
}
void QGCGoogleEarthView::initializeGoogleEarth() void QGCGoogleEarthView::initializeGoogleEarth()
{ {
if (!gEarthInitialized) if (!gEarthInitialized)
...@@ -256,13 +230,44 @@ void QGCGoogleEarthView::initializeGoogleEarth() ...@@ -256,13 +230,44 @@ void QGCGoogleEarthView::initializeGoogleEarth()
if (!webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool()) if (!webViewMac->page()->currentFrame()->evaluateJavaScript("isInitialized();").toBool())
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
if (!webViewWin->dynamicCall("InvokeScript(const QString&)", QString("isInitialized")).toBool()) static bool first = true;
if (first)
#endif #endif
{ {
QTimer::singleShot(200, this, SLOT(initializeGoogleEarth())); QTimer::singleShot(500, this, SLOT(initializeGoogleEarth()));
first = false;
}
else
{
#ifdef _MSC_VER
QAxObject* doc = webViewWin->querySubObject("Document()");
IDispatch* Disp;
IDispatch* winDoc = NULL;
//332C4425-26CB-11D0-B483-00C04FD90119 IHTMLDocument2
//25336920-03F9-11CF-8FD0-00AA00686F13 HTMLDocument
doc->queryInterface(QUuid("{332C4425-26CB-11D0-B483-00C04FD90119}"), (void**)(&winDoc));
if (winDoc)
{
// Security:
// CoInternetSetFeatureEnabled
// (FEATURE_LOCALMACHINE_LOCKDOWN, SET_FEATURE_ON_PROCESS, TRUE);
//
IHTMLDocument2* document = NULL;
winDoc->QueryInterface( IID_IHTMLDocument2, (void**)&document );
IHTMLWindow2 *window = NULL;
document->get_parentWindow( &window );
jScriptWin = new QAxObject(window, webViewWin);
connect(jScriptWin, SIGNAL(exception(int,QString,QString,QString)), this, SLOT(printWinException(int,QString,QString,QString)));
jScriptInitialized = true;
} }
else else
{ {
qDebug() << "COULD NOT GET DOCUMENT OBJECT! Aborting";
}
#endif
// Set home location // Set home location
setHome(47.3769, 8.549444, 500); setHome(47.3769, 8.549444, 500);
...@@ -315,8 +320,7 @@ void QGCGoogleEarthView::updateState() ...@@ -315,8 +320,7 @@ void QGCGoogleEarthView::updateState()
pitch = mav->getPitch(); pitch = mav->getPitch();
yaw = mav->getYaw(); yaw = mav->getYaw();
#ifdef Q_OS_MAC javaScript(QString("setAircraftPositionAttitude(%1, %2, %3, %4, %6, %7, %8);")
webViewMac->page()->currentFrame()->evaluateJavaScript(QString("setAircraftPositionAttitude(%1, %2, %3, %4, %6, %7, %8);")
.arg(uasId) .arg(uasId)
.arg(lat) .arg(lat)
.arg(lon) .arg(lon)
...@@ -324,27 +328,11 @@ void QGCGoogleEarthView::updateState() ...@@ -324,27 +328,11 @@ void QGCGoogleEarthView::updateState()
.arg(roll) .arg(roll)
.arg(pitch) .arg(pitch)
.arg(yaw)); .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) if (followCamera)
{ {
#ifdef Q_OS_MAC javaScript(QString("updateFollowAircraft()"));
webViewMac->page()->currentFrame()->evaluateJavaScript(QString("updateFollowAircraft()"));
#endif
#ifdef _MSC_VER
webViewWin->dynamicCall("InvokeScript(\"updateFollowAircraft\");");
#endif
} }
} }
} }
......
...@@ -19,28 +19,33 @@ class QGCWebAxWidget : public QAxWidget ...@@ -19,28 +19,33 @@ class QGCWebAxWidget : public QAxWidget
public: public:
//Q_OBJECT //Q_OBJECT
QGCWebAxWidget(QWidget* parent = 0, Qt::WindowFlags f = 0) QGCWebAxWidget(QWidget* parent = 0, Qt::WindowFlags f = 0)
: QAxWidget(parent, f), : QAxWidget(parent, f)/*,
_document(NULL) _document(NULL)*/
{ {
// Set web browser control // Set web browser control
setControl("{8856F961-340A-11D0-A96B-00C04FD705A2}"); setControl("{8856F961-340A-11D0-A96B-00C04FD705A2}");
// WARNING: Makes it impossible to actually debug javascript. But useful in production mode
setProperty("ScriptErrorsSuppressed", true);
// see: http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx?fid=285594&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=151#GoalScriptError
//this->dynamicCall("setProperty(const QString&,
//QObject::connect(this, SIGNAL(DocumentComplete(IDispatch*, QVariant&)), this, SLOT(setDocument(IDispatch*, QVariant&))); //QObject::connect(this, SIGNAL(DocumentComplete(IDispatch*, QVariant&)), this, SLOT(setDocument(IDispatch*, QVariant&)));
} }
/*
QAxObject* document() QAxObject* document()
{ {
return _document; return _document;
} }*/
protected: protected:
/*
void setDocument(IDispatch* dispatch, QVariant& variant) void setDocument(IDispatch* dispatch, QVariant& variant)
{ {
_document = this->querySubObject("Document()"); _document = this->querySubObject("Document()");
} }
protected: QAxObject* _document;*/
QAxObject* _document;
virtual bool translateKeyEvent(int message, int keycode) const virtual bool translateKeyEvent(int message, int keycode) const
{ {
if (message >= WM_KEYFIRST && message <= WM_KEYLAST) if (message >= WM_KEYFIRST && message <= WM_KEYLAST)
...@@ -89,6 +94,12 @@ public slots: ...@@ -89,6 +94,12 @@ public slots:
void setHome(double lat, double lon, double alt); void setHome(double lat, double lon, double alt);
/** @brief Initialize Google Earth */ /** @brief Initialize Google Earth */
void initializeGoogleEarth(); void initializeGoogleEarth();
/** @brief Print a Windows exception */
void printWinException(int no, QString str1, QString str2, QString str3);
public:
/** @brief Execute java script inside the Google Earth window */
QVariant javaScript(QString javascript);
protected: protected:
void changeEvent(QEvent *e); void changeEvent(QEvent *e);
...@@ -98,9 +109,11 @@ protected: ...@@ -98,9 +109,11 @@ protected:
bool followCamera; bool followCamera;
bool trailEnabled; bool trailEnabled;
bool webViewInitialized; bool webViewInitialized;
bool jScriptInitialized;
bool gEarthInitialized; bool gEarthInitialized;
#ifdef _MSC_VER #ifdef _MSC_VER
QGCWebAxWidget* webViewWin; QGCWebAxWidget* webViewWin;
QAxObject* jScriptWin;
#endif #endif
#if (defined Q_OS_MAC) #if (defined Q_OS_MAC)
QWebView* webViewMac; QWebView* webViewMac;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment