MuseScore/libmscore/tuplet.cpp

1100 lines
38 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 "tuplet.h"
#include "score.h"
#include "chord.h"
#include "note.h"
#include "xml.h"
#include "staff.h"
2012-05-26 14:26:10 +02:00
#include "style.h"
#include "text.h"
#include "element.h"
#include "undo.h"
#include "stem.h"
2013-09-02 19:07:39 +02:00
#include "beam.h"
#include "measure.h"
2012-05-26 14:26:10 +02:00
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// Tuplet
//---------------------------------------------------------
Tuplet::Tuplet(Score* s)
2018-04-09 11:51:35 +02:00
: DurationElement(s, ElementFlag::MOVABLE | ElementFlag::SELECTABLE | ElementFlag::ON_STAFF)
2012-05-26 14:26:10 +02:00
{
2018-04-09 11:51:35 +02:00
_tick = 0;
_ratio = Fraction(1, 1);
2012-05-26 14:26:10 +02:00
_number = 0;
_hasBracket = false;
_isUp = true;
2018-03-27 14:40:34 +02:00
initSubStyle(SubStyleId::TUPLET);
2012-05-26 14:26:10 +02:00
}
Tuplet::Tuplet(const Tuplet& t)
: DurationElement(t)
{
_tick = t._tick;
_hasBracket = t._hasBracket;
_ratio = t._ratio;
_baseLen = t._baseLen;
_direction = t._direction;
_numberType = t._numberType;
_bracketType = t._bracketType;
2018-03-16 12:08:57 +01:00
_bracketWidth = t._bracketWidth;
2012-05-26 14:26:10 +02:00
_isUp = t._isUp;
p1 = t.p1;
p2 = t.p2;
_p1 = t._p1;
_p2 = t._p2;
2012-05-26 14:26:10 +02:00
2018-04-09 11:51:35 +02:00
// recreated on layout
_number = 0;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// ~Tuplet
//---------------------------------------------------------
Tuplet::~Tuplet()
{
delete _number;
}
//---------------------------------------------------------
// setSelected
//---------------------------------------------------------
void Tuplet::setSelected(bool f)
{
Element::setSelected(f);
if (_number)
_number->setSelected(f);
}
//---------------------------------------------------------
// setVisible
//---------------------------------------------------------
void Tuplet::setVisible(bool f)
{
Element::setVisible(f);
if (_number)
_number->setVisible(f);
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void Tuplet::layout()
{
if (_elements.empty()) {
qDebug("Tuplet::layout(): tuplet is empty");
return;
}
// is in a TAB without stems, skip any format: tuplets are not shown
2016-12-13 13:16:17 +01:00
if (staff() && staff()->isTabStaff(tick()) && staff()->staffType(tick())->slashStyle())
return;
2018-03-27 14:40:34 +02:00
//
// create tuplet number if necessary
//
2012-05-26 14:26:10 +02:00
qreal _spatium = spatium();
if (_numberType != TupletNumberType::NO_TEXT) {
2012-05-26 14:26:10 +02:00
if (_number == 0) {
2018-03-27 14:40:34 +02:00
_number = new Text(score());
2018-04-09 11:51:35 +02:00
_number->setComposition(true);
_number->setTrack(track());
2012-05-26 14:26:10 +02:00
_number->setParent(this);
_number->setVisible(visible());
2018-04-09 11:51:35 +02:00
// initSubStyle(SubStyleId::TUPLET); // hack
for (auto p : { Pid::FONT_FACE, Pid::FONT_SIZE, Pid::FONT_BOLD, Pid::FONT_ITALIC, Pid::FONT_UNDERLINE, Pid::ALIGN })
_number->resetProperty(p);
2012-05-26 14:26:10 +02:00
}
if (_numberType == TupletNumberType::SHOW_NUMBER)
_number->setXmlText(QString("%1").arg(_ratio.numerator()));
2012-05-26 14:26:10 +02:00
else
_number->setXmlText(QString("%1:%2").arg(_ratio.numerator()).arg(_ratio.denominator()));
2012-05-26 14:26:10 +02:00
}
else {
if (_number) {
if (_number->selected())
score()->deselect(_number);
delete _number;
_number = 0;
}
}
//
// find out main direction
//
2016-03-02 13:20:19 +01:00
if (_direction == Direction::AUTO) {
2012-05-26 14:26:10 +02:00
int up = 1;
for (const DurationElement* e : _elements) {
if (e->isChord()) {
const Chord* c = toChord(e);
2016-03-02 13:20:19 +01:00
if (c->stemDirection() != Direction::AUTO)
up += c->stemDirection() == Direction::UP ? 1000 : -1000;
2012-05-26 14:26:10 +02:00
else
up += c->up() ? 1 : -1;
}
else if (e->isTuplet()) {
2012-05-26 14:26:10 +02:00
// TODO
}
}
_isUp = up > 0;
}
else
2016-03-02 13:20:19 +01:00
_isUp = _direction == Direction::UP;
2012-05-26 14:26:10 +02:00
2018-03-27 14:40:34 +02:00
//
// find first and last chord of tuplet
// (tuplets can be nested)
//
2012-05-26 14:26:10 +02:00
const DurationElement* cr1 = _elements.front();
while (cr1->isTuplet()) {
const Tuplet* t = toTuplet(cr1);
2012-05-26 14:26:10 +02:00
if (t->elements().empty())
break;
cr1 = t->elements().front();
}
const DurationElement* cr2 = _elements.back();
while (cr2->isTuplet()) {
const Tuplet* t = toTuplet(cr2);
2012-05-26 14:26:10 +02:00
if (t->elements().empty())
break;
cr2 = t->elements().back();
}
//
// shall we draw a bracket?
//
if (_bracketType == TupletBracketType::AUTO_BRACKET) {
2013-05-24 11:44:21 +02:00
_hasBracket = false;
2018-03-27 14:40:34 +02:00
for (DurationElement* e : _elements) {
if (e->isTuplet() || e->isRest()) {
2013-05-24 11:44:21 +02:00
_hasBracket = true;
break;
}
else if (e->isChordRest()) {
ChordRest* cr = toChordRest(e);
2013-05-24 11:44:21 +02:00
//
// maybe we should check for more than one beam
//
if (cr->beam() == 0) {
2012-05-26 14:26:10 +02:00
_hasBracket = true;
break;
}
}
}
}
else
_hasBracket = _bracketType != TupletBracketType::SHOW_NO_BRACKET;
2012-05-26 14:26:10 +02:00
//
// calculate bracket start and end point p1 p2
//
2018-03-27 15:36:00 +02:00
qreal maxSlope = score()->styleD(Sid::tupletMaxSlope);
bool outOfStaff = score()->styleB(Sid::tupletOufOfStaff);
qreal vHeadDistance = score()->styleP(Sid::tupletVHeadDistance);
qreal vStemDistance = score()->styleP(Sid::tupletVStemDistance);
qreal stemLeft = score()->styleP(Sid::tupletStemLeftDistance);
qreal stemRight = score()->styleP(Sid::tupletStemRightDistance);
qreal noteLeft = score()->styleP(Sid::tupletNoteLeftDistance);
qreal noteRight = score()->styleP(Sid::tupletNoteRightDistance);
2013-09-02 19:07:39 +02:00
int move = 0;
if (outOfStaff && cr1->isChordRest() && cr2->isChordRest()) {
// account for staff move when adjusting bracket to avoid staff
// but don't attempt adjustment unless both endpoints are in same staff
if (toChordRest(cr1)->staffMove() == toChordRest(cr2)->staffMove())
move = toChordRest(cr1)->staffMove();
else
outOfStaff = false;
}
2018-03-27 14:40:34 +02:00
qreal l1 = _spatium; // bracket tip height TODO: create style value
qreal l2l = vHeadDistance; // left bracket vertical distance
qreal l2r = vHeadDistance; // right bracket vertical distance right
2013-05-24 11:44:21 +02:00
if (_isUp)
2013-09-02 19:07:39 +02:00
vHeadDistance = -vHeadDistance;
2013-05-24 11:44:21 +02:00
p1 = cr1->pagePos();
p2 = cr2->pagePos();
2013-09-02 19:07:39 +02:00
p1.rx() -= noteLeft;
p2.rx() += score()->noteHeadWidth() + noteRight;
p1.ry() += vHeadDistance;
p2.ry() += vHeadDistance;
2012-05-26 14:26:10 +02:00
2013-09-02 19:07:39 +02:00
qreal xx1 = p1.x(); // use to center the number on the beam
// follow beam angle if one beam extends over entire tuplet
bool followBeam = false;
qreal beamAdjust = 0.0;
if (cr1->beam() && cr1->beam() == cr2->beam()) {
followBeam = true;
2018-03-27 15:36:00 +02:00
beamAdjust = score()->styleP(Sid::beamWidth) * 0.5 * mag();
}
2013-05-24 11:44:21 +02:00
if (_isUp) {
if (cr1->isChord()) {
const Chord* chord1 = toChord(cr1);
2012-05-26 14:26:10 +02:00
Stem* stem = chord1->stem();
2013-09-02 19:07:39 +02:00
if (stem)
xx1 = stem->abbox().x();
2013-09-02 19:07:39 +02:00
if (chord1->up()) {
if (stem) {
if (followBeam)
p1.ry() = stem->abbox().y() - beamAdjust;
else if (chord1->beam())
2013-09-02 19:07:39 +02:00
p1.ry() = chord1->beam()->abbox().y();
else
p1.ry() = stem->abbox().y();
l2l = vStemDistance;
}
else {
p1.ry() = chord1->upNote()->abbox().top(); // whole note
2012-05-26 14:26:10 +02:00
}
}
2013-09-02 19:07:39 +02:00
else if (!chord1->up()) {
p1.ry() = chord1->upNote()->abbox().top();
if (stem)
2013-09-02 19:07:39 +02:00
p1.rx() = cr1->pagePos().x() - stemLeft;
}
2012-05-26 14:26:10 +02:00
}
if (cr2->isChord()) {
const Chord* chord2 = toChord(cr2);
2012-05-26 14:26:10 +02:00
Stem* stem = chord2->stem();
2013-09-02 19:07:39 +02:00
if (stem && chord2->up()) {
if (followBeam)
p2.ry() = stem->abbox().top() - beamAdjust;
else if (chord2->beam())
2013-09-02 19:07:39 +02:00
p2.ry() = chord2->beam()->abbox().top();
else
p2.ry() = stem->abbox().top();
l2r = vStemDistance;
p2.rx() = chord2->pagePos().x() + chord2->maxHeadWidth() + stemRight;
}
else {
p2.ry() = chord2->upNote()->abbox().top();
2012-05-26 14:26:10 +02:00
}
}
//
// special case: one of the bracket endpoints is
// a rest
//
if (cr1->isChord() && cr2->isChord()) {
2012-05-26 14:26:10 +02:00
if (p2.y() < p1.y())
p1.setY(p2.y());
else
p2.setY(p1.y());
}
else if (cr1->isChord() && !cr2->isChord()) {
2012-05-26 14:26:10 +02:00
if (p1.y() < p2.y())
p2.setY(p1.y());
else
p1.setY(p2.y());
}
2013-09-02 19:07:39 +02:00
// outOfStaff
if (outOfStaff) {
qreal min = cr1->measure()->staffabbox(cr1->staffIdx() + move).y();
2013-09-02 19:07:39 +02:00
if (min < p1.y()) {
p1.ry() = min;
l2l = vStemDistance;
}
min = cr2->measure()->staffabbox(cr2->staffIdx() + move).y();
2013-09-02 19:07:39 +02:00
if (min < p2.y()) {
p2.ry() = min;
l2r = vStemDistance;
}
}
// check that slope is no more than max
2013-09-02 19:07:39 +02:00
qreal d = (p2.y() - p1.y())/(p2.x() - p1.x());
if (d < -maxSlope) {
// move p1 y up
p1.ry() = p2.y() + maxSlope * (p2.x() - p1.x());
}
else if (d > maxSlope) {
// move p2 y up
p2.ry() = p1.ry() + maxSlope * (p2.x() - p1.x());
}
2012-05-26 14:26:10 +02:00
2013-09-02 19:07:39 +02:00
// check for collisions
2012-05-26 14:26:10 +02:00
int n = _elements.size();
if (n >= 3) {
2013-09-02 19:07:39 +02:00
d = (p2.y() - p1.y())/(p2.x() - p1.x());
2012-05-26 14:26:10 +02:00
for (int i = 1; i < (n-1); ++i) {
Element* e = _elements[i];
if (e->isChord()) {
const Chord* chord = toChord(e);
2012-05-26 14:26:10 +02:00
const Stem* stem = chord->stem();
if (stem) {
QRectF r(chord->up() ? stem->abbox() : chord->upNote()->abbox());
2012-05-26 14:26:10 +02:00
qreal y3 = r.top();
qreal x3 = r.x() + r.width() * .5;
qreal y0 = p1.y() + (x3 - p1.x()) * d;
qreal c = y0 - y3;
if (c > 0) {
p1.ry() -= c;
p2.ry() -= c;
}
}
}
}
}
}
else {
if (cr1->isChord()) {
const Chord* chord1 = toChord(cr1);
2012-05-26 14:26:10 +02:00
Stem* stem = chord1->stem();
2013-09-02 19:07:39 +02:00
if (stem)
xx1 = stem->abbox().x();
2013-09-02 19:07:39 +02:00
if (!chord1->up()) {
if (stem) {
if (followBeam)
p1.ry() = stem->abbox().bottom() + beamAdjust;
else if (chord1->beam())
2013-09-02 19:07:39 +02:00
p1.ry() = chord1->beam()->abbox().bottom();
else
p1.ry() = stem->abbox().bottom();
l2l = vStemDistance;
p1.rx() = cr1->pagePos().x() - stemLeft;
}
else {
2013-09-02 19:07:39 +02:00
p1.ry() = chord1->downNote()->abbox().bottom(); // whole note
2012-05-26 14:26:10 +02:00
}
}
2013-09-02 19:07:39 +02:00
else if (chord1->up()) {
p1.ry() = chord1->downNote()->abbox().bottom();
}
2012-05-26 14:26:10 +02:00
}
if (cr2->isChord()) {
const Chord* chord2 = toChord(cr2);
2012-05-26 14:26:10 +02:00
Stem* stem = chord2->stem();
if (stem && !chord2->up()) {
2013-05-24 11:44:21 +02:00
// if (chord2->beam())
// p2.setX(stem->abbox().x());
2018-03-27 14:40:34 +02:00
#if 0 // TODO-ws beam bbox not available at this point
if (followBeam)
p2.ry() = stem->abbox().bottom() + beamAdjust;
if (chord2->beam())
2013-09-02 19:07:39 +02:00
p2.ry() = chord2->beam()->abbox().bottom();
else
p2.ry() = stem->abbox().bottom();
2018-03-27 14:40:34 +02:00
#endif
2013-09-02 19:07:39 +02:00
l2r = vStemDistance;
2012-05-26 14:26:10 +02:00
}
2013-09-02 19:07:39 +02:00
else {
p2.ry() = chord2->downNote()->abbox().bottom();
if (stem)
p2.rx() = chord2->pagePos().x() + chord2->maxHeadWidth() + stemRight;
2012-05-26 14:26:10 +02:00
}
}
2013-09-02 19:07:39 +02:00
//
// special case: one of the bracket endpoints is
// a rest
//
if (!cr1->isChord() && cr2->isChord()) {
2012-05-26 14:26:10 +02:00
if (p2.y() > p1.y())
p1.setY(p2.y());
else
p2.setY(p1.y());
}
else if (cr1->isChord() && !cr2->isChord()) {
2012-05-26 14:26:10 +02:00
if (p1.y() > p2.y())
p2.setY(p1.y());
else
p1.setY(p2.y());
}
2013-09-02 19:07:39 +02:00
// outOfStaff
if (outOfStaff) {
qreal max = cr1->measure()->staffabbox(cr1->staffIdx() + move).bottom();
2013-09-02 19:07:39 +02:00
if (max > p1.y()) {
p1.ry() = max;
l2l = vStemDistance;
}
max = cr2->measure()->staffabbox(cr2->staffIdx() + move).bottom();
2013-09-02 19:07:39 +02:00
if (max > p2.y()) {
p2.ry() = max;
l2r = vStemDistance;
}
}
// check that slope is no more than max
2013-09-02 19:07:39 +02:00
qreal d = (p2.y() - p1.y())/(p2.x() - p1.x());
if (d < -maxSlope) {
// move p1 y up
p2.ry() = p1.y() - maxSlope * (p2.x() - p1.x());
}
else if (d > maxSlope) {
// move p2 y up
p1.ry() = p2.ry() - maxSlope * (p2.x() - p1.x());
}
2012-05-26 14:26:10 +02:00
// check for collisions
int n = _elements.size();
if (n >= 3) {
qreal d = (p2.y() - p1.y())/(p2.x() - p1.x());
for (int i = 1; i < (n-1); ++i) {
Element* e = _elements[i];
if (e->isChord()) {
const Chord* chord = toChord(e);
2012-05-26 14:26:10 +02:00
const Stem* stem = chord->stem();
if (stem) {
QRectF r(chord->up() ? chord->downNote()->abbox() : stem->abbox());
2012-05-26 14:26:10 +02:00
qreal y3 = r.bottom();
qreal x3 = r.x() + r.width() * .5;
qreal y0 = p1.y() + (x3 - p1.x()) * d;
qreal c = y0 - y3;
if (c < 0) {
p1.ry() -= c;
p2.ry() -= c;
}
}
}
}
}
}
setPos(0.0, 0.0);
QPointF mp(parent()->pagePos());
p1 -= mp;
p2 -= mp;
p1 += _p1;
p2 += _p2;
xx1 -= mp.x();
2013-09-02 19:07:39 +02:00
p1.ry() -= l2l * (_isUp ? 1.0 : -1.0);
p2.ry() -= l2r * (_isUp ? 1.0 : -1.0);
2012-05-26 14:26:10 +02:00
2018-03-27 14:40:34 +02:00
// l2l l2r, mp, _p1, _p2 const
2012-05-26 14:26:10 +02:00
// center number
qreal x3 = 0.0;
qreal numberWidth = 0.0;
if (_number) {
_number->layout();
2013-09-02 19:07:39 +02:00
numberWidth = _number->bbox().width();
//
// for beamed tuplets, center number on beam
//
2013-09-02 19:07:39 +02:00
if (cr1->beam() && cr2->beam() && cr1->beam() == cr2->beam()) {
const ChordRest* crr = toChordRest(cr1);
2013-09-02 19:07:39 +02:00
if(_isUp == crr->up()) {
qreal deltax = cr2->pagePos().x() - cr1->pagePos().x();
x3 = xx1 + deltax * .5;
}
else {
qreal deltax = p2.x() - p1.x();
x3 = p1.x() + deltax * .5;
}
}
else {
qreal deltax = p2.x() - p1.x();
x3 = p1.x() + deltax * .5;
}
2012-05-26 14:26:10 +02:00
2013-09-02 19:07:39 +02:00
qreal y3 = p1.y() + (p2.y() - p1.y()) * .5 - l1 * (_isUp ? 1.0 : -1.0);
2012-05-26 14:26:10 +02:00
_number->setPos(QPointF(x3, y3) - ipos());
}
if (_hasBracket) {
qreal slope = (p2.y() - p1.y()) / (p2.x() - p1.x());
if (_isUp) {
if (_number) {
2013-09-02 19:07:39 +02:00
bracketL[0] = QPointF(p1.x(), p1.y());
bracketL[1] = QPointF(p1.x(), p1.y() - l1);
2012-05-26 14:26:10 +02:00
qreal x = x3 - numberWidth * .5 - _spatium * .5;
qreal y = p1.y() + (x - p1.x()) * slope;
2013-09-02 19:07:39 +02:00
bracketL[2] = QPointF(x, y - l1);
2012-05-26 14:26:10 +02:00
x = x3 + numberWidth * .5 + _spatium * .5;
y = p1.y() + (x - p1.x()) * slope;
2013-09-02 19:07:39 +02:00
bracketR[0] = QPointF(x, y - l1);
bracketR[1] = QPointF(p2.x(), p2.y() - l1);
bracketR[2] = QPointF(p2.x(), p2.y());
2012-05-26 14:26:10 +02:00
}
else {
2013-09-02 19:07:39 +02:00
bracketL[0] = QPointF(p1.x(), p1.y());
bracketL[1] = QPointF(p1.x(), p1.y() - l1);
bracketL[2] = QPointF(p2.x(), p2.y() - l1);
bracketL[3] = QPointF(p2.x(), p2.y());
2012-05-26 14:26:10 +02:00
}
}
else {
if (_number) {
2013-09-02 19:07:39 +02:00
bracketL[0] = QPointF(p1.x(), p1.y());
bracketL[1] = QPointF(p1.x(), p1.y() + l1);
2012-05-26 14:26:10 +02:00
qreal x = x3 - numberWidth * .5 - _spatium * .5;
qreal y = p1.y() + (x - p1.x()) * slope;
2013-09-02 19:07:39 +02:00
bracketL[2] = QPointF(x, y + l1);
2012-05-26 14:26:10 +02:00
x = x3 + numberWidth * .5 + _spatium * .5;
y = p1.y() + (x - p1.x()) * slope;
2013-09-02 19:07:39 +02:00
bracketR[0] = QPointF(x, y + l1);
bracketR[1] = QPointF(p2.x(), p2.y() + l1);
bracketR[2] = QPointF(p2.x(), p2.y());
2012-05-26 14:26:10 +02:00
}
else {
2013-09-02 19:07:39 +02:00
bracketL[0] = QPointF(p1.x(), p1.y());
bracketL[1] = QPointF(p1.x(), p1.y() + l1);
bracketL[2] = QPointF(p2.x(), p2.y() + l1);
bracketL[3] = QPointF(p2.x(), p2.y());
2012-05-26 14:26:10 +02:00
}
}
}
2018-03-27 14:40:34 +02:00
// collect bounding box
2012-05-26 14:26:10 +02:00
QRectF r;
if (_number) {
r |= _number->bbox().translated(_number->pos());
if (_hasBracket) {
QRectF b;
b.setCoords(bracketL[1].x(), bracketL[1].y(), bracketR[2].x(), bracketR[2].y());
r |= b;
}
}
else if (_hasBracket) {
QRectF b;
b.setCoords(bracketL[1].x(), bracketL[1].y(), bracketL[3].x(), bracketL[3].y());
r |= b;
}
setbbox(r);
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void Tuplet::draw(QPainter* painter) const
{
// if in a TAB without stems, tuplets are not shown
2016-12-13 13:16:17 +01:00
if (staff() && staff()->isTabStaff(tick()) && staff()->staffType(tick())->slashStyle())
return;
2012-05-26 14:26:10 +02:00
QColor color(curColor());
if (_number) {
painter->setPen(color);
QPointF pos(_number->pos());
painter->translate(pos);
_number->draw(painter);
painter->translate(-pos);
}
if (_hasBracket) {
painter->setPen(QPen(color, _bracketWidth.val()));
2012-05-26 14:26:10 +02:00
if (!_number)
painter->drawPolyline(bracketL, 4);
else {
painter->drawPolyline(bracketL, 3);
painter->drawPolyline(bracketR, 3);
}
}
}
//---------------------------------------------------------
// Rect
// helper class
//---------------------------------------------------------
class Rect : public QRectF {
public:
Rect(const QPointF& p1, const QPointF& p2, qreal w);
};
//---------------------------------------------------------
// Rect
// construct a rectangle out of a line with width w
//---------------------------------------------------------
Rect::Rect(const QPointF& p1, const QPointF& p2, qreal w)
{
qreal w2 = w * .5;
setCoords(qMin(p1.x(), p2.x()) - w2, qMin(p1.y(), p2.y()) - w2, qMax(p1.x(), p2.x()) + w2, qMax(p1.y(), p2.y()) + w2);
}
//---------------------------------------------------------
// shape
//---------------------------------------------------------
Shape Tuplet::shape() const
{
Shape s;
if (_hasBracket) {
qreal w = _bracketWidth.val();
if (_number) {
s.add(Rect(bracketL[0], bracketL[1], w));
s.add(Rect(bracketL[1], bracketL[2], w));
s.add(Rect(bracketR[0], bracketR[1], w));
s.add(Rect(bracketR[1], bracketR[2], w));
}
else {
s.add(Rect(bracketL[0], bracketL[1], w));
s.add(Rect(bracketL[1], bracketL[2], w));
s.add(Rect(bracketL[2], bracketL[3], w));
}
}
if (_number)
s.add(_number->bbox().translated(_number->pos()));
return s;
}
//---------------------------------------------------------
// scanElements
//---------------------------------------------------------
void Tuplet::scanElements(void* data, void (*func)(void*, Element*), bool all)
{
if (_number && all)
func(data, _number);
2018-03-27 14:40:34 +02:00
func(data, this);
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void Tuplet::write(XmlWriter& xml) const
2012-05-26 14:26:10 +02:00
{
xml.stag(QString("Tuplet id=\"%1\"").arg(_id));
if (tuplet())
xml.tag("Tuplet", tuplet()->id());
Element::writeProperties(xml);
2018-03-27 15:36:00 +02:00
writeProperty(xml, Pid::DIRECTION);
writeProperty(xml, Pid::NUMBER_TYPE);
writeProperty(xml, Pid::BRACKET_TYPE);
writeProperty(xml, Pid::LINE_WIDTH);
writeProperty(xml, Pid::NORMAL_NOTES);
writeProperty(xml, Pid::ACTUAL_NOTES);
writeProperty(xml, Pid::P1);
writeProperty(xml, Pid::P2);
2012-08-10 17:01:35 +02:00
2012-05-26 14:26:10 +02:00
xml.tag("baseNote", _baseLen.name());
if (_number) {
xml.stag("Number");
_number->writeProperties(xml);
xml.etag();
}
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void Tuplet::read(XmlReader& e)
2012-05-26 14:26:10 +02:00
{
2013-01-11 18:10:18 +01:00
_id = e.intAttribute("id", 0);
while (e.readNextStartElement()) {
if (readProperties(e))
;
else
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
2016-11-02 08:42:32 +01:00
Fraction f(_ratio.denominator(), _baseLen.fraction().denominator());
setDuration(f.reduced());
}
//---------------------------------------------------------
// readProperties
//---------------------------------------------------------
bool Tuplet::readProperties(XmlReader& e)
{
const QStringRef& tag(e.name());
2018-04-09 11:51:35 +02:00
if (readStyledProperty(e, tag))
2017-01-16 20:51:12 +01:00
;
else if (tag == "normalNotes")
_ratio.setDenominator(e.readInt());
else if (tag == "actualNotes")
_ratio.setNumerator(e.readInt());
else if (tag == "p1")
_p1 = e.readPoint() * score()->spatium();
else if (tag == "p2")
_p2 = e.readPoint() * score()->spatium();
else if (tag == "baseNote")
_baseLen = TDuration(e.readElementText());
else if (tag == "Number") {
2018-03-27 14:40:34 +02:00
_number = new Text(score());
2018-04-09 11:51:35 +02:00
_number->setComposition(true);
_number->setParent(this);
2018-04-09 11:51:35 +02:00
// _number->setSubStyleId(SubStyleId::TUPLET);
// initSubStyle(SubStyleId::TUPLET); // hack: initialize number
for (auto p : { Pid::FONT_FACE, Pid::FONT_SIZE, Pid::FONT_BOLD, Pid::FONT_ITALIC, Pid::FONT_UNDERLINE, Pid::ALIGN })
_number->resetProperty(p);
_number->read(e);
_number->setVisible(visible()); //?? override saved property
_number->setTrack(track());
// move property flags from _number
2018-03-27 15:36:00 +02:00
for (auto p : { Pid::FONT_FACE, Pid::FONT_SIZE, Pid::FONT_BOLD, Pid::FONT_ITALIC, Pid::FONT_UNDERLINE, Pid::ALIGN })
setPropertyFlags(p, _number->propertyFlags(p));
2012-05-26 14:26:10 +02:00
}
else if (!DurationElement::readProperties(e))
return false;
return true;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
void Tuplet::add(Element* e)
{
#ifndef NDEBUG
for(DurationElement* el : _elements) {
2012-05-26 14:26:10 +02:00
if (el == e) {
qDebug("%p: %p %s already there", this, e, e->name());
return;
2012-05-26 14:26:10 +02:00
}
}
#endif
switch (e->type()) {
2018-03-27 14:40:34 +02:00
// case ElementType::TEXT:
// _number = toText(e);
// break;
2017-01-18 14:16:33 +01:00
case ElementType::CHORD:
case ElementType::REST:
case ElementType::TUPLET: {
2013-01-24 09:31:41 +01:00
bool found = false;
DurationElement* de = toDurationElement(e);
2012-05-26 14:26:10 +02:00
int tick = de->tick();
if (tick != -1) {
for (unsigned int i = 0; i < _elements.size(); ++i) {
2012-05-26 14:26:10 +02:00
if (_elements[i]->tick() > tick) {
_elements.insert(_elements.begin() + i, de);
2013-01-24 09:31:41 +01:00
found = true;
break;
2012-05-26 14:26:10 +02:00
}
}
}
2013-01-24 09:31:41 +01:00
if (!found)
_elements.push_back(de);
2012-05-26 14:26:10 +02:00
de->setTuplet(this);
2013-01-24 09:31:41 +01:00
}
2012-05-26 14:26:10 +02:00
break;
default:
qDebug("Tuplet::add() unknown element");
break;
}
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void Tuplet::remove(Element* e)
{
switch (e->type()) {
2018-03-27 14:40:34 +02:00
// case ElementType::TEXT:
// if (e == _number)
// _number = 0;
// break;
2017-01-18 14:16:33 +01:00
case ElementType::CHORD:
case ElementType::REST:
case ElementType::TUPLET: {
2017-12-20 16:49:30 +01:00
auto i = std::find(_elements.begin(), _elements.end(), toDurationElement(e));
if (i == _elements.end()) {
2014-04-15 17:01:41 +02:00
qDebug("Tuplet::remove: cannot find element <%s>", e->name());
2016-07-10 12:00:57 +02:00
qDebug(" elements %zu", _elements.size());
2012-05-26 14:26:10 +02:00
}
else
_elements.erase(i);
}
2012-05-26 14:26:10 +02:00
break;
default:
qDebug("Tuplet::remove: unknown element");
break;
}
}
//---------------------------------------------------------
// isEditable
//---------------------------------------------------------
bool Tuplet::isEditable() const
{
return _hasBracket;
}
2017-03-31 13:03:15 +02:00
//---------------------------------------------------------
// startEdit
//---------------------------------------------------------
void Tuplet::startEdit(EditData& ed)
{
Element::startEdit(ed);
2017-03-31 13:03:15 +02:00
ed.grips = 2;
ed.curGrip = Grip::END;
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// editDrag
//---------------------------------------------------------
2017-03-31 13:03:15 +02:00
void Tuplet::editDrag(EditData& ed)
2012-05-26 14:26:10 +02:00
{
if (ed.curGrip == Grip::START)
2012-05-26 14:26:10 +02:00
_p1 += ed.delta;
else
_p2 += ed.delta;
setGenerated(false);
layout();
2016-03-02 13:20:19 +01:00
score()->setUpdateAll();
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// updateGrips
//---------------------------------------------------------
2017-03-31 13:03:15 +02:00
void Tuplet::updateGrips(EditData& ed) const
2012-05-26 14:26:10 +02:00
{
2017-03-31 13:03:15 +02:00
ed.grip[0].translate(pagePos() + p1);
ed.grip[1].translate(pagePos() + p2);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
2012-11-19 10:08:15 +01:00
// reset
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2012-11-19 10:08:15 +01:00
void Tuplet::reset()
2012-05-26 14:26:10 +02:00
{
2018-03-27 15:36:00 +02:00
undoChangeProperty(Pid::P1, QPointF());
undoChangeProperty(Pid::P2, QPointF());
2012-11-19 10:08:15 +01:00
Element::reset();
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// dump
//---------------------------------------------------------
void Tuplet::dump() const
{
Element::dump();
qDebug("ratio %s", qPrintable(_ratio.print()));
}
//---------------------------------------------------------
// setTrack
//---------------------------------------------------------
void Tuplet::setTrack(int val)
{
if (_number)
_number->setTrack(val);
2012-05-26 14:26:10 +02:00
Element::setTrack(val);
}
//---------------------------------------------------------
// tickGreater
//---------------------------------------------------------
static bool tickGreater(const DurationElement* a, const DurationElement* b)
{
return a->tick() < b->tick();
}
//---------------------------------------------------------
// sortElements
//---------------------------------------------------------
void Tuplet::sortElements()
{
qSort(_elements.begin(), _elements.end(), tickGreater);
}
//---------------------------------------------------------
// elementsDuration
/// Get the sum of the element fraction in the tuplet,
/// even if the tuplet is not complete yet
//---------------------------------------------------------
Fraction Tuplet::elementsDuration()
{
Fraction f;
for (DurationElement* el : _elements)
2013-08-02 10:45:46 +02:00
f += el->duration();
return f;
}
2012-08-10 17:01:35 +02:00
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
QVariant Tuplet::getProperty(Pid propertyId) const
2012-05-26 14:26:10 +02:00
{
2016-03-08 17:58:04 +01:00
switch (propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::DIRECTION:
2017-06-22 12:42:14 +02:00
return QVariant::fromValue<Direction>(_direction);
2018-03-27 15:36:00 +02:00
case Pid::NUMBER_TYPE:
return int(_numberType);
2018-03-27 15:36:00 +02:00
case Pid::BRACKET_TYPE:
return int(_bracketType);
2018-03-27 15:36:00 +02:00
case Pid::LINE_WIDTH:
2018-03-16 12:08:57 +01:00
return _bracketWidth;
2018-03-27 15:36:00 +02:00
case Pid::NORMAL_NOTES:
2012-08-10 17:01:35 +02:00
return _ratio.denominator();
2018-03-27 15:36:00 +02:00
case Pid::ACTUAL_NOTES:
2012-08-10 17:01:35 +02:00
return _ratio.numerator();
2018-03-27 15:36:00 +02:00
case Pid::P1:
2012-08-10 17:01:35 +02:00
return _p1;
2018-03-27 15:36:00 +02:00
case Pid::P2:
2012-08-10 17:01:35 +02:00
return _p2;
2018-03-27 15:36:00 +02:00
case Pid::FONT_SIZE:
case Pid::FONT_FACE:
case Pid::FONT_BOLD:
case Pid::FONT_ITALIC:
case Pid::FONT_UNDERLINE:
case Pid::ALIGN:
return _number ? _number->getProperty(propertyId) : QVariant();
2012-08-10 17:01:35 +02:00
default:
2012-05-26 14:26:10 +02:00
break;
}
2012-09-25 13:33:50 +02:00
return DurationElement::getProperty(propertyId);
2012-05-26 14:26:10 +02:00
}
2012-08-10 17:01:35 +02:00
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
bool Tuplet::setProperty(Pid propertyId, const QVariant& v)
2012-05-26 14:26:10 +02:00
{
2016-03-08 17:58:04 +01:00
switch (propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::DIRECTION:
2016-03-02 13:20:19 +01:00
setDirection(v.value<Direction>());
2012-08-10 17:01:35 +02:00
break;
2018-03-27 15:36:00 +02:00
case Pid::NUMBER_TYPE:
setNumberType(TupletNumberType(v.toInt()));
2012-08-10 17:01:35 +02:00
break;
2018-03-27 15:36:00 +02:00
case Pid::BRACKET_TYPE:
setBracketType(TupletBracketType(v.toInt()));
2012-08-10 17:01:35 +02:00
break;
2018-03-27 15:36:00 +02:00
case Pid::LINE_WIDTH:
2018-03-16 12:08:57 +01:00
setBracketWidth(v.value<Spatium>());
break;
2018-03-27 15:36:00 +02:00
case Pid::NORMAL_NOTES:
2012-08-10 17:01:35 +02:00
_ratio.setDenominator(v.toInt());
break;
2018-03-27 15:36:00 +02:00
case Pid::ACTUAL_NOTES:
2012-08-10 17:01:35 +02:00
_ratio.setNumerator(v.toInt());
break;
2018-03-27 15:36:00 +02:00
case Pid::P1:
2012-08-10 17:01:35 +02:00
_p1 = v.toPointF();
break;
2018-03-27 15:36:00 +02:00
case Pid::P2:
2012-08-10 17:01:35 +02:00
_p2 = v.toPointF();
break;
2018-03-27 15:36:00 +02:00
case Pid::FONT_SIZE:
case Pid::FONT_FACE:
case Pid::FONT_BOLD:
case Pid::FONT_ITALIC:
case Pid::FONT_UNDERLINE:
case Pid::ALIGN:
if (_number)
_number->setProperty(propertyId, v);
break;
2012-08-10 17:01:35 +02:00
default:
return DurationElement::setProperty(propertyId, v);
2012-05-26 14:26:10 +02:00
}
if (!_elements.empty()) {
_elements.front()->triggerLayout();
_elements.back()->triggerLayout();
}
2012-08-10 17:01:35 +02:00
return true;
2012-05-26 14:26:10 +02:00
}
2012-08-10 17:01:35 +02:00
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
QVariant Tuplet::propertyDefault(Pid id) const
2012-05-26 14:26:10 +02:00
{
2012-08-10 17:01:35 +02:00
switch(id) {
2018-03-27 15:36:00 +02:00
case Pid::NORMAL_NOTES:
case Pid::ACTUAL_NOTES:
return 0;
2018-03-27 15:36:00 +02:00
case Pid::P1:
case Pid::P2:
2012-08-10 17:01:35 +02:00
return QPointF();
default:
return DurationElement::propertyDefault(id);
2012-05-26 14:26:10 +02:00
}
}
//---------------------------------------------------------
// sanitizeTuplet
/// Check validity of tuplets and coherence between duration
/// and baselength. Needed for importing old files due to a bug
/// in the released version for corner-case tuplets.
/// See issue #136406 and Pull request #2881
//---------------------------------------------------------
void Tuplet::sanitizeTuplet()
{
if (ratio().numerator() == ratio().reduced().numerator()) // return if the ratio is an irreducible fraction
return;
Fraction baseLenDuration = (Fraction(ratio().denominator(),1) * baseLen().fraction()).reduced();
// Due to a bug present in 2.1 (and before), a tuplet with non-reduced ratio could be
// in a corrupted state (mismatch between duration and base length).
// A tentative will now be made to retrieve the correct duration by summing up all the
// durations of the elements constituting the tuplet. This does not work for
// not-completely filled tuplets, such as tuplets in voices > 0 with
// gaps (for example, a tuplet in second voice with a deleted chordrest element)
Fraction testDuration(0,1);
for (DurationElement* de : elements()) {
if (de == 0)
continue;
Fraction elementDuration(0,1);
if (de->isTuplet()){
Tuplet* t = toTuplet(de);
t->sanitizeTuplet();
elementDuration = t->duration();
}
else {
elementDuration = de->duration();
}
testDuration += elementDuration;
}
testDuration = testDuration / ratio();
testDuration.reduce();
if (elements().back()->tick() + elements().back()->actualTicks() - elements().front()->tick() > testDuration.ticks())
return; // this tuplet has missing elements; do not sanitize
if (!(testDuration == baseLenDuration && baseLenDuration == duration())) {
Fraction f = testDuration * Fraction(1, ratio().denominator());
f.reduce();
Fraction fbl(1, f.denominator());
if (TDuration::isValid(fbl)) {
setDuration(testDuration);
setBaseLen(fbl);
qDebug("Tuplet %p sanitized",this);
}
else {
qDebug("Impossible to sanitize the tuplet");
}
}
}
} // namespace Ms
2013-05-13 18:49:17 +02:00