~ubuntu-branches/ubuntu/precise/grantlee/precise

« back to all changes in this revision

Viewing changes to gui/markupdirector.h

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee template system.
 
3
 
 
4
  Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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/>.
 
18
 
 
19
*/
 
20
 
 
21
 
 
22
#ifndef GRANTLEE_MARKUPDIRECTOR_H
 
23
#define GRANTLEE_MARKUPDIRECTOR_H
 
24
 
 
25
#include <QtGui/QTextDocument>
 
26
#include <QtGui/QTextFrame>
 
27
 
 
28
#include "abstractmarkupbuilder.h"
 
29
#include "grantlee_gui_export.h"
 
30
 
 
31
class QTextTable;
 
32
class QTextTableCell;
 
33
class QTextList;
 
34
class QTextCharFormat;
 
35
 
 
36
namespace Grantlee
 
37
{
 
38
 
 
39
class MarkupDirectorPrivate;
 
40
 
 
41
/// @headerfile markupdirector.h grantlee/markupdirector.h
 
42
 
 
43
/**
 
44
  @brief The MarkupDirector class controls and instructs a builder object to create markup output.
 
45
 
 
46
  The MarkupDirector is used with a subclass of AbstractMarkupBuilder to create a marked up document output.
 
47
 
 
48
  Usage can be quite simple.
 
49
 
 
50
  @code
 
51
 
 
52
    QTextDocument *doc = editor->document(); // editor is a QTextEdit
 
53
 
 
54
    AbstractMarkupBuilder *builder = new HTMLBuilder();
 
55
    MarkupDirector *md = new MarkupDirector(builder);
 
56
    md->processDocument(doc);
 
57
    browser->setHtml(builder->getResult()); // browser is a QTextBrowser.
 
58
 
 
59
  @endcode
 
60
 
 
61
  Or with a different builder:
 
62
 
 
63
  @code
 
64
    AbstractMarkupBuilder *builder = new PlainTextMarkupBuilder();
 
65
    MarkupDirector *md = new MarkupDirector(builder);
 
66
    md->processDocument(doc);
 
67
    browser->setPlainText(builder->getResult());
 
68
  @endcode
 
69
 
 
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.
 
71
 
 
72
  @code
 
73
    // ... Do some processing to get a QTextFrame.
 
74
    QTextFrame *frame = getFrame();
 
75
 
 
76
    AbstractMarkupBuilder *builder = new PlainTextMarkupBuilder();
 
77
    MarkupDirector *md = new MarkupDirector(builder);
 
78
 
 
79
    // Create output from only the frame.
 
80
    md->processFrame(QTextFrame::iterator(), frame);
 
81
    browser->setPlainText(builder->getResult());
 
82
  @endcode
 
83
 
 
84
  The behaviour of the MarkupDirector can be customized by subclassing. Support for custom types can also be added by implementing the processCustomFragment method.
 
85
 
 
86
  @see @ref custom_qtextobject
 
87
 
 
88
  @author Stephen Kelly <steveire@gmail.com>
 
89
*/
 
90
class GRANTLEE_GUI_EXPORT MarkupDirector
 
91
{
 
92
public:
 
93
  /**
 
94
    Construct a new MarkupDirector
 
95
  */
 
96
  MarkupDirector( AbstractMarkupBuilder *builder );
 
97
 
 
98
  /**
 
99
    Destructor
 
100
  */
 
101
  virtual ~MarkupDirector();
 
102
 
 
103
  /**
 
104
    Constructs the output by directing the builder to create the markup.
 
105
  */
 
106
  virtual void processDocument( QTextDocument* doc );
 
107
 
 
108
  /**
 
109
    Directs the builder to create output for the single @p frame. If calling this method directly, an invalid QTextFrame::iterator may be used.
 
110
  */
 
111
  virtual QTextFrame::iterator processFrame( QTextFrame::iterator it, QTextFrame *frame );
 
112
 
 
113
  /**
 
114
    Directs the builder to create output for the single @p block. If calling this method directly, an invalid QTextFrame::iterator may be used.
 
115
 
 
116
    This method does not process the contents of the @p block, but uses the processBlockContents method to do so.
 
117
  */
 
118
  virtual QTextFrame::iterator processBlock( QTextFrame::iterator it, const QTextBlock &block );
 
119
 
 
120
  /**
 
121
    Directs the builder to create output for the single @p textObject. If calling this method directly, an invalid QTextFrame::iterator may be used.
 
122
 
 
123
    The block @p block is the container of the @p textObject.
 
124
  */
 
125
  virtual QTextFrame::iterator processObject( QTextFrame::iterator it, const QTextBlock &block, QTextObject *textObject );
 
126
 
 
127
  /**
 
128
    Directs the builder to create output for the single @p textBlockGroup. If calling this method directly, an invalid QTextFrame::iterator may be used.
 
129
 
 
130
    The block @p block is the first block in the @p textBlockGroup.
 
131
  */
 
132
  virtual QPair<QTextFrame::iterator, QTextBlock> processBlockGroup( QTextFrame::iterator it, const QTextBlock &block, QTextBlockGroup *textBlockGroup );
 
133
 
 
134
  /**
 
135
    Directs the builder to create output for the single @p textList. If calling this method directly, an invalid QTextFrame::iterator may be used.
 
136
 
 
137
    The block @p block is the first block in the @p textList.
 
138
  */
 
139
  virtual QPair<QTextFrame::iterator, QTextBlock> processList( QTextFrame::iterator it, const QTextBlock &block, QTextList *textList );
 
140
 
 
141
  /**
 
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.
 
143
  */
 
144
  virtual QTextFrame::iterator processBlockContents( QTextFrame::iterator it, const QTextBlock &block );
 
145
 
 
146
  /**
 
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.
 
148
  */
 
149
  virtual void processCustomFragment( const QTextFragment &fragment, QTextDocument const *doc );
 
150
 
 
151
  /**
 
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.
 
153
  */
 
154
  virtual QTextBlock::iterator processFragment( QTextBlock::iterator it, const QTextFragment &fragment, QTextDocument const *doc );
 
155
 
 
156
  /**
 
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.
 
158
 
 
159
    If calling this method directly, an invalid QTextBlock::iterator may be used.
 
160
  */
 
161
  virtual QTextBlock::iterator processCharTextObject( QTextBlock::iterator it, const QTextFragment &fragment, QTextObject *textObject );
 
162
 
 
163
  /**
 
164
    Directs the builder to create output for the image represented by the @p imageFormat.
 
165
 
 
166
    If calling this method directly, an invalid QTextBlock::iterator may be used. @p doc is the document the fragment is in.
 
167
  */
 
168
  virtual QTextBlock::iterator processImage( QTextBlock::iterator it, const QTextImageFormat &imageFormat, QTextDocument *doc );
 
169
 
 
170
  /**
 
171
    Directs the builder to create output for the contents of the single @p table.
 
172
 
 
173
    If calling this method directly, an invalid QTextFrame::iterator may be used.
 
174
  */
 
175
  virtual QTextFrame::iterator processTable( QTextFrame::iterator it, QTextTable *table );
 
176
 
 
177
  /**
 
178
    Directs the builder to create output for the contents of the single @p tableCell. The tableCell is in the @p table.
 
179
  */
 
180
  virtual void processTableCell( const QTextTableCell &tableCell, QTextTable *table );
 
181
 
 
182
protected:
 
183
  /**
 
184
    Processes the document between @p begin and @p end
 
185
  */
 
186
  void processDocumentContents( QTextFrame::iterator begin, QTextFrame::iterator end );
 
187
 
 
188
  /**
 
189
    Iterates the iterator @p it to the first block after @p blockGroup. @p _block is any block in the @p blockGroup.
 
190
 
 
191
    The return pair is the iterator pointing after the end of @p blockGroup and the first block after @p blockGroup.
 
192
  */
 
193
  QPair<QTextFrame::iterator, QTextBlock> skipBlockGroup( QTextFrame::iterator it, const QTextBlock &_block, QTextBlockGroup *blockGroup );
 
194
 
 
195
  /**
 
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.
 
197
 
 
198
    @p openingTags should be a set of tags opened at the fragment pointed to by @p it.
 
199
  */
 
200
  QList< int > sortOpeningOrder( QSet< int > openingTags, QTextBlock::iterator it ) const;
 
201
 
 
202
  /**
 
203
    Directs the builder to close the appropriate tags at the position of @p it.
 
204
  */
 
205
  virtual void processClosingElements( QTextBlock::iterator it );
 
206
 
 
207
  /**
 
208
    Directs the builder to open the appropriate tags at the position of @p it.
 
209
  */
 
210
  virtual void processOpeningElements( QTextBlock::iterator it );
 
211
 
 
212
  /**
 
213
    Returns the tags that should be closed at the position of @p it.
 
214
  */
 
215
  virtual QSet< int > getElementsToClose( QTextBlock::iterator it ) const;
 
216
 
 
217
  /**
 
218
    Returns the tags that should be opened at the position of @p it.
 
219
  */
 
220
  virtual QList< int > getElementsToOpen( QTextBlock::iterator it );
 
221
 
 
222
  /**
 
223
    Flags for the tags that may be open.
 
224
  */
 
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.
 
238
  };
 
239
 
 
240
protected:
 
241
#ifndef Q_QDOC
 
242
  MarkupDirectorPrivate * const d_ptr;
 
243
#endif
 
244
 
 
245
  /**
 
246
    The builder this MarkupDirector is operating on. This is available when subclassing to customize behaviour.
 
247
  */
 
248
  AbstractMarkupBuilder *m_builder;
 
249
 
 
250
#ifndef Q_QDOC
 
251
private:
 
252
  Q_DECLARE_PRIVATE(MarkupDirector)
 
253
#endif
 
254
};
 
255
 
 
256
}
 
257
 
 
258
#endif