MuseScore  3.4
Music composition and notation
segment.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2002-2011 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //=============================================================================
12 
13 #ifndef __SEGMENT_H__
14 #define __SEGMENT_H__
15 
16 #include "element.h"
17 #include "shape.h"
18 #include "mscore.h"
19 
20 namespace Ms {
21 
22 class Measure;
23 class Segment;
24 class ChordRest;
25 class Spanner;
26 class System;
27 
28 //------------------------------------------------------------------------
29 // @@ Segment
30 // A segment holds all vertical aligned staff elements.
31 // Segments are typed and contain only Elements of the same type.
32 //
33 // All Elements in a segment start at the same tick. The Segment can store one Element for
34 // each voice in each staff in the score.
35 // Some elements (Clef, KeySig, TimeSig etc.) are assumed to always have voice zero
36 // and can be found in _elist[staffIdx * VOICES];
37 
38 // Segments are children of Measures and store Clefs, KeySigs, TimeSigs,
39 // BarLines and ChordRests.
40 //
41 // @P annotations array[Element] the list of annotations (read only)
42 // @P next Segment the next segment in the whole score; null at last score segment (read-only)
43 // @P nextInMeasure Segment the next segment in measure; null at last measure segment (read-only)
44 // @P prev Segment the previous segment in the whole score; null at first score segment (read-only)
45 // @P prevInMeasure Segment the previous segment in measure; null at first measure segment (read-only)
46 // @P segmentType enum (Segment.All, .Ambitus, .BarLine, .Breath, .ChordRest, .Clef, .EndBarLine, .Invalid, .KeySig, .KeySigAnnounce, .StartRepeatBarLine, .TimeSig, .TimeSigAnnounce)
47 // @P tick int midi tick position (read only)
48 //------------------------------------------------------------------------
49 
50 class Segment final : public Element {
52  Fraction _tick; // { Fraction(0, 1) };
53  Fraction _ticks; // { Fraction(0, 1) };
55  qreal _stretch;
56 
57  Segment* _next = nullptr; // linked list of segments inside a measure
58  Segment* _prev = nullptr;
59 
60  std::vector<Element*> _annotations;
61  std::vector<Element*> _elist; // Element storage, size = staves * VOICES.
62  std::vector<Shape> _shapes; // size = staves
63  std::vector<qreal> _dotPosX; // size = staves
64 
65 
66  void init();
67  void checkEmpty() const;
68  void checkElement(Element*, int track);
69  void setEmpty(bool val) const { setFlag(ElementFlag::EMPTY, val); }
70 
71  protected:
72  Element* getElement(int staff); //??
73 
74  public:
75  Segment(Measure* m = 0);
77  Segment(const Segment&);
78  ~Segment();
79 
80  virtual Segment* clone() const { return new Segment(*this); }
81  virtual ElementType type() const { return ElementType::SEGMENT; }
82 
83  virtual void setScore(Score*) override;
84 
85  Segment* next() const { return _next; }
86  Segment* next(SegmentType) const;
87  Segment* nextActive() const;
88  Segment* nextEnabled() const;
89  void setNext(Segment* e) { _next = e; }
90 
91  Segment* prev() const { return _prev; }
92  Segment* prev(SegmentType) const;
93  Segment* prevActive() const;
94  Segment* prevEnabled() const;
95  void setPrev(Segment* e) { _prev = e; }
96 
97  // don’t stop at measure boundary:
98  Segment* next1() const;
99  Segment* next1enabled() const;
100  Segment* next1MM() const;
101  Segment* next1MMenabled() const;
102  Segment* next1(SegmentType) const;
103  Segment* next1MM(SegmentType) const;
104 
105  Segment* prev1() const;
106  Segment* prev1enabled() const;
107  Segment* prev1MM() const;
108  Segment* prev1MMenabled() const;
109  Segment* prev1(SegmentType) const;
110  Segment* prev1MM(SegmentType) const;
111 
112  Segment* nextCR(int track = -1, bool sameStaff = false) const;
113 
114  ChordRest* nextChordRest(int track, bool backwards = false) const;
115 
116  Element* element(int track) const { return _elist[track]; }
117 
118  // a variant of the above function, specifically designed to be called from QML
119  //@ returns the element at track 'track' (null if none)
120  Ms::Element* elementAt(int track) const;
121 
122  const std::vector<Element*>& elist() const { return _elist; }
123  std::vector<Element*>& elist() { return _elist; }
124 
125  void removeElement(int track);
126  void setElement(int track, Element* el);
127  virtual void scanElements(void* data, void (*func)(void*, Element*), bool all=true);
128 
129  Measure* measure() const { return toMeasure(parent()); }
130  System* system() const { return toSystem(parent()->parent()); }
131  qreal x() const { return ipos().x(); }
132  void setX(qreal v) { rxpos() = v; }
133 
134  void insertStaff(int staff);
135  void removeStaff(int staff);
136 
137  virtual void add(Element*);
138  virtual void remove(Element*);
139  void swapElements(int i1, int i2);
140 
141  void sortStaves(QList<int>& dst);
142  const char* subTypeName() const;
143 
144  static const char* subTypeName(SegmentType);
146 
148  void setSegmentType(SegmentType t);
149 
150  bool empty() const { return flag(ElementFlag::EMPTY); }
151  bool written() const { return flag(ElementFlag::WRITTEN); }
152  void setWritten(bool val) const { setFlag(ElementFlag::WRITTEN, val); }
153 
154  void fixStaffIdx();
155 
156  qreal stretch() const { return _stretch; }
157  void setStretch(qreal v) { _stretch = v; }
158 
159  virtual Fraction rtick() const override { return _tick; }
160  void setRtick(const Fraction& v) { Q_ASSERT(v >= Fraction(0,1)); _tick = v; }
161  virtual Fraction tick() const override;
162 
163  Fraction ticks() const { return _ticks; }
164  void setTicks(const Fraction& v) { _ticks = v; }
165 
166  bool splitsTuplet() const;
167 
168  const std::vector<Element*>& annotations() const { return _annotations; }
169  void clearAnnotations();
170  void removeAnnotation(Element* e);
171  bool hasAnnotationOrElement(ElementType type, int minTrack, int maxTrack) const;
172  Element* findAnnotation(ElementType type, int minTrack, int maxTrack);
173  std::vector<Element*> findAnnotations(ElementType type, int minTrack, int maxTrack);
174  bool hasElements() const;
175 
176 
177  qreal dotPosX(int staffIdx) const { return _dotPosX[staffIdx]; }
178  void setDotPosX(int staffIdx, qreal val) { _dotPosX[staffIdx] = val; }
179 
181  void setExtraLeadingSpace(Spatium v) { _extraLeadingSpace = v; }
182 
183  virtual void write(XmlWriter&) const;
184  virtual void read(XmlReader&);
185 
186  virtual QVariant getProperty(Pid propertyId) const;
187  virtual bool setProperty(Pid propertyId, const QVariant&);
188  virtual QVariant propertyDefault(Pid) const;
189 
190  bool operator<(const Segment&) const;
191  bool operator>(const Segment&) const;
192 
193  virtual QString accessibleExtraInfo() const override;
194 
195  Element* firstInNextSegments(int activeStaff); //<
196  Element* lastInPrevSegments(int activeStaff); //<
197  Element* firstElement(int staff); //< These methods are used for navigation
198  Element* lastElement(int staff); //< for next-element and prev-element
199  Element* firstElementOfSegment(Segment* s, int activeStaff);
200  Element* nextElementOfSegment(Segment* s, Element* e, int activeStaff);
201  Element* prevElementOfSegment(Segment* s, Element* e, int activeStaff);
202  Element* lastElementOfSegment(Segment* s, int activeStaff);
205  Element* firstAnnotation(Segment* s, int activeStaff);
206  Element* lastAnnotation(Segment* s, int activeStaff);
207  Spanner* firstSpanner(int activeStaff);
208  Spanner* lastSpanner(int activeStaff);
209  bool notChordRestType(Segment* s);
210  using Element::nextElement;
211  Element* nextElement(int activeStaff);
212  using Element::prevElement;
213  Element* prevElement(int activeStaff);
214 
215  std::vector<Shape> shapes() { return _shapes; }
216  const std::vector<Shape>& shapes() const { return _shapes; }
217  const Shape& staffShape(int staffIdx) const { return _shapes[staffIdx]; }
218  Shape& staffShape(int staffIdx) { return _shapes[staffIdx]; }
219  void createShapes();
220  void createShape(int staffIdx);
221  qreal minRight() const;
222  qreal minLeft(const Shape&) const;
223  qreal minLeft() const;
224  qreal minHorizontalDistance(Segment*, bool isSystemGap) const;
225  qreal minHorizontalCollidingDistance(Segment* ns) const;
226 
227  // some helper function
228  ChordRest* cr(int track) const { return toChordRest(_elist[track]); }
229  bool isType(const SegmentType t) const{ return int(_segmentType) & int(t); }
231  bool isClefType() const { return _segmentType == SegmentType::Clef; }
233  bool isKeySigType() const { return _segmentType == SegmentType::KeySig; }
234  bool isAmbitusType() const { return _segmentType == SegmentType::Ambitus; }
235  bool isTimeSigType() const { return _segmentType == SegmentType::TimeSig; }
237  bool isBarLineType() const { return _segmentType == SegmentType::BarLine; }
238  bool isBreathType() const { return _segmentType == SegmentType::Breath; }
243  };
244 
245 //---------------------------------------------------------
246 // nextActive
247 //---------------------------------------------------------
248 
250  {
251  Segment* ns = next();
252  while (ns && !(ns->enabled() && ns->visible()))
253  ns = ns->next();
254  return ns;
255  }
256 
257 //---------------------------------------------------------
258 // nextEnabled
259 //---------------------------------------------------------
260 
262  {
263  Segment* ns = next();
264  while (ns && !ns->enabled())
265  ns = ns->next();
266  return ns;
267  }
268 
269 //---------------------------------------------------------
270 // prevActive
271 //---------------------------------------------------------
272 
274  {
275  Segment* ps = prev();
276  while (ps && !(ps->enabled() && ps->visible()))
277  ps = ps->prev();
278  return ps;
279  }
280 
281 //---------------------------------------------------------
282 // prevEnabled
283 //---------------------------------------------------------
284 
286  {
287  Segment* ps = prev();
288  while (ps && !ps->enabled())
289  ps = ps->prev();
290  return ps;
291  }
292 
293 } // namespace Ms
294 
295 Q_DECLARE_METATYPE(Ms::SegmentType);
296 
297 #endif
298 
std::vector< Shape > shapes()
Definition: segment.h:215
virtual Element * prevElement()
Definition: element.cpp:1618
virtual void read(XmlReader &)
Definition: segment.cpp:843
virtual void scanElements(void *data, void(*func)(void *, Element *), bool all=true)
Definition: segment.cpp:1068
Staff * staff() const
Definition: element.cpp:254
Element * lastInPrevSegments(int activeStaff)
Definition: segment.cpp:1798
Segment * prev1enabled() const
Definition: segment.cpp:323
Segment * next1() const
return next Segment, don’t stop searching at end of Measure
Definition: segment.cpp:223
void removeElement(int track)
Definition: segment.cpp:95
bool isKeySigAnnounceType() const
Definition: segment.h:241
qreal minHorizontalDistance(Segment *, bool isSystemGap) const
Definition: segment.cpp:2052
qreal _stretch
Definition: segment.h:55
Segment * nextEnabled() const
Definition: segment.h:261
Pid
Definition: property.h:62
Definition: xml.h:67
void removeAnnotation(Element *e)
Definition: segment.cpp:1033
void setStretch(qreal v)
Definition: segment.h:157
int staffIdx() const
Definition: element.h:322
const Shape & staffShape(int staffIdx) const
Definition: segment.h:217
Fraction ticks() const
Definition: segment.h:163
void sortStaves(QList< int > &dst)
Definition: segment.cpp:753
void clearAnnotations()
Definition: segment.cpp:1047
Element * lastElement(int staff)
Definition: segment.cpp:1116
Segment * prevEnabled() const
Definition: segment.h:285
void init()
Definition: segment.cpp:199
bool isChordRestType() const
Definition: segment.h:239
Element * element(int track) const
Definition: segment.h:116
virtual ElementType type() const
Definition: segment.h:81
Virtual base class for slurs, ties, lines etc.
Definition: spanner.h:136
Element * firstInNextSegments(int activeStaff)
Definition: segment.cpp:1246
virtual Segment * clone() const
Definition: segment.h:80
one measure in a system
Definition: measure.h:65
Fraction _tick
Definition: segment.h:52
Element * getElement(int staff)
Definition: segment.cpp:1148
bool isStartRepeatBarLineType() const
Definition: segment.h:236
Segment * _prev
Definition: segment.h:58
Shape & staffShape(int staffIdx)
Definition: segment.h:218
bool isBreathType() const
Definition: segment.h:238
std::vector< Element * > _annotations
Definition: segment.h:60
virtual void write(XmlWriter &) const
Definition: segment.cpp:827
Segment(Measure *m=0)
Definition: segment.cpp:113
bool isAmbitusType() const
Definition: segment.h:234
Base class of score layout elements.
Definition: element.h:158
void swapElements(int i1, int i2)
Definition: segment.cpp:813
void setTicks(const Fraction &v)
Definition: segment.h:164
Segment * next() const
Definition: segment.h:85
Element * firstAnnotation(Segment *s, int activeStaff)
Definition: segment.cpp:1212
bool isTimeSigAnnounceType() const
Definition: segment.h:242
int track() const
Definition: element.h:316
bool isHeaderClefType() const
Definition: segment.h:232
qreal minRight() const
Definition: segment.cpp:1994
qreal & rxpos()
Definition: element.h:236
qreal x() const
Definition: segment.h:131
Element * parent() const
Definition: element.h:192
virtual bool setProperty(Pid propertyId, const QVariant &)
Definition: segment.cpp:893
bool written() const
Definition: segment.h:151
void setNext(Segment *e)
Definition: segment.h:89
Element * prevAnnotation(Element *e)
Definition: segment.cpp:1193
Definition: score.h:391
virtual Fraction rtick() const override
Definition: segment.h:159
Element * lastElementOfSegment(Segment *s, int activeStaff)
Definition: segment.cpp:1395
Measure * measure() const
Definition: segment.h:129
qreal dotPosX(int staffIdx) const
Definition: segment.h:177
Element * nextAnnotation(Element *e)
Definition: segment.cpp:1173
virtual QVariant getProperty(Pid propertyId) const
Definition: segment.cpp:863
~Segment()
Definition: segment.cpp:183
System * system() const
Definition: segment.h:130
const std::vector< Element * > & annotations() const
Definition: segment.h:168
void removeStaff(int staff)
Definition: segment.cpp:433
const std::vector< Shape > & shapes() const
Definition: segment.h:216
void setEmpty(bool val) const
Definition: segment.h:69
bool isBarLineType() const
Definition: segment.h:237
Segment * next1enabled() const
Definition: segment.cpp:235
bool isBeginBarLineType() const
Definition: segment.h:230
bool splitsTuplet() const
Definition: segment.cpp:917
std::vector< qreal > _dotPosX
Definition: segment.h:63
Element * firstElementOfSegment(Segment *s, int activeStaff)
Definition: segment.cpp:1273
virtual Pid propertyId(const QStringRef &xmlName) const override
Definition: element.cpp:1326
void setFlag(ElementFlag f, bool v)
Definition: element.h:203
SegmentType
Definition: types.h:333
Segment * next1MM() const
Definition: segment.cpp:247
void setX(qreal v)
Definition: segment.h:132
bool isEndBarLineType() const
Definition: segment.h:240
ChordRest * nextChordRest(int track, bool backwards=false) const
Definition: segment.cpp:399
void createShapes()
Definition: segment.cpp:1899
virtual Fraction tick() const override
Definition: segment.cpp:212
qreal minLeft() const
Definition: segment.cpp:2021
Spanner * firstSpanner(int activeStaff)
Definition: segment.cpp:1420
Segment * prev1() const
return previous Segment, don’t stop searching at Measure begin
Definition: segment.cpp:315
bool visible() const
Definition: element.h:210
Definition: segment.h:50
Segment * next1MMenabled() const
Definition: segment.cpp:273
Segment * prevActive() const
Definition: segment.h:273
Element * lastAnnotation(Segment *s, int activeStaff)
Definition: segment.cpp:1225
virtual QVariant propertyDefault(Pid) const
Definition: segment.cpp:879
Segment * nextCR(int track=-1, bool sameStaff=false) const
Definition: segment.cpp:370
std::vector< Element * > & elist()
Definition: segment.h:123
Definition: aeolus.cpp:26
Definition: spatium.h:25
std::vector< Element * > findAnnotations(ElementType type, int minTrack, int maxTrack)
Returns the list of found annotations or nullptr if nothing was found.
Definition: segment.cpp:1020
bool empty() const
Definition: segment.h:150
Segment * prev1MMenabled() const
Definition: segment.cpp:339
Definition: xml.h:218
std::vector< Element * > _elist
Definition: segment.h:61
Spatium extraLeadingSpace() const
Definition: segment.h:180
SegmentType _segmentType
Definition: segment.h:51
Spanner * lastSpanner(int activeStaff)
Definition: segment.cpp:1441
void setPrev(Segment *e)
Definition: segment.h:95
void setExtraLeadingSpace(Spatium v)
Definition: segment.h:181
bool enabled() const
Definition: element.h:419
Ms::Element * elementAt(int track) const
Definition: segment.cpp:1058
qreal stretch() const
Definition: segment.h:156
Fraction _ticks
Definition: segment.h:53
virtual Element * nextElement()
Definition: element.cpp:1584
bool operator>(const Segment &) const
return true if segment is after s in list
Definition: segment.cpp:956
void setDotPosX(int staffIdx, qreal val)
Definition: segment.h:178
void setWritten(bool val) const
Definition: segment.h:152
const std::vector< Element * > & elist() const
Definition: segment.h:122
bool hasAnnotationOrElement(ElementType type, int minTrack, int maxTrack) const
return true if an annotation of type type or and element is found in the track range ...
Definition: segment.cpp:989
bool isKeySigType() const
Definition: segment.h:233
Definition: chordrest.h:48
void setSegmentType(SegmentType t)
Definition: segment.cpp:162
const char * subTypeName() const
Definition: segment.cpp:47
void checkEmpty() const
Definition: segment.cpp:794
virtual void setScore(Score *) override
Definition: segment.cpp:172
bool isType(const SegmentType t) const
Definition: segment.h:229
Segment * nextActive() const
Definition: segment.h:249
Segment * prev() const
Definition: segment.h:91
Element * prevElementOfSegment(Segment *s, Element *e, int activeStaff)
Definition: segment.cpp:1340
void fixStaffIdx()
Definition: segment.cpp:780
void checkElement(Element *, int track)
Definition: segment.cpp:453
One row of measures for all instruments; a complete piece of the timeline.
Definition: system.h:79
bool operator<(const Segment &) const
return true if segment is before s in list
Definition: segment.cpp:938
Definition: shape.h:42
void createShape(int staffIdx)
Definition: segment.cpp:1910
virtual void add(Element *)
Definition: segment.cpp:471
bool hasElements() const
Returns true if the segment has at least one element.
Definition: segment.cpp:975
Definition: fraction.h:46
ElementType
Definition: types.h:34
std::vector< Shape > _shapes
Definition: segment.h:62
ChordRest * cr(int track) const
Definition: segment.h:228
bool flag(ElementFlag f) const
Definition: element.h:205
bool isTimeSigType() const
Definition: segment.h:235
virtual QString accessibleExtraInfo() const override
Definition: segment.cpp:1838
Segment * prev1MM() const
Definition: segment.cpp:331
bool notChordRestType(Segment *s)
Definition: segment.cpp:1467
Element * findAnnotation(ElementType type, int minTrack, int maxTrack)
Returns the first found annotation of type type or nullptr if nothing was found.
Definition: segment.cpp:1006
void setRtick(const Fraction &v)
Definition: segment.h:160
qreal minHorizontalCollidingDistance(Segment *ns) const
Definition: segment.cpp:2037
Element * firstElement(int staff)
Definition: segment.cpp:1093
Element * nextElementOfSegment(Segment *s, Element *e, int activeStaff)
Definition: segment.cpp:1291
Spatium _extraLeadingSpace
Definition: segment.h:54
const QPointF & ipos() const
Definition: element.h:229
void insertStaff(int staff)
Definition: segment.cpp:413
bool isClefType() const
Definition: segment.h:231
void setElement(int track, Element *el)
Definition: segment.cpp:78
Segment * _next
Definition: segment.h:57
SegmentType segmentType() const
Definition: segment.h:147