MuseScore  3.4
Music composition and notation
xml.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2004-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 __XML_H__
14 #define __XML_H__
15 
16 #include "connector.h"
17 #include "stafftype.h"
18 #include "interval.h"
19 #include "element.h"
20 #include "select.h"
21 
22 namespace Ms {
23 
24 enum class PlaceText : char;
25 enum class ClefType : signed char;
26 class Spanner;
27 class Beam;
28 class Tuplet;
29 class Measure;
30 class LinkedElements;
31 
32 //---------------------------------------------------------
33 // SpannerValues
34 //---------------------------------------------------------
35 
36 struct SpannerValues {
37  int spannerId;
39  int track2;
40  };
41 
42 //---------------------------------------------------------
43 // TextStyleMap
44 //---------------------------------------------------------
45 
46 struct TextStyleMap {
47  QString name;
49  };
50 
51 //---------------------------------------------------------
52 // LinksIndexer
53 //---------------------------------------------------------
54 
55 class LinksIndexer {
56  int _lastLocalIndex { -1 };
57  Location _lastLinkedElementLoc { Location::absolute() };
58 
59  public:
60  int assignLocalIndex(const Location& mainElementInfo);
61  };
62 
63 //---------------------------------------------------------
64 // XmlReader
65 //---------------------------------------------------------
66 
67 class XmlReader : public QXmlStreamReader {
68  QString docName; // used for error reporting
69 
70  // For readahead possibility.
71  // If needed, must be explicitly set by setReadAheadDevice.
72  QIODevice* _readAheadDevice = nullptr;
73 
74  // Score read context (for read optimizations):
75  Fraction _tick { Fraction(0, 1) };
76  Fraction _tickOffset { Fraction(0, 1) };
77  int _intTick { 0 };
78  int _track { 0 };
79  int _trackOffset { 0 };
80  bool _pasteMode { false }; // modifies read behaviour on paste operation
81  Measure* _lastMeasure { 0 };
82  Measure* _curMeasure { 0 };
83  int _curMeasureIdx { 0 };
84  QHash<int, Beam*> _beams;
85  QHash<int, Tuplet*> _tuplets;
86 
87  QList<SpannerValues> _spannerValues;
88  QList<std::pair<int,Spanner*>> _spanner;
89  QList<StaffType> _staffTypes;
90  QList<std::pair<Element*, QPointF>> _fixOffsets;
91 
92  std::vector<std::unique_ptr<ConnectorInfoReader>> _connectors;
93  std::vector<std::unique_ptr<ConnectorInfoReader>> _pendingConnectors; // connectors that are pending to be updated and added to _connectors. That will happen when checkConnectors() is called.
94 
95  void htmlToString(int level, QString*);
97  QMap<int, LinkedElements*> _elinks; // for reading old files (< 3.01)
98  QMap<int, QList<QPair<LinkedElements*, Location>>> _staffLinkedElements; // one list per staff
100  QMultiMap<int, int> _tracks;
101 
102  QList<TextStyleMap> userTextStyles;
103 
104  void addConnectorInfo(std::unique_ptr<ConnectorInfoReader>);
105  void removeConnector(const ConnectorInfoReader*); // Removes the whole ConnectorInfo chain from the connectors list.
106 
107  public:
108  XmlReader(QFile* f) : QXmlStreamReader(f), docName(f->fileName()) {}
109  XmlReader(const QByteArray& d, const QString& st = QString()) : QXmlStreamReader(d), docName(st) {}
110  XmlReader(QIODevice* d, const QString& st = QString()) : QXmlStreamReader(d), docName(st) {}
111  XmlReader(const QString& d, const QString& st = QString()) : QXmlStreamReader(d), docName(st) {}
112  XmlReader(const XmlReader&) = delete;
113  XmlReader& operator=(const XmlReader&) = delete;
114  ~XmlReader();
115 
116  bool hasAccidental; // used for userAccidental backward compatibility
117  void unknown();
118 
119  // attribute helper routines:
120  QString attribute(const char* s) const { return attributes().value(s).toString(); }
121  QString attribute(const char* s, const QString&) const;
122  int intAttribute(const char* s) const;
123  int intAttribute(const char* s, int _default) const;
124  double doubleAttribute(const char* s) const;
125  double doubleAttribute(const char* s, double _default) const;
126  bool hasAttribute(const char* s) const;
127 
128  // helper routines based on readElementText():
129  int readInt() { return readElementText().toInt(); }
130  int readInt(bool* ok) { return readElementText().toInt(ok); }
131  int readIntHex() { return readElementText().toInt(0, 16); }
132  double readDouble() { return readElementText().toDouble(); }
133  qlonglong readLongLong() { return readElementText().toLongLong(); }
134 
135  double readDouble(double min, double max);
136  bool readBool();
137  QPointF readPoint();
138  QSizeF readSize();
139  QRectF readRect();
140  QColor readColor();
141  Fraction readFraction();
142  QString readXml();
143 
144  void setDocName(const QString& s) { docName = s; }
145  QString getDocName() const { return docName; }
146 
147  Fraction tick() const { return _tick + _tickOffset; }
148  Fraction rtick() const ;
149  void setTick(const Fraction& f);
150  void incTick(const Fraction& f);
151  void setTickOffset(const Fraction& val) { _tickOffset = val; }
152 
153  int track() const { return _track + _trackOffset; }
154  void setTrackOffset(int val) { _trackOffset = val; }
155  int trackOffset() const { return _trackOffset; }
156  void setTrack(int val) { _track = val; }
157  bool pasteMode() const { return _pasteMode; }
158  void setPasteMode(bool v) { _pasteMode = v; }
159 
160  Location location(bool forceAbsFrac = false) const;
161  void fillLocation(Location&, bool forceAbsFrac = false) const;
162  void setLocation(const Location&); // sets a new reading point, taking into
163  // account its type (absolute or relative).
164 
165  void addBeam(Beam* s);
166  Beam* findBeam(int id) const { return _beams.value(id); }
167 
168  void addTuplet(Tuplet* s);
169  Tuplet* findTuplet(int id) const { return _tuplets.value(id); }
170  QHash<int, Tuplet*>& tuplets() { return _tuplets; }
171 
172  void setLastMeasure(Measure* m) { _lastMeasure = m; }
173  Measure* lastMeasure() const { return _lastMeasure; }
174  void setCurrentMeasure(Measure* m) { _curMeasure = m; }
175  Measure* currentMeasure() const { return _curMeasure; }
176  void setCurrentMeasureIndex(int idx) { _curMeasureIdx = idx; }
177  int currentMeasureIndex() const { return _curMeasureIdx; }
178 
179  void removeSpanner(const Spanner*);
180  void addSpanner(int id, Spanner*);
181  Spanner* findSpanner(int id);
182 
183  int spannerId(const Spanner*); // returns spanner id, allocates new one if none exists
184 
185  void addSpannerValues(const SpannerValues& sv) { _spannerValues.append(sv); }
186  const SpannerValues* spannerValues(int id) const;
187 
188  void addConnectorInfoLater(std::unique_ptr<ConnectorInfoReader> c) { _pendingConnectors.push_back(std::move(c)); } // add connector info to be checked after calling checkConnectors()
189  void checkConnectors();
190  void reconnectBrokenConnectors();
191 
192  QList<StaffType>& staffType() { return _staffTypes; }
193  Interval transpose() const { return _transpose; }
194  void setTransposeChromatic(int v) { _transpose.chromatic = v; }
195  void setTransposeDiatonic(int v) { _transpose.diatonic = v; }
196 
197  LinkedElements* getLink(bool masterScore, const Location& l, int localIndexDiff);
198  void addLink(Staff* staff, LinkedElements* link);
199  QMap<int, LinkedElements*>& linkIds() { return _elinks; }
200  QMultiMap<int, int>& tracks() { return _tracks; }
201 
202  void checkTuplets();
203  Tid addUserTextStyle(const QString& name);
204  Tid lookupUserTextStyle(const QString& name);
205 
206  // Ownership on read ahead device is NOT transferred to XmlReader.
207  void setReadAheadDevice(QIODevice* dev) { if (!dev->isSequential()) _readAheadDevice = dev; }
208  bool readAheadAvailable() const { return bool(_readAheadDevice); }
209  void performReadAhead(std::function<void(QIODevice&)> readAheadRoutine);
210 
211  QList<std::pair<Element*, QPointF>>& fixOffsets() { return _fixOffsets; }
212  };
213 
214 //---------------------------------------------------------
215 // XmlWriter
216 //---------------------------------------------------------
217 
218 class XmlWriter : public QTextStream {
219  static const int BS = 2048;
220 
222  QList<QString> stack;
224 
225  Fraction _curTick { 0, 1 }; // used to optimize output
226  Fraction _tickDiff { 0, 1 };
227  int _curTrack { -1 };
228  int _trackDiff { 0 }; // saved track is curTrack-trackDiff
229 
230  bool _clipboardmode { false }; // used to modify write() behaviour
231  bool _excerptmode { false }; // true when writing a part
232  bool _writeOmr { true }; // false if writing into *.msc file
233  bool _writeTrack { false };
234  bool _writePosition { false };
235 
237  QMap<int, int> _lidLocalIndices;
238 
239  std::vector<std::pair<const ScoreElement*, QString>> _elements;
240  bool _recordElements = false;
241 
242  void putLevel();
243 
244  public:
245  XmlWriter(Score*);
246  XmlWriter(Score* s, QIODevice* dev);
247 
248  Fraction curTick() const { return _curTick; }
249  void setCurTick(const Fraction& v) { _curTick = v; }
250  void incCurTick(const Fraction& v) { _curTick += v; }
251 
252  int curTrack() const { return _curTrack; }
253  void setCurTrack(int v) { _curTrack = v; }
254 
255  Fraction tickDiff() const { return _tickDiff; }
256  void setTickDiff(const Fraction& v) { _tickDiff = v; }
257 
258  int trackDiff() const { return _trackDiff; }
259  void setTrackDiff(int v) { _trackDiff = v; }
260 
261  bool clipboardmode() const { return _clipboardmode; }
262  bool excerptmode() const { return _excerptmode; }
263  bool writeOmr() const { return _writeOmr; }
264  bool writeTrack() const { return _writeTrack; }
265  bool writePosition() const { return _writePosition; }
266 
267  void setClipboardmode(bool v) { _clipboardmode = v; }
268  void setExcerptmode(bool v) { _excerptmode = v; }
269  void setWriteOmr(bool v) { _writeOmr = v; }
270  void setWriteTrack(bool v) { _writeTrack= v; }
271  void setWritePosition(bool v) { _writePosition = v; }
272 
273  int assignLocalIndex(const Location& mainElementLocation);
274  void setLidLocalIndex(int lid, int localIndex) { _lidLocalIndices.insert(lid, localIndex); }
275  int lidLocalIndex(int lid) const { Q_ASSERT(_lidLocalIndices.contains(lid)); return _lidLocalIndices[lid]; }
276 
277  const std::vector<std::pair<const ScoreElement*, QString>>& elements() const { return _elements; }
278  void setRecordElements(bool record) { _recordElements = record; }
279 
280  void sTag(const char* name, Spatium sp) { XmlWriter::tag(name, QVariant(sp.val())); }
281  void pTag(const char* name, PlaceText);
282 
283  void header();
284 
285  void stag(const QString&);
286  void etag();
287 
288  void stag(const ScoreElement* se, const QString& attributes = QString());
289  void stag(const QString& name, const ScoreElement* se, const QString& attributes = QString());
290 
291  void tagE(const QString&);
292  void tagE(const char* format, ...);
293  void ntag(const char* name);
294  void netag(const char* name);
295 
296  void tag(Pid id, void* data, void* defaultVal);
297  void tag(Pid id, QVariant data, QVariant defaultData = QVariant());
298  void tag(const char* name, QVariant data, QVariant defaultData = QVariant());
299  void tag(const QString&, QVariant data);
300  void tag(const char* name, const char* s) { tag(name, QVariant(s)); }
301  void tag(const char* name, const QString& s) { tag(name, QVariant(s)); }
302  void tag(const char* name, const QWidget*);
303 
304  void comment(const QString&);
305 
306  void writeXml(const QString&, QString s);
307  void dump(int len, const unsigned char* p);
308 
309  void setFilter(SelectionFilter f) { _filter = f; }
310  bool canWrite(const Element*) const;
311  bool canWriteVoice(int track) const;
312 
313  static QString xmlString(const QString&);
314  static QString xmlString(ushort c);
315  };
316 
318 } // namespace Ms
319 #endif
320 
Helper class for reading beams, tuplets and spanners.
Definition: connector.h:96
QMap< int, LinkedElements * > & linkIds()
Definition: xml.h:199
void incCurTick(const Fraction &v)
Definition: xml.h:250
QMultiMap< int, int > _tracks
Definition: xml.h:100
Global staff data not directly related to drawing.
Definition: staff.h:62
signed char chromatic
Definition: interval.h:24
Definition: beam.h:37
QMap< int, QList< QPair< LinkedElements *, Location > > > _staffLinkedElements
Definition: xml.h:98
Pid
Definition: property.h:62
QMap< int, LinkedElements * > _elinks
Definition: xml.h:97
Definition: xml.h:67
double readDouble()
Definition: xml.h:132
void tag(Pid id, void *data, void *defaultVal)
bool pasteMode() const
Definition: xml.h:157
XmlReader(QIODevice *d, const QString &st=QString())
Definition: xml.h:110
void setDocName(const QString &s)
Definition: xml.h:144
Tuplet * findTuplet(int id) const
Definition: xml.h:169
Virtual base class for slurs, ties, lines etc.
Definition: spanner.h:136
Definition: scoreElement.h:146
one measure in a system
Definition: measure.h:65
void setCurrentMeasure(Measure *m)
Definition: xml.h:174
void tag(const char *name, const QString &s)
Definition: xml.h:301
Definition: scoreElement.h:173
int lidLocalIndex(int lid) const
Definition: xml.h:275
void setPasteMode(bool v)
Definition: xml.h:158
Score * _score
Definition: xml.h:221
Base class of score layout elements.
Definition: element.h:158
void setWriteTrack(bool v)
Definition: xml.h:270
QHash< int, Tuplet * > _tuplets
Definition: xml.h:85
QMultiMap< int, int > & tracks()
Definition: xml.h:200
Measure * lastMeasure() const
Definition: xml.h:173
int track2
Definition: xml.h:39
void setWritePosition(bool v)
Definition: xml.h:271
void setFilter(SelectionFilter f)
Definition: xml.h:309
void setTrackOffset(int val)
Definition: xml.h:154
void addSpannerValues(const SpannerValues &sv)
Definition: xml.h:185
int curTrack() const
Definition: xml.h:252
QList< std::pair< Element *, QPointF > > & fixOffsets()
Definition: xml.h:211
std::vector< std::unique_ptr< ConnectorInfoReader > > _pendingConnectors
Definition: xml.h:93
Definition: score.h:391
constexpr qreal val() const
Definition: spatium.h:32
void setReadAheadDevice(QIODevice *dev)
Definition: xml.h:207
qlonglong readLongLong()
Definition: xml.h:133
Example of 1/8 triplet: _baseLen = 1/8 _actualNotes = 3 _normalNotes = 2 (3 notes played in the time ...
Definition: tuplet.h:37
int readInt()
Definition: xml.h:129
int spannerId
Definition: xml.h:37
void setRecordElements(bool record)
Definition: xml.h:278
int currentMeasureIndex() const
Definition: xml.h:177
void sTag(const char *name, Spatium sp)
Definition: xml.h:280
void setCurTick(const Fraction &v)
Definition: xml.h:249
static constexpr Location absolute()
Definition: location.h:50
int readInt(bool *ok)
Definition: xml.h:130
Measure * currentMeasure() const
Definition: xml.h:175
QList< std::pair< int, Spanner * > > _spanner
Definition: xml.h:88
Beam * findBeam(int id) const
Definition: xml.h:166
Interval transpose() const
Definition: xml.h:193
PlaceText readPlacement(XmlReader &)
Definition: xmlreader.cpp:423
QString docName
Definition: xml.h:68
Tid
Enumerates the list of built-in text substyles.
Definition: types.h:371
void setTickOffset(const Fraction &val)
Definition: xml.h:151
void setTrackDiff(int v)
Definition: xml.h:259
Definition: select.h:115
QList< StaffType > _staffTypes
Definition: xml.h:89
bool clipboardmode() const
Definition: xml.h:261
void setTickDiff(const Fraction &v)
Definition: xml.h:256
bool readAheadAvailable() const
Definition: xml.h:208
void setLastMeasure(Measure *m)
Definition: xml.h:172
Definition: interval.h:22
QHash< int, Beam * > _beams
Definition: xml.h:84
QList< std::pair< Element *, QPointF > > _fixOffsets
Definition: xml.h:90
Definition: aeolus.cpp:26
int readIntHex()
Definition: xml.h:131
Fraction tickDiff() const
Definition: xml.h:255
const std::vector< std::pair< const ScoreElement *, QString > > & elements() const
Definition: xml.h:277
Definition: spatium.h:25
QHash< int, Tuplet * > & tuplets()
Definition: xml.h:170
signed char diatonic
Definition: interval.h:23
void setExcerptmode(bool v)
Definition: xml.h:268
Definition: xml.h:218
void tag(const char *name, const char *s)
Definition: xml.h:300
void setTransposeChromatic(int v)
Definition: xml.h:194
void setLidLocalIndex(int lid, int localIndex)
Definition: xml.h:274
Fraction tick2
Definition: xml.h:38
void setTrack(int val)
Definition: xml.h:156
Interval _transpose
Definition: xml.h:96
std::vector< std::unique_ptr< ConnectorInfoReader > > _connectors
Definition: xml.h:92
XmlReader(const QString &d, const QString &st=QString())
Definition: xml.h:111
Tid ss
Definition: xml.h:48
void setTransposeDiatonic(int v)
Definition: xml.h:195
bool excerptmode() const
Definition: xml.h:262
bool writePosition() const
Definition: xml.h:265
int track() const
Definition: xml.h:153
Definition: location.h:32
XmlReader(QFile *f)
Definition: xml.h:108
Definition: xml.h:46
bool writeTrack() const
Definition: xml.h:264
Fraction tick() const
Definition: xml.h:147
QList< SpannerValues > _spannerValues
Definition: xml.h:87
int trackDiff() const
Definition: xml.h:258
XmlReader(const QByteArray &d, const QString &st=QString())
Definition: xml.h:109
QList< StaffType > & staffType()
Definition: xml.h:192
Definition: xml.h:36
Fraction curTick() const
Definition: xml.h:248
void setClipboardmode(bool v)
Definition: xml.h:267
PlaceText
Definition: mscore.h:115
ClefType
Definition: clef.h:37
Definition: fraction.h:46
LinksIndexer _linksIndexer
Definition: xml.h:99
QMap< int, int > _lidLocalIndices
Definition: xml.h:237
QList< TextStyleMap > userTextStyles
Definition: xml.h:102
QList< QString > stack
Definition: xml.h:222
LinksIndexer _linksIndexer
Definition: xml.h:236
void addConnectorInfoLater(std::unique_ptr< ConnectorInfoReader > c)
Definition: xml.h:188
void setWriteOmr(bool v)
Definition: xml.h:269
QString attribute(const char *s) const
Definition: xml.h:120
void setCurTrack(int v)
Definition: xml.h:253
int trackOffset() const
Definition: xml.h:155
bool hasAccidental
Definition: xml.h:116
void setCurrentMeasureIndex(int idx)
Definition: xml.h:176
SelectionFilter _filter
Definition: xml.h:223
QString name
Definition: xml.h:47
bool writeOmr() const
Definition: xml.h:263
std::vector< std::pair< const ScoreElement *, QString > > _elements
Definition: xml.h:239
QString getDocName() const
Definition: xml.h:145