MuseScore/libmscore/splitMeasure.cpp

106 lines
3.4 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 "undo.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() == 0) {
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
2017-03-14 17:00:38 +01:00
int stick = measure->tick();
int etick = measure->endTick();
std::list<std::tuple<Spanner*, int, int>> 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
}
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:
2017-03-14 17:00:38 +01:00
insertMeasure(ElementType::MEASURE, nm, true);
Measure* m2 = toMeasure(nm ? nm->prev() : lastMeasure());
insertMeasure(ElementType::MEASURE, m2, true);
Measure* m1 = toMeasure(m2->prev());
2012-05-26 14:26:10 +02:00
int tick = segment->tick();
m1->setTick(measure->tick());
m2->setTick(tick);
int ticks1 = segment->tick() - measure->tick();
int ticks2 = measure->ticks() - ticks1;
m1->setTimesig(measure->timesig());
m2->setTimesig(measure->timesig());
m1->adjustToLen(Fraction::fromTicks(ticks1), false);
m2->adjustToLen(Fraction::fromTicks(ticks2), false);
2014-05-14 19:19:29 +02:00
range.write(this, m1->tick());
for (auto i : sl) {
Spanner* s = std::get<0>(i);
int tick = std::get<1>(i);
int ticks = std::get<2>(i);
if (s->tick() != tick)
s->undoChangeProperty(Pid::SPANNER_TICK, tick);
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
}