This patch also fixes a race condition that could happen due to the focusOutEvent. When user changes the document via KJotsTreeView, there are several places which react to this change:
- Slot KJotsWidget::selectionChanged, which tries to save the "old" document
- Slot KJotsEdit::selectionChanged, which tries to setReadOnly based on first element of selection
- Slots KJotsWidget::rowsInserted/rowsRemoved, which change the KJotsEdit::document() to the new one
- KJotsEdit::focusOutEvent triggers, which also tries to save the "old" document.
The focusOutEvent could actually come AFTER the selection has changed, but BEFORE the setDocument call,
which leaves KJotsEdit in an invalid state: the document() corresponds to one note, while selection points to another.
Because of that, savePage actually overwrites "new" note with the contents of "old" one. I lost several notes because of it.
This is hard to trigger manually, but it's sufficient to do a random combination of following things:
- Select couple of notes from a book using mouse, so KJotsBrowser appears
- Select a single note from a book using mouse
- Quickly press Ctrl+PageUp / Ctrl+PageDown multiple times to switch between notes
Instead, I propose the following:
- Don't let KJotsEdit interact with selectionModel, instead just provide it with a single QModelIndex pointing to a document (so that its state is always consistent)
- This is done via KJotsEdit::setModelIndex method, which does all the work in a single place:
- Saves the old document if necessary
- Calls KJotsEdit::setDocument if necessary
- Also sets the QTextCursor (if it was stored)
- tries to setReadOnly based on NoteLockAttribute
This patch also fixes visual glitch:
- User tries to switch focus i.e. to KFontChooser to pick font he wants
- KJotsEdit catches focusOutEvent and does the autosave (savePage call)
- Autosave modifies the underlying data in the model via model()->setData()
- Selection model inside KJotsWidget catches dataChanged signal and calls renderSelection slot
- It switches focus back to KJotsEdit
so user has to press onto the KFontChooser second time (the same issue does not trigger again because autosave is skipped when document is not modified)
The fix is that KJotsEdit decides it should grab the focus only when the new document was opened explicitly.
Also, remove selectionModel from KJotsBrowser as it's not used there anyways.