Showing the changes to interface/src/scripting/WebWindow.cpp (1 of 3 files). Show all
Unified · Split
| @@ -0,0 +1,68 @@ | ||
| 1 | +// | |
| 2 | +// WebWindow.cpp | |
| 3 | +// interface/src/scripting | |
| 4 | +// | |
| 5 | +// Created by Ryan Huffman on 11/06/14. | |
| 6 | +// Copyright 2014 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include <QVBoxLayout> | |
| 13 | +#include <QApplication> | |
| 14 | +#include <QWebFrame> | |
| 15 | +#include <QWebView> | |
| 16 | + | |
| 17 | +#include "WindowScriptingInterface.h" | |
| 18 | +#include "WebWindow.h" | |
| 19 | + | |
| 20 | +ScriptEventBridge::ScriptEventBridge(QObject* parent) : QObject(parent) { | |
| 21 | +} | |
| 22 | + | |
| 23 | +void ScriptEventBridge::emitWebEvent(const QString& data) { | |
| 24 | + emit webEventReceived(data); | |
| 25 | +} | |
| 26 | + | |
| 27 | +void ScriptEventBridge::emitScriptEvent(const QString& data) { | |
| 28 | + emit scriptEventReceived(data); | |
| 29 | +} | |
| 30 | + | |
| 31 | +WebWindow::WebWindow(const QString& url, int width, int height) | |
| 32 | + : QObject(NULL), | |
| 33 | + _window(new QWidget(NULL, Qt::Tool)), | |
| 34 | + _eventBridge(new ScriptEventBridge(this)) { | |
| 35 | + | |
| 36 | + QWebView* webView = new QWebView(_window); | |
| 37 | + webView->page()->mainFrame()->addToJavaScriptWindowObject("EventBridge", _eventBridge); | |
| 38 | + webView->setUrl(url); | |
| 39 | + QVBoxLayout* layout = new QVBoxLayout(_window); | |
| 40 | + _window->setLayout(layout); | |
| 41 | + layout->addWidget(webView); | |
| 42 | + layout->setSpacing(0); | |
| 43 | + layout->setContentsMargins(0, 0, 0, 0); | |
| 44 | + _window->setGeometry(0, 0, width, height); | |
| 45 | + | |
| 46 | + connect(this, &WebWindow::destroyed, _window, &QWidget::deleteLater); | |
| 47 | +} | |
| 48 | + | |
| 49 | +WebWindow::~WebWindow() { | |
| 50 | +} | |
| 51 | + | |
| 52 | +void WebWindow::setVisible(bool visible) { | |
| 53 | + QMetaObject::invokeMethod(_window, "setVisible", Qt::BlockingQueuedConnection, Q_ARG(bool, visible)); | |
| 54 | +} | |
| 55 | + | |
| 56 | +QScriptValue WebWindow::constructor(QScriptContext* context, QScriptEngine* engine) { | |
| 57 | + WebWindow* retVal; | |
| 58 | + QString file = context->argument(0).toString(); | |
| 59 | + QMetaObject::invokeMethod(WindowScriptingInterface::getInstance(), "doCreateWebWindow", Qt::BlockingQueuedConnection, | |
| 60 | + Q_RETURN_ARG(WebWindow*, retVal), | |
| 61 | + Q_ARG(const QString&, file), | |
| 62 | + Q_ARG(int, context->argument(1).toInteger()), | |
| 63 | + Q_ARG(int, context->argument(2).toInteger())); | |
| 64 | + | |
| 65 | + connect(engine, &QScriptEngine::destroyed, retVal, &WebWindow::deleteLater); | |
| 66 | + | |
| 67 | + return engine->newQObject(retVal);//, QScriptEngine::ScriptOwnership); | |
| 68 | +} | |