MuseScore/libmscore/fingering.cpp

241 lines
9.1 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
2018-03-21 15:34:32 +01:00
// Copyright (C) 2010-2018 Werner Schweer
2012-05-26 14:26:10 +02:00
//
// 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 "fingering.h"
#include "score.h"
#include "staff.h"
2012-05-26 14:26:10 +02:00
#include "undo.h"
2014-04-09 16:09:21 +02:00
#include "xml.h"
#include "chord.h"
#include "part.h"
#include "measure.h"
#include "stem.h"
#include "skyline.h"
#include "system.h"
2012-05-26 14:26:10 +02:00
2013-05-13 18:49:17 +02:00
namespace Ms {
//---------------------------------------------------------
// fingeringStyle
//---------------------------------------------------------
2018-08-01 11:46:07 +02:00
static const ElementStyle fingeringStyle {
{ Sid::fingeringPlacement, Pid::PLACEMENT },
2018-08-01 11:46:07 +02:00
};
2018-03-21 14:05:33 +01:00
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// Fingering
2018-03-21 15:34:32 +01:00
// Element(Score* = 0, ElementFlags = ElementFlag::NOTHING);
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2018-08-01 11:46:07 +02:00
Fingering::Fingering(Score* s, Tid tid, ElementFlags ef)
: TextBase(s, tid, ef)
2018-03-21 14:05:33 +01:00
{
setPlacement(Placement::ABOVE);
2018-08-01 11:46:07 +02:00
initElementStyle(&fingeringStyle);
2018-03-21 14:05:33 +01:00
}
Fingering::Fingering(Score* s, ElementFlags ef)
2018-08-01 11:46:07 +02:00
: Fingering(s, Tid::FINGERING, ef)
2012-05-26 14:26:10 +02:00
{
}
//---------------------------------------------------------
// layoutType
//---------------------------------------------------------
ElementType Fingering::layoutType()
{
switch (tid()) {
case Tid::FINGERING:
case Tid::RH_GUITAR_FINGERING:
case Tid::STRING_NUMBER:
return ElementType::CHORD;
default:
return ElementType::NOTE;
}
}
//---------------------------------------------------------
// calculatePlacement
//---------------------------------------------------------
Placement Fingering::calculatePlacement() const
{
Note* n = note();
if (!n)
return Placement::ABOVE;
Chord* chord = n->chord();
Staff* staff = chord->staff();
Part* part = staff->part();
int nstaves = part->nstaves();
bool voices = chord->measure()->hasVoices(staff->idx());
bool below = voices ? !chord->up() : (nstaves > 1) && (staff->rstaff() == nstaves - 1);
return below ? Placement::BELOW : Placement::ABOVE;
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void Fingering::layout()
{
if (parent()) {
Fraction tick = parent()->tick();
const Staff* st = staff();
if (st && st->isTabStaff(tick) && !st->staffType(tick)->showTabFingering()) {
setbbox(QRectF());
return;
}
}
2017-12-27 11:51:00 +01:00
TextBase::layout();
rypos() = 0.0; // handle placement below
if (autoplace() && note()) {
Note* n = note();
Chord* chord = n->chord();
bool voices = chord->measure()->hasVoices(chord->staffIdx());
bool tight = voices && chord->notes().size() == 1 && !chord->beam() && tid() != Tid::STRING_NUMBER;
qreal headWidth = n->bboxRightPos();
// temporarily exclude self from chord shape
setAutoplace(false);
if (layoutType() == ElementType::CHORD) {
Stem* stem = chord->stem();
Segment* s = chord->segment();
Measure* m = s->measure();
SysStaff* ss = m->system()->staff(chord->vStaffIdx());
Staff* vStaff = chord->staff(); // TODO: use current height at tick
if (n->mirror())
rxpos() -= n->ipos().x();
rxpos() += headWidth * .5;
if (placeAbove()) {
if (tight) {
if (chord->stem())
rxpos() -= 0.8 * spatium();
rypos() -= spatium() * 1.5;
}
else {
QRectF r = bbox().translated(m->pos() + s->pos() + chord->pos() + n->pos() + pos());
qreal yOff = offset().y() - propertyDefault(Pid::OFFSET).toPointF().y();
r.translate(0.0, -yOff);
SkylineLine sk(false);
sk.add(r.x(), r.bottom(), r.width());
qreal d = sk.minDistance(ss->skyline().north());
if (d > 0.0)
rypos() -= d + height() * .25;
// force extra space above staff & chord (but not other fingerings)
qreal top;
if (chord->up() && chord->beam() && stem) {
top = stem->y() + stem->bbox().top();
}
else {
Note* un = chord->upNote();
top = qMin(0.0, un->y() + un->bbox().top());
}
qreal minDistance = score()->styleS(Sid::fingeringMinDistance).val() * spatium();
top -= minDistance;
qreal diff = (bbox().bottom() + ipos().y() + n->y()) - top;
if (diff > 0.0)
rypos() -= diff;
}
}
else {
if (tight) {
if (chord->stem())
rxpos() += 0.8 * spatium();
rypos() += spatium() * 1.5;
}
else {
QRectF r = bbox().translated(m->pos() + s->pos() + chord->pos() + n->pos() + pos());
qreal yOff = offset().y() - propertyDefault(Pid::OFFSET).toPointF().y();
r.translate(0.0, -yOff);
SkylineLine sk(true);
sk.add(r.x(), r.top(), r.width());
qreal d = ss->skyline().south().minDistance(sk);
if (d > 0.0)
rypos() += d + height() * .25;
// force extra space below staff & chord (but not other fingerings)
qreal bottom;
if (!chord->up() && chord->beam() && stem) {
bottom = stem->y() + stem->bbox().bottom();
}
else {
Note* dn = chord->downNote();
bottom = qMax(vStaff->height(), dn->y() + dn->bbox().bottom());
}
qreal minDistance = score()->styleS(Sid::fingeringMinDistance).val() * spatium();
bottom += minDistance;
qreal diff = bottom - (bbox().top() + ipos().y() + n->y());
if (diff > 0.0)
rypos() += diff;
}
}
}
else if (tid() == Tid::LH_GUITAR_FINGERING) {
// place to left of note
qreal left = n->shape().left();
if (left - n->x() > 0.0)
rxpos() -= left;
else
rxpos() -= n->x();
}
// for other fingering styles, do not autoplace
// restore autoplace
setAutoplace(true);
}
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void Fingering::draw(QPainter* painter) const
{
2017-12-27 11:51:00 +01:00
TextBase::draw(painter);
}
//---------------------------------------------------------
// accessibleInfo
//---------------------------------------------------------
2016-02-04 17:06:32 +01:00
QString Fingering::accessibleInfo() const
{
QString rez = Element::accessibleInfo();
2018-08-01 11:46:07 +02:00
if (tid() == Tid::STRING_NUMBER)
rez += " " + QObject::tr("String number");
return QString("%1: %2").arg(rez).arg(plainText());
}
2017-01-05 14:53:21 +01:00
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
QVariant Fingering::propertyDefault(Pid id) const
2017-01-05 14:53:21 +01:00
{
switch (id) {
case Pid::PLACEMENT:
return int(calculatePlacement());
2018-03-27 15:36:00 +02:00
case Pid::SUB_STYLE:
2018-08-01 11:46:07 +02:00
return int(Tid::FINGERING);
2017-01-05 14:53:21 +01:00
default:
2017-12-27 11:51:00 +01:00
return TextBase::propertyDefault(id);
2017-01-05 14:53:21 +01:00
}
}
2013-05-13 18:49:17 +02:00
}