-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapplication_manager.cpp
More file actions
191 lines (163 loc) · 4.92 KB
/
application_manager.cpp
File metadata and controls
191 lines (163 loc) · 4.92 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#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);
});
m_event_loop_updater = new EventLoopUpdater(m_engine, jl_get_function(m_qml_mod, "process_eventloop_updates"));
ForeignThreadManager::instance(); // Make sure the main thread is put in GC safe mode
const int status = app->exec();
if (status != 0)
{
qWarning() << "Application exited with status " << status;
}
QGuiApplication::sendPostedEvents();
QGuiApplication::processEvents(QEventLoop::AllEvents);
cleanup();
}
void ApplicationManager::add_import_path(std::string path)
{
m_import_paths.push_back(path);
}
void ApplicationManager::queue_process_eventloop_updates()
{
assert(m_event_loop_updater != nullptr);
QMetaObject::invokeMethod(m_event_loop_updater, [this]{ m_event_loop_updater->process_eventloop_updates(); }, Qt::QueuedConnection);
}
ApplicationManager::ApplicationManager()
{
if(QProcessEnvironment::systemEnvironment().contains("QSG_RENDER_LOOP"))
{
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;
ForeignThreadManager::instance().cleanup();
m_event_loop_updater = 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();
for(const std::string& path : m_import_paths)
{
e->addImportPath(QString::fromStdString(path));
}
}
void ApplicationManager::process_events()
{
QGuiApplication::sendPostedEvents();
QGuiApplication::processEvents(QEventLoop::AllEvents, 15);
}
jl_module_t* ApplicationManager::m_qml_mod = nullptr;
EventLoopUpdater::EventLoopUpdater(QObject *parent, jl_value_t *f) : QObject(parent), m_process_eventloop_updates(f)
{
}
void EventLoopUpdater::process_eventloop_updates()
{
GCGuard gc_guard;
m_process_eventloop_updates();
}
}