Skip to content

Commit

Permalink
ENG-53: FretDiagrams create horizontal space
Browse files Browse the repository at this point in the history
This commit adds a case for including fretboard diagrams when
creating horizontal spacing in the ChordRest::shape() function. In
pursuit of this, it also extracts a calculateBoundingRect function from
FretDiagram::layout().

NOTE: In order to allow "orphaned" FretDiagrams to still create space,
the spacer is added to all staves. This is redundant in some cases, so
a more performant solution may be desirable in the future.
  • Loading branch information
iveshenry18 authored and vpereverzev committed Jun 15, 2021
1 parent 8124260 commit 3476c0f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
18 changes: 17 additions & 1 deletion libmscore/chordrest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,12 +1305,28 @@ Shape ChordRest::shape() const
if (e->isHarmony() && e->staffIdx() == staffIdx()) {
Harmony* h = toHarmony(e);
// calculate bbox only (do not reset position)
h->layout1();
if (h->bbox().isEmpty()) h->layout1();
const qreal margin = styleP(Sid::minHarmonyDistance) * 0.5;
x1 = qMin(x1, e->bbox().x() - margin + e->pos().x());
x2 = qMax(x2, e->bbox().x() + e->bbox().width() + margin + e->pos().x());
adjustWidth = true;
}
else if (e->isFretDiagram()) {
FretDiagram* fd = toFretDiagram(e);
if (fd->bbox().isEmpty()) fd->calculateBoundingRect();
qreal margin = styleP(Sid::fretMinDistance) * 0.5;
x1 = qMin(x1, e->bbox().x() - margin + e->pos().x());
x2 = qMax(x2, e->bbox().x() + e->bbox().width() + margin + e->pos().x());
adjustWidth = true;
if (fd->harmony()) {
Harmony* h = fd->harmony();
if (h->bbox().isEmpty()) h->layout1();
margin = styleP(Sid::minHarmonyDistance) * 0.5;
x1 = qMin(x1, h->bbox().x() - margin + h->pos().x() + e->pos().x());
x2 = qMax(x2, h->bbox().x() + h->bbox().width() + margin + h->pos().x() + e->pos().x());
adjustWidth = true;
}
}
}
if (adjustWidth)
shape.addHorizontalSpacing(Shape::SPACING_HARMONY, x1, x2);
Expand Down
14 changes: 11 additions & 3 deletions libmscore/fret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,10 @@ void FretDiagram::draw(QPainter* painter) const
}

//---------------------------------------------------------
// layout
// calculateBoundingRect()
//---------------------------------------------------------

void FretDiagram::layout()
void FretDiagram::calculateBoundingRect()
{
qreal _spatium = spatium() * _userMag;
stringLw = _spatium * 0.08;
Expand Down Expand Up @@ -492,7 +492,15 @@ void FretDiagram::layout()
}

bbox().setRect(x, y, w, h);
}

//---------------------------------------------------------
// layout
//---------------------------------------------------------

void FretDiagram::layout()
{
calculateBoundingRect();
if (!parent() || !parent()->isSegment()) {
setPos(QPointF());
return;
Expand Down Expand Up @@ -522,7 +530,7 @@ void FretDiagram::layout()
mainWidth = stringDist * (_strings - 1);
else if (_orientation == Orientation::HORIZONTAL)
mainWidth = fretDist * (_frets + 0.5);
setPos((noteheadWidth - mainWidth)/2, -(h + styleP(Sid::fretY)));
setPos((noteheadWidth - mainWidth)/2, -(bbox().height() + styleP(Sid::fretY)));

autoplaceSegmentElement();

Expand Down
1 change: 1 addition & 0 deletions libmscore/fret.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class FretDiagram final : public Element {

ElementType type() const override { return ElementType::FRET_DIAGRAM; }
void layout() override;
void calculateBoundingRect();
void write(XmlWriter& xml) const override;
void writeNew(XmlWriter& xml) const;
void writeOld(XmlWriter& xml) const;
Expand Down

0 comments on commit 3476c0f

Please sign in to comment.