File: editharmony.cpp

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-21
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 219,852 kB
  • sloc: cpp: 291,412; xml: 200,226; sh: 3,779; ansic: 1,447; python: 393; makefile: 246; perl: 82; pascal: 79
file content (284 lines) | stat: -rw-r--r-- 9,404 bytes parent folder | download | duplicates (5)
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

//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Copyright (C) 2002-2017 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 "musescore.h"
#include "scoreview.h"
#include "texttools.h"
#include "libmscore/chordrest.h"
#include "libmscore/harmony.h"
#include "libmscore/score.h"
#include "libmscore/segment.h"
#include "libmscore/measure.h"

namespace Ms {

//---------------------------------------------------------
//   harmonyTab
//---------------------------------------------------------

void ScoreView::harmonyTab(bool back)
      {
      Harmony* harmony = toHarmony(editData.element);
      if (!harmony->parent() || !harmony->parent()->isSegment()) {
            qDebug("no segment parent");
            return;
            }
      int track        = harmony->track();
      Segment* segment = toSegment(harmony->parent());
      if (!segment) {
            qDebug("harmonyTicksTab: no segment");
            return;
            }

      // moving to next/prev measure

      Measure* measure = segment->measure();
      if (measure) {
            if (back)
                  measure = measure->prevMeasure();
            else
                  measure = measure->nextMeasure();
            }
      if (!measure) {
            qDebug("no prev/next measure");
            return;
            }

      segment = measure->findSegment(SegmentType::ChordRest, measure->tick());
      if (!segment) {
            qDebug("no ChordRest segment as measure");
            return;
            }

      changeState(ViewState::NORMAL);

      // search for next chord name
      harmony = 0;
      for (Element* e : segment->annotations()) {
            if (e->isHarmony() && e->track() == track) {
                  Harmony* h = toHarmony(e);
                  harmony = h;
                  break;
                  }
            }

      if (!harmony) {
            harmony = new Harmony(_score);
            harmony->setTrack(track);
            harmony->setParent(segment);
            _score->startCmd();
            _score->undoAddElement(harmony);
            _score->endCmd();
            }

      _score->select(harmony, SelectType::SINGLE, 0);
      startEdit(harmony, Grip::NO_GRIP);

      adjustCanvasPosition(harmony, false);
      TextCursor* cursor = harmony->cursor(editData);
      cursor->moveCursorToEnd();
      _score->update();
      }

//---------------------------------------------------------
//   harmonyBeatsTab
//    manages [;:], moving forward or back to the next beat
//    and Space/Shift-Space, to stop at next note, rest, harmony or beat.
//---------------------------------------------------------

void ScoreView::harmonyBeatsTab(bool noterest, bool back)
      {
      Harmony* harmony = toHarmony(editData.element);
      if (!harmony->parent() || !harmony->parent()->isSegment()) {
            qDebug("no segment parent");
            return;
            }
      int track        = harmony->track();
      Segment* segment = toSegment(harmony->parent());
      if (!segment) {
            qDebug("no segment");
            return;
            }
      Measure* measure = segment->measure();
      Fraction tick = segment->tick();

      if (back && tick == measure->tick()) {
            // previous bar, if any
            measure = measure->prevMeasure();
            if (!measure) {
                  qDebug("no previous measure");
                  return;
                  }
            }

      Fraction f = measure->ticks();
      int ticksPerBeat   = f.ticks() / ((f.numerator()>3 && (f.numerator()%3)==0 && f.denominator()>4) ? f.numerator()/3 : f.numerator());
      Fraction tickInBar = tick - measure->tick();
      Fraction newTick   = measure->tick()
                           + Fraction::fromTicks((
                              (tickInBar.ticks() + (back? -1 : ticksPerBeat)) / ticksPerBeat
                              )
                              * ticksPerBeat);

      changeState(ViewState::NORMAL);

      _score->startCmd();
      // look for next/prev beat, note, rest or chord
      for (;;) {
            segment = back ? segment->prev1(SegmentType::ChordRest) : segment->next1(SegmentType::ChordRest);

            if (!segment || (back ? (segment->tick() < newTick) : (segment->tick() > newTick))) {
                  // no segment or moved past the beat - create new segment
                  if (!back && newTick >= measure->tick() + f) {
                        // next bar, if any
                        measure = measure->nextMeasure();
                        if (!measure) {
                              qDebug("no next measure");
                              return;
                              }
                        }
                  segment = new Segment(measure, SegmentType::ChordRest, newTick - measure->tick());
                  if (!segment) {
                        qDebug("no prev segment");
                        return;
                        }
                  _score->undoAddElement(segment);
                  break;
                  }

            if (segment->tick() == newTick)
                  break;

            if (noterest) {
                  int minTrack = (track / VOICES ) * VOICES;
                  int maxTrack = minTrack + (VOICES-1);
                  if (segment->hasAnnotationOrElement(ElementType::HARMONY, minTrack, maxTrack))
                        break;
                  }
            }

      // search for next chord name
      harmony = 0;
      for (Element* e : segment->annotations()) {
            if (e->isHarmony() && e->track() == track) {
                  Harmony* h = toHarmony(e);
                  harmony = h;
                  break;
                  }
            }

      if (!harmony) {
            harmony = new Harmony(_score);
            harmony->setTrack(track);
            harmony->setParent(segment);
            _score->undoAddElement(harmony);
            }
      _score->endCmd();

      _score->select(harmony, SelectType::SINGLE, 0);
      startEdit(harmony, Grip::NO_GRIP);

      adjustCanvasPosition(harmony, false);
      TextCursor* cursor = harmony->cursor(editData);
      cursor->moveCursorToEnd();
      _score->update();
      }

//---------------------------------------------------------
//   harmonyTicksTab
//    manages [Ctrl] [1]-[9], moving forward the given number of ticks
//---------------------------------------------------------

void ScoreView::harmonyTicksTab(const Fraction& ticks)
      {
      Harmony* harmony = static_cast<Harmony*>(editData.element);
      if (!harmony->parent() || !harmony->parent()->isSegment()) {
            qDebug("no segment parent");
            return;
            }
      int track        = harmony->track();
      Segment* segment = toSegment(harmony->parent());
      if (!segment) {
            qDebug("no segment");
            return;
            }
      Measure* measure = segment->measure();

      Fraction newTick   = segment->tick() + ticks;

      // find the measure containing the target tick
      while (newTick >= measure->tick() + measure->ticks()) {
            measure = measure->nextMeasure();
            if (!measure) {
                  qDebug("no next measure");
                  return;
                  }
            }

      changeState(ViewState::NORMAL);

      // look for a segment at this tick; if none, create one
      while (segment && segment->tick() < newTick)
            segment = segment->next1(SegmentType::ChordRest);
      if (!segment || segment->tick() > newTick) {      // no ChordRest segment at this tick
            segment = new Segment(measure, SegmentType::ChordRest, newTick - measure->tick());
            _score->startCmd();
            _score->undoAddElement(segment);
            _score->endCmd();
            }

      // search for next chord name
      harmony = 0;
      for (Element* e : segment->annotations()) {
            if (e->isHarmony() && e->track() == track) {
                  Harmony* h = toHarmony(e);
                  harmony = h;
                  break;
                  }
            }

      if (!harmony) {
            harmony = new Harmony(_score);
            harmony->setTrack(track);
            harmony->setParent(segment);
            _score->startCmd();
            _score->undoAddElement(harmony);
            _score->endCmd();
            }

      _score->select(harmony, SelectType::SINGLE, 0);
      startEdit(harmony, Grip::NO_GRIP);

      adjustCanvasPosition(harmony, false);
      TextCursor* cursor = harmony->cursor(editData);
      cursor->moveCursorToEnd();
      _score->update();
      }

//---------------------------------------------------------
//   harmonyEndEdit
//---------------------------------------------------------

void ScoreView::harmonyEndEdit()
      {
      Harmony* harmony = toHarmony(editData.element);

      if (harmony->empty()) {
            _score->startCmd();
            _score->undoRemoveElement(harmony);
            _score->endCmd();
            }
      }

}