Overte C++ Documentation
OffscreenQmlSurface.h
1 //
2 // Created by Bradley Austin Davis on 2015-04-04
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 #pragma once
11 #ifndef hifi_OffscreenQmlSurface_h
12 #define hifi_OffscreenQmlSurface_h
13 
14 #include <qml/OffscreenSurface.h>
15 #include <QtGui/qevent.h>
16 
17 #include <QTouchEvent>
18 
19 #include <ScriptEngine.h>
20 #include <PointerEvent.h>
21 
22 using QmlContextCallback = std::function<void(QQmlContext*)>;
23 
24 class OffscreenQmlSurface : public hifi::qml::OffscreenSurface {
25  using Parent = hifi::qml::OffscreenSurface;
26  Q_OBJECT
27  Q_PROPERTY(bool focusText READ isFocusText NOTIFY focusTextChanged)
28 public:
29  ~OffscreenQmlSurface();
30 
31  static void addWhitelistContextHandler(const std::initializer_list<QUrl>& urls, const QmlContextCallback& callback);
32  static void addWhitelistContextHandler(const QUrl& url, const QmlContextCallback& callback) { addWhitelistContextHandler({ { url } }, callback); };
33  static void applyWhiteList(const QUrl& url,QQmlContext* context);
34 
35  bool isFocusText() const { return _focusText; }
36  bool getCleaned() { return _isCleaned; }
37 
38  bool eventFilter(QObject* originalDestination, QEvent* event) override;
39  void setKeyboardRaised(QObject* object, bool raised, bool numeric = false, bool passwordField = false);
40  Q_INVOKABLE void synthesizeKeyPress(QString key, QObject* targetOverride = nullptr);
41  Q_INVOKABLE void lowerKeyboard();
42  PointerEvent::EventType choosePointerEventType(QEvent::Type type);
43  Q_INVOKABLE unsigned int deviceIdByTouchPoint(qreal x, qreal y);
44 
45 
46 signals:
47  void focusObjectChanged(QObject* newFocus);
48  void focusTextChanged(bool focusText);
49  void audioOutputDeviceChanged(const QString& deviceName);
50 
51  // web event bridge
52  void webEventReceived(const QVariant& message);
53  // script event bridge
54  void scriptEventReceived(const QVariant& message);
55  // qml event bridge
56  void fromQml(const QVariant& message);
57 
58 public slots:
59  void focusDestroyed(QObject *obj);
60  // script event bridge
61  void emitScriptEvent(const QVariant& scriptMessage);
62  // web event bridge
63  void emitWebEvent(const QVariant& webMessage);
64  // qml event bridge
65  void sendToQml(const QVariant& message);
66 
67 protected:
68  void loadFromQml(const QUrl& qmlSource, QQuickItem* parent, const QJSValue& callback) override;
69  void clearFocusItem();
70  void setFocusText(bool newFocusText);
71  void initializeEngine(QQmlEngine* engine) override;
72  void onRootContextCreated(QQmlContext* qmlContext) override;
73 
74 private:
75  void onRootCreated() override;
76  void onItemCreated(QQmlContext* qmlContext, QQuickItem* newItem) override;
77  QQmlContext* contextForUrl(const QUrl& url, QQuickItem* parent, bool forceNewContext = false) override;
78  QPointF mapWindowToUi(const QPointF& sourcePosition, QObject* sourceObject);
79 
80 private slots:
81  void onFocusObjectChanged(QObject* newFocus) override;
82 
83 public slots:
84  void hoverBeginEvent(const PointerEvent& event, class QTouchDevice& device);
85  void hoverEndEvent(const PointerEvent& event, class QTouchDevice& device);
86  bool handlePointerEvent(const PointerEvent& event, class QTouchDevice& device, bool release = false);
87  void changeAudioOutputDevice(const QString& deviceName, bool isHtmlUpdate = false);
88  void forceHtmlAudioOutputDeviceUpdate();
89  void forceQmlAudioOutputDeviceUpdate();
90 
91 private:
92  bool _focusText { false };
93  bool _isCleaned{ false };
94  QQuickItem* _currentFocusItem { nullptr };
95 
96  struct TouchState {
97  QTouchEvent::TouchPoint touchPoint;
98  bool hovering { false };
99  bool pressed { false };
100  };
101 
102  bool _pressed;
103  bool _touchBeginAccepted { false };
104  std::map<uint32_t, TouchState> _activeTouchPoints;
105 
106  QString _currentAudioOutputDevice;
107  QTimer _audioOutputUpdateTimer;
108 };
109 
110 #endif
Represents a 2D or 3D pointer to the scripting engine. Exposed as PointerEvent
Definition: PointerEvent.h:30