Overte C++ Documentation
OffscreenQmlElement.h
1 //
2 // OffscreenUi.h
3 // interface/src/entities
4 //
5 // Created by Bradley Austin Davis on 2015-07-17
6 // Copyright 2015 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 #pragma once
12 #ifndef hifi_OffscreenQmlElement_h
13 #define hifi_OffscreenQmlElement_h
14 
15 #include <QQuickItem>
16 
17 #include <PathUtils.h>
18 
19 class QQmlContext;
20 
21 #define HIFI_QML_DECL \
22 private: \
23  static const QString NAME; \
24  static const QUrl QML; \
25  static bool registered; \
26 public: \
27  static void registerType(); \
28  static void show(std::function<void(QQmlContext*, QObject*)> f = [](QQmlContext*, QObject*) {}); \
29  static void hide(); \
30  static void toggle(std::function<void(QQmlContext*, QObject*)> f = [](QQmlContext*, QObject*) {}); \
31  static void load(std::function<void(QQmlContext*, QObject*)> f = [](QQmlContext*, QObject*) {}); \
32 private:
33 
34 #define HIFI_QML_DECL_LAMBDA \
35 protected: \
36  static const QString NAME; \
37  static const QUrl QML; \
38 public: \
39  static void registerType(); \
40  static void show(); \
41  static void hide(); \
42  static void toggle(); \
43  static void load(); \
44 private:
45 
46 #define HIFI_QML_DEF(x) \
47  const QUrl x::QML = PathUtils::qmlUrl(#x ".qml"); \
48  const QString x::NAME = #x; \
49  bool x::registered = false; \
50  \
51  void x::registerType() { \
52  qmlRegisterType<x>("Hifi", 1, 0, NAME.toLocal8Bit().constData()); \
53  } \
54  \
55  void x::show(std::function<void(QQmlContext*, QObject*)> f) { \
56  auto offscreenUI = DependencyManager::get<OffscreenUi>(); \
57  if (!registered) { \
58  x::registerType(); \
59  } \
60  if (offscreenUI) { \
61  offscreenUI->show(QML, NAME, f); \
62  } \
63  } \
64  \
65  void x::hide() { \
66  if (auto offscreenUI = DependencyManager::get<OffscreenUi>()) { \
67  offscreenUI->hide(NAME); \
68  } \
69  } \
70  \
71  void x::toggle(std::function<void(QQmlContext*, QObject*)> f) { \
72  if (auto offscreenUI = DependencyManager::get<OffscreenUi>()) { \
73  offscreenUI->toggle(QML, NAME, f); \
74  } \
75  } \
76  void x::load(std::function<void(QQmlContext*, QObject*)> f) { \
77  if (auto offscreenUI = DependencyManager::get<OffscreenUi>()) { \
78  offscreenUI->load(QML, f); \
79  } \
80  }
81 
82 #define HIFI_QML_DEF_LAMBDA(x, f) \
83  const QUrl x::QML = QUrl(#x ".qml"); \
84  const QString x::NAME = #x; \
85  \
86  void x::registerType() { \
87  qmlRegisterType<x>("Hifi", 1, 0, NAME.toLocal8Bit().constData()); \
88  } \
89  void x::show() { \
90  if (auto offscreenUI = DependencyManager::get<OffscreenUi>()) { \
91  offscreenUI->show(QML, NAME, f); \
92  } \
93  } \
94  void x::hide() { \
95  if (auto offscreenUI = DependencyManager::get<OffscreenUi>()) { \
96  offscreenUI->hide(NAME); \
97  } \
98  } \
99  \
100  void x::toggle() { \
101  if (auto offscreenUI = DependencyManager::get<OffscreenUi>()) { \
102  offscreenUI->toggle(QML, NAME, f); \
103  } \
104  } \
105  void x::load() { \
106  if (auto offscreenUI = DependencyManager::get<OffscreenUi>()) { \
107  offscreenUI->load(QML, f); \
108  } \
109  }
110 
111 #endif