PdLayer.hpp
1 #pragma once
2 #include <Process/GenericProcessFactory.hpp>
3 #include <Process/WidgetLayer/WidgetProcessFactory.hpp>
4 
5 #include <Control/DefaultEffectItem.hpp>
6 #include <Effect/EffectFactory.hpp>
7 #include <Pd/Inspector/PdInspectorWidget.hpp>
8 #include <Pd/PdProcess.hpp>
9 
10 #include <QFileInfo>
11 #include <QProcess>
12 
13 #include <z_libpd.h>
14 namespace Pd
15 {
16 /*
17 struct UiWrapper : public QWidget
18 {
19  QPointer<const ProcessModel> m_model;
20  QProcess m_process;
21  UiWrapper(
22  const ProcessModel& proc,
23  const score::DocumentContext& ctx,
24  QWidget* parent)
25  : m_model{&proc}
26  {
27  setGeometry(0, 0, 0, 0);
28 
29  connect(
30  &m_process,
31  qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
32  this,
33  &QWidget::deleteLater);
34  connect(
35  &proc,
36  &IdentifiedObjectAbstract::identified_object_destroying,
37  this,
38  &QWidget::deleteLater);
39  const auto& bin = locatePdBinary();
40  if (!bin.isEmpty())
41  {
42  m_process.start(bin, {proc.script()});
43  }
44  }
45 
46  void closeEvent(QCloseEvent* event) override
47  {
48  QPointer<UiWrapper> p(this);
49  if (m_model)
50  {
51  const_cast<QWidget*&>(m_model->externalUI) = nullptr;
52  m_model->externalUIVisible(false);
53  }
54  if (p)
55  QWidget::closeEvent(event);
56  }
57 
58  ~UiWrapper()
59  {
60  if (m_model)
61  {
62  const_cast<QWidget*&>(m_model->externalUI) = nullptr;
63  m_model->externalUIVisible(false);
64  }
65  m_process.terminate();
66  }
67 };
68 */
69 struct UiWrapper : public QWidget
70 {
71  QPointer<const ProcessModel> m_model;
72  std::shared_ptr<Pd::Instance> m_instance;
73  UiWrapper(const ProcessModel& proc, const score::DocumentContext& ctx, QWidget* parent)
74  : m_model{&proc}
75  , m_instance{proc.m_instance}
76  {
77  setGeometry(0, 0, 0, 0);
78 
79  connect(
81  &QWidget::deleteLater);
82  const auto& bin = locatePdBinary();
83  if(!bin.isEmpty())
84  {
85  libpd_set_instance(m_instance->instance);
86  libpd_start_gui(locatePdResourceFolder().toUtf8().constData());
87  m_instance->ui_open = true;
88  }
89  startTimer(8);
90  }
91 
92  QString locatePdResourceFolder() noexcept
93  {
94 #if defined(__linux__)
95  for(auto path :
96  {"/usr/lib64/puredata", "/usr/lib64/pd", "/usr/lib/puredata", "/usr/lib/pd"})
97  if(QFile::exists(path))
98  return path;
99 #endif
100  return QFileInfo{locatePdBinary()}.absolutePath();
101  }
102 
103  void closeEvent(QCloseEvent* event) override
104  {
105  QPointer<UiWrapper> p(this);
106 
107  if(m_instance->ui_open)
108  {
109  m_instance->ui_open = false;
110  libpd_set_instance(m_instance->instance);
111  libpd_stop_gui();
112  }
113 
114  if(m_model)
115  {
116  const_cast<QWidget*&>(m_model->externalUI) = nullptr;
117  m_model->externalUIVisible(false);
118  }
119 
120  if(p)
121  {
122  QWidget::closeEvent(event);
123  }
124  }
125 
126  ~UiWrapper()
127  {
128  if(m_instance->ui_open)
129  {
130  m_instance->ui_open = false;
131  libpd_set_instance(m_instance->instance);
132  libpd_stop_gui();
133  }
134 
135  if(m_model)
136  {
137  const_cast<QWidget*&>(m_model->externalUI) = nullptr;
138  m_model->externalUIVisible(false);
139  }
140  }
141 
142  void timerEvent(QTimerEvent* event) override
143  {
144  libpd_set_instance(m_instance->instance);
145  libpd_poll_gui();
146  }
147 };
148 
151 }
void identified_object_destroying(IdentifiedObjectAbstract *o)
To be called by subclasses.
Definition: PdProcess.hpp:40
Definition: DefaultEffectItem.hpp:26
Definition: EffectFactory.hpp:66
Definition: PdLayer.hpp:70
Definition: DocumentContext.hpp:18