-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapplication_manager.cpp
157 lines (136 loc) · 3.96 KB
/
application_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "jlcxx/functions.hpp"
#include "foreign_thread_manager.hpp"
#include "application_manager.hpp"
namespace qmlwrap
{
void julia_message_output(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtDebugMsg:
jl_safe_printf("Qt Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtInfoMsg:
jl_safe_printf("Qt Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtWarningMsg:
jl_safe_printf("Qt Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtCriticalMsg:
jl_safe_printf("Qt Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtFatalMsg:
jl_safe_printf("Qt Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
}
}
// Singleton implementation
ApplicationManager& ApplicationManager::instance()
{
static ApplicationManager m_instance;
return m_instance;
}
JuliaAPI* ApplicationManager::julia_api()
{
if(JuliaSingleton::s_singletonInstance == nullptr)
{
JuliaSingleton::s_singletonInstance = new JuliaAPI();
}
return JuliaSingleton::s_singletonInstance;
}
ApplicationManager::~ApplicationManager()
{
}
// Init the app with a new QQmlApplicationEngine
QQmlApplicationEngine* ApplicationManager::init_qmlapplicationengine()
{
check_no_engine();
QQmlApplicationEngine* e = new QQmlApplicationEngine();
set_engine(e);
return e;
}
QQmlEngine* ApplicationManager::init_qmlengine()
{
check_no_engine();
set_engine(new QQmlEngine());
return m_engine;
}
QQmlEngine* ApplicationManager::get_qmlengine()
{
return m_engine;
}
QQuickView* ApplicationManager::init_qquickview()
{
check_no_engine();
QQuickView* view = new QQuickView();
set_engine(view->engine());
return view;
}
QQmlContext* ApplicationManager::root_context()
{
return m_root_ctx;
}
// Blocking call to exec, running the Qt event loop
void ApplicationManager::exec()
{
QGuiApplication* app = dynamic_cast<QGuiApplication*>(QGuiApplication::instance());
if(m_engine == nullptr)
{
throw std::runtime_error("QML engine is not initialized, can't exec");
}
QObject::connect(m_engine, &QQmlEngine::exit, [this,app](const int status)
{
app->exit(status);
});
ForeignThreadManager::instance().clear(QThread::currentThread());
const int status = app->exec();
if (status != 0)
{
qWarning() << "Application exited with status " << status;
}
QGuiApplication::sendPostedEvents();
QGuiApplication::processEvents(QEventLoop::AllEvents);
cleanup();
}
ApplicationManager::ApplicationManager()
{
qputenv("QSG_RENDER_LOOP", QProcessEnvironment::systemEnvironment().value("QSG_RENDER_LOOP").toLocal8Bit());
qInstallMessageHandler(julia_message_output);
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setProfile(QSurfaceFormat::CoreProfile);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setMajorVersion(3);
format.setMinorVersion(3);
//format.setOption(QSurfaceFormat::DebugContext); // note: this needs OpenGL 4.3
QSurfaceFormat::setDefaultFormat(format);
}
void ApplicationManager::cleanup()
{
if(m_engine != nullptr)
{
delete m_engine;
m_engine = nullptr;
}
delete JuliaSingleton::s_singletonInstance;
JuliaSingleton::s_singletonInstance = nullptr;
}
void ApplicationManager::check_no_engine()
{
if(m_engine != nullptr)
{
throw std::runtime_error("Existing engine, aborting creation");
}
julia_api();
}
void ApplicationManager::set_engine(QQmlEngine* e)
{
m_engine = e;
m_root_ctx = e->rootContext();
}
void ApplicationManager::process_events()
{
QGuiApplication::sendPostedEvents();
QGuiApplication::processEvents(QEventLoop::AllEvents, 15);
}
jl_module_t* ApplicationManager::m_qml_mod = nullptr;
}