Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MU4] Fixed various crashes during removing staves #9369

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/engraving/libmscore/edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4965,33 +4965,45 @@ void Score::undoInsertPart(Part* part, int idx)

void Score::undoRemoveStaff(Staff* staff)
{
const int idx = staff->idx();
Q_ASSERT(idx >= 0);
const int staffIndex = staff->idx();
Q_ASSERT(staffIndex >= 0);

std::vector<Spanner*> toRemove;
for (auto i = _spanner.cbegin(); i != _spanner.cend(); ++i) {
Spanner* s = i->second;
if (s->staffIdx() == idx && (idx != 0 || !s->systemFlag())) {
toRemove.push_back(s);
auto removingAllowed = [staffIndex, this](const Spanner* spanner) {
if (spanner->staffIdx() != staffIndex) {
return false;
}

return staffIndex != 0 || _staves.size() == 1 || !spanner->systemFlag();
};

std::vector<Spanner*> spannersToRemove;

for (auto it = _spanner.cbegin(); it != _spanner.cend(); ++it) {
Spanner* spanner = it->second;

if (removingAllowed(spanner)) {
spannersToRemove.push_back(spanner);
}
}
for (Spanner* s : _unmanagedSpanner) {
if (s->staffIdx() == idx && (idx != 0 || !s->systemFlag())) {
toRemove.push_back(s);

for (Spanner* spanner : _unmanagedSpanner) {
if (removingAllowed(spanner)) {
spannersToRemove.push_back(spanner);
}
}
for (Spanner* s : toRemove) {
s->undoUnlink();
undo(new RemoveElement(s));

for (Spanner* spanner : spannersToRemove) {
spanner->undoUnlink();
undo(new RemoveElement(spanner));
}

//
// adjust measures
//
for (Measure* m = staff->score()->firstMeasure(); m; m = m->nextMeasure()) {
m->cmdRemoveStaves(idx, idx + 1);
m->cmdRemoveStaves(staffIndex, staffIndex + 1);
if (m->hasMMRest()) {
m->mmRest()->cmdRemoveStaves(idx, idx + 1);
m->mmRest()->cmdRemoveStaves(staffIndex, staffIndex + 1);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/engraving/libmscore/engravingitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,11 @@ PointF EngravingItem::canvasPos() const
qFatal("this %s parent %s\n", name(), parent()->name());
}
if (measure) {
p.ry() += measure->staffLines(vStaffIdx())->y();
const StaffLines* lines = measure->staffLines(vStaffIdx());
p.ry() += lines ? lines->y() : 0;

system = measure->system();

if (system) {
Page* page = system->page();
if (page) {
Expand Down
19 changes: 14 additions & 5 deletions src/engraving/libmscore/measure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,14 @@ void Measure::cmdRemoveStaves(int sStaff, int eStaff)
{
int sTrack = sStaff * VOICES;
int eTrack = eStaff * VOICES;

auto removingAllowed = [this, sStaff, eStaff](const EngravingItem* item) {
int staffIndex = item->staffIdx();
int staffCount = score()->nstaves();

return (staffIndex >= sStaff) && (staffIndex < eStaff) && (!item->systemFlag() || staffCount == 1);
};

for (Segment* s = first(); s; s = s->next()) {
for (int track = eTrack - 1; track >= sTrack; --track) {
EngravingItem* el = s->element(track);
Expand All @@ -1257,20 +1265,21 @@ void Measure::cmdRemoveStaves(int sStaff, int eStaff)
score()->undo(new RemoveElement(el));
}
}
foreach (EngravingItem* e, s->annotations()) {
int staffIdx = e->staffIdx();
if ((staffIdx >= sStaff) && (staffIdx < eStaff) && !e->systemFlag()) {

for (EngravingItem* e : s->annotations()) {
if (removingAllowed(e)) {
e->undoUnlink();
score()->undo(new RemoveElement(e));
}
}
}

for (EngravingItem* e : el()) {
if (e->track() == -1) {
continue;
}
int staffIdx = e->staffIdx();
if (staffIdx >= sStaff && (staffIdx < eStaff) && !e->systemFlag()) {

if (removingAllowed(e)) {
e->undoUnlink();
score()->undo(new RemoveElement(e));
}
Expand Down
3 changes: 2 additions & 1 deletion src/engraving/libmscore/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1294,9 +1294,10 @@ void System::scanElements(void* data, void (* func)(void*, EngravingItem*), bool

qreal System::staffYpage(int staffIdx) const
{
IF_ASSERT_FAILED(!(_staves.size() <= staffIdx || staffIdx < 0)) {
if (staffIdx < 0 || staffIdx >= _staves.size()) {
return pagePos().y();
}

return _staves[staffIdx]->y() + y();
}

Expand Down