MuseScore/libmscore/trill.cpp

586 lines
18 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 {
// 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);
}
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);
2013-11-19 16:06:08 +01:00
qreal w1 = f->bbox(start, mag).width();
qreal w2 = f->width(fill, mag);
qreal w3 = f->width(end, mag);
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 (autoplace())
setUserOff(QPointF());
if (staff())
2016-12-23 12:05:18 +01:00
setMag(staff()->mag(tick()));
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);
2013-08-30 16:03:46 +02:00
a->adjustReadPos();
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 (parent()) {
2016-11-18 14:28:53 +01:00
qreal yo = score()->styleP(trill()->placeBelow() ? StyleIdx::trillPosBelow : StyleIdx::trillPosAbove);
rypos() = yo;
if (autoplace()) {
qreal minDistance = spatium();
Shape s1 = shape().translated(pos());
if (trill()->placeAbove()) {
qreal d = system()->topDistance(staffIdx(), s1);
if (d > -minDistance)
rUserYoffset() = -d - minDistance;
}
else {
qreal d = system()->bottomDistance(staffIdx(), s1);
if (d > -minDistance)
rUserYoffset() = d + minDistance;
}
}
else
adjustReadPos();
}
}
//---------------------------------------------------------
// 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
{
2017-03-31 13:03:15 +02:00
if (data.element->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.element;
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
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant TrillSegment::getProperty(P_ID id) const
{
switch (id) {
2014-05-26 18:18:01 +02:00
case P_ID::TRILL_TYPE:
case P_ID::ORNAMENT_STYLE:
case P_ID::PLACEMENT:
case P_ID::PLAY:
2013-05-02 16:12:17 +02:00
return trill()->getProperty(id);
default:
return LineSegment::getProperty(id);
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool TrillSegment::setProperty(P_ID id, const QVariant& v)
{
switch (id) {
2014-05-26 18:18:01 +02:00
case P_ID::TRILL_TYPE:
case P_ID::ORNAMENT_STYLE:
case P_ID::PLACEMENT:
case P_ID::PLAY:
2013-05-02 16:12:17 +02:00
return trill()->setProperty(id, v);
default:
return LineSegment::setProperty(id, v);
}
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant TrillSegment::propertyDefault(P_ID id) const
{
switch (id) {
2014-05-26 18:18:01 +02:00
case P_ID::TRILL_TYPE:
case P_ID::ORNAMENT_STYLE:
case P_ID::PLACEMENT:
case P_ID::PLAY:
2013-05-02 16:12:17 +02:00
return trill()->propertyDefault(id);
default:
return LineSegment::propertyDefault(id);
}
}
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);
}
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// Trill
//---------------------------------------------------------
Trill::Trill(Score* s)
: SLine(s)
{
2014-06-25 14:07:43 +02:00
_trillType = Type::TRILL_LINE;
2013-08-30 16:03:46 +02:00
_accidental = 0;
_ornamentStyle = MScore::OrnamentStyle::DEFAULT;
setPlayArticulation(true);
setPlacement(Element::Placement::ABOVE);
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-21 04:01:14 +02:00
#if 0
// this is now handled differently, in SLine::linePos
2012-05-26 14:26:10 +02:00
//
// special case:
// if end segment is first chord/rest segment in measure,
// shorten trill line so it ends at end of previous measure
//
2014-08-21 04:01:14 +02:00
qreal _spatium = spatium();
2013-06-10 11:03:34 +02:00
Segment* seg1 = startSegment();
Segment* seg2 = endSegment();
2014-08-06 12:28:58 +02:00
if (seg1
&& seg2
2012-05-26 14:26:10 +02:00
&& (seg1->system() == seg2->system())
&& (spannerSegments().size() == 1)
&& (seg2->tick() == seg2->measure()->tick())
) {
qreal x1 = seg2->pagePos().x();
Measure* m = seg2->measure()->prevMeasure();
if (m) {
Segment* s2 = m->last();
qreal x2 = s2->pagePos().x();
qreal dx = x1 - x2 + _spatium * .3;
ls->setPos2(ls->ipos2() + QPointF(-dx, 0.0));
ls->layout();
}
}
2014-08-21 04:01:14 +02:00
#endif
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
//---------------------------------------------------------
LineSegment* Trill::createLineSegment()
{
TrillSegment* seg = new TrillSegment(score());
seg->setTrack(track());
seg->setColor(color());
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;
2014-07-21 13:24:21 +02:00
xml.stag(QString("%1 id=\"%2\"").arg(name()).arg(xml.spannerId(this)));
xml.tag("subtype", trillTypeName());
writeProperty(xml, P_ID::PLAY);
writeProperty(xml, P_ID::ORNAMENT_STYLE);
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
{
2013-01-11 18:10:18 +01:00
qDeleteAll(spannerSegments());
2012-05-26 14:26:10 +02:00
spannerSegments().clear();
2013-01-11 18:10:18 +01:00
2014-07-21 13:24:21 +02:00
e.addSpanner(e.intAttribute("id", -1), this);
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")
setProperty(P_ID::ORNAMENT_STYLE, Ms::getProperty(P_ID::ORNAMENT_STYLE, e));
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
}
//---------------------------------------------------------
// trillTypeName
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
QString Trill::trillTypeName() const
2012-05-26 14:26:10 +02:00
{
for (TrillTableItem i : trillTable) {
if (i.type == trillType())
return i.name;
2012-05-26 14:26:10 +02:00
}
qDebug("unknown Trill subtype %d", int(trillType()));
return "?";
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// 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
//---------------------------------------------------------
QVariant Trill::getProperty(P_ID propertyId) const
{
switch(propertyId) {
2014-05-26 18:18:01 +02:00
case P_ID::TRILL_TYPE:
2014-05-25 18:51:19 +02:00
return int(trillType());
case P_ID::ORNAMENT_STYLE:
return int(ornamentStyle());
case P_ID::PLAY:
return bool(playArticulation());
2012-09-17 18:09:30 +02:00
default:
break;
}
return SLine::getProperty(propertyId);
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool Trill::setProperty(P_ID propertyId, const QVariant& val)
{
switch(propertyId) {
2014-05-26 18:18:01 +02:00
case P_ID::TRILL_TYPE:
2014-06-25 14:07:43 +02:00
setTrillType(Type(val.toInt()));
2012-09-17 18:09:30 +02:00
break;
case P_ID::PLAY:
setPlayArticulation(val.toBool());
break;
case P_ID::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;
}
2016-03-02 13:20:19 +01:00
score()->setLayoutAll();
2012-09-17 18:09:30 +02:00
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant Trill::propertyDefault(P_ID propertyId) const
{
switch(propertyId) {
2014-05-26 18:18:01 +02:00
case P_ID::TRILL_TYPE:
2012-09-17 18:09:30 +02:00
return 0;
case P_ID::ORNAMENT_STYLE:
//return int(score()->style()->ornamentStyle(_ornamentStyle));
return int(MScore::OrnamentStyle::DEFAULT);
case P_ID::PLAY:
return true;
case P_ID::PLACEMENT:
return int(Element::Placement::ABOVE);
2012-09-17 18:09:30 +02:00
default:
return SLine::propertyDefault(propertyId);
}
return QVariant();
}
//---------------------------------------------------------
// undoSetTrillType
2012-09-17 18:09:30 +02:00
//---------------------------------------------------------
2014-06-25 14:07:43 +02:00
void Trill::undoSetTrillType(Type val)
2012-09-17 18:09:30 +02:00
{
2016-06-09 09:26:13 +02:00
undoChangeProperty(P_ID::TRILL_TYPE, int(val));
2012-09-17 18:09:30 +02:00
}
2012-10-27 14:46:35 +02:00
//---------------------------------------------------------
// setYoff
//---------------------------------------------------------
void Trill::setYoff(qreal val)
{
2016-11-18 14:28:53 +01:00
rUserYoffset() += val * spatium() - score()->styleP(StyleIdx::trillPosAbove);
2012-10-27 14:46:35 +02:00
}
2014-08-06 12:28:58 +02:00
//---------------------------------------------------------
// 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
}