lenmus / lomse

A C++ library for rendering, editing and playing back music scores.
MIT License
117 stars 28 forks source link

New strategy for undo/redo #372

Closed cecilios closed 2 years ago

cecilios commented 2 years ago

This PR changes the strategy for undo/redo when using edition commands. This change was needed:

In the old approach for undo/redo based on checkpoints, it was necessary to save the document state before applying a command. But in practice, this resulted in having to save the whole document (or part of it) in source format for every command.

The main problem of this approach was that the checkpoint format was LDP, but LDP doesn't support as many music notation elements as other supported formats, such as MusicXML, and thus, undo/redo didn't work for MusicXML. It also required a perfectly matched pair of LDP importer and exporter, that must be updated for every new supported feature, and this was a constant source of undo/redo bugs when more features are added to lomse.

A second problem was memory and processing time consumption: the checkpoint data (saving the whole document or parts of it in source format for each command) could consume a lot of memory for large documents. Also exporting and importing source format for every command requires excesive time for large documents.

This PR changes the strategy for undo/redo that now is based on saving an initial copy of the internal model and the issued edition commands. For restoring the state to any previous point, the internal model is restored from the copy (just replacing a pointer) and all commands until the desired one are re-played. Simple and perfect undo, very fast and with mínimal memory consumption. It also avoids undo/redo maintenace when developping more commands, and is valid for all source formats.

Changes

Other changes