Spline/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 <Spline/Commands.hpp>
12 
13 namespace Spline
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_constants();
41  expr.register_symbol_table();
42 
43  setText(R"_(x := cos(2 * pi * t);
44 y := sin(2 * pi * t);
45 )_");
46  }
47 
48  void on_accepted() override
49  {
50  this->setError(0, QString{});
51  auto txt = this->text().toStdString();
52  bool ok = expr.set_expression(txt);
53  if(!ok)
54  {
55  setError(0, QString::fromStdString(expr.error()));
56  return;
57  }
58  else
59  {
60  ossia::spline_data data;
61  for(t = 0.; t < 1.; t += m_step)
62  {
63  expr.value();
64  data.points.push_back({x, y});
65  }
66  {
67  t = 1.;
68  expr.value();
69  data.points.push_back({x, y});
70  }
71 
72  CommandDispatcher<>{m_context.commandStack}.submit<ChangeSpline>(
73  m_model, std::move(data));
74  }
75  }
76  double t{}, x{}, y{};
77  double m_step{0.03};
78  ossia::math_expression expr;
79 
80  const ProcessModel& m_model;
81 };
82 
83 }
The CommandDispatcher class.
Definition: CommandDispatcher.hpp:13
Definition: ScriptEditor.hpp:21
Definition: plugins/score-plugin-spline/Spline/commands.hpp:13
Definition: Spline/GeneratorDialog.hpp:16
Definition: score-plugin-spline/Spline/Model.hpp:19
Definition: DocumentContext.hpp:18