Loading...
Searching...
No Matches
CurveSegmentData.hpp
1#pragma once
2#include <Curve/Palette/CurvePoint.hpp>
3
4#include <score/model/IdentifiedObject.hpp>
5#include <score/plugins/UuidKey.hpp>
6#include <score/serialization/DataStreamFwd.hpp>
7#include <score/tools/IdentifierGeneration.hpp>
8
9#include <ossia/detail/algorithms.hpp>
10
11#include <QVariant>
12
13namespace Curve
14{
15class SegmentFactory;
16class SegmentModel;
17class Category_k;
18struct SegmentData;
19/* TODO it would maybe faster to have them on the heap and use QPointer for
20 * caching ...
21class SegmentId
22{
23 public:
24 using value_type = std::optional<int32_t>;
25 explicit SegmentId() = default;
26 explicit SegmentId(value_type val) : m_id {val} { }
27 SegmentId(const SegmentId&) = delete;
28 SegmentId(SegmentId&& ) = delete;
29 SegmentId& operator=(const SegmentId&) = delete;
30 SegmentId& operator=(SegmentId&& ) = delete;
31
32 friend bool operator== (const id_base_t& lhs, const id_base_t& rhs)
33 { return lhs.m_id == rhs.m_id; }
34
35 friend bool operator!= (const id_base_t& lhs, const id_base_t& rhs)
36 { return lhs.m_id != rhs.m_id; }
37
38 friend bool operator< (const id_base_t& lhs, const id_base_t& rhs)
39 { return *lhs.val() < *rhs.val(); }
40
41 explicit operator bool() const
42 { return bool(m_id); }
43
44 explicit operator value_type() const
45 { return m_id; }
46
47 const value_type& val() const
48 { return m_id; }
49
50 void setVal(value_type&& val)
51 { m_id = val; }
52
53 void unset()
54 { m_id = value_type(); }
55
56 private:
57 mutable CurveSegmentData* m_ptr{};
58 value_type m_id {};
59};*/
60
61// An object wrapper useful for saving / loading
63{
64 SegmentData() = default;
65 SegmentData(const SegmentData&) = default;
66 SegmentData(SegmentData&&) = default;
67 SegmentData& operator=(const SegmentData&) = default;
68 SegmentData& operator=(SegmentData&&) = default;
69
71 Id<SegmentModel> i, Curve::Point s, Curve::Point e, OptionalId<SegmentModel> prev,
72 OptionalId<SegmentModel> foll, const UuidKey<Curve::SegmentFactory>& t,
73 QVariant data)
74 : id(std::move(i))
75 , start(s)
76 , end(e)
77 , previous(std::move(prev))
78 , following(std::move(foll))
79 , type(t)
80 , specificSegmentData(std::move(data))
81 {
82 }
83
85
86 Curve::Point start, end;
87 OptionalId<SegmentModel> previous, following;
88
90 QVariant specificSegmentData;
91
92 double x() const { return start.x(); }
93 bool operator==(const SegmentData& other) = delete;
94 bool operator!=(const SegmentData& other) = delete;
95};
96
97inline bool operator<(const SegmentData& lhs, const SegmentData& rhs)
98{
99 return lhs.x() < rhs.x();
100}
101
102inline bool operator<=(const SegmentData& lhs, const SegmentData& rhs)
103{
104 return lhs.x() <= rhs.x();
105}
106
107// REFACTORME with SettableIdentifierGeneration...
108template <typename Container>
109Id<SegmentModel> getSegmentId(const Container& ids)
110{
111 Id<SegmentModel> id{};
112 auto end = ids.end();
113 do
114 {
116 } while(ids.find(id) != end);
117
118 return id;
119}
120
121inline Id<SegmentModel> getSegmentId(const std::vector<SegmentData>& ids)
122{
123 Id<SegmentModel> id{};
124
125 auto end = ids.end();
126 do
127 {
129 } while(ossia::find_if(ids, [&](const auto& other) { return other.id == id; }) != end);
130
131 return id;
132}
133
134inline Id<SegmentModel> getSegmentId(const std::vector<Id<SegmentModel>>& ids)
135{
136 Id<SegmentModel> id{};
137
138 auto end = ids.end();
139 do
140 {
142 } while(ossia::find_if(ids, [&](const auto& other) { return other == id; }) != end);
143
144 return id;
145}
146
147// We don't want crashes on invalid ids search
149{
150public:
151 std::size_t operator()(const Id<SegmentModel>& id) const { return id.val(); }
152};
153
154enum Segments
155{
156 Hashed
157};
158}
159
160SCORE_SERIALIZE_DATASTREAM_DECLARE(, Curve::SegmentData)
161Q_DECLARE_METATYPE(Curve::SegmentData)
162W_REGISTER_ARGTYPE(Curve::SegmentData)
163
164#define CURVE_SEGMENT_FACTORY_METADATA(Export, Model, Uuid) \
165 template <> \
166 struct Export Metadata<ConcreteKey_k, Model> \
167 { \
168 static const auto& get() \
169 { \
170 static const UuidKey<Curve::SegmentFactory> k{Uuid}; \
171 return k; \
172 } \
173 };
174
175#define CURVE_SEGMENT_METADATA(Export, Model, Uuid, ObjectKey, PrettyName, Category) \
176 OBJECTKEY_METADATA(Export, Model, ObjectKey) \
177 CURVE_SEGMENT_FACTORY_METADATA(Export, Model, Uuid) \
178 template <> \
179 struct Export Metadata<PrettyName_k, Model> \
180 { \
181 static auto get() { return QObject::tr(PrettyName); } \
182 }; \
183 template <> \
184 struct Export Metadata<Curve::Category_k, Model> \
185 { \
186 static auto get() { return QObject::tr(Category); } \
187 };
Metadata to categorize objects: curves, audio, etc.
Definition CurveSegmentData.hpp:149
Definition UuidKey.hpp:344
The id_base_t class.
Definition Identifier.hpp:59
Utilities and base classes for 1D curves.
Definition FocusDispatcher.hpp:12
Definition CurveSegmentData.hpp:63
static int32_t getRandomId()
getNextId
Definition IdentifierGeneration.cpp:38