Origin: upstream, commit:1c15bb0e49405508fe9803b2e371f42f3a61a1bb
Author: MarcSabatella <marc@outsideshore.com>
Description: fix #298273: divider not displayed in some cases
 Resolves: https://musescore.org/en/node/298273
 .
 System dividers were not being displayed in certain cases:
 if a fixed spacer is used, or in single page view.
 In addition, dividers were displaying that shouldn't be
 if layout changes and a system that was formerly not last on page
 suddenly becomes last on page,
 This is due to a series of errors in layoutPage()
 where the dividers are managed.
 This fix involves a number of aspects:
 1) checkDivider now takes an extra boolean parameter to force deletion
 2) we always call checkDivider with that parameter set to true
 for the last system of a page
 3) in the case where we don't stretch system distance
 (the clause checking sList, noVerticalStretch, or System layout mode),
 don't just remove dividers, but do the normal checkDivider call,
 which adds or removes dividers as appropriate
 4) in the calls to checkDivider at the end of the function
 (which handle the normal case of non-final systems on the page),
 dion't skip the checkDivider calls if a system hasFixedDownDistance.
 I believe that check was added because it is appropriate in other places
 that also check vBox, so it may have looked like this code should match.
 But it shouldn't, there is no reason to skip dividers in this case.
 Only the stretch calculations should be skipped.

--- a/libmscore/layout.cpp
+++ b/libmscore/layout.cpp
@@ -1471,10 +1471,10 @@ void Score::connectTies(bool silent)
 //   checkDivider
 //---------------------------------------------------------
 
-static void checkDivider(bool left, System* s, qreal yOffset)
+static void checkDivider(bool left, System* s, qreal yOffset, bool remove = false)
       {
       SystemDivider* divider = left ? s->systemDividerLeft() : s->systemDividerRight();
-      if (s->score()->styleB(left ? Sid::dividerLeft : Sid::dividerRight)) {
+      if ((s->score()->styleB(left ? Sid::dividerLeft : Sid::dividerRight)) && !remove) {
             if (!divider) {
                   divider = new SystemDivider(s->score());
                   divider->setDividerType(left ? SystemDivider::Type::LEFT : SystemDivider::Type::RIGHT);
@@ -1534,23 +1534,25 @@ static void layoutPage(Page* page, qreal
             sList.push_back(s1);
             }
 
+      // last systenm needs no divider
+      System* lastSystem = page->systems().back();
+      checkDivider(true, lastSystem, 0.0, true);      // remove
+      checkDivider(false, lastSystem, 0.0, true);     // remove
+
       if (sList.empty() || MScore::noVerticalStretch || score->layoutMode() == LayoutMode::SYSTEM) {
             if (score->layoutMode() == LayoutMode::FLOAT) {
                   qreal y = restHeight * .5;
                   for (System* system : page->systems())
                         system->move(QPointF(0.0, y));
                   }
-            // remove system dividers
-            for (System* s : page->systems()) {
-                  SystemDivider* sd = s->systemDividerLeft();
-                  if (sd) {
-                        s->remove(sd);
-                        delete sd;
-                        }
-                  sd = s->systemDividerRight();
-                  if (sd) {
-                        s->remove(sd);
-                        delete sd;
+            // system dividers
+            for (int i = 0; i < gaps; ++i) {
+                  System* s1 = page->systems().at(i);
+                  System* s2 = page->systems().at(i+1);
+                  if (!(s1->vbox() || s2->vbox())) {
+                        qreal yOffset = s1->height() + (s1->distance()-s1->height()) * .5;
+                        checkDivider(true,  s1, yOffset);
+                        checkDivider(false, s1, yOffset);
                         }
                   }
             return;
@@ -1603,7 +1605,7 @@ static void layoutPage(Page* page, qreal
             s1->rypos() = y;
             y          += s1->distance();
 
-            if (!(s1->vbox() || s2->vbox() || s1->hasFixedDownDistance())) {
+            if (!(s1->vbox() || s2->vbox())) {
                   qreal yOffset = s1->height() + (s1->distance()-s1->height()) * .5;
                   checkDivider(true,  s1, yOffset);
                   checkDivider(false, s1, yOffset);
