PDA

View Full Version : programmatic way to determine if in a macro



shevitz
2nd December 2010, 17:40
Howdy,

I have a custom model with undo/redo implemented via QUndoStack. Some of
my operations require macros with some complicated logic. Is there any way to
programmatically determine if you are currently inside an uncompleted macro?

thanks,
Danny

wysota
2nd December 2010, 18:00
If you only create macros by creating commands with parents then your commands can check their contructor's "parent" argument to see if they are part of a macro. You can even access the parent's list of children to see if there are more commands to follow.

shevitz
2nd December 2010, 18:18
Thanks for the response. This doesn't work so well for me though. Currently, my macros are defined in the view, since that is where the composite actions happen (like copy/delete for DnD "move"). The atomic commands happen in the model.
They don't know anything about the view and the macros living there, so it is hard to set command parents.

There is another problem, too. I need to check whether there is macro underway at some point in the view code. I need to decide whether to end the macro or not. I have no handle to a command to see whether it is part of a macro.
Parents would let me if a command is part of a macro, but not whether a macro is underway or completed.

thanks again,
Danny

wysota
2nd December 2010, 18:24
It's hard for me to imagine what is going on in your program. By the way, did you follow the QQ article on using undo/redo with the model-view architecture?

shevitz
2nd December 2010, 18:59
I have read the undo/redo article on model-view controller.

Here is what the app is trying to do. I am constructing a tree (QTreeView) via Drag and Drop off a toolbar. Items are symbolically dropped onto their desired location in the tree. As the drop happens, I open the editor
to allow the user to enter text into the new node. The dropping of the node and the editing of the text are the contents of the macro. The problem is that all the DnD machinery is over by the time the editor opens.
The macro opens with the node drop. The text is edited at human timescales, so at some programmatically infinite time in the future, the macro needs to stop (at the end of the text editing). The right time to end the macro is at the commitData
member function. The problem is that you can open the node editor anytime you want by double clicking and editing the text. So sometimes in the commit, you end the macro (after a drop), and sometimes
you don't (when choosing to edit). Hence my problem of trying to determine programmatically whether a macro (the drop/edit one) is underway or not.

I appreciate you help,
Danny

Added after 16 minutes:

BTW, I am currently solving the problem, but leaving a "cookie", a hidden data member in the tree, to show I am currently in a macro, but this strikes me as extremely inelegant, and dynamic inspection of the macro state would be a much better solution.

D

wysota
2nd December 2010, 20:52
I would do it differently. Make your two atomic commands (add and edit) mergable - if an edit of the same node comes after a drop simply merge the two commands by reimplementing QUndoCommand::mergeWith(). Then you won't need any macros.

shevitz
2nd December 2010, 22:20
thanks for the suggestion. I am trying to figure out whether or not it will work for me.

D