mirror of
https://github.com/overte-org/overte.git
synced 2025-04-21 09:24:00 +02:00
Add WebWindow widget for scripts
This commit is contained in:
parent
1707025bd0
commit
3e109ee770
3 changed files with 124 additions and 0 deletions
|
@ -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());
|
||||
|
|
68
interface/src/scripting/WebWindow.cpp
Normal file
68
interface/src/scripting/WebWindow.cpp
Normal file
|
@ -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 <QVBoxLayout>
|
||||
#include <QApplication>
|
||||
#include <QWebFrame>
|
||||
#include <QWebView>
|
||||
|
||||
#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);
|
||||
}
|
53
interface/src/scripting/WebWindow.h
Normal file
53
interface/src/scripting/WebWindow.h
Normal file
|
@ -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 <QScriptContext>
|
||||
#include <QScriptEngine>
|
||||
|
||||
// #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
|
Loading…
Reference in a new issue