-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathEffectLayer.cpp
More file actions
395 lines (339 loc) · 11.2 KB
/
Copy pathEffectLayer.cpp
File metadata and controls
395 lines (339 loc) · 11.2 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "EffectLayer.hpp"
#include "core/view/FixedTabWidget.hpp"
#include "core/view/Window.hpp"
#include "score/actions/MenuManager.hpp"
#include "score/plugins/panel/PanelDelegate.hpp"
#include <Process/ApplicationPlugin.hpp>
#include <Process/Commands/LoadPreset.hpp>
#include <Process/Commands/LoadPresetCommandFactory.hpp>
#include <Process/Dataflow/CableCopy.hpp>
#include <Process/Focus/FocusDispatcher.hpp>
#include <Process/Process.hpp>
#include <Process/ProcessMimeSerialization.hpp>
#include <Process/Style/Pixmaps.hpp>
#include <score/command/Dispatchers/CommandDispatcher.hpp>
#include <score/document/DocumentContext.hpp>
#include <score/graphics/GraphicWidgets.hpp>
#include <score/model/EntitySerialization.hpp>
#include <score/model/path/PathSerialization.hpp>
#include <score/widgets/HelpInteraction.hpp>
#include <core/document/Document.hpp>
#include <core/view/ScriptPanelDelegate.hpp>
#include <ossia/detail/thread.hpp>
#include <QMenu>
#include <qtoolbar.h>
#include <wobjectimpl.h>
W_OBJECT_IMPL(Process::EffectLayerPresenter)
namespace Process
{
EffectLayerView::EffectLayerView(QGraphicsItem* parent)
: Process::LayerView{parent}
{
}
EffectLayerView::~EffectLayerView() { }
void EffectLayerView::setWidth(qreal val, qreal defaultWidth)
{
m_defaultWidth = defaultWidth;
LayerView::setWidth(val);
}
void EffectLayerView::paint_impl(QPainter*) const { }
EffectLayerPresenter::EffectLayerPresenter(
const ProcessModel& model, Process::EffectLayerView* view, const Context& ctx,
QObject* parent)
: LayerPresenter{model, view, ctx, parent}
, m_view{view}
{
putToFront();
}
EffectLayerPresenter::~EffectLayerPresenter() { }
void EffectLayerPresenter::setWidth(qreal val, qreal defaultWidth)
{
m_view->setWidth(val, defaultWidth);
}
void EffectLayerPresenter::setHeight(qreal val)
{
m_view->setHeight(val);
}
void EffectLayerPresenter::putToFront()
{
m_view->setVisible(true);
}
void EffectLayerPresenter::putBehind()
{
m_view->setVisible(false);
}
void EffectLayerPresenter::on_zoomRatioChanged(ZoomRatio) { }
void EffectLayerPresenter::parentGeometryChanged() { }
void EffectLayerPresenter::fillContextMenu(
QMenu& menu, QPoint pos, QPointF scenepos, const LayerContextMenuManager& mgr)
{
}
QGraphicsItem* makeScriptButton(
ProcessModel& effect, const score::DocumentContext& context, QObject* self,
QGraphicsItem* root)
{
auto& pixmaps = Process::Pixmaps::instance();
auto& facts = context.app.interfaces<Process::LayerFactoryList>();
auto fact = facts.findDefaultFactory(effect);
if(effect.flags() & Process::ProcessFlags::ScriptEditingSupported)
{
auto ui_btn = new score::QGraphicsPixmapToggle{
pixmaps.show_script_on, pixmaps.show_script_off, root};
ui_btn->setToolTip(
QObject::tr("Show/hide UI\nShow the process's script editor for JS, shaders, etc."));
QObject::connect(
ui_btn, &score::QGraphicsPixmapToggle::toggled, self,
[=, &effect, &context](bool b) {
Process::setupScriptUI(effect, *fact, context, b);
});
if(effect.scriptUI)
ui_btn->setState(true);
QObject::connect(
&effect, &Process::ProcessModel::scriptUIVisible, ui_btn,
[=](bool v) { ui_btn->setState(v); });
return ui_btn;
}
return nullptr;
}
void setupScriptUI(
Process::ProcessModel& proc, const Process::LayerFactory& fact,
const score::DocumentContext& ctx, bool show)
{
if(show)
{
if(proc.scriptUI)
return;
if(auto win = fact.makeScriptUI(proc, ctx, nullptr))
{
const_cast<QWidget*&>(proc.scriptUI) = win;
if(auto score_view = static_cast<score::View*>(score::GUIAppContext().mainWindow))
{
auto [idx, toggle] = score_view->rightTabs->addTab(
win, score::PanelStatus{
false, true, Qt::RightDockWidgetArea, -100000, proc.prettyName(),
"script", QKeySequence{}});
auto& mw = ctx.app.menus.get().at(score::Menus::Windows());
score_view->addAction(toggle);
mw.menu()->addAction(toggle);
toggle->toggle();
score_view->rightTabs->toolbar()->actions().back()->trigger();
QObject::connect(
win, &QWidget::destroyed, score_view, [&ctx, score_view, toggle] {
auto& mw = ctx.app.menus.get().at(score::Menus::Windows());
mw.menu()->removeAction(toggle);
score_view->removeAction(toggle);
score_view->rightTabs->toolbar()->removeAction(toggle);
score_view->rightTabs->toolbar()->actions().front()->trigger();
});
}
}
}
else
{
if(auto win = proc.scriptUI)
{
delete win;
const_cast<QWidget*&>(proc.scriptUI) = nullptr;
}
}
}
void setupExternalUI(
Process::ProcessModel& proc, const Process::LayerFactory& fact,
const score::DocumentContext& ctx, bool show)
{
if(show)
{
if(proc.externalUI)
return;
if(auto win = fact.makeExternalUI(proc, ctx, nullptr))
{
const_cast<QWidget*&>(proc.externalUI) = win;
win->show();
}
}
else
{
if(auto win = proc.externalUI)
{
win->close();
delete win;
const_cast<QWidget*&>(proc.externalUI) = nullptr;
}
}
}
void setupExternalUI(
Process::ProcessModel& proc, const score::DocumentContext& ctx, bool show)
{
auto& facts = ctx.app.interfaces<Process::LayerFactoryList>();
auto fact = facts.findDefaultFactory(proc);
if(!fact || !fact->hasExternalUI(proc, ctx))
return;
setupExternalUI(proc, *fact, ctx, show);
}
QGraphicsItem* makeExternalUIButton(
ProcessModel& effect, const score::DocumentContext& context, QObject* self,
QGraphicsItem* root)
{
auto& pixmaps = Process::Pixmaps::instance();
auto& facts = context.app.interfaces<Process::LayerFactoryList>();
auto fact = facts.findDefaultFactory(effect);
if(fact && fact->hasExternalUI(effect, context))
{
auto ui_btn = new score::QGraphicsPixmapToggle{
pixmaps.show_ui_on, pixmaps.show_ui_off, root};
ui_btn->setToolTip(
QObject::tr("Show/hide UI\nShow the process's interface for instance for VSTs "
"or script editor for JS, etc."));
QObject::connect(
ui_btn, &score::QGraphicsPixmapToggle::toggled, self,
[=, &effect, &context](bool b) {
Process::setupExternalUI(effect, *fact, context, b);
});
if(effect.externalUI)
ui_btn->setState(true);
QObject::connect(
&effect, &Process::ProcessModel::externalUIVisible, ui_btn,
[=](bool v) { ui_btn->setState(v); });
return ui_btn;
}
return nullptr;
}
score::QGraphicsDraggablePixmap* makePresetButton(
const ProcessModel& proc, const score::DocumentContext& context, QObject* self,
QGraphicsItem* root)
{
auto& pixmaps = Process::Pixmaps::instance();
auto ui_btn
= new score::QGraphicsDraggablePixmap{pixmaps.preset_on, pixmaps.preset_off, root};
ui_btn->setToolTip(
QObject::tr(
"Presets\nDrag to the library to save the current preset. If there "
"are existing presets, they will be shown in a menu."));
ui_btn->createDrag = [&proc](QMimeData& mime) {
QByteArray data;
{
JSONReader r;
r.stream.StartObject();
copyProcess(r, proc);
r.obj["Path"] = score::IDocument::path(proc);
r.obj["View"] = QStringLiteral("Nodal");
r.stream.EndObject();
data = r.toByteArray();
}
mime.setData(score::mime::layerdata(), data);
mime.setData(score::mime::processpreset(), proc.savePreset().toJson());
};
ui_btn->click = [&proc, &context](Qt::MouseButton btn, QPointF screenPos) {
auto& pplug = context.app.applicationPlugin<Process::ApplicationPlugin>();
const auto& presets = pplug.presets;
switch(btn)
{
default:
break;
case Qt::LeftButton: {
// Preset menu
auto menu = new QMenu;
menu->addAction(
"Save current preset", menu, [&proc, &pplug] { pplug.savePreset(&proc); });
std::vector<const Process::Preset*> goodPresets;
const auto& k = proc.concreteKey();
const auto& e = proc.effect();
for(auto& preset : presets)
{
if(preset.key.key == k && preset.key.effect == e)
goodPresets.push_back(&preset);
}
menu->addSeparator();
for(auto p : goodPresets)
{
menu->addAction(p->name, menu, [p, &proc, &context] {
auto& load_preset_ifaces
= context.app.interfaces<LoadPresetCommandFactoryList>();
auto cmd = load_preset_ifaces.make(
&LoadPresetCommandFactory::make, proc, *p, context);
if(cmd)
{
CommandDispatcher<> c{context.commandStack};
c.submit(cmd);
}
});
}
if(auto proc_builtins = proc.builtinPresets(); !proc_builtins.empty())
{
if(!goodPresets.empty())
menu->addSeparator();
for(auto& p : proc_builtins)
{
// FIXME try to understand why just p.name does not work here
menu->addAction("" + p.name, menu, [p = std::move(p), &proc, &context] {
auto& load_preset_ifaces
= context.app.interfaces<LoadPresetCommandFactoryList>();
auto cmd = load_preset_ifaces.make(
&LoadPresetCommandFactory::make, proc, std::move(p), context);
if(cmd)
{
CommandDispatcher<> c{context.commandStack};
c.submit(cmd);
}
});
}
}
menu->exec(screenPos.toPoint());
menu->deleteLater();
break;
}
case Qt::ForwardButton:
case Qt::BackButton: {
std::vector<const Process::Preset*> goodPresets;
const auto& k = proc.concreteKey();
const auto& e = proc.effect();
for(auto& preset : presets)
if(preset.key.key == k && preset.key.effect == e)
goodPresets.push_back(&preset);
auto bps = proc.builtinPresets();
for(auto& bp : bps)
goodPresets.push_back(&bp);
if(goodPresets.size() < 2)
return;
int i = 0;
for(auto& fx : goodPresets)
{
if(fx->key.effect == e)
break;
i++;
}
if(btn == Qt::ForwardButton)
i++;
else
i--;
if(i >= std::ssize(goodPresets))
i = 0;
else if(i < 0)
i = std::ssize(goodPresets) - 1;
auto& load_preset_ifaces
= context.app.interfaces<LoadPresetCommandFactoryList>();
auto cmd = load_preset_ifaces.make(
&LoadPresetCommandFactory::make, proc, *goodPresets[i], context);
if(cmd)
{
CommandDispatcher<> c{context.commandStack};
c.submit(cmd);
}
break;
}
}
};
return ui_btn;
}
void copyProcess(JSONReader& r, const Process::ProcessModel& proc)
{
const auto& ctx = score::IDocument::documentContext(proc);
std::vector<const Process::ProcessModel*> vp{&proc};
std::vector<Path<Process::ProcessModel>> vpath{proc};
// Object is not created here but in SlotHeader
r.obj["PID"] = ossia::get_pid();
r.obj["Document"] = ctx.document.id();
r.obj["Process"] = proc;
r.obj["Cables"] = Process::cablesToCopy(vp, vpath, ctx);
}
}