Loading...
Searching...
No Matches
Selection.hpp
1#pragma once
2#include <score/model/IdentifiedObjectAbstract.hpp>
3
4#include <QPointer>
5
6#include <verdigris>
7
11class Selection final : private QList<QPointer<IdentifiedObjectAbstract>>
12{
13 using base_type = QList<QPointer<IdentifiedObjectAbstract>>;
14
15public:
16 using base_type::at;
17 using base_type::base_type;
18 using base_type::begin;
19 using base_type::cbegin;
20 using base_type::cend;
21 using base_type::clear;
22 using base_type::constBegin;
23 using base_type::constEnd;
24 using base_type::contains;
25 using base_type::empty;
26 using base_type::end;
27 using base_type::erase;
28 using base_type::removeAll;
29 using base_type::size;
30
31 static Selection fromList(const QList<IdentifiedObjectAbstract*>& other)
32 {
33 Selection s;
34 for(auto elt : other)
35 {
36 s.base_type::append(elt);
37 }
38 return s;
39 }
40
41 bool contains(const IdentifiedObjectAbstract& obj) const noexcept
42 {
43 for(const auto& ptr : *this)
44 {
45 if(ptr.data() == &obj)
46 return true;
47 }
48 return false;
49 }
50
51 bool contains(const IdentifiedObjectAbstract* obj) const noexcept
52 {
53 for(const auto& ptr : *this)
54 {
55 if(ptr.data() == obj)
56 return true;
57 }
58 return false;
59 }
60
61 void append(const IdentifiedObjectAbstract& obj) { append(&obj); }
62
63 void append(const IdentifiedObjectAbstract* obj)
64 {
65 auto ptr = const_cast<IdentifiedObjectAbstract*>(obj);
66 if(!contains(ptr))
67 base_type::append(ptr);
68 }
69
70 void append(const base_type::value_type& obj)
71 {
72 if(!contains(obj))
73 base_type::append(obj);
74 }
75
76 void append(base_type::value_type&& obj)
77 {
78 if(!contains(obj))
79 base_type::append(std::move(obj));
80 }
81
82 bool operator==(const Selection& other) const
83 {
84 return static_cast<const base_type&>(*this) == static_cast<const base_type&>(other);
85 }
86
87 bool operator!=(const Selection& other) const
88 {
89 return static_cast<const base_type&>(*this) != static_cast<const base_type&>(other);
90 }
91
92 QList<const IdentifiedObjectAbstract*> toList() const
93 {
94 QList<const IdentifiedObjectAbstract*> l;
95 for(const auto& elt : *this)
96 l.push_back(elt);
97 return l;
98 }
99
100 SCORE_LIB_BASE_EXPORT
101 void removeDuplicates();
102};
103
104template <typename T>
118Selection filterSelections(T* pressedModel, Selection sel, bool cumulation)
119{
120 if(!cumulation)
121 {
122 sel.clear();
123 }
124
125 const auto ptr = const_cast<std::remove_const_t<T>*>(pressedModel);
126 // If the pressed element is selected
127 if(pressedModel->selection.get())
128 {
129 if(cumulation)
130 {
131 sel.removeAll(ptr);
132 }
133 else
134 {
135 sel.append(ptr);
136 }
137 }
138 else
139 {
140 sel.append(ptr);
141 }
142
143 return sel;
144}
145
152inline Selection filterSelections(
153 Selection& newSelection, const Selection& currentSelection, bool cumulation)
154{
155 if(cumulation)
156 {
157 for(auto& elt : currentSelection)
158 newSelection.append(elt);
159 }
160
161 return newSelection;
162}
163
164inline QDataStream& operator<<(QDataStream& i, const Selection& sel)
165{
166 return i;
167}
168inline QDataStream& operator>>(QDataStream& i, Selection& sel)
169{
170 return i;
171}
172Q_DECLARE_METATYPE(Selection)
173W_REGISTER_ARGTYPE(Selection)
Base class for IdentifiedObject.
Definition IdentifiedObjectAbstract.hpp:16
Definition Selection.hpp:12