ExecutionHelpers.hpp
1 #pragma once
2 #include <JS/Qml/QmlObjects.hpp>
3 #include <Library/LibrarySettings.hpp>
4 
5 #include <ossia/detail/logger.hpp>
6 
7 #include <QDir>
8 #include <QQmlComponent>
9 #include <QQmlEngine>
10 #include <QUrl>
11 
12 namespace JS
13 {
14 inline JS::Script* createJSObject(const QString& val, QQmlEngine* m_engine)
15 {
16  if(val.trimmed().startsWith("import"))
17  {
18  QQmlComponent c{m_engine};
19  {
20  auto& lib = score::AppContext().settings<Library::Settings::Model>();
21  // FIXME QTBUG-107204
22  QString path = lib.getDefaultLibraryPath() + QDir::separator() + "Scripts"
23  + QDir::separator() + "include" + QDir::separator();
24 
25  c.setData(val.toUtf8(), QUrl::fromLocalFile(path));
26  }
27  const auto& errs = c.errors();
28  if(!errs.empty())
29  {
30  ossia::logger().error(
31  "Uncaught exception at line {} : {}", errs[0].line(),
32  errs[0].toString().toStdString());
33  }
34  else
35  {
36  auto object = c.create();
37  auto obj = qobject_cast<JS::Script*>(object);
38  if(obj)
39  return obj;
40  delete object;
41  return nullptr;
42  }
43  }
44  else
45  {
46  qDebug() << "URL: " << val << QUrl::fromLocalFile(val);
47  QQmlComponent c{m_engine, QUrl::fromLocalFile(val)};
48  const auto& errs = c.errors();
49  if(!errs.empty())
50  {
51  ossia::logger().error(
52  "Uncaught exception at line {} : {}", errs[0].line(),
53  errs[0].toString().toStdString());
54  }
55  else
56  {
57  auto object = c.create();
58  auto obj = qobject_cast<JS::Script*>(object);
59  if(obj)
60  return obj;
61  delete object;
62  return nullptr;
63  }
64  }
65  return nullptr;
66 }
67 }
Definition: LibrarySettings.hpp:46
T & settings() const
Access a specific Settings model instance.
Definition: ApplicationContext.hpp:40