forked from musescore/MuseScore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.cpp
3933 lines (3609 loc) · 162 KB
/
cmd.cpp
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2013 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
//=============================================================================
/**
\file
Handling of several GUI commands.
*/
#include <assert.h>
#include "types.h"
#include "musescoreCore.h"
#include "score.h"
#include "utils.h"
#include "key.h"
#include "clef.h"
#include "navigate.h"
#include "slur.h"
#include "tie.h"
#include "note.h"
#include "rest.h"
#include "chord.h"
#include "text.h"
#include "sig.h"
#include "staff.h"
#include "part.h"
#include "style.h"
#include "page.h"
#include "barline.h"
#include "tuplet.h"
#include "xml.h"
#include "ottava.h"
#include "trill.h"
#include "pedal.h"
#include "hairpin.h"
#include "textline.h"
#include "keysig.h"
#include "volta.h"
#include "dynamic.h"
#include "box.h"
#include "harmony.h"
#include "system.h"
#include "stafftext.h"
#include "articulation.h"
#include "layoutbreak.h"
#include "drumset.h"
#include "beam.h"
#include "lyrics.h"
#include "pitchspelling.h"
#include "measure.h"
#include "tempo.h"
#include "undo.h"
#include "timesig.h"
#include "repeat.h"
#include "tempotext.h"
#include "noteevent.h"
#include "breath.h"
#include "stringdata.h"
#include "stafftype.h"
#include "segment.h"
#include "chordlist.h"
#include "mscore.h"
#include "accidental.h"
#include "sequencer.h"
#include "tremolo.h"
#include "rehearsalmark.h"
#include "sym.h"
namespace Ms {
//---------------------------------------------------------
// reset
//---------------------------------------------------------
void CmdState::reset()
{
layoutFlags = LayoutFlag::NO_FLAGS;
_updateMode = UpdateMode::DoNothing;
_startTick = Fraction(-1,1);
_endTick = Fraction(-1,1);
_startStaff = -1;
_endStaff = -1;
_el = nullptr;
_oneElement = true;
_mb = nullptr;
_oneMeasureBase = true;
_locked = false;
}
//---------------------------------------------------------
// setTick
//---------------------------------------------------------
void CmdState::setTick(const Fraction& t)
{
if (_locked)
return;
if (_startTick == Fraction(-1,1) || t < _startTick)
_startTick = t;
if (_endTick == Fraction(-1,1) || t > _endTick)
_endTick = t;
setUpdateMode(UpdateMode::Layout);
}
//---------------------------------------------------------
// setStaff
//---------------------------------------------------------
void CmdState::setStaff(int st)
{
Q_ASSERT(st > -2);
if (_locked || st == -1)
return;
if (_startStaff == -1 || st < _startStaff)
_startStaff = st;
if (_endStaff == -1 || st > _endStaff)
_endStaff = st;
}
//---------------------------------------------------------
// setMeasureBase
//---------------------------------------------------------
void CmdState::setMeasureBase(const MeasureBase* mb)
{
if (!mb || _mb == mb || _locked)
return;
_oneMeasureBase = !_mb;
_mb = mb;
}
//---------------------------------------------------------
// setElement
//---------------------------------------------------------
void CmdState::setElement(const Element* e)
{
if (!e || _el == e || _locked)
return;
_oneElement = !_el;
_el = e;
if (_oneMeasureBase)
setMeasureBase(e->findMeasureBase());
}
//---------------------------------------------------------
// unsetElement
//---------------------------------------------------------
void CmdState::unsetElement(const Element* e)
{
if (_el == e)
_el = nullptr;
if (_mb == e)
_mb = nullptr;
}
//---------------------------------------------------------
// element
//---------------------------------------------------------
const Element* CmdState::element() const
{
if (_oneElement)
return _el;
if (_oneMeasureBase)
return _mb;
return nullptr;
}
//---------------------------------------------------------
// setUpdateMode
//---------------------------------------------------------
void CmdState::_setUpdateMode(UpdateMode m)
{
_updateMode = m;
}
void CmdState::setUpdateMode(UpdateMode m)
{
if (int(m) > int(_updateMode))
_setUpdateMode(m);
}
//---------------------------------------------------------
// startCmd
/// Start a GUI command by clearing the redraw area
/// and starting a user-visible undo.
//---------------------------------------------------------
void Score::startCmd()
{
if (MScore::debugMode)
qDebug("===startCmd()");
cmdState().reset();
// Start collecting low-level undo operations for a
// user-visible undo action.
if (undoStack()->active()) {
qDebug("Score::startCmd(): cmd already active");
return;
}
undoStack()->beginMacro(this);
}
//---------------------------------------------------------
// undoRedo
//---------------------------------------------------------
void Score::undoRedo(bool undo, EditData* ed)
{
if (readOnly())
return;
cmdState().reset();
if (undo)
undoStack()->undo(ed);
else
undoStack()->redo(ed);
update(false);
masterScore()->setPlaylistDirty(); // TODO: flag all individual operations
updateSelection();
}
//---------------------------------------------------------
// endCmd
/// End a GUI command by (if \a undo) ending a user-visble undo
/// and (always) updating the redraw area.
//---------------------------------------------------------
void Score::endCmd(bool rollback)
{
if (!undoStack()->active()) {
qDebug("Score::endCmd(): no cmd active");
update();
return;
}
if (readOnly() || MScore::_error != MS_NO_ERROR)
rollback = true;
if (rollback)
undoStack()->current()->unwind();
update(false);
if (MScore::debugMode)
qDebug("===endCmd() %d", undoStack()->current()->childCount());
const bool noUndo = undoStack()->current()->empty(); // nothing to undo?
undoStack()->endMacro(noUndo);
if (dirty()) {
masterScore()->setPlaylistDirty(); // TODO: flag individual operations
masterScore()->setAutosaveDirty(true);
}
MuseScoreCore::mscoreCore->endCmd();
cmdState().reset();
}
#ifndef NDEBUG
//---------------------------------------------------------
// CmdState::dump
//---------------------------------------------------------
void CmdState::dump()
{
qDebug("CmdState: mode %d %d-%d", int(_updateMode), _startTick.ticks(), _endTick.ticks());
// bool _excerptsChanged { false };
// bool _instrumentsChanged { false };
}
#endif
//---------------------------------------------------------
// update
// layout & update
//---------------------------------------------------------
void Score::update(bool resetCmdState)
{
bool updateAll = false;
for (MasterScore* ms : *movements()) {
CmdState& cs = ms->cmdState();
ms->deletePostponed();
if (cs.layoutRange()) {
for (Score* s : ms->scoreList())
s->doLayoutRange(cs.startTick(), cs.endTick());
updateAll = true;
}
}
for (MasterScore* ms : *movements()) {
CmdState& cs = ms->cmdState();
if (updateAll || cs.updateAll()) {
for (Score* s : scoreList()) {
for (MuseScoreView* v : s->viewer) {
v->updateAll();
}
}
}
else if (cs.updateRange()) {
// updateRange updates only current score
qreal d = spatium() * .5;
_updateState.refresh.adjust(-d, -d, 2 * d, 2 * d);
for (MuseScoreView* v : viewer)
v->dataChanged(_updateState.refresh);
_updateState.refresh = QRectF();
}
const InputState& is = inputState();
if (is.noteEntryMode() && is.segment()) {
setPlayPos(is.segment()->tick());
}
if (playlistDirty()) {
for (Score* s : scoreList())
emit s->playlistChanged();
masterScore()->setPlaylistClean();
}
if (resetCmdState)
cs.reset();
}
if (_selection.isRange())
_selection.updateSelectedElements();
}
//---------------------------------------------------------
// deletePostponed
//---------------------------------------------------------
void Score::deletePostponed()
{
for (ScoreElement* e : _updateState._deleteList) {
if (e->isSystem()) {
System* s = toSystem(e);
for (SpannerSegment* ss : s->spannerSegments()) {
if (ss->system() == s)
ss->setSystem(0);
}
}
}
qDeleteAll(_updateState._deleteList);
_updateState._deleteList.clear();
}
//---------------------------------------------------------
// cmdAddSpanner
// drop VOLTA, OTTAVA, TRILL, PEDAL, DYNAMIC
// HAIRPIN, LET_RING, VIBRATO and TEXTLINE
//---------------------------------------------------------
void Score::cmdAddSpanner(Spanner* spanner, const QPointF& pos, bool firstStaffOnly)
{
int staffIdx;
Segment* segment;
MeasureBase* mb = pos2measure(pos, &staffIdx, 0, &segment, 0);
if (firstStaffOnly)
staffIdx = 0;
// ignore if we do not have a measure
if (mb == 0 || mb->type() != ElementType::MEASURE) {
qDebug("cmdAddSpanner: cannot put object here");
delete spanner;
return;
}
// all spanners live in voice 0 (except slurs/ties)
int track = staffIdx == -1 ? -1 : staffIdx * VOICES;
spanner->setTrack(track);
spanner->setTrack2(track);
if (spanner->anchor() == Spanner::Anchor::SEGMENT) {
spanner->setTick(segment->tick());
Fraction lastTick = lastMeasure()->tick() + lastMeasure()->ticks();
Fraction tick2 = qMin(segment->measure()->tick() + segment->measure()->ticks(), lastTick);
spanner->setTick2(tick2);
}
else { // Anchor::MEASURE, Anchor::CHORD, Anchor::NOTE
Measure* m = toMeasure(mb);
QRectF b(m->canvasBoundingRect());
if (pos.x() >= (b.x() + b.width() * .5) && m != lastMeasureMM() && m->nextMeasure()->system() == m->system())
m = m->nextMeasure();
spanner->setTick(m->tick());
spanner->setTick2(m->endTick());
}
spanner->eraseSpannerSegments();
undoAddElement(spanner);
select(spanner, SelectType::SINGLE, 0);
}
//---------------------------------------------------------
// cmdAddSpanner
// used when applying a spanner to a selection
//---------------------------------------------------------
void Score::cmdAddSpanner(Spanner* spanner, int staffIdx, Segment* startSegment, Segment* endSegment)
{
int track = staffIdx * VOICES;
spanner->setTrack(track);
spanner->setTrack2(track);
for (auto ss : spanner->spannerSegments())
ss->setTrack(track);
spanner->setTick(startSegment->tick());
Fraction tick2;
if (!endSegment)
tick2 = lastSegment()->tick();
else if (endSegment == startSegment)
tick2 = startSegment->measure()->last()->tick();
else
tick2 = endSegment->tick();
spanner->setTick2(tick2);
#if 0 // TODO
TextLine* tl = toTextLine(spanner);
if (tl) {
StyledPropertyListIdx st;
Text* t;
// begin
t = tl->beginTextElement();
if (t) {
st = t->textStyleType();
if (st >= StyledPropertyListIdx::DEFAULT)
t->textStyle().restyle(MScore::baseStyle().textStyle(st), textStyle(st));
}
// continue
t = tl->continueTextElement();
if (t) {
st = t->textStyleType();
if (st >= StyledPropertyListIdx::DEFAULT)
t->textStyle().restyle(MScore::baseStyle().textStyle(st), textStyle(st));
}
// end
t = tl->endTextElement();
if (t) {
st = t->textStyleType();
if (st >= StyledPropertyListIdx::DEFAULT)
t->textStyle().restyle(MScore::baseStyle().textStyle(st), textStyle(st));
}
}
#endif
undoAddElement(spanner);
}
//---------------------------------------------------------
// expandVoice
// fills gaps in voice with rests,
// from previous cr (or beginning of measure) to next cr (or end of measure)
//---------------------------------------------------------
void Score::expandVoice(Segment* s, int track)
{
if (!s) {
qDebug("expand voice: no segment");
return;
}
if (s->element(track))
return;
// find previous segment with cr in this track
Segment* ps;
for (ps = s; ps; ps = ps->prev(SegmentType::ChordRest)) {
if (ps->element(track))
break;
}
if (ps) {
ChordRest* cr = toChordRest(ps->element(track));
Fraction tick = cr->tick() + cr->actualTicks();
if (tick > s->tick()) {
// previous cr extends past current segment
qDebug("expandVoice: cannot insert element here");
return;
}
if (cr->isChord()) {
// previous cr ends on or before current segment
// for chords, move ps to just after cr ends
// so we can fill any gap that might exist
// but don't move ps if previous cr is a rest
// this will be combined with any new rests needed to fill up to s->tick() below
ps = ps->measure()->undoGetSegment(SegmentType::ChordRest, tick);
}
}
//
// fill up to s->tick() with rests
//
Measure* m = s->measure();
Fraction stick = ps ? ps->tick() : m->tick();
Fraction ticks = s->tick() - stick;
if (ticks.isNotZero())
setRest(stick, track, ticks, false, 0);
//
// fill from s->tick() until next chord/rest in measure
//
Segment* ns;
for (ns = s->next(SegmentType::ChordRest); ns; ns = ns->next(SegmentType::ChordRest)) {
if (ns->element(track))
break;
}
ticks = ns ? (ns->tick() - s->tick()) : (m->ticks() - s->rtick());
if (ticks == m->ticks())
addRest(s, track, TDuration(TDuration::DurationType::V_MEASURE), 0);
else
setRest(s->tick(), track, ticks, false, 0);
}
void Score::expandVoice()
{
Segment* s = _is.segment();
int track = _is.track();
expandVoice(s, track);
}
//---------------------------------------------------------
// cmdAddInterval
//---------------------------------------------------------
void Score::cmdAddInterval(int val, const std::vector<Note*>& nl)
{
startCmd();
for (Note* on : nl) {
Note* note = new Note(this);
Chord* chord = on->chord();
note->setParent(chord);
note->setTrack(chord->track());
int valTmp = val < 0 ? val+1 : val-1;
int npitch;
int ntpc1;
int ntpc2;
bool accidental = _is.noteEntryMode() && _is.accidentalType() != AccidentalType::NONE;
bool forceAccidental = false;
if (abs(valTmp) != 7 || accidental) {
int line = on->line() - valTmp;
Fraction tick = chord->tick();
Staff* estaff = staff(on->staffIdx() + chord->staffMove());
ClefType clef = estaff->clef(tick);
Key key = estaff->key(tick);
int ntpc;
if (accidental) {
AccidentalVal acci = Accidental::subtype2value(_is.accidentalType());
int step = absStep(line, clef);
int octave = step / 7;
npitch = step2pitch(step) + octave * 12 + int(acci);
forceAccidental = (npitch == line2pitch(line, clef, key));
ntpc = step2tpc(step % 7, acci);
}
else {
npitch = line2pitch(line, clef, key);
ntpc = pitch2tpc(npitch, key, Prefer::NEAREST);
}
Interval v = on->part()->instrument(tick)->transpose();
if (v.isZero())
ntpc1 = ntpc2 = ntpc;
else {
if (styleB(Sid::concertPitch)) {
v.flip();
ntpc1 = ntpc;
ntpc2 = Ms::transposeTpc(ntpc, v, true);
}
else {
npitch += v.chromatic;
ntpc2 = ntpc;
ntpc1 = Ms::transposeTpc(ntpc, v, true);
}
}
}
else { //special case for octave
Interval interval(7, 12);
if (val < 0)
interval.flip();
transposeInterval(on->pitch(), on->tpc(), &npitch, &ntpc1, interval, false);
ntpc1 = on->tpc1();
ntpc2 = on->tpc2();
}
if (npitch < 0 || npitch > 127) {
delete note;
endCmd();
return;
}
note->setPitch(npitch, ntpc1, ntpc2);
undoAddElement(note);
if (forceAccidental) {
Accidental* a = new Accidental(this);
a->setAccidentalType(_is.accidentalType());
a->setRole(AccidentalRole::USER);
a->setParent(note);
undoAddElement(a);
}
setPlayNote(true);
select(note, SelectType::SINGLE, 0);
}
if (_is.noteEntryMode())
_is.setAccidentalType(AccidentalType::NONE);
_is.moveToNextInputPos();
endCmd();
}
//---------------------------------------------------------
// setGraceNote
/// Create a grace note in front of a normal note.
/// \arg ch is the chord of the normal note
/// \arg pitch is the pitch of the grace note
/// \arg is the grace note type
/// \len is the visual duration of the grace note (1/16 or 1/32)
//---------------------------------------------------------
Note* Score::setGraceNote(Chord* ch, int pitch, NoteType type, int len)
{
Note* note = new Note(this);
Chord* chord = new Chord(this);
// allow grace notes to be added to other grace notes
// by really adding to parent chord
if (ch->noteType() != NoteType::NORMAL)
ch = toChord(ch->parent());
chord->setTrack(ch->track());
chord->setParent(ch);
chord->add(note);
note->setPitch(pitch);
// find corresponding note within chord and use its tpc information
for (Note* n : ch->notes()) {
if (n->pitch() == pitch) {
note->setTpc1(n->tpc1());
note->setTpc2(n->tpc2());
break;
}
}
// note with same pitch not found, derive tpc from pitch / key
if (!tpcIsValid(note->tpc1()) || !tpcIsValid(note->tpc2()))
note->setTpcFromPitch();
TDuration d;
d.setVal(len);
chord->setDurationType(d);
chord->setTicks(d.fraction());
chord->setNoteType(type);
chord->setMag(ch->staff()->mag(chord->tick()) * styleD(Sid::graceNoteMag));
undoAddElement(chord);
select(note, SelectType::SINGLE, 0);
return note;
}
//---------------------------------------------------------
// createCRSequence
// Create a rest or chord of len f.
// If f is not a basic len, create several rests or
// tied chords.
//
// f total len of ChordRest
// cr prototype CR
// tick start position in measure
//---------------------------------------------------------
void Score::createCRSequence(const Fraction& f, ChordRest* cr, const Fraction& t)
{
Fraction tick(t);
Measure* measure = cr->measure();
ChordRest* ocr = 0;
for (TDuration d : toDurationList(f, true)) {
ChordRest* ncr = toChordRest(cr->clone());
ncr->setDurationType(d);
ncr->setTicks(d.fraction());
undoAddCR(ncr, measure, measure->tick() + tick);
if (cr->isChord() && ocr) {
Chord* nc = toChord(ncr);
Chord* oc = toChord(ocr);
for (unsigned int i = 0; i < oc->notes().size(); ++i) {
Note* on = oc->notes()[i];
Note* nn = nc->notes()[i];
Tie* tie = new Tie(this);
tie->setStartNote(on);
tie->setEndNote(nn);
tie->setTrack(cr->track());
on->setTieFor(tie);
nn->setTieBack(tie);
undoAddElement(tie);
}
}
tick += ncr->actualTicks();
ocr = ncr;
}
}
//---------------------------------------------------------
// setNoteRest
// pitch == -1 -> set rest
// return segment of last created note/rest
//---------------------------------------------------------
Segment* Score::setNoteRest(Segment* segment, int track, NoteVal nval, Fraction sd, Direction stemDirection, bool forceAccidental, bool rhythmic)
{
Q_ASSERT(segment->segmentType() == SegmentType::ChordRest);
bool isRest = nval.pitch == -1;
Fraction tick = segment->tick();
Element* nr = 0;
Tie* tie = 0;
ChordRest* cr = toChordRest(segment->element(track));
Measure* measure = 0;
for (;;) {
if (track % VOICES)
expandVoice(segment, track);
// the returned gap ends at the measure boundary or at tuplet end
Fraction dd = makeGap(segment, track, sd, cr ? cr->tuplet() : 0);
if (dd.isZero()) {
qDebug("cannot get gap at %d type: %d/%d", tick.ticks(), sd.numerator(),
sd.denominator());
break;
}
measure = segment->measure();
std::vector<TDuration> dl;
if (rhythmic)
dl = toRhythmicDurationList(dd, isRest, segment->rtick(), sigmap()->timesig(tick).nominal(), measure, 1);
else
dl = toDurationList(dd, true);
size_t n = dl.size();
for (size_t i = 0; i < n; ++i) {
const TDuration& d = dl[i];
ChordRest* ncr;
Note* note = 0;
Tie* addTie = 0;
if (isRest) {
nr = ncr = new Rest(this);
nr->setTrack(track);
ncr->setDurationType(d);
ncr->setTicks(d == TDuration::DurationType::V_MEASURE ? measure->ticks() : d.fraction());
}
else {
nr = note = new Note(this);
if (tie) {
tie->setEndNote(note);
note->setTieBack(tie);
addTie = tie;
}
Chord* chord = new Chord(this);
chord->setTrack(track);
chord->setDurationType(d);
chord->setTicks(d.fraction());
chord->setStemDirection(stemDirection);
chord->add(note);
note->setNval(nval, tick);
if (forceAccidental) {
int tpc = styleB(Sid::concertPitch) ? nval.tpc1 : nval.tpc2;
AccidentalVal alter = tpc2alter(tpc);
AccidentalType at = Accidental::value2subtype(alter);
Accidental* a = new Accidental(this);
a->setAccidentalType(at);
a->setRole(AccidentalRole::USER);
note->add(a);
}
ncr = chord;
if (i+1 < n) {
tie = new Tie(this);
tie->setStartNote(note);
tie->setTrack(track);
note->setTieFor(tie);
}
}
ncr->setTuplet(cr ? cr->tuplet() : 0);
undoAddCR(ncr, measure, tick);
if (addTie)
undoAddElement(addTie);
setPlayNote(true);
segment = ncr->segment();
tick += ncr->actualTicks();
}
sd -= dd;
if (sd.isZero())
break;
Segment* nseg = tick2segment(tick, false, SegmentType::ChordRest);
if (nseg == 0) {
qDebug("reached end of score");
break;
}
segment = nseg;
cr = toChordRest(segment->element(track));
if (cr == 0) {
if (track % VOICES)
cr = addRest(segment, track, TDuration(TDuration::DurationType::V_MEASURE), 0);
else {
qDebug("no rest in voice 0");
break;
}
}
//
// Note does not fit on current measure, create Tie to
// next part of note
if (!isRest) {
tie = new Tie(this);
tie->setStartNote((Note*)nr);
tie->setTrack(nr->track());
((Note*)nr)->setTieFor(tie);
}
}
if (tie)
connectTies();
if (nr) {
if (_is.slur() && nr->type() == ElementType::NOTE) {
// If the start element was the same as the end element when the slur was created,
// the end grip of the front slur segment was given an x-offset of 3.0 * spatium().
// Now that the slur is about to be given a new end element, this should be reset.
if (_is.slur()->endElement() == _is.slur()->startElement())
_is.slur()->frontSegment()->reset();
//
// extend slur
//
Chord* chord = toNote(nr)->chord();
_is.slur()->undoChangeProperty(Pid::SPANNER_TICKS, chord->tick() - _is.slur()->tick());
for (ScoreElement* se : _is.slur()->linkList()) {
Slur* slur = toSlur(se);
for (ScoreElement* ee : chord->linkList()) {
Element* e = static_cast<Element*>(ee);
if (e->score() == slur->score() && e->track() == slur->track2()) {
slur->score()->undo(new ChangeSpannerElements(slur, slur->startElement(), e));
break;
}
}
}
}
select(nr, SelectType::SINGLE, 0);
}
return segment;
}
//---------------------------------------------------------
// makeGap
// make time gap at tick by removing/shortening
// chord/rest
//
// if keepChord, the chord at tick is not removed
//
// gap does not exceed measure or scope of tuplet
//
// return size of actual gap
//---------------------------------------------------------
Fraction Score::makeGap(Segment* segment, int track, const Fraction& _sd, Tuplet* tuplet, bool keepChord)
{
Q_ASSERT(_sd.numerator());
Measure* measure = segment->measure();
Fraction accumulated;
Fraction sd = _sd;
//
// remember first segment which should
// not be deleted (it may contain other elements we want to preserve)
//
Segment* firstSegment = segment;
const Fraction firstSegmentEnd = firstSegment->tick() + firstSegment->ticks();
Fraction nextTick = segment->tick();
for (Segment* seg = firstSegment; seg; seg = seg->next(SegmentType::ChordRest)) {
//
// voices != 0 may have gaps:
//
ChordRest* cr = toChordRest(seg->element(track));
if (!cr) {
if (seg->tick() < nextTick)
continue;
Segment* seg1 = seg->next(SegmentType::ChordRest);
Fraction tick2 = seg1 ? seg1->tick() : seg->measure()->tick() + seg->measure()->ticks();
segment = seg;
Fraction td(tick2 - seg->tick());
if (td > sd)
td = sd;
accumulated += td;
sd -= td;
if (sd.isZero())
break;
nextTick = tick2;
continue;
}
if (seg->tick() > nextTick) {
// there was a gap
Fraction td(seg->tick() - nextTick);
if (td > sd)
td = sd;
accumulated += td;
sd -= td;
if (sd.isZero())
break;
}
//
// limit to tuplet level
//
if (tuplet) {
bool tupletEnd = true;
Tuplet* t = cr->tuplet();
while (t) {
if (cr->tuplet() == tuplet) {
tupletEnd = false;
break;
}
t = t->tuplet();
}
if (tupletEnd)
break;
}
Fraction td(cr->ticks());
// remove tremolo between 2 notes, if present
if (cr->isChord()) {
Chord* c = toChord(cr);
if (c->tremolo()) {
Tremolo* tremolo = c->tremolo();
if (tremolo->twoNotes())
undoRemoveElement(tremolo);
}
}
Tuplet* ltuplet = cr->tuplet();
if (ltuplet != tuplet) {
//
// Current location points to the start of a (nested)tuplet.
// We have to remove the complete tuplet.
// get top level tuplet
while (ltuplet->tuplet())
ltuplet = ltuplet->tuplet();
// get last segment of tuplet, drilling down to leaf nodes as necessary
Tuplet* t = ltuplet;
while (t->elements().back()->isTuplet())
t = toTuplet(t->elements().back());
seg = toChordRest(t->elements().back())->segment();
// now delete the full tuplet
td = ltuplet->ticks();
cmdDeleteTuplet(ltuplet, false);
tuplet = 0;
}
else {
if (seg != firstSegment || !keepChord)
undoRemoveElement(cr);
// even if there was a tuplet, we didn't remove it
ltuplet = 0;
}
Fraction timeStretch = cr->staff()->timeStretch(cr->tick());
nextTick += actualTicks(td, tuplet, timeStretch);
if (sd < td) {
//
// we removed too much
//
accumulated = _sd;
Fraction rd = td - sd;
std::vector<TDuration> dList = toDurationList(rd, false);
if (dList.empty())
break;
Fraction tick = cr->tick() + actualTicks(sd, tuplet, timeStretch);
if ((tuplet == 0) && (((measure->tick() - tick).ticks() % dList[0].ticks().ticks()) == 0)) {
for (TDuration d : dList) {
if (ltuplet) {
// take care not to recreate tuplet we just deleted
Rest* r = setRest(tick, track, d.fraction(), false, 0, false);
tick += r->actualTicks();
}
else {
tick += addClone(cr, tick, d)->actualTicks();
}
}
}
else {
for (int i = int(dList.size()) - 1; i >= 0; --i) {
if (ltuplet) {
// take care not to recreate tuplet we just deleted
Rest* r = setRest(tick, track, dList[i].fraction(), false, 0, false);
tick += r->actualTicks();