Loading...
Searching...
No Matches
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>
9namespace JS
10{
12 : public QObject
14{
15 SCORE_CONCRETE("6e72e377-efdd-4e3c-9900-922b618e7d70")
16
17public:
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 if(path.find("companion-bundled-modules") == std::string_view::npos)
38 add(QString::fromUtf8(path.data(), path.length()));
39 }
40
41 bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
42 {
43 return add(path);
44 }
45};
46
48 : public QObject
50{
51 SCORE_CONCRETE("21f405da-a249-4e39-b405-9173aff11b26")
52
53 QSet<QString> acceptedFiles() const noexcept override { return {"js"}; }
54
55 bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
56 {
57 QFile f{path};
58 if(!f.open(QIODevice::ReadOnly))
59 return false;
60
61 auto& p = ctx.app.panel<JS::PanelDelegate>();
62 if(QFileInfo{f}.suffix() == "mjs")
63 {
64 p.importModule(path);
65 }
66 else
67 {
68 auto data = f.readAll();
69 p.evaluate(data);
70 }
71 return true;
72 }
73};
74
75class LibraryHandler final
76 : public QObject
78{
79 SCORE_CONCRETE("5231ea8b-da66-4c6f-9e34-d9a79cbc494a")
80
81 QSet<QString> acceptedFiles() const noexcept override { return {"qml"}; }
82
83 static inline const QRegularExpression scoreImport{"import Score [0-9].[0-9]"};
84
85 Library::Subcategories categories;
86
88 override
89 {
90 // TODO relaunch whenever library path changes...
92 QModelIndex node = model.find(key);
93 if(node == QModelIndex{})
94 return;
95
96 categories.init(node, ctx);
97 }
98
99 void addPath(std::string_view path) override
100 {
101 QFileInfo file{QString::fromUtf8(path.data(), path.length())};
103 pdata.prettyName = file.completeBaseName();
105 pdata.customData = [&] {
106 QFile f(file.absoluteFilePath());
107 f.open(QIODevice::ReadOnly);
108 return f.readAll().trimmed();
109 }();
110
111 {
112 auto matches = scoreImport.match(pdata.customData);
113 if(matches.hasMatch())
114 {
115 categories.add(file, std::move(pdata));
116 }
117 }
118 }
119};
120
121}
Definition LibraryHandler.hpp:50
Definition LibraryHandler.hpp:78
Definition LibraryHandler.hpp:14
Definition ConsolePanel.hpp:37
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