Skip to content

Commit

Permalink
Fix page layout. "layoutPage()" was sometimes called with negativ res…
Browse files Browse the repository at this point in the history
…tHeight

resulting in fancy system positions.
  • Loading branch information
wschweer committed Nov 2, 2018
1 parent 0128daf commit 89ac0a6
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 14 deletions.
23 changes: 12 additions & 11 deletions libmscore/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1428,12 +1428,14 @@ static void checkDivider(bool left, System* s, qreal yOffset)

static void layoutPage(Page* page, qreal restHeight)
{
Score* score = page->score();
int nsystems = page->systems().size() - 1;
// Q_ASSERT(restHeight >= 0);

Score* score = page->score();
int gaps = page->systems().size() - 1;

QList<System*> sList;

for (int i = 0; i < nsystems; ++i) {
for (int i = 0; i < gaps; ++i) {
System* s1 = page->systems().at(i);
System* s2 = page->systems().at(i+1);
s1->setDistance(s2->y() - s1->y());
Expand Down Expand Up @@ -1465,14 +1467,12 @@ static void layoutPage(Page* page, qreal restHeight)
}

std::sort(sList.begin(), sList.end(), [](System* a, System* b) { return a->distance() < b->distance(); });

qreal maxDist = score->styleP(Sid::maxSystemDistance);

qreal dist = sList[0]->distance();
for (int i = 1; i < sList.size(); ++i) {
qreal ndist = sList[i]->distance();
qreal fill = ndist - dist;
dist = ndist;
qreal fill = ndist - dist;
if (fill > 0.0) {
qreal totalFill = fill * i;
if (totalFill > restHeight) {
Expand All @@ -1490,11 +1490,12 @@ static void layoutPage(Page* page, qreal restHeight)
if (restHeight <= 0)
break;
}
dist = ndist;
}

if (restHeight > 0.0) {
qreal fill = restHeight / sList.size();
for (int i = 0; i < sList.size(); ++i) {
System* s = sList[i];
for (System* s : sList) {
qreal d = s->distance() + fill;
if ((d - s->height()) > maxDist)
d = qMax(maxDist + s->height(), s->distance());
Expand All @@ -1503,7 +1504,7 @@ static void layoutPage(Page* page, qreal restHeight)
}

qreal y = page->systems().at(0)->y();
for (int i = 0; i < nsystems; ++i) {
for (int i = 0; i < gaps; ++i) {
System* s1 = page->systems().at(i);
System* s2 = page->systems().at(i+1);
s1->rypos() = y;
Expand Down Expand Up @@ -3603,11 +3604,11 @@ void LayoutContext::collectPage()
dist += vbox->bottomGap();
else if (!prevSystem->hasFixedDownDistance())
dist += qMax(curSystem->minBottom(), slb);

breakPage = (y + dist) >= ey && breakPages;
}
if (breakPage) {
Box* vbox = prevSystem->vbox();
qreal dist = vbox ? vbox->bottomGap() : qMax(-prevSystem->minBottom(), slb);
qreal dist = qMax(prevSystem->minBottom(), slb);
layoutPage(page, ey - (y + dist));
break;
}
Expand Down
1 change: 1 addition & 0 deletions libmscore/mscore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ bool MScore::showMeasureShapes = false;
bool MScore::noHorizontalStretch = false;
bool MScore::noVerticalStretch = false;
bool MScore::showBoundingRect = false;
bool MScore::showSystemBoundingRect = false;
bool MScore::showCorruptedMeasures = true;
bool MScore::useFallbackFont = true;
bool MScore::autoplaceSlurs = true;
Expand Down
1 change: 1 addition & 0 deletions libmscore/mscore.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ class MScore : public QObject {
static bool showSkylines;
static bool showMeasureShapes;
static bool showBoundingRect;
static bool showSystemBoundingRect;
static bool showCorruptedMeasures;
static bool useFallbackFont;
static bool autoplaceSlurs;
Expand Down
42 changes: 39 additions & 3 deletions libmscore/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ qreal System::minDistance(System* s2) const
if (vbox() && !s2->vbox())
return qMax(vbox()->bottomGap(), s2->minTop());
else if (!vbox() && s2->vbox())
return qMax(s2->vbox()->topGap(), -minBottom());
return qMax(s2->vbox()->topGap(), minBottom());
else if (vbox() && s2->vbox())
return s2->vbox()->topGap() + vbox()->bottomGap();

Expand Down Expand Up @@ -1138,14 +1138,45 @@ qreal System::bottomDistance(int staffIdx, const SkylineLine& s) const
return staff(staffIdx)->skyline().south().minDistance(s);
}

//---------------------------------------------------------
// firstVisibleSysStaff
//---------------------------------------------------------

SysStaff* System::firstVisibleSysStaff() const
{
for (SysStaff* s : _staves) {
if (s->show())
return s;
}
qDebug("no sys staff");
return 0;
}

//---------------------------------------------------------
// lastVisibleSysStaff
//---------------------------------------------------------

SysStaff* System::lastVisibleSysStaff() const
{
for (int i = _staves.size() - 1; i >= 0; --i) {
if (_staves[i]->show())
return _staves[i];
}
qDebug("no sys staff");
return 0;
}

//---------------------------------------------------------
// minTop
// Return the minimum top margin.
//---------------------------------------------------------

qreal System::minTop() const
{
return -staff(0)->skyline().north().max();
SysStaff* s = firstVisibleSysStaff();
if (s)
return -s->skyline().north().max();
return 0.0;
}

//---------------------------------------------------------
Expand All @@ -1155,7 +1186,12 @@ qreal System::minTop() const

qreal System::minBottom() const
{
return -staves()->back()->skyline().south().max();
if (vbox())
return vbox()->height() + vbox()->bottomGap();
SysStaff* s = lastVisibleSysStaff();
if (s)
return s->skyline().south().max();
return 0.0;
}

//---------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions libmscore/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class System final : public Element {
mutable bool fixedDownDistance { false };
qreal _distance; // temp. variable used during layout

SysStaff* firstVisibleSysStaff() const;
SysStaff* lastVisibleSysStaff() const;

public:
System(Score*);
~System();
Expand Down
10 changes: 10 additions & 0 deletions mscore/musescore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,9 @@ MuseScore::MuseScore()
a = getAction("show-bounding-rect");
a->setCheckable(true);
menuDebug->addAction(a);
a = getAction("show-system-bounding-rect");
a->setCheckable(true);
menuDebug->addAction(a);
a = getAction("show-corrupted-measures");
a->setCheckable(true);
a->setChecked(true);
Expand Down Expand Up @@ -5829,6 +5832,13 @@ void MuseScore::cmd(QAction* a, const QString& cmd)
cs->update();
}
}
else if (cmd == "show-system-bounding-rect") {
MScore::showSystemBoundingRect = a->isChecked();
if (cs) {
cs->setLayoutAll();
cs->update();
}
}
else if (cmd == "show-corrupted-measures") {
MScore::showCorruptedMeasures = a->isChecked();
if (cs) {
Expand Down
10 changes: 10 additions & 0 deletions mscore/scoreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,16 @@ void ScoreView::paint(const QRect& r, QPainter& p)

#ifndef NDEBUG
if (!score()->printing()) {
if (MScore::showSystemBoundingRect) {
for (const System* system : page->systems()) {
QPointF pt(system->ipos());
qreal h = system->minBottom() + system->minTop();
p.translate(pt);
QRectF r(0.0, -system->minTop(), system->width(), h);
p.drawRect(r);
p.translate(-pt);
}
}
if (MScore::showSegmentShapes) {
for (const System* system : page->systems()) {
for (const MeasureBase* mb : system->measures()) {
Expand Down
10 changes: 10 additions & 0 deletions mscore/shortcut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3635,6 +3635,16 @@ Shortcut Shortcut::_sc[] = {
Icons::Invalid_ICON,
Qt::ApplicationShortcut
},
{
MsWidget::MAIN_WINDOW,
STATE_ALL,
"show-system-bounding-rect",
"Show System Bounding Rectangles",
"Show bounding rectangles for systems",
0,
Icons::Invalid_ICON,
Qt::ApplicationShortcut
},
{
MsWidget::MAIN_WINDOW,
STATE_ALL,
Expand Down
1 change: 1 addition & 0 deletions mscore/workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ void Workspace::initWorkspace()
break;
}
}
Q_ASSERT(!Workspace::workspaces().empty());
if (currentWorkspace == 0)
currentWorkspace = Workspace::workspaces().at(0);
}
Expand Down

0 comments on commit 89ac0a6

Please sign in to comment.