tallforasmurf / PPQT

A post-processing tool for PGDP written in Python, PyQt4, and Qt
GNU General Public License v3.0
4 stars 2 forks source link

More defensive programming important #144

Open tallforasmurf opened 11 years ago

tallforasmurf commented 11 years ago

In several bugs PPQT has hit some Python error, e.g. an index error, case in point #141. The app doesn't quit, the Python parts are executed out of the Qt framework. But sometimes the edit document is damaged and the editor acts unpredictably until the app is killed and restarted. There's a potential for lost or damaged user data. A likely cause of this is that the app has started a BeginEditBlock (single-undo sequence) on a text cursor before the crash. Control returns to the caller and the text cursor is garbage-collected, but no EndEditBlock was executed.

To prevent these problems, in V2 every operation that uses Begin/EndEditBlock (there are several, e.g. footnotes, page number insertion, etc) must be moved into a function that takes a prepared textCursor as its argument. The call must be in a try block, e.g.

work_tc = QTextCursor(the-document)
work_tc.beginEditBlock()
try:
    complex_edit_func(work_tc)
    work_tc.endEditBlock() # all good - leave undo to user
except:
    work_tc.endEditBlock() # complex_edit_func barfed
    the-document.undo() # undo however much it did
    pqMsgs.tell_the_user(sys.exc_info) # report the error

The point is to ensure (1) that a textCursor is not trashed before the undo macro is properly closed; (2) any partial results of a failed operation are undone so the user is not faced with partial changes; (3) a booboo is reported in some coherent fashion not just as a console dump and a funny-looking document.