-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathApplicationPlugin.cpp
More file actions
171 lines (150 loc) · 5.26 KB
/
Copy pathApplicationPlugin.cpp
File metadata and controls
171 lines (150 loc) · 5.26 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
#include "ApplicationPlugin.hpp"
#include <JS/DocumentPlugin.hpp>
#include <JS/Qml/DeviceContext.hpp>
#include <JS/Qml/EditContext.hpp>
#include <JS/Qml/Utils.hpp>
#include <JS/Qml/ViewContext.hpp>
#include <Library/LibrarySettings.hpp>
#include <LocalTree/LocalTreeDocumentPlugin.hpp>
#include <core/application/ApplicationInterface.hpp>
#include <core/document/Document.hpp>
#include <core/presenter/DocumentManager.hpp>
#include <ossia/detail/thread.hpp>
#include <ossia-qt/invoke.hpp>
#include <ossia-qt/qml_protocols.hpp>
#include <QCommandLineParser>
#if __has_include(<QQuickWindow>)
#include <QGuiApplication>
#include <QQuickItem>
#include <QQuickWindow>
#endif
#if SCORE_HAS_GPU_JS
#include <Gfx/Settings/Model.hpp>
#endif
#include <ossia/network/context.hpp>
namespace JS
{
ApplicationPlugin::ApplicationPlugin(const score::GUIApplicationContext& ctx)
: score::GUIApplicationPlugin{ctx}
{
// For the console
m_consoleEngine.globalObject().setProperty("Score", m_consoleEngine.newQObject(new EditJsContext));
m_consoleEngine.globalObject().setProperty("Util", m_consoleEngine.newQObject(new JsUtils));
m_consoleEngine.globalObject().setProperty(
"System", m_consoleEngine.newQObject(new JsSystem));
m_consoleEngine.globalObject().setProperty(
"Library", m_consoleEngine.newQObject(new JsLibrary));
m_consoleEngine.globalObject().setProperty("Device", m_consoleEngine.newQObject(new DeviceContext{m_consoleEngine}));
m_consoleEngine.globalObject().setProperty("View", m_consoleEngine.newQObject(new JsViewContext));
connect(&m_consoleEngine, &QQmlEngine::exit, this, [&] {
for(auto& doc : score::GUIAppContext().docManager.documents())
doc->commandStack().markCurrentIndexAsSaved();
qApp->quit();
QTimer::singleShot(
500, [] { score::GUIApplicationInterface::instance().forceExit(); });
});
m_asioContext = std::make_shared<ossia::net::network_context>();
m_processMessages = true;
m_consoleEngine.globalObject().setProperty(
"Protocols",
m_consoleEngine.newQObject(new ossia::qt::qml_protocols{m_asioContext, this}));
m_asioThread = std::thread{[this] {
ossia::set_thread_name("ossia app asio");
while(m_processMessages)
{
m_asioContext->run();
}
}};
// For scripts of processes that run in the ui thread:
m_scriptProcessUIEngine.globalObject().setProperty(
"Util", m_scriptProcessUIEngine.newQObject(new JsUtils));
m_scriptProcessUIEngine.globalObject().setProperty(
"System", m_scriptProcessUIEngine.newQObject(new JsSystem));
m_scriptProcessUIEngine.globalObject().setProperty(
"Library", m_scriptProcessUIEngine.newQObject(new JsLibrary));
m_scriptProcessUIEngine.globalObject().setProperty(
"View", m_scriptProcessUIEngine.newQObject(new JsViewContext));
// Command-line option parsing
QCommandLineParser parser;
QCommandLineOption script_opt(
"script", QCoreApplication::translate("js", "script"), "Script", "");
parser.addOption(script_opt);
parser.parse(ctx.applicationSettings.arguments);
this->m_start_script = parser.value(script_opt);
}
void ApplicationPlugin::on_newDocument(score::Document& doc)
{
score::addDocumentPlugin<DocumentPlugin>(doc);
}
ApplicationPlugin::~ApplicationPlugin()
{
m_processMessages = false;
m_asioContext->context.stop();
m_asioThread.join();
}
void ApplicationPlugin::on_createdDocument(score::Document& doc)
{
// Local Tree
LocalTree::DocumentPlugin* lt = doc.context().findPlugin<LocalTree::DocumentPlugin>();
if(lt)
{
auto& root = lt->device().get_root_node();
auto node = root.create_child("script");
auto address = node->create_parameter(ossia::val_type::STRING);
address->set_value(std::string{});
address->set_access(ossia::access_mode::SET);
address->add_callback([&](const ossia::value& v) {
ossia::qt::run_async(
this, [this, str = QString::fromStdString(ossia::convert<std::string>(v))] {
auto res = m_consoleEngine.evaluate(str);
if(res.isError())
{
qDebug() << res.toString();
}
});
});
}
// Custom data
if(auto customData = doc.context().findPlugin<DocumentPlugin>(); !customData)
score::addDocumentPlugin<DocumentPlugin>(doc);
if(!m_start_script.isEmpty())
{
QTimer::singleShot(100, this, [this] { m_consoleEngine.evaluate(m_start_script); });
}
}
void ApplicationPlugin::afterStartup()
{
// Dummy engine setup for JS processes
// eng.importModule(
// "/home/jcelerier/Documents/ossia/score/packages/default/Scripts/include/"
// "tonal.mjs");
for(auto& p : this->context.settings<Library::Settings::Model>().getIncludePaths())
{
m_scriptProcessUIEngine.addImportPath(p);
}
#if __has_include(<QQuickWindow>)
if(QFileInfo f{context.applicationSettings.ui}; f.isFile())
{
m_comp = new QQmlComponent{&m_consoleEngine, f.absoluteFilePath(), this};
if(auto obj = m_comp->create())
{
if(auto item = qobject_cast<QQuickItem*>(obj))
{
m_window = new QQuickWindow{};
m_window->setWidth(640);
m_window->setHeight(480);
item->setParentItem(m_window->contentItem());
m_window->show();
return;
}
}
else
{
qDebug() << m_comp->errorString();
qGuiApp->exit(1);
}
delete m_comp;
}
#endif
}
}