cengels/skywriter

Work formatting into TextFormatter

cengels opened this issue · 1 comments

At the moment the theme's formatting information (including font style, size, and color) is directly applied to the underlying QTextDocument's text blocks. This is not the right approach. Only user-defined and undoable formatting data should be applied this way. General formatting data should be applied using QTextLayout's additionalFormats, which is also the way QSyntaxHighlighter applies its syntax highlighting, so it seems reasonable to use the TextFormatter (which inherits from QSyntaxHighlighter) for this purpose.

The biggest hurdle for this improvement is the fact that QSyntaxHighlighter (and the underlying QTextLayout) only support setting document-wide QTextCharFormats, not QTextBlockFormats. This means that this approach cannot be used for block formats (especially for headings and scene breaks) and a different approach must be used that nonetheless also does not add a new undo stack.

The other hurdle is that it is not possible to change the text inside slots for either the QTextDocument::contentsChange() or QTextDocument::contentsChanged() signals. Modifying the document in those blocks results in a corrupted undo/redo stack. May be related to the filed Qt bug 13936. Unfortunately that issue has been open since 2010, so it seems unlikely it will ever be fixed. There may be no other choice than to disable the undo/redo that QTextDocument offers and implement a custom undo manager instead. That would be a ton of work for a minor bugfix though. (EDIT: After more testing, it seems like the bug is not caused by the contentsChange() or contentsChanged() signal but is actually a general bug with QTextCursor::joinPreviousEditBlock() when used in combination with QTextCursor::setBlockFormat(), which in the joinPreviousEditBlock() call to essentially be ignored and for two undo states to be created).

This fixes the following bugs:

When switching themes, empty paragraphs will maintain their original theme

This is especially noticeable on themes with different fonts/font sizes. Non-empty paragraphs will properly switch their fonts and font sizes. Empty paragraphs will, however, maintain the theme used at the start of the application, even when the user types something.

After switching themes, using the undo command changes the font formatting back, but not the colors

This essentially results in a corrupted active theme.

No more formatting has been added to the TextFormatter as there wasn't actually more formatting to add. Block formats are not supported by QSyntaxHighlighter (or the underlying QTextLayout). However, the latest commit should now prevent char formats including the font information from being added to the QTextDocument explicitly, which now results in more consistent behaviour when switching themes.