Hooked up EventBridge to TabletScriptingInterface.

This commit is contained in:
Anthony J. Thibault 2017-01-03 15:43:21 -08:00
parent 850db0b6b3
commit 3c2f9acd39
6 changed files with 55 additions and 17 deletions

View file

@ -3,6 +3,7 @@ import QtQuick 2.0
Item { Item {
id: tabletRoot id: tabletRoot
objectName: "tabletRoot" objectName: "tabletRoot"
property var eventBridge;
function loadSource(url) { function loadSource(url) {
loader.source = url; loader.source = url;
@ -19,6 +20,13 @@ Item {
width: parent.width width: parent.width
height: parent.height height: parent.height
onLoaded: {
// propogate eventBridge to WebEngineView
if (loader.item.hasOwnProperty("eventBridge")) {
loader.item.eventBridge = eventBridge;
}
}
} }
width: 480 width: 480

View file

@ -1,9 +1,10 @@
import QtQuick 2.0 import QtQuick 2.0
import QtWebEngine 1.2 import QtWebEngine 1.2
WebEngineView { import "../../controls" as Controls
id: webEngineView
height: parent.height Controls.WebView {
width: parent.width
} }

View file

@ -61,7 +61,7 @@ Web3DOverlay::~Web3DOverlay() {
if (rootItem && rootItem->objectName() == "tabletRoot") { if (rootItem && rootItem->objectName() == "tabletRoot") {
auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>(); auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>();
tabletScriptingInterface->setQmlTabletRoot("com.highfidelity.interface.tablet.system", nullptr); tabletScriptingInterface->setQmlTabletRoot("com.highfidelity.interface.tablet.system", nullptr, nullptr);
} }
// Fix for crash in QtWebEngineCore when rapidly switching domains // Fix for crash in QtWebEngineCore when rapidly switching domains
@ -138,7 +138,7 @@ void Web3DOverlay::loadSourceURL() {
if (_webSurface->getRootItem() && _webSurface->getRootItem()->objectName() == "tabletRoot") { if (_webSurface->getRootItem() && _webSurface->getRootItem()->objectName() == "tabletRoot") {
auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>(); auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>();
tabletScriptingInterface->setQmlTabletRoot("com.highfidelity.interface.tablet.system", _webSurface->getRootItem()); tabletScriptingInterface->setQmlTabletRoot("com.highfidelity.interface.tablet.system", _webSurface->getRootItem(), _webSurface.data());
} }
} }
} }

View file

@ -268,7 +268,7 @@ void RenderableWebEntityItem::loadSourceURL() {
if (_webSurface->getRootItem() && _webSurface->getRootItem()->objectName() == "tabletRoot") { if (_webSurface->getRootItem() && _webSurface->getRootItem()->objectName() == "tabletRoot") {
auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>(); auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>();
tabletScriptingInterface->setQmlTabletRoot("com.highfidelity.interface.tablet.system", _webSurface->getRootItem()); tabletScriptingInterface->setQmlTabletRoot("com.highfidelity.interface.tablet.system", _webSurface->getRootItem(), _webSurface.data());
} }
} }
} }
@ -372,7 +372,7 @@ void RenderableWebEntityItem::destroyWebSurface() {
if (rootItem && rootItem->objectName() == "tabletRoot") { if (rootItem && rootItem->objectName() == "tabletRoot") {
auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>(); auto tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>();
tabletScriptingInterface->setQmlTabletRoot("com.highfidelity.interface.tablet.system", nullptr); tabletScriptingInterface->setQmlTabletRoot("com.highfidelity.interface.tablet.system", nullptr, nullptr);
} }
// Fix for crash in QtWebEngineCore when rapidly switching domains // Fix for crash in QtWebEngineCore when rapidly switching domains

View file

@ -29,10 +29,10 @@ QObject* TabletScriptingInterface::getTablet(const QString& tabletId) {
} }
} }
void TabletScriptingInterface::setQmlTabletRoot(QString tabletId, QQuickItem* qmlTabletRoot) { void TabletScriptingInterface::setQmlTabletRoot(QString tabletId, QQuickItem* qmlTabletRoot, QObject* qmlOffscreenSurface) {
TabletProxy* tablet = qobject_cast<TabletProxy*>(getTablet(tabletId)); TabletProxy* tablet = qobject_cast<TabletProxy*>(getTablet(tabletId));
if (tablet) { if (tablet && qmlOffscreenSurface) {
tablet->setQmlTabletRoot(qmlTabletRoot); tablet->setQmlTabletRoot(qmlTabletRoot, qmlOffscreenSurface);
} else { } else {
qCWarning(scriptengine) << "TabletScriptingInterface::setupTablet() bad tablet object"; qCWarning(scriptengine) << "TabletScriptingInterface::setupTablet() bad tablet object";
} }
@ -72,10 +72,12 @@ static void addButtonProxyToQmlTablet(QQuickItem* qmlTablet, TabletButtonProxy*
buttonProxy->setQmlButton(qobject_cast<QQuickItem*>(qmlButton)); buttonProxy->setQmlButton(qobject_cast<QQuickItem*>(qmlButton));
} }
void TabletProxy::setQmlTabletRoot(QQuickItem* qmlTabletRoot) { void TabletProxy::setQmlTabletRoot(QQuickItem* qmlTabletRoot, QObject* qmlOffscreenSurface) {
std::lock_guard<std::mutex> guard(_mutex); std::lock_guard<std::mutex> guard(_mutex);
_qmlOffscreenSurface = qmlOffscreenSurface;
_qmlTabletRoot = qmlTabletRoot; _qmlTabletRoot = qmlTabletRoot;
if (_qmlTabletRoot) { if (_qmlTabletRoot && _qmlOffscreenSurface) {
QObject::connect(_qmlOffscreenSurface, SIGNAL(webEventReceived(QVariant)), this, SIGNAL(webEventReceived(QVariant)));
gotoHomeScreen(); gotoHomeScreen();
} else { } else {
removeButtonsFromHomeScreen(); removeButtonsFromHomeScreen();
@ -141,6 +143,12 @@ void TabletProxy::removeButton(QObject* tabletButtonProxy) {
} }
} }
void TabletProxy::emitScriptEvent(QVariant msg) {
if (_qmlOffscreenSurface) {
QMetaObject::invokeMethod(_qmlOffscreenSurface, "emitScriptEvent", Qt::AutoConnection, Q_ARG(QVariant, msg));
}
}
void TabletProxy::addButtonsToHomeScreen() { void TabletProxy::addButtonsToHomeScreen() {
auto tablet = getQmlTablet(); auto tablet = getQmlTablet();
if (!tablet) { if (!tablet) {

View file

@ -37,7 +37,7 @@ public:
*/ */
Q_INVOKABLE QObject* getTablet(const QString& tabletId); Q_INVOKABLE QObject* getTablet(const QString& tabletId);
void setQmlTabletRoot(QString tabletId, QQuickItem* qmlTabletRoot); void setQmlTabletRoot(QString tabletId, QQuickItem* qmlTabletRoot, QObject* qmlOffscreenSurface);
protected: protected:
std::mutex _mutex; std::mutex _mutex;
@ -46,7 +46,7 @@ protected:
/**jsdoc /**jsdoc
* @class TabletProxy * @class TabletProxy
* @property name {string} name of this tablet * @property name {string} READ_ONLY: name of this tablet
*/ */
class TabletProxy : public QObject { class TabletProxy : public QObject {
Q_OBJECT Q_OBJECT
@ -54,7 +54,7 @@ class TabletProxy : public QObject {
public: public:
TabletProxy(QString name); TabletProxy(QString name);
void setQmlTabletRoot(QQuickItem* qmlTabletRoot); void setQmlTabletRoot(QQuickItem* qmlTabletRoot, QObject* qmlOffscreenSurface);
/**jsdoc /**jsdoc
* transition to the home screen * transition to the home screen
@ -85,6 +85,23 @@ public:
Q_INVOKABLE void removeButton(QObject* tabletButtonProxy); Q_INVOKABLE void removeButton(QObject* tabletButtonProxy);
QString getName() const { return _name; } QString getName() const { return _name; }
/**jsdoc
* Used to send an event to the html/js embedded in the tablet
* @function TabletProxy#emitScriptEvent
* @param msg {object|string}
*/
Q_INVOKABLE void emitScriptEvent(QVariant msg);
signals:
/**jsdoc
* Signaled when this tablet receives an event from the html/js embedded in the tablet
* @function TabletProxy#webEventReceived
* @param msg {object|string}
* @returns {Signal}
*/
void webEventReceived(QVariant msg);
protected: protected:
void addButtonsToHomeScreen(); void addButtonsToHomeScreen();
@ -95,19 +112,23 @@ protected:
std::mutex _mutex; std::mutex _mutex;
std::vector<QSharedPointer<TabletButtonProxy>> _tabletButtonProxies; std::vector<QSharedPointer<TabletButtonProxy>> _tabletButtonProxies;
QQuickItem* _qmlTabletRoot { nullptr }; QQuickItem* _qmlTabletRoot { nullptr };
QObject* _qmlOffscreenSurface { nullptr };
}; };
/**jsdoc /**jsdoc
* @class TabletButtonProxy * @class TabletButtonProxy
* @property imageUrl {string} * @property uuid {QUuid} READ_ONLY: uniquely identifies this button
*/ */
class TabletButtonProxy : public QObject { class TabletButtonProxy : public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QUuid uuid READ getUuid)
public: public:
TabletButtonProxy(const QVariantMap& properties); TabletButtonProxy(const QVariantMap& properties);
void setQmlButton(QQuickItem* qmlButton); void setQmlButton(QQuickItem* qmlButton);
QUuid getUuid() const { return _uuid; }
/**jsdoc /**jsdoc
* Returns the current value of this button's properties * Returns the current value of this button's properties
* @function TabletButtonProxy#getProperties * @function TabletButtonProxy#getProperties