History

Merge branch 'master' into M22147

# Conflicts: # interface/src/scripting/DesktopScriptingInterface.h # interface/src/ui/InteractiveWindow.h

David Rowe committed 7 years ago

Commit 414f2722 parents 9c12a39 154844341 files · +1057 −132Browse files

Showing the changes to interface/src/ui/InteractiveWindow.cpp (1 of 41 files). Show all

Unified · Split

interface/src/ui/InteractiveWindow.cpp+418−0
@@ -0,0 +1,418 @@
1+//
2+// InteractiveWindow.cpp
3+// libraries/ui/src
4+//
5+// Created by Thijs Wenker on 2018-06-25
6+// Copyright 2018 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 "InteractiveWindow.h"
13+
14+#include "Application.h"
15+#include <QtQml/QQmlContext>
16+#include <QtCore/QThread>
17+#include <QtGui/QGuiApplication>
18+#include <QtQuick/QQuickWindow>
19+#include <QQuickView>
20+
21+#include <DependencyManager.h>
22+#include <DockWidget.h>
23+#include <RegisteredMetaTypes.h>
24+
25+#include "OffscreenUi.h"
26+#include "shared/QtHelpers.h"
27+#include "MainWindow.h"
28+
29+#ifdef Q_OS_WIN
30+#include <WinUser.h>
31+#endif
32+
33+static auto CONTENT_WINDOW_QML = QUrl("InteractiveWindow.qml");
34+
35+static const char* const FLAGS_PROPERTY = "flags";
36+static const char* const SOURCE_PROPERTY = "source";
37+static const char* const TITLE_PROPERTY = "title";
38+static const char* const POSITION_PROPERTY = "position";
39+static const char* const INTERACTIVE_WINDOW_POSITION_PROPERTY = "interactiveWindowPosition";
40+static const char* const SIZE_PROPERTY = "size";
41+static const char* const INTERACTIVE_WINDOW_SIZE_PROPERTY = "interactiveWindowSize";
42+static const char* const VISIBLE_PROPERTY = "visible";
43+static const char* const INTERACTIVE_WINDOW_VISIBLE_PROPERTY = "interactiveWindowVisible";
44+static const char* const EVENT_BRIDGE_PROPERTY = "eventBridge";
45+static const char* const PRESENTATION_MODE_PROPERTY = "presentationMode";
46+static const char* const DOCKED_PROPERTY = "presentationWindowInfo";
47+static const char* const DOCK_AREA_PROPERTY = "dockArea";
48+
49+static const QStringList KNOWN_SCHEMES = QStringList() << "http" << "https" << "file" << "about" << "atp" << "qrc";
50+
51+static const int DEFAULT_HEIGHT = 60;
52+
53+static void dockWidgetDeleter(DockWidget* dockWidget) {
54+ dockWidget->deleteLater();
55+}
56+
57+void registerInteractiveWindowMetaType(QScriptEngine* engine) {
58+ qScriptRegisterMetaType(engine, interactiveWindowPointerToScriptValue, interactiveWindowPointerFromScriptValue);
59+}
60+
61+QScriptValue interactiveWindowPointerToScriptValue(QScriptEngine* engine, const InteractiveWindowPointer& in) {
62+ return engine->newQObject(in, QScriptEngine::ScriptOwnership);
63+}
64+
65+void interactiveWindowPointerFromScriptValue(const QScriptValue& object, InteractiveWindowPointer& out) {
66+ if (const auto interactiveWindow = qobject_cast<InteractiveWindowPointer>(object.toQObject())) {
67+ out = interactiveWindow;
68+ }
69+}
70+
71+/**jsdoc
72+ * A set of properties used when creating an <code>InteractiveWindow</code>.
73+ * @typedef {object} InteractiveWindow.Properties
74+ * @property {string} [title="InteractiveWindow] - The title of the window.
75+ * @property {Vec2} [position] - The initial position of the window, in pixels.
76+ * @property {Vec2} [size] - The initial size of the window, in pixels
77+ * @property {boolean} [visible=true] - <code>true</code> to make the window visible when created, <code>false</code> to make
78+ * it invisible.
79+ * @property {InteractiveWindow.PresentationMode} [presentationMode=Desktop.PresentationMode.VIRTUAL] -
80+ * <code>Desktop.PresentationMode.VIRTUAL</code> to display the window inside Interface, <code>.NATIVE</code> to display it
81+ * as its own separate window.
82+ * @property {InteractiveWindow.Flags} [flags=0] - Window behavior flags, set at window creation. Possible flag values are
83+ * provided as {@link Desktop|Desktop.ALWAYS_ON_TOP} and {@link Desktop|Desktop.CLOSE_BUTTON_HIDES}.
84+ */
85+InteractiveWindow::InteractiveWindow(const QString& sourceUrl, const QVariantMap& properties) {
86+ bool docked = false;
87+ InteractiveWindowPresentationMode presentationMode = InteractiveWindowPresentationMode::Native;
88+
89+ if (properties.contains(PRESENTATION_MODE_PROPERTY)) {
90+ presentationMode = (InteractiveWindowPresentationMode) properties[PRESENTATION_MODE_PROPERTY].toInt();
91+ }
92+
93+ if (properties.contains(DOCKED_PROPERTY) && presentationMode == InteractiveWindowPresentationMode::Native) {
94+ QVariantMap nativeWindowInfo = properties[DOCKED_PROPERTY].toMap();
95+ Qt::DockWidgetArea dockArea = Qt::TopDockWidgetArea;
96+ QString title;
97+ QSize windowSize(DEFAULT_HEIGHT, DEFAULT_HEIGHT);
98+
99+ if (properties.contains(TITLE_PROPERTY)) {
100+ title = properties[TITLE_PROPERTY].toString();
101+ }
102+ if (properties.contains(SIZE_PROPERTY)) {
103+ const auto size = vec2FromVariant(properties[SIZE_PROPERTY]);
104+ windowSize = QSize(size.x, size.y);
105+ }
106+
107+ auto mainWindow = qApp->getWindow();
108+ _dockWidget = std::shared_ptr<DockWidget>(new DockWidget(title, mainWindow), dockWidgetDeleter);
109+
110+ if (nativeWindowInfo.contains(DOCK_AREA_PROPERTY)) {
111+ DockArea dockedArea = (DockArea) nativeWindowInfo[DOCK_AREA_PROPERTY].toInt();
112+ switch (dockedArea) {
113+ case DockArea::TOP:
114+ dockArea = Qt::TopDockWidgetArea;
115+ _dockWidget->setFixedHeight(windowSize.height());
116+ break;
117+ case DockArea::BOTTOM:
118+ dockArea = Qt::BottomDockWidgetArea;
119+ _dockWidget->setFixedHeight(windowSize.height());
120+ break;
121+ case DockArea::LEFT:
122+ dockArea = Qt::LeftDockWidgetArea;
123+ _dockWidget->setFixedWidth(windowSize.width());
124+ break;
125+ case DockArea::RIGHT:
126+ dockArea = Qt::RightDockWidgetArea;
127+ _dockWidget->setFixedWidth(windowSize.width());
128+ break;
129+
130+ default:
131+ _dockWidget->setFixedHeight(DEFAULT_HEIGHT);
132+ break;
133+ }
134+ }
135+
136+ auto quickView = _dockWidget->getQuickView();
137+ QObject::connect(quickView.get(), &QQuickView::statusChanged, [&, this] (QQuickView::Status status) {
138+ if (status == QQuickView::Ready) {
139+ QQuickItem* rootItem = _dockWidget->getRootItem();
140+ QObject::connect(rootItem, SIGNAL(sendToScript(QVariant)), this, SLOT(qmlToScript(const QVariant&)), Qt::QueuedConnection);
141+ }
142+ });
143+ _dockWidget->setSource(QUrl(sourceUrl));
144+ mainWindow->addDockWidget(dockArea, _dockWidget.get());
145+ _dockedWindow = docked;
146+ } else {
147+ auto offscreenUi = DependencyManager::get<OffscreenUi>();
148+ // Build the event bridge and wrapper on the main thread
149+ offscreenUi->loadInNewContext(CONTENT_WINDOW_QML, [&](QQmlContext* context, QObject* object) {
150+ _qmlWindow = object;
151+ context->setContextProperty(EVENT_BRIDGE_PROPERTY, this);
152+ if (properties.contains(FLAGS_PROPERTY)) {
153+ object->setProperty(FLAGS_PROPERTY, properties[FLAGS_PROPERTY].toUInt());
154+ }
155+ if (properties.contains(PRESENTATION_MODE_PROPERTY)) {
156+ object->setProperty(PRESENTATION_MODE_PROPERTY, properties[PRESENTATION_MODE_PROPERTY].toInt());
157+ }
158+ if (properties.contains(TITLE_PROPERTY)) {
159+ object->setProperty(TITLE_PROPERTY, properties[TITLE_PROPERTY].toString());
160+ }
161+ if (properties.contains(SIZE_PROPERTY)) {
162+ const auto size = vec2FromVariant(properties[SIZE_PROPERTY]);
163+ object->setProperty(INTERACTIVE_WINDOW_SIZE_PROPERTY, QSize(size.x, size.y));
164+ }
165+ if (properties.contains(POSITION_PROPERTY)) {
166+ const auto position = vec2FromVariant(properties[POSITION_PROPERTY]);
167+ object->setProperty(INTERACTIVE_WINDOW_POSITION_PROPERTY, QPointF(position.x, position.y));
168+ }
169+ if (properties.contains(VISIBLE_PROPERTY)) {
170+ object->setProperty(VISIBLE_PROPERTY, properties[INTERACTIVE_WINDOW_VISIBLE_PROPERTY].toBool());
171+ }
172+
173+ connect(object, SIGNAL(sendToScript(QVariant)), this, SLOT(qmlToScript(const QVariant&)), Qt::QueuedConnection);
174+ connect(object, SIGNAL(interactiveWindowPositionChanged()), this, SIGNAL(positionChanged()), Qt::QueuedConnection);
175+ connect(object, SIGNAL(interactiveWindowSizeChanged()), this, SIGNAL(sizeChanged()), Qt::QueuedConnection);
176+ connect(object, SIGNAL(interactiveWindowVisibleChanged()), this, SIGNAL(visibleChanged()), Qt::QueuedConnection);
177+ connect(object, SIGNAL(presentationModeChanged()), this, SIGNAL(presentationModeChanged()), Qt::QueuedConnection);
178+ connect(object, SIGNAL(titleChanged()), this, SIGNAL(titleChanged()), Qt::QueuedConnection);
179+ connect(object, SIGNAL(windowClosed()), this, SIGNAL(closed()), Qt::QueuedConnection);
180+ connect(object, SIGNAL(selfDestruct()), this, SLOT(close()), Qt::QueuedConnection);
181+
182+#ifdef Q_OS_WIN
183+ connect(object, SIGNAL(nativeWindowChanged()), this, SLOT(parentNativeWindowToMainWindow()), Qt::QueuedConnection);
184+ connect(object, SIGNAL(interactiveWindowVisibleChanged()), this, SLOT(parentNativeWindowToMainWindow()), Qt::QueuedConnection);
185+ connect(object, SIGNAL(presentationModeChanged()), this, SLOT(parentNativeWindowToMainWindow()), Qt::QueuedConnection);
186+#endif
187+
188+ QUrl sourceURL{ sourceUrl };
189+ // If the passed URL doesn't correspond to a known scheme, assume it's a local file path
190+ if (!KNOWN_SCHEMES.contains(sourceURL.scheme(), Qt::CaseInsensitive)) {
191+ sourceURL = QUrl::fromLocalFile(sourceURL.toString()).toString();
192+ }
193+ object->setProperty(SOURCE_PROPERTY, sourceURL);
194+ });
195+ }
196+}
197+
198+InteractiveWindow::~InteractiveWindow() {
199+ close();
200+}
201+
202+void InteractiveWindow::sendToQml(const QVariant& message) {
203+ // Forward messages received from the script on to QML
204+ if (_dockedWindow) {
205+ QQuickItem* rootItem = _dockWidget->getRootItem();
206+ if (rootItem) {
207+ QMetaObject::invokeMethod(_qmlWindow, "fromScript", Qt::QueuedConnection, Q_ARG(QVariant, message));
208+ }
209+ } else {
210+ QMetaObject::invokeMethod(_qmlWindow, "fromScript", Qt::QueuedConnection, Q_ARG(QVariant, message));
211+ }
212+}
213+
214+void InteractiveWindow::emitScriptEvent(const QVariant& scriptMessage) {
215+ if (QThread::currentThread() != thread()) {
216+ QMetaObject::invokeMethod(this, "emitScriptEvent", Qt::QueuedConnection, Q_ARG(QVariant, scriptMessage));
217+ } else {
218+ emit scriptEventReceived(scriptMessage);
219+ }
220+}
221+
222+void InteractiveWindow::emitWebEvent(const QVariant& webMessage) {
223+ if (QThread::currentThread() != thread()) {
224+ QMetaObject::invokeMethod(this, "emitWebEvent", Qt::QueuedConnection, Q_ARG(QVariant, webMessage));
225+ } else {
226+ emit webEventReceived(webMessage);
227+ }
228+}
229+
230+void InteractiveWindow::close() {
231+ if (QThread::currentThread() != thread()) {
232+ QMetaObject::invokeMethod(this, "close");
233+ return;
234+ }
235+
236+ if (_qmlWindow) {
237+ _qmlWindow->deleteLater();
238+ }
239+
240+ qApp->getWindow()->removeDockWidget(_dockWidget.get());
241+ _dockWidget = nullptr;
242+ _qmlWindow = nullptr;
243+}
244+
245+void InteractiveWindow::show() {
246+ if (QThread::currentThread() != thread()) {
247+ QMetaObject::invokeMethod(this, "show");
248+ return;
249+ }
250+
251+ if (_qmlWindow) {
252+ QMetaObject::invokeMethod(_qmlWindow, "show", Qt::DirectConnection);
253+ }
254+}
255+
256+void InteractiveWindow::raise() {
257+ if (QThread::currentThread() != thread()) {
258+ QMetaObject::invokeMethod(this, "raise");
259+ return;
260+ }
261+
262+ if (_qmlWindow) {
263+ QMetaObject::invokeMethod(_qmlWindow, "raiseWindow", Qt::DirectConnection);
264+ }
265+}
266+
267+void InteractiveWindow::qmlToScript(const QVariant& message) {
268+ if (message.canConvert<QJSValue>()) {
269+ emit fromQml(qvariant_cast<QJSValue>(message).toVariant());
270+ } else if (message.canConvert<QString>()) {
271+ emit fromQml(message.toString());
272+ } else {
273+ qWarning() << "Unsupported message type " << message;
274+ }
275+}
276+
277+void InteractiveWindow::setVisible(bool visible) {
278+ if (QThread::currentThread() != thread()) {
279+ QMetaObject::invokeMethod(this, "setVisible", Q_ARG(bool, visible));
280+ return;
281+ }
282+
283+ if (!_qmlWindow.isNull()) {
284+ _qmlWindow->setProperty(INTERACTIVE_WINDOW_VISIBLE_PROPERTY, visible);
285+ }
286+}
287+
288+bool InteractiveWindow::isVisible() const {
289+ if (QThread::currentThread() != thread()) {
290+ bool result = false;
291+ BLOCKING_INVOKE_METHOD(const_cast<InteractiveWindow*>(this), "isVisible", Q_RETURN_ARG(bool, result));
292+ return result;
293+ }
294+
295+ if (_qmlWindow.isNull()) {
296+ return false;
297+ }
298+
299+ return _qmlWindow->property(INTERACTIVE_WINDOW_VISIBLE_PROPERTY).toBool();
300+}
301+
302+glm::vec2 InteractiveWindow::getPosition() const {
303+ if (QThread::currentThread() != thread()) {
304+ glm::vec2 result;
305+ BLOCKING_INVOKE_METHOD(const_cast<InteractiveWindow*>(this), "getPosition", Q_RETURN_ARG(glm::vec2, result));
306+ return result;
307+ }
308+
309+ if (_qmlWindow.isNull()) {
310+ return {};
311+ }
312+
313+ return toGlm(_qmlWindow->property(INTERACTIVE_WINDOW_POSITION_PROPERTY).toPointF());
314+}
315+
316+void InteractiveWindow::setPosition(const glm::vec2& position) {
317+ if (QThread::currentThread() != thread()) {
318+ QMetaObject::invokeMethod(this, "setPosition", Q_ARG(const glm::vec2&, position));
319+ return;
320+ }
321+
322+ if (!_qmlWindow.isNull()) {
323+ _qmlWindow->setProperty(INTERACTIVE_WINDOW_POSITION_PROPERTY, QPointF(position.x, position.y));
324+ QMetaObject::invokeMethod(_qmlWindow, "updateInteractiveWindowPositionForMode", Qt::DirectConnection);
325+ }
326+}
327+
328+glm::vec2 InteractiveWindow::getSize() const {
329+ if (QThread::currentThread() != thread()) {
330+ glm::vec2 result;
331+ BLOCKING_INVOKE_METHOD(const_cast<InteractiveWindow*>(this), "getSize", Q_RETURN_ARG(glm::vec2, result));
332+ return result;
333+ }
334+
335+ if (_qmlWindow.isNull()) {
336+ return {};
337+ }
338+ return toGlm(_qmlWindow->property(INTERACTIVE_WINDOW_SIZE_PROPERTY).toSize());
339+}
340+
341+void InteractiveWindow::setSize(const glm::vec2& size) {
342+ if (QThread::currentThread() != thread()) {
343+ QMetaObject::invokeMethod(this, "setSize", Q_ARG(const glm::vec2&, size));
344+ return;
345+ }
346+
347+ if (!_qmlWindow.isNull()) {
348+ _qmlWindow->setProperty(INTERACTIVE_WINDOW_SIZE_PROPERTY, QSize(size.x, size.y));
349+ QMetaObject::invokeMethod(_qmlWindow, "updateInteractiveWindowSizeForMode", Qt::DirectConnection);
350+ }
351+}
352+
353+QString InteractiveWindow::getTitle() const {
354+ if (QThread::currentThread() != thread()) {
355+ QString result;
356+ BLOCKING_INVOKE_METHOD(const_cast<InteractiveWindow*>(this), "getTitle", Q_RETURN_ARG(QString, result));
357+ return result;
358+ }
359+
360+ if (_qmlWindow.isNull()) {
361+ return QString();
362+ }
363+ return _qmlWindow->property(TITLE_PROPERTY).toString();
364+}
365+
366+void InteractiveWindow::setTitle(const QString& title) {
367+ if (QThread::currentThread() != thread()) {
368+ QMetaObject::invokeMethod(this, "setTitle", Q_ARG(const QString&, title));
369+ return;
370+ }
371+
372+ if (!_qmlWindow.isNull()) {
373+ _qmlWindow->setProperty(TITLE_PROPERTY, title);
374+ }
375+}
376+
377+int InteractiveWindow::getPresentationMode() const {
378+ if (QThread::currentThread() != thread()) {
379+ int result;
380+ BLOCKING_INVOKE_METHOD(const_cast<InteractiveWindow*>(this), "getPresentationMode",
381+ Q_RETURN_ARG(int, result));
382+ return result;
383+ }
384+
385+ if (_qmlWindow.isNull()) {
386+ return Virtual;
387+ }
388+ return _qmlWindow->property(PRESENTATION_MODE_PROPERTY).toInt();
389+}
390+
391+void InteractiveWindow::parentNativeWindowToMainWindow() {
392+#ifdef Q_OS_WIN
393+ if (QThread::currentThread() != thread()) {
394+ QMetaObject::invokeMethod(this, "parentNativeWindowToMainWindow");
395+ return;
396+ }
397+ if (_qmlWindow.isNull()) {
398+ return;
399+ }
400+ const auto nativeWindowProperty = _qmlWindow->property("nativeWindow");
401+ if (nativeWindowProperty.isNull() || !nativeWindowProperty.isValid()) {
402+ return;
403+ }
404+ const auto nativeWindow = qvariant_cast<QQuickWindow*>(nativeWindowProperty);
405+ SetWindowLongPtr((HWND)nativeWindow->winId(), GWLP_HWNDPARENT, (LONG)MainWindow::findMainWindow()->winId());
406+#endif
407+}
408+
409+void InteractiveWindow::setPresentationMode(int presentationMode) {
410+ if (QThread::currentThread() != thread()) {
411+ QMetaObject::invokeMethod(this, "setPresentationMode", Q_ARG(int, presentationMode));
412+ return;
413+ }
414+
415+ if (!_qmlWindow.isNull()) {
416+ _qmlWindow->setProperty(PRESENTATION_MODE_PROPERTY, presentationMode);
417+ }
418+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t