fix #31576: get right clef & key when inserting measures

This PR is tested to work when inserting a measure in front of a clef change or key change, as well as at the beginning of a score.  At various times we've had some but not all of these cases working.  I have added comments to explain what the code is doing.