Cutelee
6.1.0
|
It is possible using the QTextObjectInterface API to construct QTextDocument instances containing custom text objects. If processing a QTextDocument containing custom objects through the MarkupDirector and AbstractMarkupBuilder classes, it is necessary to implement support for the custom types in Cutelee.
In this example, we create a builder with a new method addAudioTag
. As it may be desired to implement multiple builders with support for the addAudioTag
method, it makes sense to put that call into an interface.
We create a small interface for supporting the addition of audio markup. The interface should inherit from the AbstractMarkupBuilder interface, so that functionality from that interface is also available.
Customized builders inheriting from an existing builder and the new interface can then be created.
The implementation of each concrete builder is appropriate to the type of markup being created.
The final part to be implemented is support for the custom type in a MarkupDirector. The MarkupDirector::processCustomFragment method is called for any QTextFragments with custom QTextFormat types. This method can be implemented to instruct the builder to create appropriate output for the type.
We create a subclass of Cutelee::MarkupDirector to take our new AbstractAudioBuilder
interface and implement the processCustomFragment
method to handle QTextFragments with our custom format type. The name of the audio file referenced in the document is then extracted and used as a parameter in the addAudioTag
method.
The custom AudioTextDocumentDirector and builders can then be used to create output including markup for custom types.
In a simple case, this could create output such as:
<p>Here is a paragraph with an inline audio element: <audio src="http://www.example.com/mysound.ogg" />. <p>And another paragraph.
or if the AudioPlainTextBuilder was used instead:
Here is a paragraph with an inline audio element: [1]. And another paragraph. ---- [1] http://www.example.com/mysound.ogg
The textedit example in the Cutelee source distribution implements this feature.