MuseScore/libmscore/volta.cpp

402 lines
13 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2011 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 "volta.h"
#include "style.h"
#include "xml.h"
#include "score.h"
#include "text.h"
2016-07-10 12:00:57 +02:00
#include "system.h"
#include "measure.h"
#include "score.h"
#include "tempo.h"
#include "changeMap.h"
#include "staff.h"
2012-05-26 14:26:10 +02:00
2013-05-13 18:49:17 +02:00
namespace Ms {
2018-08-01 11:46:07 +02:00
static const ElementStyle voltaStyle {
{ Sid::voltaFontFace, Pid::BEGIN_FONT_FACE },
{ Sid::voltaFontFace, Pid::CONTINUE_FONT_FACE },
{ Sid::voltaFontFace, Pid::END_FONT_FACE },
{ Sid::voltaFontSize, Pid::BEGIN_FONT_SIZE },
{ Sid::voltaFontSize, Pid::CONTINUE_FONT_SIZE },
{ Sid::voltaFontSize, Pid::END_FONT_SIZE },
{ Sid::voltaFontStyle, Pid::BEGIN_FONT_STYLE },
{ Sid::voltaFontStyle, Pid::CONTINUE_FONT_STYLE },
{ Sid::voltaFontStyle, Pid::END_FONT_STYLE },
2018-08-01 11:46:07 +02:00
{ Sid::voltaAlign, Pid::BEGIN_TEXT_ALIGN },
{ Sid::voltaAlign, Pid::CONTINUE_TEXT_ALIGN },
{ Sid::voltaAlign, Pid::END_TEXT_ALIGN },
{ Sid::voltaOffset, Pid::BEGIN_TEXT_OFFSET },
{ Sid::voltaOffset, Pid::CONTINUE_TEXT_OFFSET },
{ Sid::voltaOffset, Pid::END_TEXT_OFFSET },
{ Sid::voltaLineWidth, Pid::LINE_WIDTH },
{ Sid::voltaLineStyle, Pid::LINE_STYLE },
{ Sid::voltaHook, Pid::BEGIN_HOOK_HEIGHT },
{ Sid::voltaHook, Pid::END_HOOK_HEIGHT },
{ Sid::voltaPosAbove, Pid::OFFSET },
2018-08-01 11:46:07 +02:00
};
//---------------------------------------------------------
// VoltaSegment
//---------------------------------------------------------
2019-03-24 22:19:09 +01:00
VoltaSegment::VoltaSegment(Spanner* sp, Score* s) : TextLineBaseSegment(sp, s, ElementFlag::MOVABLE | ElementFlag::ON_STAFF | ElementFlag::SYSTEM)
{
}
2012-10-27 14:46:35 +02:00
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void VoltaSegment::layout()
{
TextLineBaseSegment::layout();
autoplaceSpannerSegment();
2012-10-27 14:46:35 +02:00
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// propertyDelegate
2013-05-02 16:12:17 +02:00
//---------------------------------------------------------
Element* VoltaSegment::propertyDelegate(Pid pid)
2013-05-02 16:12:17 +02:00
{
if (pid == Pid::BEGIN_HOOK_TYPE || pid == Pid::END_HOOK_TYPE || pid == Pid::VOLTA_ENDING)
return spanner();
return TextLineBaseSegment::propertyDelegate(pid);
}
2013-05-02 16:12:17 +02:00
//---------------------------------------------------------
2012-05-26 14:26:10 +02:00
// Volta
//---------------------------------------------------------
Volta::Volta(Score* s)
2018-08-01 11:46:07 +02:00
: TextLineBase(s, ElementFlag::SYSTEM)
2012-05-26 14:26:10 +02:00
{
setPlacement(Placement::ABOVE);
2018-08-01 11:46:07 +02:00
initElementStyle(&voltaStyle);
2018-03-27 14:40:34 +02:00
setBeginTextPlace(PlaceText::BELOW);
setContinueTextPlace(PlaceText::BELOW);
2018-03-27 14:40:34 +02:00
setLineVisible(true);
2018-03-27 15:36:00 +02:00
resetProperty(Pid::BEGIN_TEXT);
resetProperty(Pid::CONTINUE_TEXT);
resetProperty(Pid::END_TEXT);
resetProperty(Pid::BEGIN_TEXT_PLACE);
resetProperty(Pid::CONTINUE_TEXT_PLACE);
resetProperty(Pid::END_TEXT_PLACE);
resetProperty(Pid::BEGIN_HOOK_TYPE);
resetProperty(Pid::END_HOOK_TYPE);
2012-05-26 14:26:10 +02:00
setAnchor(VOLTA_ANCHOR);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// setText
//---------------------------------------------------------
void Volta::setText(const QString& s)
{
2017-02-07 18:48:23 +01:00
setBeginText(s);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// text
//---------------------------------------------------------
QString Volta::text() const
{
2017-02-07 18:48:23 +01:00
return beginText();
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void Volta::read(XmlReader& e)
2012-05-26 14:26:10 +02:00
{
eraseSpannerSegments();
2013-01-11 18:10:18 +01:00
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
2017-02-07 18:48:23 +01:00
if (tag == "endings") {
2013-01-11 18:10:18 +01:00
QString s = e.readElementText();
2012-05-26 14:26:10 +02:00
QStringList sl = s.split(",", QString::SkipEmptyParts);
_endings.clear();
2016-07-10 12:00:57 +02:00
for (const QString& l : sl) {
2012-05-26 14:26:10 +02:00
int i = l.simplified().toInt();
_endings.append(i);
}
}
else if (!readProperties(e))
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
}
//---------------------------------------------------------
// readProperties
//---------------------------------------------------------
bool Volta::readProperties(XmlReader& e)
{
if (!TextLineBase::readProperties(e))
return false;
if (anchor() != VOLTA_ANCHOR) {
// Volta strictly assumes that its anchor is measure, so don't let old scores override this.
qWarning("Correcting volta anchor type from %d to %d", int(anchor()), int(VOLTA_ANCHOR));
setAnchor(VOLTA_ANCHOR);
}
return true;
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void Volta::write(XmlWriter& xml) const
2012-05-26 14:26:10 +02:00
{
xml.stag(this);
TextLineBase::writeProperties(xml);
2012-05-26 14:26:10 +02:00
QString s;
2016-07-10 12:00:57 +02:00
for (int i : _endings) {
2012-05-26 14:26:10 +02:00
if (!s.isEmpty())
s += ", ";
s += QString("%1").arg(i);
}
xml.tag("endings", s);
xml.etag();
}
//---------------------------------------------------------
// createLineSegment
//---------------------------------------------------------
static const ElementStyle voltaSegmentStyle {
{ Sid::voltaPosAbove, Pid::OFFSET },
{ Sid::voltaMinDistance, Pid::MIN_DISTANCE },
};
2012-05-26 14:26:10 +02:00
LineSegment* Volta::createLineSegment()
{
VoltaSegment* vs = new VoltaSegment(this, score());
vs->setTrack(track());
vs->initElementStyle(&voltaSegmentStyle);
return vs;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// hasEnding
//---------------------------------------------------------
bool Volta::hasEnding(int repeat) const
{
2016-07-10 12:00:57 +02:00
for (int ending : endings()) {
2012-05-26 14:26:10 +02:00
if (ending == repeat)
return true;
}
return false;
}
//---------------------------------------------------------
// lastEnding
//---------------------------------------------------------
int Volta::lastEnding() const
{
if (_endings.isEmpty())
return 0;
return _endings.last();
}
2012-09-17 15:37:31 +02:00
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
QVariant Volta::getProperty(Pid propertyId) const
2012-09-17 15:37:31 +02:00
{
switch (propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::VOLTA_ENDING:
2014-09-25 11:34:26 +02:00
return QVariant::fromValue(endings());
2012-09-17 15:37:31 +02:00
default:
break;
}
return TextLineBase::getProperty(propertyId);
2012-09-17 15:37:31 +02:00
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
bool Volta::setProperty(Pid propertyId, const QVariant& val)
2012-09-17 15:37:31 +02:00
{
switch (propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::VOLTA_ENDING:
2014-09-25 11:34:26 +02:00
setEndings(val.value<QList<int>>());
break;
2012-09-17 15:37:31 +02:00
default:
if (!TextLineBase::setProperty(propertyId, val))
2012-09-17 15:37:31 +02:00
return false;
break;
}
triggerLayout();
2012-09-17 15:37:31 +02:00
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
QVariant Volta::propertyDefault(Pid propertyId) const
2012-09-17 15:37:31 +02:00
{
switch (propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::VOLTA_ENDING:
2014-09-25 11:34:26 +02:00
return QVariant::fromValue(QList<int>());
2018-03-27 15:36:00 +02:00
case Pid::ANCHOR:
return int(VOLTA_ANCHOR);
2018-03-27 15:36:00 +02:00
case Pid::BEGIN_HOOK_TYPE:
2017-02-07 18:48:23 +01:00
return int(HookType::HOOK_90);
2018-03-27 15:36:00 +02:00
case Pid::END_HOOK_TYPE:
2018-03-27 14:40:34 +02:00
return int(HookType::NONE);
2018-03-27 15:36:00 +02:00
case Pid::BEGIN_TEXT:
case Pid::CONTINUE_TEXT:
case Pid::END_TEXT:
2018-03-27 14:40:34 +02:00
return "";
2018-03-27 15:36:00 +02:00
case Pid::LINE_VISIBLE:
2018-03-27 14:40:34 +02:00
return true;
2018-03-27 15:36:00 +02:00
case Pid::BEGIN_TEXT_PLACE:
case Pid::CONTINUE_TEXT_PLACE:
case Pid::END_TEXT_PLACE:
2018-03-27 14:40:34 +02:00
return int(PlaceText::ABOVE);
2014-06-18 20:57:45 +02:00
2018-09-13 14:39:27 +02:00
case Pid::PLACEMENT:
return int(Placement::ABOVE);
2012-09-17 15:37:31 +02:00
default:
return TextLineBase::propertyDefault(propertyId);
2012-09-17 15:37:31 +02:00
}
}
//---------------------------------------------------------
// layoutSystem
//---------------------------------------------------------
SpannerSegment * Volta::layoutSystem(System * system)
{
SpannerSegment* voltaSegment= SLine::layoutSystem(system);
// we need set tempo in layout because all tempos of score is set in layout
// so fermata in seconda volta works correct because fermata apply itself tempo during layouting
setTempo();
return voltaSegment;
}
//---------------------------------------------------------
// setVelocity
//---------------------------------------------------------
void Volta::setVelocity() const
{
Measure* startMeasure = Spanner::startMeasure();
Measure* endMeasure = Spanner::endMeasure();
if (startMeasure && endMeasure) {
if (!endMeasure->repeatEnd())
return;
Fraction startTick = Fraction::fromTicks(startMeasure->tick().ticks() - 1);
Fraction endTick = Fraction::fromTicks((endMeasure->tick() + endMeasure->ticks()).ticks() - 1);
Staff* st = staff();
ChangeMap& velo = st->velocities();
auto prevVelo = velo.val(startTick);
velo.addFixed(endTick, prevVelo);
}
}
//---------------------------------------------------------
// setChannel
//---------------------------------------------------------
void Volta::setChannel() const
{
Measure* startMeasure = Spanner::startMeasure();
Measure* endMeasure = Spanner::endMeasure();
if (startMeasure && endMeasure) {
if (!endMeasure->repeatEnd())
return;
Fraction startTick = startMeasure->tick() - Fraction::fromTicks(1);
Fraction endTick = endMeasure->endTick() - Fraction::fromTicks(1);
Staff* st = staff();
for (int voice = 0; voice < VOICES; ++voice) {
int channel = st->channel(startTick, voice);
2018-11-10 13:22:54 +01:00
st->insertIntoChannelList(voice, endTick, channel);
}
}
}
//---------------------------------------------------------
// setTempo
//---------------------------------------------------------
void Volta::setTempo() const
{
Measure* startMeasure = Spanner::startMeasure();
Measure* endMeasure = Spanner::endMeasure();
if (startMeasure && endMeasure) {
if (!endMeasure->repeatEnd())
return;
Fraction startTick = startMeasure->tick() - Fraction::fromTicks(1);
Fraction endTick = endMeasure->endTick() - Fraction::fromTicks(1);
qreal tempoBeforeVolta = score()->tempomap()->tempo(startTick.ticks());
score()->setTempo(endTick, tempoBeforeVolta);
}
}
2012-10-27 14:46:35 +02:00
//---------------------------------------------------------
2017-02-07 18:48:23 +01:00
// accessibleInfo
//---------------------------------------------------------
2017-02-07 18:48:23 +01:00
QString Volta::accessibleInfo() const
{
2017-02-07 18:48:23 +01:00
return QString("%1: %2").arg(Element::accessibleInfo()).arg(text());
}
//---------------------------------------------------------
2017-02-07 18:48:23 +01:00
// setVoltaType
// deprecated
2013-08-09 13:19:54 +02:00
//---------------------------------------------------------
2017-02-07 18:48:23 +01:00
void Volta::setVoltaType(Type val)
2013-08-09 13:19:54 +02:00
{
2017-02-07 18:48:23 +01:00
setEndHookType(Type::CLOSED == val ? HookType::HOOK_90 : HookType::NONE);
2013-08-09 13:19:54 +02:00
}
//---------------------------------------------------------
2017-02-07 18:48:23 +01:00
// voltaType
// deprecated
//---------------------------------------------------------
2017-02-07 18:48:23 +01:00
Volta::Type Volta::voltaType() const
{
2017-02-07 18:48:23 +01:00
return endHookType() != HookType::NONE ? Type::CLOSED : Type::OPEN;
}
2013-05-13 18:49:17 +02:00
}