LibraryHandler.hpp
1 #pragma once
2 #include <JS/ConsolePanel.hpp>
3 #include <JS/JSProcessModel.hpp>
4 #include <Library/LibraryInterface.hpp>
5 #include <Library/ProcessesItemModel.hpp>
6 
7 #include <QFile>
8 #include <QFileInfo>
9 namespace JS
10 {
12  : public QObject
14 {
15  SCORE_CONCRETE("6e72e377-efdd-4e3c-9900-922b618e7d70")
16 
17 public:
18  JS::PanelDelegate* panel{};
19 
20  QSet<QString> acceptedFiles() const noexcept override { return {"mjs"}; }
21 
22  bool add(const QString& path)
23  {
24  QFile f{path};
25  if(!f.open(QIODevice::ReadOnly))
26  return false;
27 
28  if(!panel)
29  panel = &score::GUIAppContext().panel<JS::PanelDelegate>();
30 
31  panel->importModule(path);
32  return true;
33  }
34 
35  void addPath(std::string_view path) override
36  {
37  add(QString::fromUtf8(path.data(), path.length()));
38  }
39 
40  bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
41  {
42  return add(path);
43  }
44 };
45 
47  : public QObject
49 {
50  SCORE_CONCRETE("21f405da-a249-4e39-b405-9173aff11b26")
51 
52  QSet<QString> acceptedFiles() const noexcept override { return {"js"}; }
53 
54  bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
55  {
56  QFile f{path};
57  if(!f.open(QIODevice::ReadOnly))
58  return false;
59 
60  auto& p = ctx.app.panel<JS::PanelDelegate>();
61  if(QFileInfo{f}.suffix() == "mjs")
62  {
63  p.importModule(path);
64  }
65  else
66  {
67  auto data = f.readAll();
68  p.evaluate(data);
69  }
70  return true;
71  }
72 };
73 
74 class LibraryHandler final
75  : public QObject
77 {
78  SCORE_CONCRETE("5231ea8b-da66-4c6f-9e34-d9a79cbc494a")
79 
80  QSet<QString> acceptedFiles() const noexcept override { return {"qml"}; }
81 
82  static inline const QRegularExpression scoreImport{"import Score [0-9].[0-9]"};
83 
84  Library::Subcategories categories;
85 
86  void setup(Library::ProcessesItemModel& model, const score::GUIApplicationContext& ctx)
87  override
88  {
89  // TODO relaunch whenever library path changes...
91  QModelIndex node = model.find(key);
92  if(node == QModelIndex{})
93  return;
94 
95  categories.init(node, ctx);
96  }
97 
98  void addPath(std::string_view path) override
99  {
100  QFileInfo file{QString::fromUtf8(path.data(), path.length())};
101  Library::ProcessData pdata;
102  pdata.prettyName = file.completeBaseName();
104  pdata.customData = [&] {
105  QFile f(file.absoluteFilePath());
106  f.open(QIODevice::ReadOnly);
107  return f.readAll().trimmed();
108  }();
109 
110  {
111  auto matches = scoreImport.match(pdata.customData);
112  if(matches.hasMatch())
113  {
114  categories.add(file, std::move(pdata));
115  }
116  }
117  }
118 };
119 
120 }
Definition: LibraryHandler.hpp:49
Definition: LibraryHandler.hpp:77
Definition: LibraryHandler.hpp:14
Definition: LibraryInterface.hpp:22
Definition: ProcessesItemModel.hpp:45
Definition: ProcessesItemModel.hpp:32
Definition: ProcessesItemModel.hpp:86
Static metadata implementation.
Definition: lib/score/tools/Metadata.hpp:36
Definition: ObjectMatches.hpp:6
Definition: DocumentContext.hpp:18
Specializes ApplicationContext with the QMainWindow.
Definition: GUIApplicationContext.hpp:15
T & panel() const
Access to a specific PanelDelegate.
Definition: GUIApplicationContext.hpp:36