d/Spline3D/GeneratorDialog.hpp
1 #pragma once
2 #include <Process/Script/ScriptEditor.hpp>
3 
4 #include <ossia/math/math_expression.hpp>
5 
6 #include <QDialog>
7 #include <QDoubleSpinBox>
8 #include <QFormLayout>
9 #include <QHBoxLayout>
10 
11 #include <Spline3D/Commands.hpp>
12 
13 namespace Spline3D
14 {
16 {
17 public:
19  const ProcessModel& model, const score::DocumentContext& ctx, QWidget* parent)
20  : Process::ScriptDialog{"exprtk", ctx, parent}
21  , m_model{model}
22  {
23  auto step = new QDoubleSpinBox{this};
24  step->setRange(0.0001, 0.3);
25  step->setValue(m_step);
26  step->setSingleStep(0.001);
27  step->setDecimals(8);
28  //step->setStepType(QSpinBox::AdaptiveDecimalStepType);
29  auto lay = static_cast<QBoxLayout*>(this->layout());
30  auto controls = new QFormLayout;
31  controls->addRow("Step (smaller is more precise)", step);
32  lay->insertLayout(2, controls);
33  connect(
34  step, qOverload<double>(&QDoubleSpinBox::valueChanged), this,
35  [this](double step) { m_step = step; });
36 
37  expr.add_variable("t", t);
38  expr.add_variable("x", x);
39  expr.add_variable("y", y);
40  expr.add_variable("z", z);
41  expr.add_constants();
42  expr.register_symbol_table();
43 
44  setText(R"_(x := cos(2 * pi * t);
45 y := sin(2 * pi * t);
46 z := sin(7 * pi * t);
47 )_");
48  }
49 
50  void on_accepted() override
51  {
52  this->setError(0, QString{});
53  auto txt = this->text().toStdString();
54  bool ok = expr.set_expression(txt);
55  if(!ok)
56  {
57  setError(0, QString::fromStdString(expr.error()));
58  return;
59  }
60  else
61  {
62  ossia::spline3d_data data;
63  for(t = 0.; t < 1.; t += m_step)
64  {
65  expr.value();
66  data.points.push_back({x, y, z});
67  }
68  {
69  t = 1.;
70  expr.value();
71  data.points.push_back({x, y, z});
72  }
73 
74  CommandDispatcher<>{m_context.commandStack}.submit<ChangeSpline>(
75  m_model, std::move(data));
76  }
77  }
78  double t{}, x{}, y{}, z{};
79  double m_step{0.03};
80  ossia::math_expression expr;
81 
82  const ProcessModel& m_model;
83 };
84 
85 }
The CommandDispatcher class.
Definition: CommandDispatcher.hpp:13
Definition: ScriptEditor.hpp:21
Definition: plugins/score-plugin-spline3d/Spline3D/commands.hpp:13
Definition: d/Spline3D/GeneratorDialog.hpp:16
Definition: score-plugin-spline3d/Spline3D/Model.hpp:19
Definition: DocumentContext.hpp:18