MuseScore  3.4
Music composition and notation
spanner.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2010-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 __SPANNER_H__
14 #define __SPANNER_H__
15 
16 #include "element.h"
17 
18 namespace Ms {
19 
20 class Spanner;
21 
22 //---------------------------------------------------------
23 // SpannerSegmentType
24 //---------------------------------------------------------
25 
26 enum class SpannerSegmentType {
28  };
29 
30 //---------------------------------------------------------
31 // SpannerEditData
32 //---------------------------------------------------------
33 
35  public:
38  int editTick;
39  int editTick2;
41  QList<QPointF> userOffsets;
42  QList<QPointF> userOffsets2;
43  };
44 
45 //---------------------------------------------------------
46 // @@ SpannerSegment
48 //---------------------------------------------------------
49 
50 class SpannerSegment : public Element {
53 
54  protected:
55  QPointF _p2;
56  QPointF _offset2;
57 
58  public:
62  virtual SpannerSegment* clone() const = 0;
63 
64  virtual qreal mag() const override;
65  virtual Fraction tick() const override;
66 
67  Spanner* spanner() const { return _spanner; }
68  Spanner* setSpanner(Spanner* val) { return _spanner = val; }
69 
70  void setSpannerSegmentType(SpannerSegmentType s) { _spannerSegmentType = s; }
71  SpannerSegmentType spannerSegmentType() const { return _spannerSegmentType; }
72  bool isSingleType() const { return spannerSegmentType() == SpannerSegmentType::SINGLE; }
73  bool isBeginType() const { return spannerSegmentType() == SpannerSegmentType::BEGIN; }
74  bool isSingleBeginType() const { return isSingleType() || isBeginType(); }
75  bool isSingleEndType() const { return isSingleType() || isEndType(); }
76  bool isMiddleType() const { return spannerSegmentType() == SpannerSegmentType::MIDDLE; }
77  bool isEndType() const { return spannerSegmentType() == SpannerSegmentType::END; }
78 
79  void setSystem(System* s);
80  System* system() const { return toSystem(parent()); }
81 
82  const QPointF& userOff2() const { return _offset2; }
83  void setUserOff2(const QPointF& o) { _offset2 = o; }
84  void setUserXoffset2(qreal x) { _offset2.setX(x); }
85  qreal& rUserXoffset2() { return _offset2.rx(); }
86  qreal& rUserYoffset2() { return _offset2.ry(); }
87 
88  void setPos2(const QPointF& p) { _p2 = p; }
89  //TODO: rename to spanSegPosWithUserOffset()
90  QPointF pos2() const { return _p2 + _offset2; }
91  //TODO: rename to spanSegPos()
92  const QPointF& ipos2() const { return _p2; }
93  QPointF& rpos2() { return _p2; }
94  qreal& rxpos2() { return _p2.rx(); }
95  qreal& rypos2() { return _p2.ry(); }
96 
97  virtual bool isEditable() const override { return true; }
98 
99  QByteArray mimeData(const QPointF& dragOffset) const override;
100 
101  virtual QVariant getProperty(Pid id) const override;
102  virtual bool setProperty(Pid id, const QVariant& v) override;
103  virtual QVariant propertyDefault(Pid id) const override;
104  virtual Element* propertyDelegate(Pid) override;
105  virtual void undoChangeProperty(Pid id, const QVariant&, PropertyFlags ps) override;
107 
108  virtual Sid getPropertyStyle(Pid id) const override;
109  virtual PropertyFlags propertyFlags(Pid id) const override;
110  virtual void resetProperty(Pid id) override;
111  virtual void styleChanged() override;
112  void reset() override;
113 
114  virtual void setSelected(bool f) override;
115  virtual void setVisible(bool f) override;
116  virtual void setColor(const QColor& col) override;
117 
118  virtual Element* nextSegmentElement() override;
119  virtual Element* prevSegmentElement() override;
120  virtual QString accessibleInfo() const override;
121  virtual void triggerLayout() const override;
122  void autoplaceSpannerSegment();
123  };
124 
125 //----------------------------------------------------------------------------------
126 // @@ Spanner
128 //
129 // @P anchor enum (Spanner.CHORD, Spanner.MEASURE, Spanner.NOTE, Spanner.SEGMENT)
130 // @P endElement Element the element the spanner end is anchored to (read-only)
131 // @P startElement Element the element the spanner start is anchored to (read-only)
132 // @P tick int tick start position
133 // @P tick2 int tick end position
134 //----------------------------------------------------------------------------------
135 
136 class Spanner : public Element {
137  Q_GADGET
138  public:
139  enum class Anchor {
141  };
142  Q_ENUM(Anchor);
143  private:
144 
145  Element* _startElement { 0 };
146  Element* _endElement { 0 };
147 
148  Anchor _anchor { Anchor::SEGMENT };
149  Fraction _tick { Fraction(-1, 1) };
150  Fraction _ticks { Fraction(0, 1) };
151  int _track2 { -1 };
152  bool _broken { false };
153 
154  std::vector<SpannerSegment*> segments;
155  std::deque<SpannerSegment*> unusedSegments; // Currently unused segments which can be reused later.
156  // We cannot just delete them as they can be referenced
157  // in undo stack or other places already.
158 
159  protected:
160  void pushUnusedSegment(SpannerSegment* seg);
161  SpannerSegment* popUnusedSegment();
162  void reuse(SpannerSegment* seg); // called when segment from unusedSegments
163  // is added back to the spanner.
164  int reuseSegments(int number);
165  SpannerSegment* getNextLayoutSystemSegment(System* system, std::function<SpannerSegment*()> createSegment);
166  void fixupSegments(unsigned int targetNumber, std::function<SpannerSegment*()> createSegment);
167 
168  const std::vector<SpannerSegment*> spannerSegments() const { return segments; }
169 
170  public:
172  Spanner(const Spanner&);
173  ~Spanner();
174 
175  virtual qreal mag() const override;
176 
177  virtual ElementType type() const = 0;
178  virtual void setScore(Score* s) override;
179 
180  bool readProperties(XmlReader&) override;
181  void writeProperties(XmlWriter&) const override;
182 
183  void writeSpannerStart(XmlWriter& xml, const Element* current, int track, Fraction frac = { -1, 1 }) const;
184  void writeSpannerEnd(XmlWriter& xml, const Element* current, int track, Fraction frac = { -1, 1 }) const;
185  static void readSpanner(XmlReader& e, Element* current, int track);
186  static void readSpanner(XmlReader& e, Score* current, int track);
187 
188  virtual Fraction tick() const override { return _tick; }
189  Fraction tick2() const { return _tick + _ticks; }
190  Fraction ticks() const { return _ticks; }
191 
192  void setTick(const Fraction&);
193  void setTick2(const Fraction&);
194  void setTicks(const Fraction&);
195 
196  int track2() const { return _track2; }
197  void setTrack2(int v) { _track2 = v; }
198  int effectiveTrack2() const { return _track2 == -1 ? track() : _track2; }
199 
200  bool broken() const { return _broken; }
201  void setBroken(bool v) { _broken = v; }
202 
203  Anchor anchor() const { return _anchor; }
204  void setAnchor(Anchor a) { _anchor = a; }
205 
206  const std::vector<SpannerSegment*>& spannerSegments() { return segments; }
207  SpannerSegment* frontSegment() { return segments.front(); }
208  const SpannerSegment* frontSegment() const { return segments.front(); }
209  SpannerSegment* backSegment() { return segments.back(); }
210  const SpannerSegment* backSegment() const { return segments.back(); }
211  SpannerSegment* segmentAt(int n) { return segments[n]; }
212  const SpannerSegment* segmentAt(int n) const { return segments[n]; }
213  size_t nsegments() const { return segments.size(); }
214  bool segmentsEmpty() const { return segments.empty(); }
215  void eraseSpannerSegments();
216 
217  virtual SpannerSegment* layoutSystem(System*);
218  virtual void layoutSystemsDone();
219 
220  virtual void triggerLayout() const override;
221  virtual void triggerLayoutAll() const override;
222  virtual void add(Element*) override;
223  virtual void remove(Element*) override;
224  virtual void scanElements(void* data, void (*func)(void*, Element*), bool all=true) override;
225  bool removeSpannerBack();
226  virtual void removeUnmanaged();
227  virtual void insertTimeUnmanaged(const Fraction& tick, const Fraction& len);
228 
229  QVariant getProperty(Pid propertyId) const;
230  bool setProperty(Pid propertyId, const QVariant& v);
231  QVariant propertyDefault(Pid propertyId) const;
232  virtual void undoChangeProperty(Pid id, const QVariant&, PropertyFlags ps) override;
233 
234  void computeStartElement();
235  void computeEndElement();
236  static Note* endElementFromSpanner(Spanner* sp, Element* newStart);
237  static Note* startElementFromSpanner(Spanner* sp, Element* newEnd);
238  void setNoteSpan(Note* startNote, Note* endNote);
239 
240  Element* startElement() const { return _startElement; }
241  Element* endElement() const { return _endElement; }
242 
243  Measure* startMeasure() const;
244  Measure* endMeasure() const;
245 
246  void setStartElement(Element* e);
247  void setEndElement(Element* e);
248 
249  ChordRest* startCR();
250  ChordRest* endCR();
251 
252  Chord* startChord();
253  Chord* endChord();
254 
255  Segment* startSegment() const;
256  Segment* endSegment() const;
257 
258  virtual void setSelected(bool f) override;
259  virtual void setVisible(bool f) override;
260  virtual void setAutoplace(bool f) override;
261  virtual void setColor(const QColor& col) override;
262  Spanner* nextSpanner(Element* e, int activeStaff);
263  Spanner* prevSpanner(Element* e, int activeStaff);
264  virtual Element* nextSegmentElement() override;
265  virtual Element* prevSegmentElement() override;
266 
267  friend class SpannerSegment;
269  };
270 
271 } // namespace Ms
272 #endif
const QPointF & userOff2() const
Definition: spanner.h:82
Element * editStartElement
Definition: spanner.h:36
Pid
Definition: property.h:62
void setPos2(const QPointF &p)
Definition: spanner.h:88
Definition: xml.h:67
Definition: editdrumset.cpp:32
virtual void undoChangeProperty(Pid id, const QVariant &, PropertyFlags ps)
Definition: scoreElement.cpp:291
int track2() const
Definition: spanner.h:196
bool segmentsEmpty() const
Definition: spanner.h:214
virtual bool isEditable() const override
Definition: spanner.h:97
parent: System
Definition: spanner.h:50
bool broken() const
Definition: spanner.h:200
QFlags< ElementFlag > ElementFlags
Definition: element.h:101
Virtual base class for slurs, ties, lines etc.
Definition: spanner.h:136
one measure in a system
Definition: measure.h:65
System * system() const
Definition: spanner.h:80
void setSpannerSegmentType(SpannerSegmentType s)
Definition: spanner.h:70
SpannerSegmentType _spannerSegmentType
Definition: spanner.h:52
bool isMiddleType() const
Definition: spanner.h:76
bool isEndType() const
Definition: spanner.h:77
Element * endElement() const
Definition: spanner.h:241
SpannerSegment * segmentAt(int n)
Definition: spanner.h:211
Definition: element.h:504
Base class of score layout elements.
Definition: element.h:158
bool isSingleEndType() const
Definition: spanner.h:75
int effectiveTrack2() const
Definition: spanner.h:198
Spanner * spanner() const
Definition: spanner.h:67
const SpannerSegment * frontSegment() const
Definition: spanner.h:208
const SpannerSegment * backSegment() const
Definition: spanner.h:210
qreal & rUserYoffset2()
Definition: spanner.h:86
SpannerSegmentType spannerSegmentType() const
Definition: spanner.h:71
void setAnchor(Anchor a)
Definition: spanner.h:204
Definition: score.h:391
void setUserOff2(const QPointF &o)
Definition: spanner.h:83
Anchor
Definition: spanner.h:139
PropertyFlags
Definition: property.h:54
QPointF _offset2
Definition: spanner.h:56
Anchor anchor() const
Definition: spanner.h:203
const QPointF & ipos2() const
Definition: spanner.h:92
virtual Fraction tick() const override
Definition: spanner.h:188
const std::vector< SpannerSegment * > & spannerSegments()
Definition: spanner.h:206
void setBroken(bool v)
Definition: spanner.h:201
SpannerSegment * backSegment()
Definition: spanner.h:209
qreal & rUserXoffset2()
Definition: spanner.h:85
int editTick2
Definition: spanner.h:39
SpannerSegmentType
Definition: spanner.h:26
Element * editEndElement
Definition: spanner.h:37
bool isSingleBeginType() const
Definition: spanner.h:74
Definition: segment.h:50
bool isBeginType() const
Definition: spanner.h:73
Definition: aeolus.cpp:26
int editTrack2
Definition: spanner.h:40
std::vector< SpannerSegment * > segments
Definition: spanner.h:154
Sid
Definition: style.h:33
QPointF _p2
Definition: spanner.h:55
qreal & rypos2()
Definition: spanner.h:95
Definition: xml.h:218
const SpannerSegment * segmentAt(int n) const
Definition: spanner.h:212
QList< QPointF > userOffsets2
Definition: spanner.h:42
size_t nsegments() const
Definition: spanner.h:213
QList< QPointF > userOffsets
Definition: spanner.h:41
Graphic representation of a chord.
Definition: chord.h:55
void setTrack2(int v)
Definition: spanner.h:197
Definition: chordrest.h:48
Definition: spanner.h:34
int editTick
Definition: spanner.h:38
QPointF pos2() const
Definition: spanner.h:90
const std::vector< SpannerSegment * > spannerSegments() const
Definition: spanner.h:168
qreal & rxpos2()
Definition: spanner.h:94
One row of measures for all instruments; a complete piece of the timeline.
Definition: system.h:79
Graphic representation of a note.
Definition: note.h:212
Definition: fraction.h:46
ElementType
Definition: types.h:34
void setUserXoffset2(qreal x)
Definition: spanner.h:84
Fraction ticks() const
Definition: spanner.h:190
Spanner * _spanner
Definition: spanner.h:51
Element * startElement() const
Definition: spanner.h:240
Spanner * setSpanner(Spanner *val)
Definition: spanner.h:68
SpannerSegment * frontSegment()
Definition: spanner.h:207
Pid propertyId(const QStringRef &s)
Definition: property.cpp:347
bool isSingleType() const
Definition: spanner.h:72
QPointF & rpos2()
Definition: spanner.h:93
std::deque< SpannerSegment * > unusedSegments
Definition: spanner.h:155
Fraction tick2() const
Definition: spanner.h:189