Overte C++ Documentation
QmlWebWindowClass.h
1 //
2 // Created by Bradley Austin Davis on 2015-12-15
3 // Copyright 2015 High Fidelity, Inc.
4 // Copyright 2023 Overte e.V.
5 //
6 // Distributed under the Apache License, Version 2.0.
7 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
8 // SPDX-License-Identifier: Apache-2.0
9 //
10 
11 #ifndef hifi_ui_QmlWebWindowClass_h
12 #define hifi_ui_QmlWebWindowClass_h
13 
14 #include <QtCore/QSharedPointer>
15 
16 #include "QmlWindowClass.h"
17 
18 #include <ScriptValue.h>
19 
20 class ScriptContext;
21 class ScriptEngine;
22 
23 /*@jsdoc
24  * A <code>OverlayWebWindow</code> displays an HTML window inside Interface.
25  *
26  * <p>Create using <code>new OverlayWebWindow(...)</code>.</p>
27  *
28  * @class OverlayWebWindow
29  * @param {string|OverlayWindow.Properties} [titleOrProperties="WebWindow"] - The window's title or initial property values.
30  * @param {string} [source="about:blank"] - The URL of the HTML to display. Not used unless the first parameter is the window
31  * title.
32  * @param {number} [width=0] - The width of the window interior, in pixels. Not used unless the first parameter is the window
33  * title.
34  * @param {number} [height=0] - The height of the window interior, in pixels. Not used unless the first parameter is the
35  * window title.
36  *
37  * @hifi-interface
38  * @hifi-client-entity
39  * @hifi-avatar
40  *
41  * @property {string} url - The URL of the HTML displayed in the window. <em>Read-only.</em>
42  * @property {Vec2} position - The position of the window, in pixels.
43  * @property {Vec2} size - The size of the window interior, in pixels.
44  * @property {boolean} visible - <code>true</code> if the window is visible, <code>false</code> if it isn't.
45  *
46  * @borrows OverlayWindow.initQml as initQml
47  * @borrows OverlayWindow.isVisible as isVisible
48  * @borrows OverlayWindow.setVisible as setVisible
49  * @borrows OverlayWindow.getPosition as getPosition
50  * @borrows OverlayWindow.setPosition as setPosition
51  * @borrows OverlayWindow.getSize as getSize
52  * @borrows OverlayWindow.setSize as setSize
53  * @borrows OverlayWindow.setTitle as setTitle
54  * @borrows OverlayWindow.raise as raise
55  * @borrows OverlayWindow.close as close
56  * @borrows OverlayWindow.getEventBridge as getEventBridge
57  * @comment OverlayWindow.sendToQml - Not applicable to OverlayWebWindow; documented separately.
58  * @comment OverlayWindow.clearDebugWindow - Not applicable to OverlayWebWindow; documented separately.
59  * @comment OverlayWindow.emitScriptEvent - Documented separately.
60  * @borrows OverlayWindow.emitWebEvent as emitWebEvent
61  * @borrows OverlayWindow.visibleChanged as visibleChanged
62  * @borrows OverlayWindow.positionChanged as positionChanged
63  * @borrows OverlayWindow.sizeChanged as sizeChanged
64  * @borrows OverlayWindow.moved as moved
65  * @borrows OverlayWindow.resized as resized
66  * @borrows OverlayWindow.closed as closed
67  * @comment OverlayWindow.fromQml - Not applicable to OverlayWebWindow; documented separately.
68  * @borrows OverlayWindow.scriptEventReceived as scriptEventReceived
69  * @comment OverlayWindow.webEventReceived - Documented separately.
70  * @borrows OverlayWindow.hasMoved as hasMoved
71  * @borrows OverlayWindow.hasClosed as hasClosed
72  * @borrows OverlayWindow.qmlToScript as qmlToScript
73  */
74 
75 /*@jsdoc
76  * @function OverlayWebWindow.clearDebugWindow
77  * @deprecated This method is deprecated and will be removed.
78  */
79 
80 /*@jsdoc
81  * @function OverlayWebWindow.sendToQML
82  * @param {string | object} message - Message.
83  * @deprecated This method is deprecated and will be removed.
84  */
85 
86 /*@jsdoc
87  * @function OverlayWebWindow.fromQML
88  * @param {object} message - Message.
89  * @returns {Signal}
90  * @deprecated This signal is deprecated and will be removed.
91  */
92 
93 /*@jsdoc
94  * Sends a message to the HTML page. To receive the message, the HTML page's script must connect to the <code>EventBridge</code>
95  * that is automatically provided for the script:
96  * <pre class="prettyprint"><code>EventBridge.scriptEventReceived.connect(function(message) {
97  * ...
98  * });</code></pre>
99  * @function OverlayWebWindow.emitScriptEvent
100  * @param {string|object} message - The message to send to the embedded HTML page.
101  * @example <caption>Send and receive messages with an HTML window.</caption>
102  * // JavaScript file.
103  *
104  * var overlayWebWindow = new OverlayWebWindow({
105  * title: "Overlay Web Window",
106  * source: Script.resolvePath("OverlayWebWindow.html"),
107  * width: 400,
108  * height: 300
109  * });
110  *
111  * overlayWebWindow.webEventReceived.connect(function (message) {
112  * print("Message received: " + message);
113  * });
114  *
115  * Script.setTimeout(function () {
116  * overlayWebWindow.emitScriptEvent("Hello world!");
117  * }, 2000);
118  *
119  * @example
120  * // HTML file, "OverlayWebWindow.html".
121  *
122  * <!DOCTYPE html>
123  * <html>
124  * <head>
125  * <meta charset="utf-8" />
126  * </head>
127  * <body>
128  * <p id="hello">...</p>
129  * </body>
130  * <script>
131  * EventBridge.scriptEventReceived.connect(function (message) {
132  * document.getElementById("hello").innerHTML = message;
133  * EventBridge.emitWebEvent("Hello back!");
134  * });
135  * </script>
136  * </html>
137  */
138 
139 /*@jsdoc
140  * Triggered when a message from the HTML page is received. The HTML page can send a message by calling:
141  * <pre class="prettyprint"><code>EventBridge.emitWebEvent(message);</code></pre>
142  * @function OverlayWebWindow.webEventReceived
143  * @param {string|object} message - The message received.
144  * @returns {Signal}
145  */
146 
147 
148 // FIXME refactor this class to be a QQuickItem derived type and eliminate the needless wrapping
149 class QmlWebWindowClass : public QmlWindowClass {
150  Q_OBJECT
151  Q_PROPERTY(QString url READ getURL CONSTANT)
152 
153 private:
154  static ScriptValue internal_constructor(ScriptContext* context, ScriptEngine* engine, bool restricted);
155 public:
156  QmlWebWindowClass(bool restricted) : QmlWindowClass(restricted) {}
157 
158  static ScriptValue constructor(ScriptContext* context, ScriptEngine* engine) {
159  return internal_constructor(context, engine, false);
160  }
161 
162  static ScriptValue restricted_constructor(ScriptContext* context, ScriptEngine* engine ){
163  return internal_constructor(context, engine, true);
164  }
165 
166 public slots:
167 
168  /*@jsdoc
169  * Gets the URL of the HTML displayed.
170  * @function OverlayWebWindow.getURL
171  * @returns {string} - The URL of the HTML page displayed.
172  */
173  QString getURL();
174 
175  /*@jsdoc
176  * Loads HTML into the window, replacing current window content.
177  * @function OverlayWebWindow.setURL
178  * @param {string} url - The URL of the HTML to display.
179  */
180  void setURL(const QString& url);
181 
182  /*@jsdoc
183  * Injects a script into the HTML page, replacing any currently injected script.
184  * @function OverlayWebWindow.setScriptURL
185  * @param {string} url - The URL of the script to inject.
186  */
187  void setScriptURL(const QString& script);
188 
189 signals:
190  /*@jsdoc
191  * Triggered when the window's URL changes.
192  * @function OverlayWebWindow.urlChanged
193  * @returns {Signal}
194  */
195  void urlChanged();
196 
197 protected:
198  QString qmlSource() const override { return "QmlWebWindow.qml"; }
199 };
200 
201 #endif
[ScriptInterface] Provides an engine-independent interface for QScriptContext
Definition: ScriptContext.h:55
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93
[ScriptInterface] Provides an engine-independent interface for QScriptValue
Definition: ScriptValue.h:40