2
This file is part of the Grantlee template system.
4
Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either version
9
2 of the Licence, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library. If not, see <http://www.gnu.org/licenses/>.
22
#ifndef GRANTLEE_MARKUPDIRECTOR_H
23
#define GRANTLEE_MARKUPDIRECTOR_H
25
#include <QtGui/QTextDocument>
26
#include <QtGui/QTextFrame>
28
#include "abstractmarkupbuilder.h"
29
#include "grantlee_gui_export.h"
34
class QTextCharFormat;
39
class MarkupDirectorPrivate;
41
/// @headerfile markupdirector.h grantlee/markupdirector.h
44
@brief The MarkupDirector class controls and instructs a builder object to create markup output.
46
The MarkupDirector is used with a subclass of AbstractMarkupBuilder to create a marked up document output.
48
Usage can be quite simple.
52
QTextDocument *doc = editor->document(); // editor is a QTextEdit
54
AbstractMarkupBuilder *builder = new HTMLBuilder();
55
MarkupDirector *md = new MarkupDirector(builder);
56
md->processDocument(doc);
57
browser->setHtml(builder->getResult()); // browser is a QTextBrowser.
61
Or with a different builder:
64
AbstractMarkupBuilder *builder = new PlainTextMarkupBuilder();
65
MarkupDirector *md = new MarkupDirector(builder);
66
md->processDocument(doc);
67
browser->setPlainText(builder->getResult());
70
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.
73
// ... Do some processing to get a QTextFrame.
74
QTextFrame *frame = getFrame();
76
AbstractMarkupBuilder *builder = new PlainTextMarkupBuilder();
77
MarkupDirector *md = new MarkupDirector(builder);
79
// Create output from only the frame.
80
md->processFrame(QTextFrame::iterator(), frame);
81
browser->setPlainText(builder->getResult());
84
The behaviour of the MarkupDirector can be customized by subclassing. Support for custom types can also be added by implementing the processCustomFragment method.
86
@see @ref custom_qtextobject
88
@author Stephen Kelly <steveire@gmail.com>
90
class GRANTLEE_GUI_EXPORT MarkupDirector
94
Construct a new MarkupDirector
96
MarkupDirector( AbstractMarkupBuilder *builder );
101
virtual ~MarkupDirector();
104
Constructs the output by directing the builder to create the markup.
106
virtual void processDocument( QTextDocument* doc );
109
Directs the builder to create output for the single @p frame. If calling this method directly, an invalid QTextFrame::iterator may be used.
111
virtual QTextFrame::iterator processFrame( QTextFrame::iterator it, QTextFrame *frame );
114
Directs the builder to create output for the single @p block. If calling this method directly, an invalid QTextFrame::iterator may be used.
116
This method does not process the contents of the @p block, but uses the processBlockContents method to do so.
118
virtual QTextFrame::iterator processBlock( QTextFrame::iterator it, const QTextBlock &block );
121
Directs the builder to create output for the single @p textObject. If calling this method directly, an invalid QTextFrame::iterator may be used.
123
The block @p block is the container of the @p textObject.
125
virtual QTextFrame::iterator processObject( QTextFrame::iterator it, const QTextBlock &block, QTextObject *textObject );
128
Directs the builder to create output for the single @p textBlockGroup. If calling this method directly, an invalid QTextFrame::iterator may be used.
130
The block @p block is the first block in the @p textBlockGroup.
132
virtual QPair<QTextFrame::iterator, QTextBlock> processBlockGroup( QTextFrame::iterator it, const QTextBlock &block, QTextBlockGroup *textBlockGroup );
135
Directs the builder to create output for the single @p textList. If calling this method directly, an invalid QTextFrame::iterator may be used.
137
The block @p block is the first block in the @p textList.
139
virtual QPair<QTextFrame::iterator, QTextBlock> processList( QTextFrame::iterator it, const QTextBlock &block, QTextList *textList );
142
Directs the builder to create output for the contents of the single @p block. If calling this method directly, an invalid QTextFrame::iterator may be used.
144
virtual QTextFrame::iterator processBlockContents( QTextFrame::iterator it, const QTextBlock &block );
147
Hook for instructing the builder to create output for the @p fragemnt with a custom type. @p doc is the document the fragment is in.
149
virtual void processCustomFragment( const QTextFragment &fragment, QTextDocument const *doc );
152
Directs the builder to create output for the contents of the single @p fragment. If calling this method directly, an invalid QTextBlock::iterator may be used. @p doc is the document the fragment is in.
154
virtual QTextBlock::iterator processFragment( QTextBlock::iterator it, const QTextFragment &fragment, QTextDocument const *doc );
157
Directs the builder to create output for the contents of the single @p textObject. The @p textObject is represented in the QTextDocument with the QTextFragment @p fragment.
159
If calling this method directly, an invalid QTextBlock::iterator may be used.
161
virtual QTextBlock::iterator processCharTextObject( QTextBlock::iterator it, const QTextFragment &fragment, QTextObject *textObject );
164
Directs the builder to create output for the image represented by the @p imageFormat.
166
If calling this method directly, an invalid QTextBlock::iterator may be used. @p doc is the document the fragment is in.
168
virtual QTextBlock::iterator processImage( QTextBlock::iterator it, const QTextImageFormat &imageFormat, QTextDocument *doc );
171
Directs the builder to create output for the contents of the single @p table.
173
If calling this method directly, an invalid QTextFrame::iterator may be used.
175
virtual QTextFrame::iterator processTable( QTextFrame::iterator it, QTextTable *table );
178
Directs the builder to create output for the contents of the single @p tableCell. The tableCell is in the @p table.
180
virtual void processTableCell( const QTextTableCell &tableCell, QTextTable *table );
184
Processes the document between @p begin and @p end
186
void processDocumentContents( QTextFrame::iterator begin, QTextFrame::iterator end );
189
Iterates the iterator @p it to the first block after @p blockGroup. @p _block is any block in the @p blockGroup.
191
The return pair is the iterator pointing after the end of @p blockGroup and the first block after @p blockGroup.
193
QPair<QTextFrame::iterator, QTextBlock> skipBlockGroup( QTextFrame::iterator it, const QTextBlock &_block, QTextBlockGroup *blockGroup );
196
Returns a list of tags contained in @p openingTags sorted so they can be opened in order and will be closed in the correct order.
198
@p openingTags should be a set of tags opened at the fragment pointed to by @p it.
200
QList< int > sortOpeningOrder( QSet< int > openingTags, QTextBlock::iterator it ) const;
203
Directs the builder to close the appropriate tags at the position of @p it.
205
virtual void processClosingElements( QTextBlock::iterator it );
208
Directs the builder to open the appropriate tags at the position of @p it.
210
virtual void processOpeningElements( QTextBlock::iterator it );
213
Returns the tags that should be closed at the position of @p it.
215
virtual QSet< int > getElementsToClose( QTextBlock::iterator it ) const;
218
Returns the tags that should be opened at the position of @p it.
220
virtual QList< int > getElementsToOpen( QTextBlock::iterator it );
223
Flags for the tags that may be open.
225
enum OpenElementValues {
226
None = 0x0, /// No tags are open
227
SuperScript = 0x01, /// A superscript tag is open
228
SubScript = 0x02, /// A subscript tag is open
229
Anchor = 0x04, /// An anchor tag is open
230
SpanForeground = 0x08, /// A foreground altering span tag is open.
231
SpanBackground = 0x10, /// A background altering span tag is open.
232
SpanFontFamily = 0x20, /// A font family altering span tag is open.
233
SpanFontPointSize = 0x40, /// A font size altering span tag is open.
234
Strong = 0x80, /// A strong tag is open.
235
Emph = 0x100, /// A emphasis tag is open.
236
Underline = 0x200, /// An underline tag is open.
237
StrikeOut = 0x400 /// A strikeout tag is open.
242
MarkupDirectorPrivate * const d_ptr;
246
The builder this MarkupDirector is operating on. This is available when subclassing to customize behaviour.
248
AbstractMarkupBuilder *m_builder;
252
Q_DECLARE_PRIVATE(MarkupDirector)