Cutelee 6.1.0
Cutelee::MarkupDirector Class Reference

Instructs a builder object to create markup output. More...

#include <cutelee/markupdirector.h>

Public Member Functions

 MarkupDirector (AbstractMarkupBuilder *builder)
virtual ~MarkupDirector ()
virtual QTextFrame::iterator processBlock (QTextFrame::iterator it, const QTextBlock &block)
virtual QTextFrame::iterator processBlockContents (QTextFrame::iterator it, const QTextBlock &block)
virtual std::pair< QTextFrame::iterator, QTextBlockprocessBlockGroup (QTextFrame::iterator it, const QTextBlock &block, QTextBlockGroup *textBlockGroup)
virtual QTextBlock::iterator processCharTextObject (QTextBlock::iterator it, const QTextFragment &fragment, QTextObject *textObject)
virtual void processCustomFragment (const QTextFragment &fragment, QTextDocument const *doc)
virtual void processDocument (QTextDocument *doc)
virtual QTextBlock::iterator processFragment (QTextBlock::iterator it, const QTextFragment &fragment, QTextDocument const *doc)
virtual QTextFrame::iterator processFrame (QTextFrame::iterator it, QTextFrame *frame)
virtual QTextBlock::iterator processImage (QTextBlock::iterator it, const QTextImageFormat &imageFormat, QTextDocument *doc)
virtual std::pair< QTextFrame::iterator, QTextBlockprocessList (QTextFrame::iterator it, const QTextBlock &block, QTextList *textList)
virtual QTextFrame::iterator processObject (QTextFrame::iterator it, const QTextBlock &block, QTextObject *textObject)
virtual QTextFrame::iterator processTable (QTextFrame::iterator it, QTextTable *table)
virtual void processTableCell (const QTextTableCell &tableCell, QTextTable *table)

Protected Types

enum  OpenElementValues {
  None , SuperScript , SubScript , Anchor ,
  SpanForeground , SpanBackground , SpanFontFamily , SpanFontPointSize ,
  Strong , Emph , Underline , StrikeOut
}

Protected Member Functions

virtual QSet< int > getElementsToClose (QTextBlock::iterator it) const
virtual QList< int > getElementsToOpen (QTextBlock::iterator it)
virtual void processClosingElements (QTextBlock::iterator it)
void processDocumentContents (QTextFrame::iterator begin, QTextFrame::iterator end)
virtual void processOpeningElements (QTextBlock::iterator it)
std::pair< QTextFrame::iterator, QTextBlockskipBlockGroup (QTextFrame::iterator it, const QTextBlock &_block, QTextBlockGroup *blockGroup)
QList< int > sortOpeningOrder (QSet< int > openingTags, QTextBlock::iterator it) const

Protected Attributes

AbstractMarkupBuilderm_builder

Detailed Description

The MarkupDirector is used with an implementation of AbstractMarkupBuilder to create a marked up document output.

Usage can be quite simple.

auto doc = editor->document(); // editor is a QTextEdit
auto builder = new HTMLBuilder();
auto md = new MarkupDirector(builder);
md->processDocument(doc);
browser->setHtml(builder->getResult()); // browser is a QTextBrowser.
MarkupDirector(AbstractMarkupBuilder *builder)

Or with a different builder:

auto builder = new PlainTextMarkupBuilder();
auto md = new MarkupDirector(builder);
md->processDocument(doc);
browser->setPlainText(builder->getResult());
Creates a simple marked up plain text document.

The MarkupDirector also provides API for processing just part of a QTextDocument, such as a QTextFrame or a QTextBlock. The appropriate method may then be called with an invalid iterator as appropriate.

// ... Do some processing to get a QTextFrame.
auto frame = getFrame();
auto builder = new PlainTextMarkupBuilder();
auto md = new MarkupDirector(builder);
// Create output from only the frame.
md->processFrame(QTextFrame::iterator(), frame);
browser->setPlainText(builder->getResult());

The behaviour of the MarkupDirector can be customized by subclassing. Support for custom types can also be added by implementing the processCustomFragment method.

See also
Handling custom QTextObjects
Author
Stephen Kelly steve.nosp@m.ire@.nosp@m.gmail.nosp@m..com

Definition at line 92 of file markupdirector.h.

Member Enumeration Documentation

◆ OpenElementValues

Flags for the tags that may be open.

Enumerator
SuperScript 

No tags are open.

SubScript 

A superscript tag is open.

Anchor 

A subscript tag is open.

SpanForeground 

An anchor tag is open.

SpanBackground 

A foreground altering span tag is open.

SpanFontFamily 

A background altering span tag is open.

SpanFontPointSize 

A font family altering span tag is open.

Strong 

A font size altering span tag is open.

Emph 

A strong tag is open.

Underline 

A emphasis tag is open.

StrikeOut 

An underline tag is open.

Definition at line 273 of file markupdirector.h.

Constructor & Destructor Documentation

◆ MarkupDirector()

MarkupDirector::MarkupDirector ( AbstractMarkupBuilder * builder)

Constructor

Definition at line 42 of file markupdirector.cpp.

References m_builder.

Referenced by getElementsToClose(), getElementsToOpen(), processClosingElements(), and processOpeningElements().

◆ ~MarkupDirector()

MarkupDirector::~MarkupDirector ( )
virtual

Destructor

Definition at line 47 of file markupdirector.cpp.

Member Function Documentation

◆ getElementsToClose()

◆ getElementsToOpen()

◆ processBlock()

QTextFrame::iterator MarkupDirector::processBlock ( QTextFrame::iterator it,
const QTextBlock & block )
virtual

Directs the builder to create output for the single block. If calling this method directly, an invalid QTextFrame::iterator may be used.

This method does not process the contents of the block, but uses the processBlockContents method to do so.

Definition at line 80 of file markupdirector.cpp.

References QTextFrame::iterator::atEnd(), QTextBlock::blockFormat(), QTextBlock::document(), QTextBlock::isValid(), QTextDocument::objectForFormat(), processBlockContents(), and processObject().

Referenced by processDocumentContents().

◆ processBlockContents()

QTextFrame::iterator MarkupDirector::processBlockContents ( QTextFrame::iterator it,
const QTextBlock & block )
virtual

Directs the builder to create output for the contents of the single block. If calling this method directly, an invalid QTextFrame::iterator may be used.

Definition at line 224 of file markupdirector.cpp.

References QTextBlockFormat::alignment(), QTextFrame::iterator::atEnd(), QTextBlock::begin(), QTextBlock::blockFormat(), QTextFormat::BlockTrailingHorizontalRulerWidth, QTextBlock::document(), m_builder, processFragment(), and QTextBlock::textList().

Referenced by processBlock(), and processList().

◆ processBlockGroup()

std::pair< QTextFrame::iterator, QTextBlock > MarkupDirector::processBlockGroup ( QTextFrame::iterator it,
const QTextBlock & block,
QTextBlockGroup * textBlockGroup )
virtual

Directs the builder to create output for the single textBlockGroup. If calling this method directly, an invalid QTextFrame::iterator may be used.

The block block is the first block in the textBlockGroup.

Definition at line 442 of file markupdirector.cpp.

References processList(), and skipBlockGroup().

Referenced by processList(), and processObject().

◆ processCharTextObject()

QTextBlock::iterator MarkupDirector::processCharTextObject ( QTextBlock::iterator it,
const QTextFragment & fragment,
QTextObject * textObject )
virtual

Directs the builder to create output for the contents of the single textObject. The textObject is represented in the QTextDocument with the QTextFragment fragment.

If calling this method directly, an invalid QTextBlock::iterator may be used.

Definition at line 459 of file markupdirector.cpp.

References QTextBlock::iterator::atEnd(), QTextFragment::charFormat(), QTextObject::document(), and processImage().

Referenced by processFragment().

◆ processClosingElements()

void MarkupDirector::processClosingElements ( QTextBlock::iterator it)
protectedvirtual

Directs the builder to close the appropriate tags at the position of it.

Definition at line 487 of file markupdirector.cpp.

References MarkupDirector(), Anchor, Emph, getElementsToClose(), m_builder, SpanBackground, SpanFontFamily, SpanFontPointSize, SpanForeground, StrikeOut, Strong, SubScript, SuperScript, and Underline.

Referenced by processFragment().

◆ processCustomFragment()

void MarkupDirector::processCustomFragment ( const QTextFragment & fragment,
QTextDocument const * doc )
virtual

Hook for instructing the builder to create output for the fragemnt with a custom type. doc is the document the fragment is in.

Definition at line 380 of file markupdirector.cpp.

Referenced by processFragment().

◆ processDocument()

void MarkupDirector::processDocument ( QTextDocument * doc)
virtual

Constructs the output by directing the builder to create the markup.

Definition at line 453 of file markupdirector.cpp.

References processFrame(), and QTextDocument::rootFrame().

◆ processDocumentContents()

void MarkupDirector::processDocumentContents ( QTextFrame::iterator begin,
QTextFrame::iterator end )
protected

◆ processFragment()

QTextBlock::iterator MarkupDirector::processFragment ( QTextBlock::iterator it,
const QTextFragment & fragment,
QTextDocument const * doc )
virtual

Directs the builder to create output for the contents of the single fragment. If calling this method directly, an invalid QTextBlock::iterator may be used. doc is the document the fragment is in.

Definition at line 282 of file markupdirector.cpp.

References QString::at(), QTextBlock::iterator::atEnd(), QList::begin(), QChar::category(), QTextFragment::charFormat(), QChar::LineSeparator, m_builder, QTextDocument::objectForFormat(), processCharTextObject(), processClosingElements(), processCustomFragment(), processOpeningElements(), QChar::Separator_Line, QString::split(), QTextFragment::text(), and QTextFormat::UserObject.

Referenced by processBlockContents().

◆ processFrame()

QTextFrame::iterator MarkupDirector::processFrame ( QTextFrame::iterator it,
QTextFrame * frame )
virtual

Directs the builder to create output for the single frame. If calling this method directly, an invalid QTextFrame::iterator may be used.

Definition at line 69 of file markupdirector.cpp.

References QTextFrame::iterator::atEnd(), QTextFrame::begin(), QTextFrame::end(), and processDocumentContents().

Referenced by processDocument(), and processDocumentContents().

◆ processImage()

QTextBlock::iterator MarkupDirector::processImage ( QTextBlock::iterator it,
const QTextImageFormat & imageFormat,
QTextDocument * doc )
virtual

Directs the builder to create output for the image represented by the imageFormat.

If calling this method directly, an invalid QTextBlock::iterator may be used. doc is the document the fragment is in.

Definition at line 474 of file markupdirector.cpp.

References QTextBlock::iterator::atEnd(), QTextImageFormat::height(), m_builder, QTextImageFormat::name(), and QTextImageFormat::width().

Referenced by processCharTextObject().

◆ processList()

std::pair< QTextFrame::iterator, QTextBlock > MarkupDirector::processList ( QTextFrame::iterator it,
const QTextBlock & block,
QTextList * textList )
virtual

Directs the builder to create output for the single textList. If calling this method directly, an invalid QTextFrame::iterator may be used.

The block block is the first block in the textList.

Definition at line 195 of file markupdirector.cpp.

References QTextFrame::iterator::atEnd(), QTextList::format(), m_builder, processBlockContents(), processBlockGroup(), and QTextListFormat::style().

Referenced by processBlockGroup().

◆ processObject()

QTextFrame::iterator MarkupDirector::processObject ( QTextFrame::iterator it,
const QTextBlock & block,
QTextObject * textObject )
virtual

Directs the builder to create output for the single textObject. If calling this method directly, an invalid QTextFrame::iterator may be used.

The block block is the container of the textObject.

Definition at line 387 of file markupdirector.cpp.

References QTextFrame::iterator::atEnd(), and processBlockGroup().

Referenced by processBlock().

◆ processOpeningElements()

void MarkupDirector::processOpeningElements ( QTextBlock::iterator it)
protectedvirtual

◆ processTable()

◆ processTableCell()

void MarkupDirector::processTableCell ( const QTextTableCell & tableCell,
QTextTable * table )
virtual

Directs the builder to create output for the contents of the single tableCell. The tableCell is in the table.

Definition at line 187 of file markupdirector.cpp.

References QTextTableCell::begin(), QTextTableCell::end(), and processDocumentContents().

Referenced by processTable().

◆ skipBlockGroup()

std::pair< QTextFrame::iterator, QTextBlock > MarkupDirector::skipBlockGroup ( QTextFrame::iterator it,
const QTextBlock & _block,
QTextBlockGroup * blockGroup )
protected

Iterates the iterator it to the first block after blockGroup. _block is any block in the blockGroup.

The return pair is the iterator pointing after the end of blockGroup and the first block after blockGroup.

Definition at line 401 of file markupdirector.cpp.

References QTextFrame::iterator::atEnd().

Referenced by processBlockGroup().

◆ sortOpeningOrder()

QList< int > MarkupDirector::sortOpeningOrder ( QSet< int > openingTags,
QTextBlock::iterator it ) const
protected

Returns a list of tags contained in openingTags sorted so they can be opened in order and will be closed in the correct order.

openingTags should be a set of tags opened at the fragment pointed to by it.

Definition at line 873 of file markupdirector.cpp.

References QTextBlock::iterator::atEnd(), getElementsToClose(), QList::prepend(), QSet::remove(), and QSet::size().

Referenced by getElementsToOpen().

Member Data Documentation

◆ m_builder

AbstractMarkupBuilder* Cutelee::MarkupDirector::m_builder
protected

The builder this MarkupDirector is operating on. This is available when subclassing to customize behaviour.

Definition at line 297 of file markupdirector.h.

Referenced by MarkupDirector(), processBlockContents(), processClosingElements(), processFragment(), processImage(), processList(), processOpeningElements(), and processTable().