lundi 23 mars 2015

Should I build undo stack in model or its wrapper?


I am building an application (Python/PyQt). The first-order item will be a tree view/model with many helper functions to add new items, move them around in the tree, etc.. Then I will have a main window object that will basically serve as a wrapper for the tree, with a toolbar with buttons for the user to add/remove/move items (these actions will call the helper methods built into the tree object).


It's just about done, but alas I have decided to add undo/redo functionality using Qt's undo framework. It is my first time using undo/redo functionality, and I am curious whether there is consensus/experience on whether to implement the undo stack in the main window wrapper, or push the details into the internals of the tree class. I can see going either way.


Note added: I realize this is actually largely a question about API design, it may be an inessential detail that the question is about the undo framework...


Initially I was thinking in the wrapper, so I could keep my tree smaller and compact, and I was thinking that undo/redo is largely a sort of simple wrapper itself, not part of the main tree functionality.


As I started, though, I realized I seemed to be breaking modularity, and because of the way these undo classes are implemented (they get into the internals of the tree model), it was starting to feel like I was making the wrapper concern itself with too many details of how the tree is implemented (which sort of defeats the purpose of a simple wrapper).


So now I'm leaning toward incorporating the undo stack, in particular QUndoCommand classes, into the tree model, which is happy to deal with all these internals. This would likely simplify the wrapper, which is sort of the point. In other words, I'm thinking that I was naive about how complicated it was to add undo/redo functionality. It isn't simple, but something the model itself really should be working on.


That's where I am, but I am completely new to this, so am wondering if there is some hard-to-find-on-Google wisdom folks might have, or if I am making a silly design error I'm making that will haunt me on this project in two years.





Aucun commentaire:

Enregistrer un commentaire