CurveModel.hpp
1 #pragma once
2 #include <Curve/Segment/CurveSegmentModel.hpp>
3 
4 #include <score/model/IdentifiedObject.hpp>
5 #include <score/model/IdentifiedObjectMap.hpp>
6 #include <score/model/Identifier.hpp>
7 #include <score/serialization/VisitorInterface.hpp>
8 
9 #include <score_plugin_curve_export.h>
10 
11 #include <vector>
12 #include <verdigris>
13 
14 class Selection;
15 namespace ossia
16 {
17 struct domain;
18 class value;
19 }
20 namespace Curve
21 {
22 class PointModel;
23 struct SegmentData;
24 class SCORE_PLUGIN_CURVE_EXPORT Model final : public IdentifiedObject<Model>
25 {
26  SCORE_SERIALIZE_FRIENDS
27  W_OBJECT(Model)
28 public:
29  Model(const Id<Model>&, QObject* parent);
30 
31  template <typename Impl>
32  Model(Impl& vis, QObject* parent)
33  : IdentifiedObject{vis, parent}
34  {
35  vis.writeTo(*this);
36  }
37 
38  // These two will create points
39  void addSegment(SegmentModel* m);
40  void addSortedSegment(SegmentModel* m);
41 
42  // Won't create points, plain insertion.
43  void insertSegment(SegmentModel*);
44 
45  // Here we don't pass an id because it's more efficient
46  void removeSegment(SegmentModel* m);
47 
48  std::vector<SegmentModel*> sortedSegments() const;
49  std::vector<SegmentData> toCurveData() const;
50  void fromCurveData(const std::vector<SegmentData>& curve);
51 
52  Selection selectedChildren() const;
53  void setSelection(const Selection& s);
54 
55  void clear();
56 
57  const auto& segments() const { return m_segments; }
58  auto& segments() { return m_segments; }
59 
60  const std::vector<PointModel*>& points() const;
61  std::vector<PointModel*>& points() { return m_points; }
62 
63  double lastPointPos() const;
64 
65  std::optional<double> valueAt(double x) const noexcept;
66 
67 public:
68  void segmentAdded(const SegmentModel* arg_1)
69  E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, segmentAdded, arg_1)
70  void segmentRemoved(const Id<SegmentModel>& arg_1) E_SIGNAL(
71  SCORE_PLUGIN_CURVE_EXPORT, segmentRemoved,
72  arg_1) // dangerous if async
73  void pointAdded(const Curve::PointModel* arg_1)
74  E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, pointAdded, arg_1)
75  void pointRemoved(const Id<Curve::PointModel>& arg_1) E_SIGNAL(
76  SCORE_PLUGIN_CURVE_EXPORT, pointRemoved,
77  arg_1) // dangerous if async
78 
79  // This signal has to be emitted after big modifications.
80  // (it's an optimization to prevent updating the OSSIA API each time a
81  // segment moves).
82  void changed() E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, changed)
83  void curveReset() E_SIGNAL(
84  SCORE_PLUGIN_CURVE_EXPORT,
85  curveReset) // like changed() but for the presenter
86  void cleared() E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, cleared)
87 
88 private:
89  void addPoint(PointModel* pt);
90  void removePoint(PointModel* pt);
91 
92  IdContainer<SegmentModel> m_segments;
93  std::vector<PointModel*> m_points; // Each between 0, 1
94 };
95 
96 SCORE_PLUGIN_CURVE_EXPORT
97 std::vector<SegmentData> orderedSegments(const Model& curve);
98 
99 struct SCORE_PLUGIN_CURVE_EXPORT CurveDomain
100 {
101  CurveDomain() = default;
102  CurveDomain(const CurveDomain&) = default;
103  CurveDomain(CurveDomain&&) = default;
104  CurveDomain& operator=(const CurveDomain&) = default;
105  CurveDomain& operator=(CurveDomain&&) = default;
106  CurveDomain(const ossia::domain& dom);
107  CurveDomain(const ossia::domain& dom, const ossia::value&);
108  CurveDomain(const ossia::domain& dom, double start, double end);
109  CurveDomain(double start, double end)
110  : min{std::min(start, end)}
111  , max{std::max(start, end)}
112  , start{start}
113  , end{end}
114  {
115  }
116 
117  void refine(const ossia::domain&);
118  void ensureValid();
119 
120  double min = 0;
121  double max = 1;
122  double start = 0;
123  double end = 1;
124 };
125 }
Definition: CurveModel.hpp:25
Definition: CurvePointModel.hpp:18
Definition: CurveSegmentModel.hpp:32
A map to access child objects through their id.
Definition: IdentifiedObjectMap.hpp:16
The IdentifiedObject class.
Definition: IdentifiedObject.hpp:19
Definition: Selection.hpp:12
The id_base_t class.
Definition: Identifier.hpp:57
Utilities and base classes for 1D curves.
Definition: FocusDispatcher.hpp:12
Definition: CurveModel.hpp:100