Globals.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // Created by Tobias Hieta on 05/04/16.
  3. //
  4. #include "Globals.h"
  5. #include "ui/KonvergoWindow.h"
  6. #include <QQmlContext>
  7. static QQmlApplicationEngine* g_qmlEngine = nullptr;
  8. #define QT_FORCE_ASSERTS 1
  9. /////////////////////////////////////////////////////////////////////////////////////////
  10. QQmlApplicationEngine* Globals::Engine()
  11. {
  12. if (!g_qmlEngine)
  13. g_qmlEngine = new QQmlApplicationEngine();
  14. return g_qmlEngine;
  15. }
  16. /////////////////////////////////////////////////////////////////////////////////////////
  17. QVariant Globals::ContextProperty(const QString& property)
  18. {
  19. Q_ASSERT_X(g_qmlEngine, "Globals", "QmlEngine not inited yet");
  20. return g_qmlEngine->rootContext()->contextProperty(property);
  21. }
  22. /////////////////////////////////////////////////////////////////////////////////////////
  23. void Globals::SetContextProperty(const QString& property, QObject* object)
  24. {
  25. Q_ASSERT_X(g_qmlEngine, "Globals", "QmlEngine not inited yet");
  26. g_qmlEngine->rootContext()->setContextProperty(property, object);
  27. }
  28. /////////////////////////////////////////////////////////////////////////////////////////
  29. void Globals::SetContextProperty(const QString& property, const QVariant& value)
  30. {
  31. Q_ASSERT_X(g_qmlEngine, "Globals", "QmlEngine not inited yet");
  32. g_qmlEngine->rootContext()->setContextProperty(property, value);
  33. }
  34. /////////////////////////////////////////////////////////////////////////////////////////
  35. void Globals::EngineDestroy()
  36. {
  37. if (g_qmlEngine)
  38. delete g_qmlEngine;
  39. g_qmlEngine = nullptr;
  40. }
  41. /////////////////////////////////////////////////////////////////////////////////////////
  42. KonvergoWindow* Globals::MainWindow()
  43. {
  44. Q_ASSERT_X(g_qmlEngine, "Globals", "QmlEngine not inited yet");
  45. auto rootObject = g_qmlEngine->rootObjects().first();
  46. Q_ASSERT_X(g_qmlEngine, "Globals", "No root objects in QmlEngine");
  47. auto window = qobject_cast<KonvergoWindow*>(rootObject);
  48. Q_ASSERT_X(g_qmlEngine, "Globals", "RootObject in QmlEngine is not a KonvergoWindow");
  49. return window;
  50. }