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  void loadSegments(const std::vector<SegmentModel*>& models);
45 
46  // Here we don't pass an id because it's more efficient
47  void removeSegment(SegmentModel* m);
48 
49  std::vector<SegmentModel*> sortedSegments() const;
50  std::vector<SegmentData> toCurveData() const;
51  void fromCurveData(const std::vector<SegmentData>& curve);
52 
53  Selection selectedChildren() const;
54  void setSelection(const Selection& s);
55 
56  void clear();
57 
58  const auto& segments() const { return m_segments; }
59  auto& segments() { return m_segments; }
60 
61  const std::vector<PointModel*>& points() const;
62  std::vector<PointModel*>& points() { return m_points; }
63 
64  double lastPointPos() const;
65 
66  std::optional<double> valueAt(double x) const noexcept;
67 
68 public:
69  void segmentAdded(const SegmentModel* arg_1)
70  E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, segmentAdded, arg_1)
71  void segmentRemoved(const Id<SegmentModel>& arg_1) E_SIGNAL(
72  SCORE_PLUGIN_CURVE_EXPORT, segmentRemoved,
73  arg_1) // dangerous if async
74  void pointAdded(const Curve::PointModel* arg_1)
75  E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, pointAdded, arg_1)
76  void pointRemoved(const Id<Curve::PointModel>& arg_1) E_SIGNAL(
77  SCORE_PLUGIN_CURVE_EXPORT, pointRemoved,
78  arg_1) // dangerous if async
79 
80  // This signal has to be emitted after big modifications.
81  // (it's an optimization to prevent updating the OSSIA API each time a
82  // segment moves).
83  void changed() E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, changed)
84  void curveReset() E_SIGNAL(
85  SCORE_PLUGIN_CURVE_EXPORT,
86  curveReset) // like changed() but for the presenter
87  void cleared() E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, cleared)
88 
89 private:
90  void addPoint(PointModel* pt);
91  void removePoint(PointModel* pt);
92 
93  PointModel* createStartPoint(SegmentModel* m);
94  PointModel* createEndPoint(SegmentModel* m);
95 
96  IdContainer<SegmentModel> m_segments;
97  std::vector<PointModel*> m_points; // Each between 0, 1
98 };
99 
100 SCORE_PLUGIN_CURVE_EXPORT
101 std::vector<SegmentData> orderedSegments(const Model& curve);
102 
103 struct SCORE_PLUGIN_CURVE_EXPORT CurveDomain
104 {
105  CurveDomain() = default;
106  CurveDomain(const CurveDomain&) = default;
107  CurveDomain(CurveDomain&&) = default;
108  CurveDomain& operator=(const CurveDomain&) = default;
109  CurveDomain& operator=(CurveDomain&&) = default;
110  CurveDomain(const ossia::domain& dom);
111  CurveDomain(const ossia::domain& dom, const ossia::value&);
112  CurveDomain(const ossia::domain& dom, double start, double end);
113  CurveDomain(double start, double end)
114  : min{std::min(start, end)}
115  , max{std::max(start, end)}
116  , start{start}
117  , end{end}
118  {
119  }
120 
121  void refine(const ossia::domain&);
122  void ensureValid();
123 
124  double min = 0;
125  double max = 1;
126  double start = 0;
127  double end = 1;
128 };
129 }
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:104