MuseScore/libmscore/trill.cpp

539 lines
16 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 "trill.h"
#include "style.h"
#include "system.h"
#include "measure.h"
#include "xml.h"
#include "utils.h"
#include "sym.h"
#include "score.h"
#include "accidental.h"
#include "segment.h"
#include "staff.h"
2012-05-26 14:26:10 +02:00
2013-05-13 18:49:17 +02:00
namespace Ms {
2018-08-08 14:16:17 +02:00
//---------------------------------------------------------
// trillTable
// must be in sync with Trill::Type
//---------------------------------------------------------
const TrillTableItem trillTable[] = {
{ Trill::Type::TRILL_LINE, "trill", QT_TRANSLATE_NOOP("trillType", "Trill line") },
{ Trill::Type::UPPRALL_LINE, "upprall", QT_TRANSLATE_NOOP("trillType", "Upprall line") },
{ Trill::Type::DOWNPRALL_LINE, "downprall", QT_TRANSLATE_NOOP("trillType", "Downprall line") },
{ Trill::Type::PRALLPRALL_LINE, "prallprall", QT_TRANSLATE_NOOP("trillType", "Prallprall line") }
2015-05-05 18:46:13 +02:00
};
int trillTableSize() {
return sizeof(trillTable)/sizeof(TrillTableItem);
}
//---------------------------------------------------------
// trillStyle
//---------------------------------------------------------
static const ElementStyle trillStyle {
{ Sid::trillPlacement, Pid::PLACEMENT },
{ Sid::trillPosAbove, Pid::OFFSET },
};
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void TrillSegment::draw(QPainter* painter) const
{
2014-05-07 12:10:28 +02:00
painter->setPen(spanner()->curColor());
2013-11-19 16:06:08 +01:00
drawSymbols(_symbols, painter);
2012-05-26 14:26:10 +02:00
}
2013-08-30 16:03:46 +02:00
//---------------------------------------------------------
// add
//---------------------------------------------------------
void TrillSegment::add(Element* e)
{
e->setParent(this);
2017-01-18 14:16:33 +01:00
if (e->type() == ElementType::ACCIDENTAL) {
2013-08-30 16:03:46 +02:00
// accidental is part of trill
2017-12-20 16:49:30 +01:00
trill()->setAccidental(toAccidental(e));
2013-08-30 16:03:46 +02:00
}
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void TrillSegment::remove(Element* e)
{
if (trill()->accidental() == e) {
// accidental is part of trill
trill()->setAccidental(0);
}
}
2013-11-19 16:06:08 +01:00
//---------------------------------------------------------
// symbolLine
//---------------------------------------------------------
void TrillSegment::symbolLine(SymId start, SymId fill)
{
qreal x1 = 0;
qreal x2 = pos2().x();
qreal w = x2 - x1;
qreal mag = magS();
ScoreFont* f = score()->scoreFont();
_symbols.clear();
2016-01-04 14:48:58 +01:00
_symbols.push_back(start);
2015-05-07 13:07:44 +02:00
qreal w1 = f->advance(start, mag);
qreal w2 = f->advance(fill, mag);
2013-11-19 16:06:08 +01:00
int n = lrint((w - w1) / w2);
for (int i = 0; i < n; ++i)
2016-01-04 14:48:58 +01:00
_symbols.push_back(fill);
2013-11-19 16:06:08 +01:00
QRectF r(f->bbox(_symbols, mag));
setbbox(r);
}
void TrillSegment::symbolLine(SymId start, SymId fill, SymId end)
{
qreal x1 = 0;
qreal x2 = pos2().x();
qreal w = x2 - x1;
qreal mag = magS();
ScoreFont* f = score()->scoreFont();
_symbols.clear();
2016-01-04 14:48:58 +01:00
_symbols.push_back(start);
qreal w1 = f->advance(start, mag);
qreal w2 = f->advance(fill, mag);
qreal w3 = f->advance(end, mag);
2013-11-19 16:06:08 +01:00
int n = lrint((w - w1 - w3) / w2);
for (int i = 0; i < n; ++i)
2016-01-04 14:48:58 +01:00
_symbols.push_back(fill);
_symbols.push_back(end);
2013-11-19 16:06:08 +01:00
QRectF r(f->bbox(_symbols, mag));
setbbox(r);
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void TrillSegment::layout()
{
if (staff())
2016-12-23 12:05:18 +01:00
setMag(staff()->mag(tick()));
if (spanner()->placeBelow())
rypos() = staff() ? staff()->height() : 0.0;
2016-09-04 16:17:33 +02:00
if (isSingleType() || isBeginType()) {
2013-08-30 16:03:46 +02:00
Accidental* a = trill()->accidental();
if (a) {
a->layout();
a->setMag(a->mag() * .6);
qreal _spatium = spatium();
2013-11-21 12:37:19 +01:00
a->setPos(_spatium * 1.3, -2.2 * _spatium);
2016-09-04 16:17:33 +02:00
a->setParent(this);
2013-08-30 16:03:46 +02:00
}
2013-11-19 16:06:08 +01:00
switch (trill()->trillType()) {
2014-06-25 14:07:43 +02:00
case Trill::Type::TRILL_LINE:
2013-11-19 16:06:08 +01:00
symbolLine(SymId::ornamentTrill, SymId::wiggleTrill);
break;
2014-06-25 14:07:43 +02:00
case Trill::Type::PRALLPRALL_LINE:
2013-11-19 16:06:08 +01:00
symbolLine(SymId::wiggleTrill, SymId::wiggleTrill);
break;
2014-06-25 14:07:43 +02:00
case Trill::Type::UPPRALL_LINE:
2013-11-22 14:29:09 +01:00
symbolLine(SymId::ornamentBottomLeftConcaveStroke,
SymId::ornamentZigZagLineNoRightEnd, SymId::ornamentZigZagLineWithRightEnd);
2013-11-19 16:06:08 +01:00
break;
2014-06-25 14:07:43 +02:00
case Trill::Type::DOWNPRALL_LINE:
2013-11-22 14:29:09 +01:00
symbolLine(SymId::ornamentLeftVerticalStroke,
SymId::ornamentZigZagLineNoRightEnd, SymId::ornamentZigZagLineWithRightEnd);
break;
2013-11-19 16:06:08 +01:00
}
2013-08-30 16:03:46 +02:00
}
2013-11-19 16:06:08 +01:00
else
symbolLine(SymId::wiggleTrill, SymId::wiggleTrill);
if (isStyled(Pid::OFFSET))
roffset() = trill()->propertyDefault(Pid::OFFSET).toPointF();
autoplaceSpannerSegment();
}
//---------------------------------------------------------
// shape
//---------------------------------------------------------
Shape TrillSegment::shape() const
{
return Shape(bbox());
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// acceptDrop
//---------------------------------------------------------
2017-03-31 13:03:15 +02:00
bool TrillSegment::acceptDrop(EditData& data) const
2012-05-26 14:26:10 +02:00
{
if (data.dropElement->isAccidental())
2012-05-26 14:26:10 +02:00
return true;
return false;
}
//---------------------------------------------------------
// drop
//---------------------------------------------------------
2017-03-31 13:03:15 +02:00
Element* TrillSegment::drop(EditData& data)
2012-05-26 14:26:10 +02:00
{
Element* e = data.dropElement;
2017-03-31 13:03:15 +02:00
switch (e->type()) {
2017-01-18 14:16:33 +01:00
case ElementType::ACCIDENTAL:
2012-05-26 14:26:10 +02:00
e->setParent(trill());
score()->undoAddElement(e);
break;
default:
delete e;
e = 0;
break;
}
return e;
}
2013-05-02 16:12:17 +02:00
//---------------------------------------------------------
// propertyDelegate
2013-05-02 16:12:17 +02:00
//---------------------------------------------------------
Element* TrillSegment::propertyDelegate(Pid pid)
2013-05-02 16:12:17 +02:00
{
if (pid == Pid::TRILL_TYPE || pid == Pid::ORNAMENT_STYLE || pid == Pid::PLACEMENT || pid == Pid::PLAY)
return spanner();
return LineSegment::propertyDelegate(pid);
2013-05-02 16:12:17 +02:00
}
2013-09-02 12:29:25 +02:00
//---------------------------------------------------------
// scanElements
//---------------------------------------------------------
2013-10-24 12:09:00 +02:00
void TrillSegment::scanElements(void* data, void (*func)(void*, Element*), bool /*all*/)
2013-09-02 12:29:25 +02:00
{
func(data, this);
if (isSingleType() || isBeginType()) {
2013-09-02 12:29:25 +02:00
Accidental* a = trill()->accidental();
if (a)
func(data, a);
}
}
//---------------------------------------------------------
// getPropertyStyle
//---------------------------------------------------------
Sid TrillSegment::getPropertyStyle(Pid pid) const
{
if (pid == Pid::OFFSET)
return spanner()->placeAbove() ? Sid::trillPosAbove : Sid::trillPosBelow;
return LineSegment::getPropertyStyle(pid);
}
Sid Trill::getPropertyStyle(Pid pid) const
{
if (pid == Pid::OFFSET)
return placeAbove() ? Sid::trillPosAbove : Sid::trillPosBelow;
return SLine::getPropertyStyle(pid);
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// Trill
//---------------------------------------------------------
Trill::Trill(Score* s)
: SLine(s)
{
_trillType = Type::TRILL_LINE;
_accidental = 0;
_ornamentStyle = MScore::OrnamentStyle::DEFAULT;
setPlayArticulation(true);
initElementStyle(&trillStyle);
2013-08-30 16:03:46 +02:00
}
Trill::~Trill()
{
delete _accidental;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
void Trill::add(Element* e)
{
2017-01-18 14:16:33 +01:00
if (e->type() == ElementType::ACCIDENTAL) {
2012-05-26 14:26:10 +02:00
e->setParent(this);
2017-12-20 16:49:30 +01:00
_accidental = toAccidental(e);
2012-05-26 14:26:10 +02:00
}
else
SLine::add(e);
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void Trill::remove(Element* e)
{
2013-08-30 16:03:46 +02:00
if (e == _accidental)
_accidental = 0;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void Trill::layout()
{
SLine::layout();
2013-06-20 18:48:28 +02:00
if (score() == gscore)
return;
2014-08-19 13:34:14 +02:00
if (spannerSegments().empty())
return;
2017-12-20 16:49:30 +01:00
TrillSegment* ls = toTrillSegment(frontSegment());
2014-08-19 13:34:14 +02:00
if (spannerSegments().empty())
qDebug("Trill: no segments");
2013-08-30 16:03:46 +02:00
if (_accidental)
_accidental->setParent(ls);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// createLineSegment
//---------------------------------------------------------
static const ElementStyle trillSegmentStyle {
{ Sid::trillPosAbove, Pid::OFFSET },
{ Sid::trillMinDistance, Pid::MIN_DISTANCE },
};
2012-05-26 14:26:10 +02:00
LineSegment* Trill::createLineSegment()
{
TrillSegment* seg = new TrillSegment(this, score());
2012-05-26 14:26:10 +02:00
seg->setTrack(track());
seg->setColor(color());
seg->initElementStyle(&trillSegmentStyle);
2012-05-26 14:26:10 +02:00
return seg;
}
//---------------------------------------------------------
// Trill::write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void Trill::write(XmlWriter& xml) const
2012-05-26 14:26:10 +02:00
{
2014-08-12 20:10:46 +02:00
if (!xml.canWrite(this))
return;
xml.stag(this);
xml.tag("subtype", trillTypeName());
2018-03-27 15:36:00 +02:00
writeProperty(xml, Pid::PLAY);
writeProperty(xml, Pid::ORNAMENT_STYLE);
writeProperty(xml, Pid::PLACEMENT);
2012-05-26 14:26:10 +02:00
SLine::writeProperties(xml);
2013-08-30 16:03:46 +02:00
if (_accidental)
_accidental->write(xml);
2012-05-26 14:26:10 +02:00
xml.etag();
}
//---------------------------------------------------------
// Trill::read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void Trill::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());
2012-05-26 14:26:10 +02:00
if (tag == "subtype")
setTrillType(e.readElementText());
2012-05-26 14:26:10 +02:00
else if (tag == "Accidental") {
2013-08-30 16:03:46 +02:00
_accidental = new Accidental(score());
_accidental->read(e);
_accidental->setParent(this);
2012-05-26 14:26:10 +02:00
}
else if ( tag == "ornamentStyle")
2018-10-26 10:41:07 +02:00
readProperty(e, Pid::ORNAMENT_STYLE);
else if ( tag == "play")
setPlayArticulation(e.readBool());
2012-05-26 14:26:10 +02:00
else if (!SLine::readProperties(e))
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
}
//---------------------------------------------------------
// setTrillType
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
void Trill::setTrillType(const QString& s)
2012-05-26 14:26:10 +02:00
{
if (s == "0") {
2014-06-25 14:07:43 +02:00
_trillType = Type::TRILL_LINE;
return;
}
if (s == "pure") {
2015-07-12 10:10:36 +02:00
_trillType = Type::PRALLPRALL_LINE; // obsolete, compatibility only
return;
}
for (TrillTableItem i : trillTable) {
if (s.compare(i.name) == 0) {
_trillType = i.type;
return;
}
}
qDebug("Trill::setSubtype: unknown <%s>", qPrintable(s));
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
2019-05-31 11:47:18 +02:00
// type2name
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2019-05-31 11:47:18 +02:00
QString Trill::type2name(Trill::Type t)
2012-05-26 14:26:10 +02:00
{
for (TrillTableItem i : trillTable) {
2019-05-31 11:47:18 +02:00
if (i.type == t)
return i.name;
2012-05-26 14:26:10 +02:00
}
2019-05-31 11:47:18 +02:00
qDebug("unknown Trill subtype %d", int(t));
return "?";
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// trillTypeName
//---------------------------------------------------------
2019-05-31 11:47:18 +02:00
QString Trill::trillTypeName() const
{
return type2name(trillType());
}
//---------------------------------------------------------
// trillTypeName
//---------------------------------------------------------
2016-02-04 17:06:32 +01:00
QString Trill::trillTypeUserName() const
{
2014-08-26 17:39:31 +02:00
return qApp->translate("trillType", trillTable[static_cast<int>(trillType())].userName.toUtf8().constData());
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// scanElements
//---------------------------------------------------------
void Trill::scanElements(void* data, void (*func)(void*, Element*), bool all)
{
2013-08-30 16:03:46 +02:00
if (_accidental)
_accidental->scanElements(data, func, all);
func(data, this); // ?
2012-05-26 14:26:10 +02:00
SLine::scanElements(data, func, all);
}
2012-09-17 18:09:30 +02:00
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
QVariant Trill::getProperty(Pid propertyId) const
2012-09-17 18:09:30 +02:00
{
switch(propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::TRILL_TYPE:
2014-05-25 18:51:19 +02:00
return int(trillType());
2018-03-27 15:36:00 +02:00
case Pid::ORNAMENT_STYLE:
return int(ornamentStyle());
2018-03-27 15:36:00 +02:00
case Pid::PLAY:
return bool(playArticulation());
2012-09-17 18:09:30 +02:00
default:
break;
}
return SLine::getProperty(propertyId);
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
bool Trill::setProperty(Pid propertyId, const QVariant& val)
2012-09-17 18:09:30 +02:00
{
switch(propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::TRILL_TYPE:
2014-06-25 14:07:43 +02:00
setTrillType(Type(val.toInt()));
2012-09-17 18:09:30 +02:00
break;
2018-03-27 15:36:00 +02:00
case Pid::PLAY:
setPlayArticulation(val.toBool());
break;
2018-03-27 15:36:00 +02:00
case Pid::ORNAMENT_STYLE:
setOrnamentStyle(MScore::OrnamentStyle(val.toInt()));
break;
2012-09-17 18:09:30 +02:00
default:
if (!SLine::setProperty(propertyId, val))
return false;
break;
}
2019-10-24 15:49:23 +02:00
triggerLayoutAll();
2012-09-17 18:09:30 +02:00
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
QVariant Trill::propertyDefault(Pid propertyId) const
2012-09-17 18:09:30 +02:00
{
2018-08-08 14:16:17 +02:00
switch (propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::TRILL_TYPE:
2012-09-17 18:09:30 +02:00
return 0;
2018-03-27 15:36:00 +02:00
case Pid::ORNAMENT_STYLE:
//return int(score()->style()->ornamentStyle(_ornamentStyle));
return int(MScore::OrnamentStyle::DEFAULT);
2018-03-27 15:36:00 +02:00
case Pid::PLAY:
return true;
2018-03-27 15:36:00 +02:00
case Pid::PLACEMENT:
return score()->styleV(Sid::trillPlacement);
2012-09-17 18:09:30 +02:00
default:
return SLine::propertyDefault(propertyId);
}
}
2019-05-31 11:47:18 +02:00
//---------------------------------------------------------
// propertyId
//---------------------------------------------------------
Pid Trill::propertyId(const QStringRef& name) const
{
if (name == "subtype")
return Pid::TRILL_TYPE;
return SLine::propertyId(name);
}
//---------------------------------------------------------
// accessibleInfo
//---------------------------------------------------------
2016-02-04 17:06:32 +01:00
QString Trill::accessibleInfo() const
{
return QString("%1: %2").arg(Element::accessibleInfo()).arg(trillTypeUserName());
}
2013-05-13 18:49:17 +02:00
}