-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathConsolePanel.cpp
More file actions
220 lines (191 loc) · 5.89 KB
/
ConsolePanel.cpp
File metadata and controls
220 lines (191 loc) · 5.89 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "ConsolePanel.hpp"
#include <JS/ApplicationPlugin.hpp>
#include <score/widgets/HelpInteraction.hpp>
#include <score/widgets/PromptLineEdit.hpp>
#include <QCompleter>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QJSCompleter>
#include <QJSValueIterator>
#include <QQmlEngine>
#include <QStandardPaths>
namespace JS
{
PanelDelegate::PanelDelegate(const score::GUIApplicationContext& ctx)
: score::PanelDelegate{ctx}
, m_engine{ctx.guiApplicationPlugin<JS::ApplicationPlugin>().m_consoleEngine}
, m_widget{new QWidget}
{
m_engine.installExtensions(QJSEngine::ConsoleExtension);
m_engine.globalObject().setProperty(
"ActionContext", m_engine.newQObject(new ActionContext));
auto lay = new QVBoxLayout;
m_widget->setLayout(lay);
score::setHelp(m_widget,
QObject::tr("This panel prompts the scripting console \n"
"still in early development"));
m_edit = new QPlainTextEdit{m_widget};
m_edit->setTextInteractionFlags(Qt::TextEditorInteraction);
lay->addWidget(m_edit, 1);
m_lineEdit = new score::PromptLineEdit{m_widget};
static const QString history_save_path
= QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
+ QDir::separator() + "ossia" + QDir::separator() + "console_history.txt";
m_lineEdit->historyLoad(history_save_path);
lay->addWidget(m_lineEdit, 0);
m_edit->appendPlainText(
"Welcome to the ossia score scripting console!\n"
"The console provides a JavaScript ES7 environment.\n"
"Read more about the avaiable functions here:\n"
"-> https://ossia.io/score-docs/panels/console.html\n\n");
auto c = new QJSCompleter{m_lineEdit};
auto& plug = this->context().guiApplicationPlugin<JS::ApplicationPlugin>();
auto console_engine_object = plug.m_consoleEngine.globalObject();
c->registerObject("Score", console_engine_object.property("Score").toQObject());
c->registerObject("Util", console_engine_object.property("Util").toQObject());
c->registerObject("Device", console_engine_object.property("Device").toQObject());
c->setCompletionMode(QCompleter::CompletionMode::PopupCompletion);
c->setWidget(m_lineEdit);
m_lineEdit->setCompleter(c);
connect(
m_lineEdit, &score::PromptLineEdit::lineEntered, this,
[this](const QString& line) {
if(!line.isEmpty())
{
m_lineEdit->historySave(history_save_path);
evaluate(line);
m_lineEdit->clear();
m_edit->verticalScrollBar()->setValue(m_edit->verticalScrollBar()->maximum());
}
});
}
QJSEngine& PanelDelegate::engine() noexcept
{
return m_engine;
}
void PanelDelegate::evaluate(const QString& txt)
{
m_edit->appendPlainText("> " + txt);
auto res = m_engine.evaluate(txt);
if(res.isError())
{
m_edit->appendPlainText("ERROR: " + res.toString() + "\n");
}
else if(!res.isUndefined())
{
m_edit->appendPlainText(res.toString() + "\n");
}
}
void PanelDelegate::compute(const QString& txt, std::function<void(QVariant)> r)
{
m_edit->appendPlainText("> " + txt);
auto res = m_engine.evaluate(txt);
if(res.isError())
{
m_edit->appendPlainText("ERROR: " + res.toString() + "\n");
}
else if(!res.isUndefined())
{
if(r)
r(res.toVariant());
}
else
{
qDebug() << res.toString();
}
}
QMenu* PanelDelegate::addMenu(QMenu* cur, QStringList names)
{
QMenu* newmenu{};
for(int i = 0; i < names.size() - 1; i++)
{
for(auto act : cur->findChildren<QMenu*>(QString{}, Qt::FindDirectChildrenOnly))
{
if(act->title() == names[i])
{
cur = act;
goto ok;
}
}
newmenu = new QMenu{names[i], cur};
cur->addMenu(newmenu);
cur = newmenu;
ok:
continue;
}
return cur;
}
void PanelDelegate::importModule(const QString& path)
{
QJSValue mod = m_engine.importModule(path);
if(auto init = mod.property("initialize"); init.isCallable())
init.call();
if(auto init = mod.property("actions"); init.isArray())
{
QJSValueIterator it(init);
while(it.hasNext())
{
if(it.next())
{
auto obj = it.value();
if(obj.isObject())
{
auto name = obj.property("name");
auto context = obj.property("context");
auto action = obj.property("action");
auto shortcut = obj.property("shortcut");
if(!name.isString())
return;
if(context.toString() != "Menu")
return;
if(!action.isCallable())
return;
auto names = name.toString().split('/');
if(names.empty())
return;
score::Menu& script = this->context().menus.get().at(score::Menus::Scripts());
if(auto act = script.menu()->actions()[0];
act->objectName() == "DefaultScriptMenuAction")
script.menu()->removeAction(act);
auto menu = addMenu(script.menu(), names);
auto act = new QAction{names.back(), this};
if(auto str = shortcut.toString(); !str.isEmpty())
{
act->setShortcut(QKeySequence::fromString(str));
}
connect(
act, &QAction::triggered, this, [action = std::move(action)]() mutable {
auto res = action.call();
if(res.isError())
{
qDebug() << "Script error: " << res.errorType() << res.toString();
}
});
menu->addAction(act);
}
}
}
}
auto obj = m_engine.globalObject();
obj.setProperty(QFileInfo{path}.baseName(), mod);
if(mod.isString() || mod.isError())
m_edit->appendPlainText(mod.toString());
}
QWidget* PanelDelegate::widget()
{
return m_widget;
}
const score::PanelStatus& PanelDelegate::defaultPanelStatus() const
{
static const score::PanelStatus status{
false,
false,
Qt::RightDockWidgetArea,
0,
QObject::tr("Console"),
"console",
QObject::tr("Ctrl+Shift+C")};
return status;
}
}