File: location.cpp

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 210,672 kB
  • sloc: cpp: 291,093; xml: 200,238; sh: 3,779; ansic: 1,447; python: 393; makefile: 240; perl: 82; pascal: 79
file content (293 lines) | stat: -rw-r--r-- 9,420 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Copyright (C) 2018 Werner Schweer
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

#include "location.h"

#include "chord.h"
#include "element.h"
#include "measure.h"
#include "mscore.h"
#include "xml.h"

namespace Ms {

static constexpr Location absDefaults = Location::absolute();
static constexpr Location relDefaults = Location::relative();

//---------------------------------------------------------
//   Location::track
//---------------------------------------------------------

int Location::track() const
      {
      if ((_staff == absDefaults._staff) || (_voice == absDefaults._voice))
            return INT_MIN;
      return VOICES * _staff + _voice;
      }

//---------------------------------------------------------
//   Location::setTrack
//---------------------------------------------------------

void Location::setTrack(int track)
      {
      _staff = track / VOICES;
      _voice = track % VOICES;
      }

//---------------------------------------------------------
//   Location::write
//    Only relative locations should be written
//---------------------------------------------------------

void Location::write(XmlWriter& xml) const
      {
      Q_ASSERT(isRelative());
      xml.stag("location");
      xml.tag("staves", _staff, relDefaults._staff);
      xml.tag("voices", _voice, relDefaults._voice);
      xml.tag("measures", _measure, relDefaults._measure);
      xml.tag("fractions", _frac.reduced(), relDefaults._frac);
      xml.tag("grace", _graceIndex, relDefaults._graceIndex);
      xml.tag("notes", _note, relDefaults._note);
      xml.etag();
      }

//---------------------------------------------------------
//   Location::read
//---------------------------------------------------------

void Location::read(XmlReader& e)
      {
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());

            if (tag == "staves")
                  _staff = e.readInt();
            else if (tag == "voices")
                  _voice = e.readInt();
            else if (tag == "measures")
                  _measure = e.readInt();
            else if (tag == "fractions")
                  _frac = e.readFraction();
            else if (tag == "grace")
                  _graceIndex = e.readInt();
            else if (tag == "notes")
                  _note = e.readInt();
            else
                  e.unknown();
            }
      }

//---------------------------------------------------------
//   Location::toAbsolute
//---------------------------------------------------------

void Location::toAbsolute(const Location& ref)
      {
      if (isAbsolute())
            return;
      _staff += ref._staff;
      _voice += ref._voice;
      _measure += ref._measure;
      _frac += ref._frac;
      _note += ref._note;
      _rel = false;
      }

//---------------------------------------------------------
//   Location::toRelative
//---------------------------------------------------------

void Location::toRelative(const Location& ref)
      {
      if (isRelative())
            return;
      _staff -= ref._staff;
      _voice -= ref._voice;
      _measure -= ref._measure;
      _frac -= ref._frac;
      _note -= ref._note;
      _rel = true;
      }

//---------------------------------------------------------
//   Location::fillPositionForElement
//    Fills default fields of Location by values relevant
//    for the given Element. This function fills only
//    position values, not dealing with parameters specific
//    for Chords and Notes, like grace index.
//---------------------------------------------------------

void Location::fillPositionForElement(const Element* e, bool absfrac)
      {
      Q_ASSERT(isAbsolute());
      if (!e) {
            qWarning("Location::fillPositionForElement: element is nullptr");
            return;
            }
      if (track() == absDefaults.track())
            setTrack(track(e));
      if (frac() == absDefaults.frac())
            setFrac(absfrac ? e->tick() : e->rtick());
      if (measure() == absDefaults.measure())
            setMeasure(absfrac ? 0 : measure(e));
      }

//---------------------------------------------------------
//   Location::fillForElement
//    Fills default fields of Location by values relevant
//    for the given Element, including parameters specific
//    for Chords and Notes.
//---------------------------------------------------------

void Location::fillForElement(const Element* e, bool absfrac)
      {
      Q_ASSERT(isAbsolute());
      if (!e) {
            qWarning("Location::fillForElement: element is nullptr");
            return;
            }

      fillPositionForElement(e, absfrac);
      setGraceIndex(graceIndex(e));
      setNote(note(e));
      }

//---------------------------------------------------------
//   Location::forElement
//---------------------------------------------------------

Location Location::forElement(const Element* e, bool absfrac)
      {
      Location i = Location::absolute();
      i.fillForElement(e, absfrac);
      return i;
      }

//---------------------------------------------------------
//   Location::positionForElement
//---------------------------------------------------------

Location Location::positionForElement(const Element* e, bool absfrac)
      {
      Location i = Location::absolute();
      i.fillPositionForElement(e, absfrac);
      return i;
      }

//---------------------------------------------------------
//   Location::track
//---------------------------------------------------------

int Location::track(const Element* e)
      {
      int track = e->track();
      if (track < 0) {
            const MeasureBase* mb = e->findMeasureBase();
            if (mb && !mb->isMeasure()) {
                  // Such elements are written in the first staff,
                  // see writeMeasure() in scorefile.cpp
                  track = 0;
                  }
            }
      return track;
      }

//---------------------------------------------------------
//   Location::measure
//---------------------------------------------------------

int Location::measure(const Element* e)
      {
      const Measure* m = toMeasure(e->findMeasure());
      if (m)
            return m->measureIndex();
      qWarning("Location::measure: cannot find element's measure (%s)", e->name());
      return 0;
      }

//---------------------------------------------------------
//   Location::graceIndex
//---------------------------------------------------------

int Location::graceIndex(const Element* e)
      {
      if (e->isChord() || (e->parent() && e->parent()->isChord())) {
            const Chord* ch = e->isChord() ? toChord(e) : toChord(e->parent());
            if (ch->isGrace())
                  return ch->graceIndex();
            }
      return absDefaults.graceIndex();
      }

//---------------------------------------------------------
//   Location::note
//---------------------------------------------------------

int Location::note(const Element* e)
      {
      if (e->isNote()) {
            const Note* n = toNote(e);
            const std::vector<Note*>& notes = n->chord()->notes();
            if (notes.size() == 1)
                  return 0;
            int noteIdx;
            for (noteIdx = 0; noteIdx < int(notes.size()); ++noteIdx) {
                  if (n == notes.at(noteIdx))
                        break;
                  }
            return noteIdx;
            }
      return absDefaults.note();
      }

//---------------------------------------------------------
//   Location::getLocationProperty
//---------------------------------------------------------

QVariant Location::getLocationProperty(Pid pid, const Element* start, const Element* end)
      {
      switch(pid) {
            case Pid::LOCATION_STAVES:
                  return (track(start) / VOICES) - (track(end) / VOICES);
            case Pid::LOCATION_VOICES:
                  return (track(start) % VOICES) - (track(end) / VOICES);
            case Pid::LOCATION_MEASURES:
                  return measure(end) - measure(start);
            case Pid::LOCATION_FRACTIONS:
                  return end->rtick() - start->rtick();
            case Pid::LOCATION_GRACE:
                  return graceIndex(end) - graceIndex(end);
            case Pid::LOCATION_NOTE:
                  return note(start) - note(end);
            default:
                  return QVariant();
            }
      }

//---------------------------------------------------------
//   Location::operator==
//---------------------------------------------------------

bool Location::operator==(const Location& pi2) const {
      const Location& pi1 = *this;
      return ((pi1._frac == pi2._frac)
             && (pi1._measure == pi2._measure)
             && (pi1._voice == pi2._voice)
             && (pi1._staff == pi2._staff)
             && (pi1._graceIndex == pi2._graceIndex)
             && (pi1._note == pi2._note)
             );
      }
}