2021-04-19 16:06:08 +02:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
* MuseScore-CLA-applies
|
|
|
|
*
|
|
|
|
* MuseScore
|
|
|
|
* Music Composition & Notation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 MuseScore BVBA and others
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 3 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-05-26 14:26:10 +02:00
|
|
|
|
|
|
|
#include "instrchange.h"
|
2021-07-28 17:20:54 +02:00
|
|
|
|
|
|
|
#include "io/xml.h"
|
|
|
|
|
2012-05-26 14:26:10 +02:00
|
|
|
#include "score.h"
|
|
|
|
#include "segment.h"
|
|
|
|
#include "staff.h"
|
|
|
|
#include "part.h"
|
|
|
|
#include "undo.h"
|
|
|
|
#include "mscore.h"
|
2015-03-13 13:11:20 +01:00
|
|
|
#include "measure.h"
|
|
|
|
#include "system.h"
|
2019-08-01 18:06:52 +02:00
|
|
|
#include "chord.h"
|
|
|
|
#include "keysig.h"
|
2012-05-26 14:26:10 +02:00
|
|
|
|
2021-06-07 19:25:41 +02:00
|
|
|
using namespace mu;
|
|
|
|
|
2013-05-13 18:49:17 +02:00
|
|
|
namespace Ms {
|
2018-08-01 11:46:07 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// instrumentChangeStyle
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
static const ElementStyle instrumentChangeStyle {
|
|
|
|
{ Sid::instrumentChangePlacement, Pid::PLACEMENT },
|
2019-05-06 20:41:21 +02:00
|
|
|
{ Sid::instrumentChangeMinDistance, Pid::MIN_DISTANCE },
|
2018-08-01 11:46:07 +02:00
|
|
|
};
|
|
|
|
|
2012-05-26 14:26:10 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// InstrumentChange
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2021-09-02 15:26:45 +02:00
|
|
|
InstrumentChange::InstrumentChange(EngravingItem* parent)
|
2021-08-30 11:17:45 +02:00
|
|
|
: TextBase(ElementType::INSTRUMENT_CHANGE, parent, Tid::INSTRUMENT_CHANGE, ElementFlag::MOVABLE | ElementFlag::ON_STAFF)
|
2012-05-26 14:26:10 +02:00
|
|
|
{
|
2018-08-01 11:46:07 +02:00
|
|
|
initElementStyle(&instrumentChangeStyle);
|
2015-03-05 16:46:37 +01:00
|
|
|
_instrument = new Instrument();
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:26:45 +02:00
|
|
|
InstrumentChange::InstrumentChange(const Instrument& i, EngravingItem* parent)
|
2021-08-30 11:17:45 +02:00
|
|
|
: TextBase(ElementType::INSTRUMENT_CHANGE, parent, Tid::INSTRUMENT_CHANGE, ElementFlag::MOVABLE | ElementFlag::ON_STAFF)
|
2015-03-05 16:46:37 +01:00
|
|
|
{
|
2018-08-01 11:46:07 +02:00
|
|
|
initElementStyle(&instrumentChangeStyle);
|
2015-03-05 16:46:37 +01:00
|
|
|
_instrument = new Instrument(i);
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:47:27 +02:00
|
|
|
InstrumentChange::InstrumentChange(const InstrumentChange& is)
|
|
|
|
: TextBase(is)
|
2015-03-05 16:46:37 +01:00
|
|
|
{
|
|
|
|
_instrument = new Instrument(*is._instrument);
|
2019-08-01 18:06:52 +02:00
|
|
|
_init = is._init;
|
2015-03-05 16:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
InstrumentChange::~InstrumentChange()
|
|
|
|
{
|
|
|
|
delete _instrument;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstrumentChange::setInstrument(const Instrument& i)
|
|
|
|
{
|
2018-07-27 21:55:07 +02:00
|
|
|
*_instrument = i;
|
|
|
|
//delete _instrument;
|
|
|
|
//_instrument = new Instrument(i);
|
2012-05-26 14:26:10 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:06:52 +02:00
|
|
|
void InstrumentChange::setupInstrument(const Instrument* instrument)
|
|
|
|
{
|
|
|
|
if (_init) {
|
|
|
|
Fraction tickStart = segment()->tick();
|
|
|
|
Part* part = staff()->part();
|
|
|
|
Interval oldV = part->instrument(tickStart)->transpose();
|
2020-05-26 15:54:26 +02:00
|
|
|
|
2019-08-01 18:06:52 +02:00
|
|
|
// change the clef for each staff
|
|
|
|
for (int i = 0; i < part->nstaves(); i++) {
|
|
|
|
if (part->instrument(tickStart)->clefType(i) != instrument->clefType(i)) {
|
|
|
|
ClefType clefType
|
2021-01-29 14:26:16 +01:00
|
|
|
= score()->styleB(Sid::concertPitch) ? instrument->clefType(i)._concertClef : instrument->clefType(i)._transposingClef;
|
2019-08-01 18:06:52 +02:00
|
|
|
// If instrument change is at the start of a measure, use the measure as the element, as this will place the instrument change before the barline.
|
2021-09-02 15:26:45 +02:00
|
|
|
EngravingItem* element = rtick().isZero() ? toEngravingItem(findMeasure()) : toEngravingItem(this);
|
2019-08-01 18:06:52 +02:00
|
|
|
score()->undoChangeClef(part->staff(i), element, clefType, true);
|
2020-05-26 15:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-01 18:06:52 +02:00
|
|
|
|
|
|
|
// Change key signature if necessary
|
|
|
|
if (instrument->transpose() != oldV) {
|
|
|
|
for (int i = 0; i < part->nstaves(); i++) {
|
|
|
|
if (!part->staff(i)->keySigEvent(tickStart).isAtonal()) {
|
|
|
|
KeySigEvent ks;
|
|
|
|
ks.setForInstrumentChange(true);
|
|
|
|
Key key = part->staff(i)->key(tickStart);
|
|
|
|
if (!score()->styleB(Sid::concertPitch)) {
|
|
|
|
key = transposeKey(key, oldV);
|
2020-05-26 15:54:26 +02:00
|
|
|
}
|
2019-08-01 18:06:52 +02:00
|
|
|
ks.setKey(key);
|
|
|
|
score()->undoChangeKeySig(part->staff(i), tickStart, ks);
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 15:54:26 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:06:52 +02:00
|
|
|
// change instrument in all linked scores
|
2021-09-02 15:11:53 +02:00
|
|
|
for (EngravingObject* se : linkList()) {
|
2019-08-01 18:06:52 +02:00
|
|
|
InstrumentChange* lic = static_cast<InstrumentChange*>(se);
|
|
|
|
Instrument* newInstrument = new Instrument(*instrument);
|
|
|
|
lic->score()->undo(new ChangeInstrument(lic, newInstrument));
|
|
|
|
}
|
2020-05-26 15:54:26 +02:00
|
|
|
|
2019-08-01 18:06:52 +02:00
|
|
|
// transpose for current score only
|
|
|
|
// this automatically propagates to linked scores
|
|
|
|
if (part->instrument(tickStart)->transpose() != oldV) {
|
|
|
|
auto i = part->instruments()->upper_bound(tickStart.ticks()); // find(), ++i
|
|
|
|
Fraction tickEnd;
|
|
|
|
if (i == part->instruments()->end()) {
|
|
|
|
tickEnd = Fraction(-1, 1);
|
|
|
|
} else {
|
|
|
|
tickEnd = Fraction::fromTicks(i->first);
|
|
|
|
}
|
|
|
|
score()->transpositionChanged(part, oldV, tickStart, tickEnd);
|
|
|
|
}
|
2020-05-26 15:54:26 +02:00
|
|
|
|
2019-08-01 18:06:52 +02:00
|
|
|
const QString newInstrChangeText = tr("To %1").arg(instrument->trackName());
|
|
|
|
undoChangeProperty(Pid::TEXT, TextBase::plainToXmlText(newInstrChangeText));
|
2020-05-26 15:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-01 18:06:52 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// keySigs
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
std::vector<KeySig*> InstrumentChange::keySigs() const
|
|
|
|
{
|
|
|
|
std::vector<KeySig*> keysigs;
|
|
|
|
Segment* seg = segment()->prev1(SegmentType::KeySig);
|
|
|
|
if (seg) {
|
|
|
|
int startVoice = part()->staff(0)->idx() * VOICES;
|
|
|
|
int endVoice = part()->staff(part()->nstaves() - 1)->idx() * VOICES;
|
|
|
|
Fraction t = tick();
|
|
|
|
for (int i = startVoice; i <= endVoice; i += VOICES) {
|
|
|
|
KeySig* ks = toKeySig(seg->element(i));
|
|
|
|
if (ks && ks->forInstrumentChange() && ks->tick() == t) {
|
|
|
|
keysigs.push_back(ks);
|
|
|
|
}
|
2020-05-26 15:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-01 18:06:52 +02:00
|
|
|
return keysigs;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// clefs
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
std::vector<Clef*> InstrumentChange::clefs() const
|
|
|
|
{
|
|
|
|
std::vector<Clef*> clefs;
|
|
|
|
Segment* seg = segment()->prev1(SegmentType::Clef);
|
|
|
|
if (seg) {
|
|
|
|
int startVoice = part()->staff(0)->idx() * VOICES;
|
|
|
|
int endVoice = part()->staff(part()->nstaves() - 1)->idx() * VOICES;
|
|
|
|
Fraction t = tick();
|
|
|
|
for (int i = startVoice; i <= endVoice; i += VOICES) {
|
|
|
|
Clef* clef = toClef(seg->element(i));
|
|
|
|
if (clef && clef->forInstrumentChange() && clef->tick() == t) {
|
|
|
|
clefs.push_back(clef);
|
|
|
|
}
|
2020-05-26 15:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-01 18:06:52 +02:00
|
|
|
return clefs;
|
|
|
|
}
|
|
|
|
|
2012-05-26 14:26:10 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// write
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2016-11-19 11:51:21 +01:00
|
|
|
void InstrumentChange::write(XmlWriter& xml) const
|
2012-05-26 14:26:10 +02:00
|
|
|
{
|
2021-09-21 14:02:14 +02:00
|
|
|
xml.startObject(this);
|
2015-06-10 17:52:00 +02:00
|
|
|
_instrument->write(xml, part());
|
2019-08-01 18:06:52 +02:00
|
|
|
if (_init) {
|
|
|
|
xml.tag("init", _init);
|
2020-05-26 15:54:26 +02:00
|
|
|
}
|
2017-12-27 11:51:00 +01:00
|
|
|
TextBase::writeProperties(xml);
|
2021-09-21 14:02:14 +02:00
|
|
|
xml.endObject();
|
2012-05-26 14:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// read
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2013-01-11 18:10:18 +01:00
|
|
|
void InstrumentChange::read(XmlReader& e)
|
2012-05-26 14:26:10 +02:00
|
|
|
{
|
2013-01-11 18:10:18 +01:00
|
|
|
while (e.readNextStartElement()) {
|
|
|
|
const QStringRef& tag(e.name());
|
2012-05-26 14:26:10 +02:00
|
|
|
if (tag == "Instrument") {
|
2015-06-10 17:52:00 +02:00
|
|
|
_instrument->read(e, part());
|
2019-08-01 18:06:52 +02:00
|
|
|
} else if (tag == "init") {
|
|
|
|
_init = e.readBool();
|
2017-12-27 11:51:00 +01:00
|
|
|
} else if (!TextBase::readProperties(e)) {
|
2013-01-11 18:10:18 +01:00
|
|
|
e.unknown();
|
2012-05-26 14:26:10 +02:00
|
|
|
}
|
2020-05-26 15:54:26 +02:00
|
|
|
}
|
2018-12-21 20:58:50 +01:00
|
|
|
if (score()->mscVersion() < 206) {
|
2015-09-21 02:13:22 +02:00
|
|
|
// previous versions did not honor transposition of instrument change
|
|
|
|
// except in ways that it should not have
|
|
|
|
// notes entered before the instrument change was added would not be altered,
|
|
|
|
// so original transposition remained in effect
|
|
|
|
// notes added afterwards would be transposed by both intervals, resulting in tpc corruption
|
|
|
|
// here we set the instrument change to inherit the staff transposition to emulate previous versions
|
|
|
|
// in Note::read(), we attempt to fix the tpc corruption
|
2018-12-21 20:58:50 +01:00
|
|
|
// There is also code in read206 to try to deal with this, but it is out of date and therefore disabled
|
|
|
|
// What this means is, scores created in 2.1 or later should be fine, scores created in 2.0 maybe not so much
|
2020-05-26 15:54:26 +02:00
|
|
|
|
2021-02-05 15:08:33 +01:00
|
|
|
Interval v = staff() ? staff()->part()->instrument(tick())->transpose() : 0;
|
2015-09-21 02:13:22 +02:00
|
|
|
_instrument->setTranspose(v);
|
|
|
|
}
|
2012-05-26 14:26:10 +02:00
|
|
|
}
|
|
|
|
|
2015-01-16 14:51:09 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// propertyDefault
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2018-03-27 15:36:00 +02:00
|
|
|
QVariant InstrumentChange::propertyDefault(Pid propertyId) const
|
2015-01-16 14:51:09 +01:00
|
|
|
{
|
|
|
|
switch (propertyId) {
|
2018-03-27 15:36:00 +02:00
|
|
|
case Pid::SUB_STYLE:
|
2018-08-01 11:46:07 +02:00
|
|
|
return int(Tid::INSTRUMENT_CHANGE);
|
2015-01-16 14:51:09 +01:00
|
|
|
default:
|
2017-12-27 11:51:00 +01:00
|
|
|
return TextBase::propertyDefault(propertyId);
|
2015-01-16 14:51:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
2018-09-21 13:45:12 +02:00
|
|
|
// layout
|
2015-01-16 14:51:09 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2018-09-21 13:45:12 +02:00
|
|
|
void InstrumentChange::layout()
|
2015-01-16 14:51:09 +01:00
|
|
|
{
|
2018-10-18 11:53:01 +02:00
|
|
|
TextBase::layout();
|
2019-05-07 06:06:22 +02:00
|
|
|
autoplaceSegmentElement();
|
2015-01-16 14:51:09 +01:00
|
|
|
}
|
2013-05-13 18:49:17 +02:00
|
|
|
}
|