MuseScore/libmscore/pedal.cpp

287 lines
10 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 "pedal.h"
#include "sym.h"
2014-04-09 16:09:21 +02:00
#include "xml.h"
2014-05-27 17:09:27 +02:00
#include "system.h"
#include "measure.h"
#include "chordrest.h"
2017-12-27 16:09:12 +01:00
#include "staff.h"
2012-05-26 14:26:10 +02:00
#include "score.h"
2013-05-13 18:49:17 +02:00
namespace Ms {
2018-08-01 11:46:07 +02:00
static const ElementStyle pedalStyle {
{ Sid::pedalFontFace, Pid::BEGIN_FONT_FACE },
{ Sid::pedalFontFace, Pid::CONTINUE_FONT_FACE },
{ Sid::pedalFontFace, Pid::END_FONT_FACE },
{ Sid::pedalFontSize, Pid::BEGIN_FONT_SIZE },
{ Sid::pedalFontSize, Pid::CONTINUE_FONT_SIZE },
{ Sid::pedalFontSize, Pid::END_FONT_SIZE },
{ Sid::pedalFontStyle, Pid::BEGIN_FONT_STYLE },
{ Sid::pedalFontStyle, Pid::CONTINUE_FONT_STYLE },
{ Sid::pedalFontStyle, Pid::END_FONT_STYLE },
2018-08-01 11:46:07 +02:00
{ Sid::pedalTextAlign, Pid::BEGIN_TEXT_ALIGN },
{ Sid::pedalTextAlign, Pid::CONTINUE_TEXT_ALIGN },
{ Sid::pedalTextAlign, Pid::END_TEXT_ALIGN },
{ Sid::pedalHookHeight, Pid::BEGIN_HOOK_HEIGHT },
{ Sid::pedalHookHeight, Pid::END_HOOK_HEIGHT },
{ Sid::pedalBeginTextOffset, Pid::BEGIN_TEXT_OFFSET },
{ Sid::pedalBeginTextOffset, Pid::CONTINUE_TEXT_OFFSET },
{ Sid::pedalBeginTextOffset, Pid::END_TEXT_OFFSET },
{ Sid::pedalPlacement, Pid::PLACEMENT },
{ Sid::pedalPosBelow, Pid::OFFSET },
2018-08-01 11:46:07 +02:00
};
2012-10-27 14:46:35 +02:00
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void PedalSegment::layout()
{
TextLineBaseSegment::layout();
if (isStyled(Pid::OFFSET))
roffset() = pedal()->propertyDefault(Pid::OFFSET).toPointF();
autoplaceSpannerSegment();
2016-11-18 14:28:53 +01:00
}
//---------------------------------------------------------
// getPropertyStyle
//---------------------------------------------------------
Sid PedalSegment::getPropertyStyle(Pid pid) const
{
if (pid == Pid::OFFSET)
return spanner()->placeAbove() ? Sid::pedalPosAbove : Sid::pedalPosBelow;
return TextLineBaseSegment::getPropertyStyle(pid);
}
Sid Pedal::getPropertyStyle(Pid pid) const
{
if (pid == Pid::OFFSET)
return placeAbove() ? Sid::pedalPosAbove : Sid::pedalPosBelow;
return TextLineBase::getPropertyStyle(pid);
}
2017-06-08 15:43:05 +02:00
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// Pedal
//---------------------------------------------------------
Pedal::Pedal(Score* s)
: TextLineBase(s)
2012-05-26 14:26:10 +02:00
{
2018-08-01 11:46:07 +02:00
initElementStyle(&pedalStyle);
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);
2018-03-27 15:36:00 +02:00
resetProperty(Pid::END_TEXT);
2018-03-27 14:40:34 +02:00
2018-03-27 15:36:00 +02:00
resetProperty(Pid::LINE_WIDTH);
resetProperty(Pid::LINE_STYLE);
2018-03-27 14:40:34 +02:00
2018-03-27 15:36:00 +02:00
resetProperty(Pid::BEGIN_HOOK_TYPE);
resetProperty(Pid::END_HOOK_TYPE);
2018-03-27 14:40:34 +02:00
2018-03-27 15:36:00 +02:00
resetProperty(Pid::BEGIN_TEXT_PLACE);
resetProperty(Pid::LINE_VISIBLE);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void Pedal::read(XmlReader& e)
2012-05-26 14:26:10 +02:00
{
2018-08-27 22:57:15 +02:00
if (score()->mscVersion() < 301)
e.addSpanner(e.intAttribute("id", -1), this);
while (e.readNextStartElement()) {
2017-02-07 18:48:23 +01:00
if (!TextLineBase::readProperties(e))
e.unknown();
}
2012-05-26 14:26:10 +02:00
}
2012-10-27 14:46:35 +02:00
2018-03-27 14:40:34 +02:00
//---------------------------------------------------------
// write
//---------------------------------------------------------
void Pedal::write(XmlWriter& xml) const
{
if (!xml.canWrite(this))
return;
xml.stag(this);
2018-03-27 14:40:34 +02:00
for (auto i : {
2018-03-27 15:36:00 +02:00
Pid::END_HOOK_TYPE,
Pid::BEGIN_TEXT,
Pid::CONTINUE_TEXT,
2018-03-27 15:36:00 +02:00
Pid::END_TEXT,
Pid::LINE_VISIBLE,
2018-03-27 15:36:00 +02:00
Pid::BEGIN_HOOK_TYPE
2018-03-27 14:40:34 +02:00
}) {
writeProperty(xml, i);
}
2018-08-01 11:46:07 +02:00
for (const StyledProperty& spp : *styledProperties())
writeProperty(xml, spp.pid);
2018-03-27 14:40:34 +02:00
SLine::writeProperties(xml);
2018-03-27 14:40:34 +02:00
xml.etag();
}
2012-10-27 14:46:35 +02:00
//---------------------------------------------------------
// createLineSegment
//---------------------------------------------------------
static const ElementStyle pedalSegmentStyle {
{ Sid::pedalPosBelow, Pid::OFFSET },
{ Sid::pedalMinDistance, Pid::MIN_DISTANCE },
};
2012-10-27 14:46:35 +02:00
LineSegment* Pedal::createLineSegment()
{
PedalSegment* p = new PedalSegment(this, score());
p->setTrack(track());
p->initElementStyle(&pedalSegmentStyle);
return p;
2012-10-27 14:46:35 +02:00
}
2013-08-09 11:42:24 +02:00
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
QVariant Pedal::propertyDefault(Pid propertyId) const
2013-08-09 11:42:24 +02:00
{
switch (propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::LINE_WIDTH:
return score()->styleP(Sid::pedalLineWidth); // return point, not spatium
2013-08-09 11:42:24 +02:00
2018-03-27 15:36:00 +02:00
case Pid::LINE_STYLE:
return score()->styleV(Sid::pedalLineStyle);
2013-08-09 11:42:24 +02:00
2018-03-27 15:36:00 +02:00
case Pid::BEGIN_TEXT:
case Pid::CONTINUE_TEXT:
2018-03-27 15:36:00 +02:00
case Pid::END_TEXT:
2018-03-27 14:40:34 +02:00
return "";
2013-08-09 11:42:24 +02:00
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::LEFT);
2017-06-08 15:43:05 +02:00
2018-03-27 15:36:00 +02:00
case Pid::BEGIN_HOOK_TYPE:
case Pid::END_HOOK_TYPE:
2018-03-27 14:40:34 +02:00
return int(HookType::NONE);
2013-08-09 11:42:24 +02:00
case Pid::LINE_VISIBLE:
return true;
case Pid::PLACEMENT:
return score()->styleV(Sid::pedalPlacement);
2013-08-09 11:42:24 +02:00
default:
2017-02-07 18:48:23 +01:00
return TextLineBase::propertyDefault(propertyId);
2013-08-09 11:42:24 +02:00
}
}
2014-05-27 17:09:27 +02:00
//---------------------------------------------------------
// linePos
// return System() coordinates
//---------------------------------------------------------
QPointF Pedal::linePos(Grip grip, System** sys) const
2014-05-27 17:09:27 +02:00
{
qreal x = 0.0;
2014-05-27 17:09:27 +02:00
qreal nhw = score()->noteHeadWidth();
System* s = nullptr;
if (grip == Grip::START) {
2017-12-20 16:49:30 +01:00
ChordRest* c = toChordRest(startElement());
if (c) {
s = c->segment()->system();
x = c->pos().x() + c->segment()->pos().x() + c->segment()->measure()->pos().x();
if (c->type() == ElementType::REST && c->durationType() == TDuration::DurationType::V_MEASURE)
x -= c->x();
if (beginHookType() == HookType::HOOK_45)
x += nhw * .5;
}
2014-05-27 17:09:27 +02:00
}
else {
Element* e = endElement();
2017-12-20 16:49:30 +01:00
ChordRest* c = toChordRest(endElement());
2017-02-07 18:48:23 +01:00
if (!e || e == startElement() || (endHookType() == HookType::HOOK_90)) {
// pedal marking on single note or ends with non-angled hook:
// extend to next note or end of measure
Segment* seg = nullptr;
if (!e)
seg = startSegment();
else
seg = c->segment();
if (seg) {
seg = seg->next();
for ( ; seg; seg = seg->next()) {
2017-03-08 13:12:26 +01:00
if (seg->segmentType() == SegmentType::ChordRest) {
// look for a chord/rest in any voice on this staff
bool crFound = false;
int track = staffIdx() * VOICES;
for (int i = 0; i < VOICES; ++i) {
if (seg->element(track + i)) {
crFound = true;
break;
}
}
if (crFound)
break;
}
2017-03-08 13:12:26 +01:00
else if (seg->segmentType() == SegmentType::EndBarLine) {
if (!seg->enabled()) {
// disabled barline layout is not reliable
// use width of measure instead
Measure* m = seg->measure();
s = seg->system();
x = m->width() + m->pos().x() - nhw * 2;
seg = nullptr;
}
break;
}
}
}
if (seg) {
s = seg->system();
x = seg->pos().x() + seg->measure()->pos().x() - nhw * 2;
}
2014-05-27 17:09:27 +02:00
}
else if (c) {
s = c->segment()->system();
x = c->pos().x() + c->segment()->pos().x() + c->segment()->measure()->pos().x();
2017-01-18 14:16:33 +01:00
if (c->type() == ElementType::REST && c->durationType() == TDuration::DurationType::V_MEASURE)
x -= c->x();
}
if (!s) {
Fraction t = tick2();
2014-05-27 17:09:27 +02:00
Measure* m = score()->tick2measure(t);
s = m->system();
x = m->tick2pos(t);
}
2017-02-07 18:48:23 +01:00
if (endHookType() == HookType::HOOK_45)
2014-05-27 17:09:27 +02:00
x += nhw * .5;
else
x += nhw;
}
2014-05-27 17:09:27 +02:00
*sys = s;
2014-07-18 17:53:08 +02:00
return QPointF(x, 0);
2014-05-27 17:09:27 +02:00
}
2013-05-13 18:49:17 +02:00
}