How to implement some custom UNDO code?

Hello! I’ve had some community feedback that it would be nice to have an undo feature for my digital sequencer module:

My module uses custom code for the mouse interactions and display of the sequences. The most common use-case would be that a user modifies the sequence, but decides to undo those modifications. Is it possible for me to implement such a feature, and if so, is there some sample code or documentation that could point me in the right direction?

Thanks!
Bret

Yeah, undo is not too difficult.

There is a global stack of things to undo, you create a subclass of the history class and implement its undo and redo methods.

When you complete a custom action, create an instance of the subclass, give it what information it needs, and put it on the stack.

Off the top of my head, I can’t remember the names of any of this, search for ‘history’

The one important thing to note is that you can’t store a reference to your module or widget in the subclass. If I do something in your module and then delete the module… I can undo twice, and you need to undo the custom action, but in a completely new instance of the module that was created when I undid the delete. Store the module Id and then find the module by id instead.

In v1 every think every action had a custom subclass, now in v2 I think Andrew has used a class with lambda callbacks in many places. Personally I always use the callback method

1 Like