From 3e109ee770c19fa4151f55aa6093adcd804360b0 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Thu, 6 Nov 2014 14:57:04 -0800 Subject: [PATCH] Add WebWindow widget for scripts --- interface/src/Application.cpp | 3 ++ interface/src/scripting/WebWindow.cpp | 68 +++++++++++++++++++++++++++ interface/src/scripting/WebWindow.h | 53 +++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 interface/src/scripting/WebWindow.cpp create mode 100644 interface/src/scripting/WebWindow.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index b773231b0f..98be839e25 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -91,6 +91,7 @@ #include "scripting/MenuScriptingInterface.h" #include "scripting/SettingsScriptingInterface.h" #include "scripting/WindowScriptingInterface.h" +#include "scripting/WebWindow.h" #include "ui/DataWebDialog.h" #include "ui/InfoView.h" @@ -3871,6 +3872,8 @@ void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scri // register `location` on the global object. scriptEngine->registerGetterSetter("location", LocationScriptingInterface::locationGetter, LocationScriptingInterface::locationSetter); + + scriptEngine->registerFunction("WebWindow", WebWindow::constructor, 1); scriptEngine->registerGlobalObject("Menu", MenuScriptingInterface::getInstance()); scriptEngine->registerGlobalObject("Settings", SettingsScriptingInterface::getInstance()); diff --git a/interface/src/scripting/WebWindow.cpp b/interface/src/scripting/WebWindow.cpp new file mode 100644 index 0000000000..dfd3104f4b --- /dev/null +++ b/interface/src/scripting/WebWindow.cpp @@ -0,0 +1,68 @@ +// +// WebWindow.cpp +// interface/src/scripting +// +// Created by Ryan Huffman on 11/06/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include +#include +#include + +#include "WindowScriptingInterface.h" +#include "WebWindow.h" + +ScriptEventBridge::ScriptEventBridge(QObject* parent) : QObject(parent) { +} + +void ScriptEventBridge::emitWebEvent(const QString& data) { + emit webEventReceived(data); +} + +void ScriptEventBridge::emitScriptEvent(const QString& data) { + emit scriptEventReceived(data); +} + +WebWindow::WebWindow(const QString& url, int width, int height) + : QObject(NULL), + _window(new QWidget(NULL, Qt::Tool)), + _eventBridge(new ScriptEventBridge(this)) { + + QWebView* webView = new QWebView(_window); + webView->page()->mainFrame()->addToJavaScriptWindowObject("EventBridge", _eventBridge); + webView->setUrl(url); + QVBoxLayout* layout = new QVBoxLayout(_window); + _window->setLayout(layout); + layout->addWidget(webView); + layout->setSpacing(0); + layout->setContentsMargins(0, 0, 0, 0); + _window->setGeometry(0, 0, width, height); + + connect(this, &WebWindow::destroyed, _window, &QWidget::deleteLater); +} + +WebWindow::~WebWindow() { +} + +void WebWindow::setVisible(bool visible) { + QMetaObject::invokeMethod(_window, "setVisible", Qt::BlockingQueuedConnection, Q_ARG(bool, visible)); +} + +QScriptValue WebWindow::constructor(QScriptContext* context, QScriptEngine* engine) { + WebWindow* retVal; + QString file = context->argument(0).toString(); + QMetaObject::invokeMethod(WindowScriptingInterface::getInstance(), "doCreateWebWindow", Qt::BlockingQueuedConnection, + Q_RETURN_ARG(WebWindow*, retVal), + Q_ARG(const QString&, file), + Q_ARG(int, context->argument(1).toInteger()), + Q_ARG(int, context->argument(2).toInteger())); + + connect(engine, &QScriptEngine::destroyed, retVal, &WebWindow::deleteLater); + + return engine->newQObject(retVal);//, QScriptEngine::ScriptOwnership); +} diff --git a/interface/src/scripting/WebWindow.h b/interface/src/scripting/WebWindow.h new file mode 100644 index 0000000000..698e43fd6c --- /dev/null +++ b/interface/src/scripting/WebWindow.h @@ -0,0 +1,53 @@ +// +// WebWindow.h +// interface/src/scripting +// +// Created by Ryan Huffman on 11/06/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_WebWindow_h +#define hifi_WebWindow_h + +#include +#include + +// #include "ScriptWebView.h" + +class ScriptEventBridge : public QObject { + Q_OBJECT +public: + ScriptEventBridge(QObject* parent = NULL); + +public slots: + void emitWebEvent(const QString& data); + void emitScriptEvent(const QString& data); + +signals: + void webEventReceived(const QString& data); + void scriptEventReceived(const QString& data); + +}; + +class WebWindow : public QObject { + Q_OBJECT + Q_PROPERTY(QObject* eventBridge READ getEventBridge) +public: + WebWindow(const QString& url, int width, int height); + ~WebWindow(); + + static QScriptValue constructor(QScriptContext* context, QScriptEngine* engine); + +public slots: + void setVisible(bool visible); + ScriptEventBridge* getEventBridge() const { return _eventBridge; } + +private: + QWidget* _window; + ScriptEventBridge* _eventBridge; +}; + +#endif