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 __MEASURE_H__
14 #define __MEASURE_H__
15 
16 /**
17  \file
18  Definition of class Measure.
19 */
20 
21 #include "measurebase.h"
22 #include "fraction.h"
23 #include "segmentlist.h"
24 
25 namespace Ms {
26 
27 class XmlWriter;
28 class Beam;
29 class Tuplet;
30 class Staff;
31 class Chord;
32 class MeasureNumber;
33 class MMRestRange;
34 class ChordRest;
35 class Score;
36 class MuseScoreView;
37 class System;
38 class Note;
39 class Spacer;
40 class TieMap;
41 class AccidentalState;
42 class Spanner;
43 class Part;
44 class RepeatMeasure;
45 
46 class MStaff;
47 
48 //---------------------------------------------------------
49 //   MeasureNumberMode
50 //---------------------------------------------------------
51 
52 enum class MeasureNumberMode : char {
53       AUTO,       // show measure number depending on style
54       SHOW,       // always show measure number
55       HIDE        // don’t show measure number
56       };
57 
58 //---------------------------------------------------------
59 //   @@ Measure
60 ///    one measure in a system
61 //
62 //   @P firstSegment    Segment       the first segment of the measure (read-only)
63 //   @P lastSegment     Segment       the last segment of the measure (read-only)
64 //---------------------------------------------------------
65 
66 class Measure final : public MeasureBase {
67       std::vector<MStaff*>  _mstaves;
68       SegmentList _segments;
69       Measure* _mmRest;       // multi measure rest which replaces a measure range
70 
71       qreal _userStretch;
72 
73       Fraction _timesig;
74 
75       int _mmRestCount;       // > 0 if this is a multi measure rest
76                               // 0 if this is the start of a mm rest (_mmRest != 0)
77                               // < 0 if this measure is covered by a mm rest
78 
79       int _playbackCount;     // temp. value used in RepeatList
80                               // counts how many times this measure was already played
81 
82       int _repeatCount;       ///< end repeat marker und repeat count
83 
84       MeasureNumberMode _noMode;
85       bool _breakMultiMeasureRest;
86 
87       void push_back(Segment* e);
88       void push_front(Segment* e);
89 
90       void fillGap(const Fraction& pos, const Fraction& len, int track, const Fraction& stretch);
91       void computeMinWidth(Segment* s, qreal x, bool isSystemHeader);
92 
93       void readVoice(XmlReader& e, int staffIdx, bool irregular);
94 
95    public:
96       Measure(Score* = 0);
97       Measure(const Measure&);
98       ~Measure();
99 
clone()100       Measure* clone() const override     { return new Measure(*this); }
type()101       ElementType type() const override { return ElementType::MEASURE; }
102       void setScore(Score* s) override;
103       Measure* cloneMeasure(Score*, const Fraction& tick, TieMap*);
104 
105       void read(XmlReader&, int idx);
read(XmlReader & d)106       void read(XmlReader& d) { read(d, 0); }
107       void readAddConnector(ConnectorInfoReader* info, bool pasteMode) override;
write(XmlWriter & xml)108       void write(XmlWriter& xml) const override { Element::write(xml); }
109       void write(XmlWriter&, int, bool writeSystemElements, bool forceTimeSig) const;
110       void writeBox(XmlWriter&) const;
111       void readBox(XmlReader&);
isEditable()112       bool isEditable() const override { return false; }
113       void checkMeasure(int idx);
114 
115       void add(Element*) override;
116       void remove(Element*) override;
117       void change(Element* o, Element* n) override;
118       void spatiumChanged(qreal oldValue, qreal newValue) override;
119 
system()120       System* system() const                      { return (System*)parent(); }
121       bool hasVoices(int staffIdx, Fraction stick, Fraction len) const;
122       bool hasVoices(int staffIdx) const;
123       void setHasVoices(int staffIdx, bool v);
124 
125       StaffLines* staffLines(int staffIdx);
126       Spacer* vspacerDown(int staffIdx) const;
127       Spacer* vspacerUp(int staffIdx) const;
128       void setStaffVisible(int staffIdx, bool visible);
129       void setStaffStemless(int staffIdx, bool stemless);
130       bool corrupted(int staffIdx) const;
131       void setCorrupted(int staffIdx, bool val);
132       void setNoText(int staffIdx, MeasureNumber*);
133       MeasureNumber* noText(int staffIdx) const;
134       void setMMRangeText(int staffIdx, MMRestRange *);
135       MMRestRange *mmRangeText(int staffIdx) const;
136 
137       void createStaves(int);
138 
measureNumberMode()139       MeasureNumberMode measureNumberMode() const     { return _noMode;      }
setMeasureNumberMode(MeasureNumberMode v)140       void setMeasureNumberMode(MeasureNumberMode v)  { _noMode = v;         }
141 
timesig()142       Fraction timesig() const             { return _timesig;     }
setTimesig(const Fraction & f)143       void setTimesig(const Fraction& f)   { _timesig = f;        }
144 
145       Fraction stretchedLen(Staff*) const;
isIrregular()146       bool isIrregular() const             { return _timesig != _len; }
147 
size()148       int size() const                     { return _segments.size();        }
first()149       Ms::Segment* first() const           { return _segments.first();       }
first(SegmentType t)150       Segment* first(SegmentType t) const  { return _segments.first(t);      }
firstEnabled()151       Segment* firstEnabled() const        { return _segments.first(ElementFlag::ENABLED); }
152 
last()153       Ms::Segment* last() const            { return _segments.last(); }
lastEnabled()154       Segment* lastEnabled() const         { return _segments.last(ElementFlag::ENABLED); }
segments()155       SegmentList& segments()              { return _segments; }
segments()156       const SegmentList& segments() const  { return _segments; }
157 
158       qreal userStretch() const;
setUserStretch(qreal v)159       void setUserStretch(qreal v)         { _userStretch = v; }
160 
161       void stretchMeasure(qreal stretch);
162       Fraction computeTicks();
163       void layout2();
164 
165       bool showsMeasureNumber();
166       bool showsMeasureNumberInAutoMode();
167       void layoutMeasureNumber();
168       void layoutMMRestRange();
169 
170       Chord* findChord(Fraction tick, int track);
171       ChordRest* findChordRest(Fraction tick, int track);
172       Fraction snap(const Fraction& tick, const QPointF p) const;
173       Fraction snapNote(const Fraction& tick, const QPointF p, int staff) const;
174 
175       Segment* searchSegment(qreal x, SegmentType st, int strack, int etrack, const Segment* preferredSegment = nullptr, qreal spacingFactor = 0.5) const;
176 
177       void insertStaff(Staff*, int staff);
178       void insertMStaff(MStaff* staff, int idx);
179       void removeMStaff(MStaff* staff, int idx);
180 
181       void moveTicks(const Fraction& diff) override;
182 
183       void cmdRemoveStaves(int s, int e);
184       void cmdAddStaves(int s, int e, bool createRest);
185       void removeStaves(int s, int e);
186       void insertStaves(int s, int e);
187 
188       qreal tick2pos(Fraction) const;
189       Segment* tick2segment(const Fraction& tick, SegmentType st = SegmentType::ChordRest);
190 
191       void sortStaves(QList<int>& dst);
192 
193       bool acceptDrop(EditData&) const override;
194       Element* drop(EditData&) override;
195 
repeatCount()196       int repeatCount() const         { return _repeatCount; }
setRepeatCount(int val)197       void setRepeatCount(int val)    { _repeatCount = val; }
198 
199       Segment* findSegmentR(SegmentType st,    const Fraction&) const;
200       Segment* undoGetSegmentR(SegmentType st, const Fraction& f);
201       Segment* getSegmentR(SegmentType st,     const Fraction& f);
202       Segment* findFirstR(SegmentType st, const Fraction& rtick) const;
203 
204       // segment routines with absolute tick values
findSegment(SegmentType st,const Fraction & f)205       Segment* findSegment(SegmentType st,    const Fraction& f) const { return findSegmentR(st, f - tick()); }
undoGetSegment(SegmentType st,const Fraction & f)206       Segment* undoGetSegment(SegmentType st, const Fraction& f)       { return undoGetSegmentR(st, f - tick()); }
getSegment(SegmentType st,const Fraction & f)207       Segment* getSegment(SegmentType st,     const Fraction& f)       { return getSegmentR(st, f - tick()); }
208 
209       void connectTremolo();
210 
211       qreal createEndBarLines(bool);
212       void barLinesSetSpan(Segment*);
213       void setEndBarLineType(BarLineType val, int track, bool visible = true, QColor color = QColor());
214 
215       RepeatMeasure* cmdInsertRepeatMeasure(int staffIdx);
216 
217       void scanElements(void* data, void (*func)(void*, Element*), bool all=true) override;
218       void createVoice(int track);
219       void adjustToLen(Fraction, bool appendRestsIfNecessary = true);
220 
221       AccidentalVal findAccidental(Note*) const;
222       AccidentalVal findAccidental(Segment* s, int staffIdx, int line, bool &error) const;
223       void exchangeVoice(int voice1, int voice2, int staffIdx);
224       void checkMultiVoices(int staffIdx);
225       bool hasVoice(int track) const;
226       bool isEmpty(int staffIdx) const;
227       bool isCutawayClef(int staffIdx) const;
228       bool isFullMeasureRest() const;
229       bool isRepeatMeasure(const Staff* staff) const;
230       bool visible(int staffIdx) const;
231       bool stemless(int staffIdx) const;
232       bool isFinalMeasureOfSection() const;
233       bool isAnacrusis() const;
234       bool isFirstInSystem() const;
235 
breakMultiMeasureRest()236       bool breakMultiMeasureRest() const        { return _breakMultiMeasureRest; }
setBreakMultiMeasureRest(bool val)237       void setBreakMultiMeasureRest(bool val)   { _breakMultiMeasureRest = val;  }
238 
239       bool empty() const;
240       bool isOnlyRests(int track) const;
241       bool isOnlyDeletedRests(int track) const;
242 
playbackCount()243       int playbackCount() const      { return _playbackCount; }
setPlaybackCount(int val)244       void setPlaybackCount(int val) { _playbackCount = val; }
245       QRectF staffabbox(int staffIdx) const;
246 
247       QVariant getProperty(Pid propertyId) const override;
248       bool setProperty(Pid propertyId, const QVariant&) override;
propertyDefault(Pid)249       QVariant propertyDefault(Pid) const override;
250 
251       bool hasMMRest() const        { return _mmRest != 0; }
isMMRest()252       bool isMMRest() const         { return _mmRestCount > 0; }
mmRest()253       Measure* mmRest() const       { return _mmRest;      }
254       const Measure* mmRest1() const;
setMMRest(Measure * m)255       void setMMRest(Measure* m)    { _mmRest = m;         }
mmRestCount()256       int mmRestCount() const       { return _mmRestCount; }    // number of measures _mmRest spans
setMMRestCount(int n)257       void setMMRestCount(int n)    { _mmRestCount = n;    }
258       Measure* mmRestFirst() const;
259       Measure* mmRestLast() const;
260 
261       Element* nextElementStaff(int staff);
262       Element* prevElementStaff(int staff);
263       QString accessibleInfo() const override;
264 
265       void addSystemHeader(bool firstSystem);
266       void addSystemTrailer(Measure* nm);
267       void removeSystemHeader();
268       void removeSystemTrailer();
269 
270       const BarLine* endBarLine() const;
271       BarLineType endBarLineType() const;
272       bool endBarLineVisible() const;
273       void triggerLayout() const override;
274       qreal basicStretch() const;
275       qreal basicWidth() const;
276       int layoutWeight(int maxMMRestLength = 0) const;
277       void computeMinWidth();
278       void checkHeader();
279       void checkTrailer();
280       void setStretchedWidth(qreal);
281       void layoutStaffLines();
282       };
283 
284 }     // namespace Ms
285 #endif
286