MuseScore/libmscore/splitMeasure.cpp

127 lines
4.2 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2012 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 "score.h"
#include "measure.h"
#include "segment.h"
#include "chordrest.h"
#include "range.h"
#include "tuplet.h"
2017-03-14 17:00:38 +01:00
#include "spanner.h"
#include "tie.h"
#include "undo.h"
#include "utils.h"
2012-05-26 14:26:10 +02:00
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// cmdSplitMeasure
//---------------------------------------------------------
void Score::cmdSplitMeasure(ChordRest* cr)
2016-08-06 10:33:32 +02:00
{
startCmd();
splitMeasure(cr->segment());
2016-08-06 10:33:32 +02:00
endCmd();
}
//---------------------------------------------------------
// splitMeasure
// return true on success
2016-08-06 10:33:32 +02:00
//---------------------------------------------------------
void Score::splitMeasure(Segment* segment)
2012-05-26 14:26:10 +02:00
{
if (segment->rtick().isZero()) {
MScore::setError(CANNOT_SPLIT_MEASURE_FIRST_BEAT);
return;
}
if (segment->splitsTuplet()) {
MScore::setError(CANNOT_SPLIT_MEASURE_TUPLET);
return;
}
2012-05-26 14:26:10 +02:00
Measure* measure = segment->measure();
ScoreRange range;
2014-05-14 19:19:29 +02:00
range.read(measure->first(), measure->last());
2012-05-26 14:26:10 +02:00
Fraction stick = measure->tick();
Fraction etick = measure->endTick();
2017-03-14 17:00:38 +01:00
std::list<std::tuple<Spanner*, Fraction, Fraction>> sl;
2017-03-14 17:00:38 +01:00
for (auto i : spanner()) {
Spanner* s = i.second;
Element* start = s->startElement();
Element* end = s->endElement();
2017-03-14 17:00:38 +01:00
if (s->tick() >= stick && s->tick() < etick)
start = nullptr;
2017-03-14 17:00:38 +01:00
if (s->tick2() >= stick && s->tick2() < etick)
end = nullptr;
if (start != s->startElement() || end != s->endElement())
undo(new ChangeStartEndSpanner(s, start, end));
if (s->tick() < stick && s->tick2() > stick)
sl.push_back(make_tuple(s, s->tick(), s->ticks()));
2017-03-14 17:00:38 +01:00
}
// Make sure ties are the beginning the split measure are restored.
std::vector<Tie*> ties;
for (int track = 0; track < ntracks(); track++) {
Chord* chord = measure->findChord(stick, track);
if (chord)
for (Note* note : chord->notes()) {
Tie* tie = note->tieBack();
if (tie)
ties.push_back(tie->clone());
}
}
2017-03-14 17:00:38 +01:00
MeasureBase* nm = measure->next();
2016-04-11 15:28:32 +02:00
undoRemoveMeasures(measure, measure);
undoInsertTime(measure->tick(), -measure->ticks());
2012-05-26 14:26:10 +02:00
// create empty measures:
insertMeasure(ElementType::MEASURE, nm, true, false);
2017-03-14 17:00:38 +01:00
Measure* m2 = toMeasure(nm ? nm->prev() : lastMeasure());
insertMeasure(ElementType::MEASURE, m2, true, false);
2017-03-14 17:00:38 +01:00
Measure* m1 = toMeasure(m2->prev());
2012-05-26 14:26:10 +02:00
Fraction tick = segment->tick();
2012-05-26 14:26:10 +02:00
m1->setTick(measure->tick());
m2->setTick(tick);
Fraction ticks1 = segment->tick() - measure->tick();
Fraction ticks2 = measure->ticks() - ticks1;
2012-05-26 14:26:10 +02:00
m1->setTimesig(measure->timesig());
m2->setTimesig(measure->timesig());
m1->adjustToLen(ticks1.reduced(), false);
m2->adjustToLen(ticks2.reduced(), false);
2014-05-14 19:19:29 +02:00
range.write(this, m1->tick());
// Restore ties the the beginning of the split measure.
for (auto tie : ties) {
tie->setEndNote(searchTieNote(tie->startNote()));
undoAddElement(tie);
}
for (auto i : sl) {
Spanner* s = std::get<0>(i);
Fraction t = std::get<1>(i);
Fraction ticks = std::get<2>(i);
2018-08-17 15:06:15 +02:00
if (s->tick() != t)
s->undoChangeProperty(Pid::SPANNER_TICK, t);
if (s->ticks() != ticks)
s->undoChangeProperty(Pid::SPANNER_TICKS, ticks);
}
2012-05-26 14:26:10 +02:00
}
2013-05-13 18:49:17 +02:00
}