Fix undoing then reapplying range selections and restoring clefs/key sigs/time sigs in effect after applying them to range
Resolves: #17718
updateSelectedElements should go to the previous segment from end of selection if the previous measure is not a MM rest. It seems this check was broken since commit f369233, which is now fixed. This caused the restored key sig etc to be shifted one bar after when undoing and reapplying it.
Now the behavior when applying an element for the first time and the behavior when undoing and reapplying is the same.
https://github.com/user-attachments/assets/7f0439b9-cb26-4875-b1c6-46a1c78acc38
Resolves: #23936
Instead of restoring the clef/key sig/time sig that was in effect in the beginning of the selection, it restores what is in effect at the end of the selection. This also means if there's a clef/key sig/time sig at the end of the selection, it will not be overwritten.
https://github.com/user-attachments/assets/7d09a8e4-60b7-4c86-8cbc-135390e12937
For the last scenario (applying a key signature to a range which already contains a key signature), it feels weird to add a duplicate key signature at the end. The behavior I would expect is to remove all key signatures within the selection because I want the whole selection to be in the key signature I apply. Implementing this would need to take into account if the key signature is local or if it's added in part scores etc, so maybe for simplicity we should just not add the duplicate key signature, or keep the current behavior.
- [x] I signed the CLA
- [x] The title of the PR describes the problem it addresses
- [x] Each commit's message describes its purpose and effects, and references the issue it resolves
- [x] If changes are extensive, there is a sequence of easily reviewable commits
- [x] The code in the PR follows the coding rules
- [x] There are no unnecessary changes
- [x] The code compiles and runs on my machine, preferably after each commit individually
- [ ] I created a unit test or vtest to verify the changes I made (if applicable)