author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qtextdocument.h" |
|
43 |
#include <qtextformat.h> |
|
44 |
#include "qtextdocumentlayout_p.h" |
|
45 |
#include "qtextdocumentfragment.h" |
|
46 |
#include "qtextdocumentfragment_p.h" |
|
47 |
#include "qtexttable.h" |
|
48 |
#include "qtextlist.h" |
|
49 |
#include <qdebug.h> |
|
50 |
#include <qregexp.h> |
|
51 |
#include <qvarlengtharray.h> |
|
52 |
#include <qtextcodec.h> |
|
53 |
#include <qthread.h> |
|
54 |
#include "qtexthtmlparser_p.h" |
|
55 |
#include "qpainter.h" |
|
56 |
#include "qprinter.h" |
|
57 |
#include "qtextedit.h" |
|
58 |
#include <qfile.h> |
|
59 |
#include <qfileinfo.h> |
|
60 |
#include <qdir.h> |
|
61 |
#include <qapplication.h> |
|
62 |
#include "qtextcontrol_p.h" |
|
63 |
#include "private/qtextedit_p.h" |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
64 |
#include "private/qdataurl_p.h" |
0 | 65 |
|
66 |
#include "qtextdocument_p.h" |
|
67 |
#include <private/qprinter_p.h> |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
68 |
#include <private/qabstracttextdocumentlayout_p.h> |
0 | 69 |
|
70 |
#include <limits.h> |
|
71 |
||
72 |
QT_BEGIN_NAMESPACE |
|
73 |
||
74 |
Q_CORE_EXPORT unsigned int qt_int_sqrt(unsigned int n); |
|
75 |
||
76 |
/*! |
|
77 |
Returns true if the string \a text is likely to be rich text; |
|
78 |
otherwise returns false. |
|
79 |
||
80 |
This function uses a fast and therefore simple heuristic. It |
|
81 |
mainly checks whether there is something that looks like a tag |
|
82 |
before the first line break. Although the result may be correct |
|
83 |
for common cases, there is no guarantee. |
|
84 |
||
85 |
This function is defined in the \c <QTextDocument> header file. |
|
86 |
*/ |
|
87 |
bool Qt::mightBeRichText(const QString& text) |
|
88 |
{ |
|
89 |
if (text.isEmpty()) |
|
90 |
return false; |
|
91 |
int start = 0; |
|
92 |
||
93 |
while (start < text.length() && text.at(start).isSpace()) |
|
94 |
++start; |
|
95 |
||
96 |
// skip a leading <?xml ... ?> as for example with xhtml |
|
97 |
if (text.mid(start, 5) == QLatin1String("<?xml")) { |
|
98 |
while (start < text.length()) { |
|
99 |
if (text.at(start) == QLatin1Char('?') |
|
100 |
&& start + 2 < text.length() |
|
101 |
&& text.at(start + 1) == QLatin1Char('>')) { |
|
102 |
start += 2; |
|
103 |
break; |
|
104 |
} |
|
105 |
++start; |
|
106 |
} |
|
107 |
||
108 |
while (start < text.length() && text.at(start).isSpace()) |
|
109 |
++start; |
|
110 |
} |
|
111 |
||
112 |
if (text.mid(start, 5).toLower() == QLatin1String("<!doc")) |
|
113 |
return true; |
|
114 |
int open = start; |
|
115 |
while (open < text.length() && text.at(open) != QLatin1Char('<') |
|
116 |
&& text.at(open) != QLatin1Char('\n')) { |
|
117 |
if (text.at(open) == QLatin1Char('&') && text.mid(open+1,3) == QLatin1String("lt;")) |
|
118 |
return true; // support desperate attempt of user to see <...> |
|
119 |
++open; |
|
120 |
} |
|
121 |
if (open < text.length() && text.at(open) == QLatin1Char('<')) { |
|
122 |
const int close = text.indexOf(QLatin1Char('>'), open); |
|
123 |
if (close > -1) { |
|
124 |
QString tag; |
|
125 |
for (int i = open+1; i < close; ++i) { |
|
126 |
if (text[i].isDigit() || text[i].isLetter()) |
|
127 |
tag += text[i]; |
|
128 |
else if (!tag.isEmpty() && text[i].isSpace()) |
|
129 |
break; |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
130 |
else if (!tag.isEmpty() && text[i] == QLatin1Char('/') && i + 1 == close) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
131 |
break; |
0 | 132 |
else if (!text[i].isSpace() && (!tag.isEmpty() || text[i] != QLatin1Char('!'))) |
133 |
return false; // that's not a tag |
|
134 |
} |
|
135 |
#ifndef QT_NO_TEXTHTMLPARSER |
|
136 |
return QTextHtmlParser::lookupElement(tag.toLower()) != -1; |
|
137 |
#else |
|
138 |
return false; |
|
139 |
#endif // QT_NO_TEXTHTMLPARSER |
|
140 |
} |
|
141 |
} |
|
142 |
return false; |
|
143 |
} |
|
144 |
||
145 |
/*! |
|
146 |
Converts the plain text string \a plain to a HTML string with |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
147 |
HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML |
0 | 148 |
entities. |
149 |
||
150 |
Example: |
|
151 |
||
152 |
\snippet doc/src/snippets/code/src_gui_text_qtextdocument.cpp 0 |
|
153 |
||
154 |
This function is defined in the \c <QTextDocument> header file. |
|
155 |
||
156 |
\sa convertFromPlainText(), mightBeRichText() |
|
157 |
*/ |
|
158 |
QString Qt::escape(const QString& plain) |
|
159 |
{ |
|
160 |
QString rich; |
|
161 |
rich.reserve(int(plain.length() * 1.1)); |
|
162 |
for (int i = 0; i < plain.length(); ++i) { |
|
163 |
if (plain.at(i) == QLatin1Char('<')) |
|
164 |
rich += QLatin1String("<"); |
|
165 |
else if (plain.at(i) == QLatin1Char('>')) |
|
166 |
rich += QLatin1String(">"); |
|
167 |
else if (plain.at(i) == QLatin1Char('&')) |
|
168 |
rich += QLatin1String("&"); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
else if (plain.at(i) == QLatin1Char('"')) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
rich += QLatin1String("""); |
0 | 171 |
else |
172 |
rich += plain.at(i); |
|
173 |
} |
|
174 |
return rich; |
|
175 |
} |
|
176 |
||
177 |
/*! |
|
178 |
\fn QString Qt::convertFromPlainText(const QString &plain, WhiteSpaceMode mode) |
|
179 |
||
180 |
Converts the plain text string \a plain to an HTML-formatted |
|
181 |
paragraph while preserving most of its look. |
|
182 |
||
183 |
\a mode defines how whitespace is handled. |
|
184 |
||
185 |
This function is defined in the \c <QTextDocument> header file. |
|
186 |
||
187 |
\sa escape(), mightBeRichText() |
|
188 |
*/ |
|
189 |
QString Qt::convertFromPlainText(const QString &plain, Qt::WhiteSpaceMode mode) |
|
190 |
{ |
|
191 |
int col = 0; |
|
192 |
QString rich; |
|
193 |
rich += QLatin1String("<p>"); |
|
194 |
for (int i = 0; i < plain.length(); ++i) { |
|
195 |
if (plain[i] == QLatin1Char('\n')){ |
|
196 |
int c = 1; |
|
197 |
while (i+1 < plain.length() && plain[i+1] == QLatin1Char('\n')) { |
|
198 |
i++; |
|
199 |
c++; |
|
200 |
} |
|
201 |
if (c == 1) |
|
202 |
rich += QLatin1String("<br>\n"); |
|
203 |
else { |
|
204 |
rich += QLatin1String("</p>\n"); |
|
205 |
while (--c > 1) |
|
206 |
rich += QLatin1String("<br>\n"); |
|
207 |
rich += QLatin1String("<p>"); |
|
208 |
} |
|
209 |
col = 0; |
|
210 |
} else { |
|
211 |
if (mode == Qt::WhiteSpacePre && plain[i] == QLatin1Char('\t')){ |
|
212 |
rich += QChar(0x00a0U); |
|
213 |
++col; |
|
214 |
while (col % 8) { |
|
215 |
rich += QChar(0x00a0U); |
|
216 |
++col; |
|
217 |
} |
|
218 |
} |
|
219 |
else if (mode == Qt::WhiteSpacePre && plain[i].isSpace()) |
|
220 |
rich += QChar(0x00a0U); |
|
221 |
else if (plain[i] == QLatin1Char('<')) |
|
222 |
rich += QLatin1String("<"); |
|
223 |
else if (plain[i] == QLatin1Char('>')) |
|
224 |
rich += QLatin1String(">"); |
|
225 |
else if (plain[i] == QLatin1Char('&')) |
|
226 |
rich += QLatin1String("&"); |
|
227 |
else |
|
228 |
rich += plain[i]; |
|
229 |
++col; |
|
230 |
} |
|
231 |
} |
|
232 |
if (col != 0) |
|
233 |
rich += QLatin1String("</p>"); |
|
234 |
return rich; |
|
235 |
} |
|
236 |
||
237 |
#ifndef QT_NO_TEXTCODEC |
|
238 |
/*! |
|
239 |
\internal |
|
240 |
||
241 |
This function is defined in the \c <QTextDocument> header file. |
|
242 |
*/ |
|
243 |
QTextCodec *Qt::codecForHtml(const QByteArray &ba) |
|
244 |
{ |
|
245 |
return QTextCodec::codecForHtml(ba); |
|
246 |
} |
|
247 |
#endif |
|
248 |
||
249 |
/*! |
|
250 |
\class QTextDocument |
|
251 |
\reentrant |
|
252 |
||
253 |
\brief The QTextDocument class holds formatted text that can be |
|
254 |
viewed and edited using a QTextEdit. |
|
255 |
||
256 |
\ingroup richtext-processing |
|
257 |
||
258 |
||
259 |
QTextDocument is a container for structured rich text documents, providing |
|
260 |
support for styled text and various types of document elements, such as |
|
261 |
lists, tables, frames, and images. |
|
262 |
They can be created for use in a QTextEdit, or used independently. |
|
263 |
||
264 |
Each document element is described by an associated format object. Each |
|
265 |
format object is treated as a unique object by QTextDocuments, and can be |
|
266 |
passed to objectForFormat() to obtain the document element that it is |
|
267 |
applied to. |
|
268 |
||
269 |
A QTextDocument can be edited programmatically using a QTextCursor, and |
|
270 |
its contents can be examined by traversing the document structure. The |
|
271 |
entire document structure is stored as a hierarchy of document elements |
|
272 |
beneath the root frame, found with the rootFrame() function. Alternatively, |
|
273 |
if you just want to iterate over the textual contents of the document you |
|
274 |
can use begin(), end(), and findBlock() to retrieve text blocks that you |
|
275 |
can examine and iterate over. |
|
276 |
||
277 |
The layout of a document is determined by the documentLayout(); |
|
278 |
you can create your own QAbstractTextDocumentLayout subclass and |
|
279 |
set it using setDocumentLayout() if you want to use your own |
|
280 |
layout logic. The document's title and other meta-information can be |
|
281 |
obtained by calling the metaInformation() function. For documents that |
|
282 |
are exposed to users through the QTextEdit class, the document title |
|
283 |
is also available via the QTextEdit::documentTitle() function. |
|
284 |
||
285 |
The toPlainText() and toHtml() convenience functions allow you to retrieve the |
|
286 |
contents of the document as plain text and HTML. |
|
287 |
The document's text can be searched using the find() functions. |
|
288 |
||
289 |
Undo/redo of operations performed on the document can be controlled using |
|
290 |
the setUndoRedoEnabled() function. The undo/redo system can be controlled |
|
291 |
by an editor widget through the undo() and redo() slots; the document also |
|
292 |
provides contentsChanged(), undoAvailable(), and redoAvailable() signals |
|
293 |
that inform connected editor widgets about the state of the undo/redo |
|
294 |
system. |
|
295 |
||
296 |
\sa QTextCursor, QTextEdit, \link richtext.html Rich Text Processing\endlink , {Text Object Example} |
|
297 |
*/ |
|
298 |
||
299 |
/*! |
|
300 |
\property QTextDocument::defaultFont |
|
301 |
\brief the default font used to display the document's text |
|
302 |
*/ |
|
303 |
||
304 |
/*! |
|
305 |
\property QTextDocument::defaultTextOption |
|
306 |
\brief the default text option will be set on all \l{QTextLayout}s in the document. |
|
307 |
||
308 |
When \l{QTextBlock}s are created, the defaultTextOption is set on their |
|
309 |
QTextLayout. This allows setting global properties for the document such as the |
|
310 |
default word wrap mode. |
|
311 |
*/ |
|
312 |
||
313 |
/*! |
|
314 |
Constructs an empty QTextDocument with the given \a parent. |
|
315 |
*/ |
|
316 |
QTextDocument::QTextDocument(QObject *parent) |
|
317 |
: QObject(*new QTextDocumentPrivate, parent) |
|
318 |
{ |
|
319 |
Q_D(QTextDocument); |
|
320 |
d->init(); |
|
321 |
} |
|
322 |
||
323 |
/*! |
|
324 |
Constructs a QTextDocument containing the plain (unformatted) \a text |
|
325 |
specified, and with the given \a parent. |
|
326 |
*/ |
|
327 |
QTextDocument::QTextDocument(const QString &text, QObject *parent) |
|
328 |
: QObject(*new QTextDocumentPrivate, parent) |
|
329 |
{ |
|
330 |
Q_D(QTextDocument); |
|
331 |
d->init(); |
|
332 |
QTextCursor(this).insertText(text); |
|
333 |
} |
|
334 |
||
335 |
/*! |
|
336 |
\internal |
|
337 |
*/ |
|
338 |
QTextDocument::QTextDocument(QTextDocumentPrivate &dd, QObject *parent) |
|
339 |
: QObject(dd, parent) |
|
340 |
{ |
|
341 |
Q_D(QTextDocument); |
|
342 |
d->init(); |
|
343 |
} |
|
344 |
||
345 |
/*! |
|
346 |
Destroys the document. |
|
347 |
*/ |
|
348 |
QTextDocument::~QTextDocument() |
|
349 |
{ |
|
350 |
} |
|
351 |
||
352 |
||
353 |
/*! |
|
354 |
Creates a new QTextDocument that is a copy of this text document. \a |
|
355 |
parent is the parent of the returned text document. |
|
356 |
*/ |
|
357 |
QTextDocument *QTextDocument::clone(QObject *parent) const |
|
358 |
{ |
|
359 |
Q_D(const QTextDocument); |
|
360 |
QTextDocument *doc = new QTextDocument(parent); |
|
361 |
QTextCursor(doc).insertFragment(QTextDocumentFragment(this)); |
|
362 |
doc->rootFrame()->setFrameFormat(rootFrame()->frameFormat()); |
|
363 |
QTextDocumentPrivate *priv = doc->d_func(); |
|
364 |
priv->title = d->title; |
|
365 |
priv->url = d->url; |
|
366 |
priv->pageSize = d->pageSize; |
|
367 |
priv->indentWidth = d->indentWidth; |
|
368 |
priv->defaultTextOption = d->defaultTextOption; |
|
369 |
priv->setDefaultFont(d->defaultFont()); |
|
370 |
priv->resources = d->resources; |
|
371 |
priv->cachedResources.clear(); |
|
372 |
#ifndef QT_NO_CSSPARSER |
|
373 |
priv->defaultStyleSheet = d->defaultStyleSheet; |
|
374 |
priv->parsedDefaultStyleSheet = d->parsedDefaultStyleSheet; |
|
375 |
#endif |
|
376 |
return doc; |
|
377 |
} |
|
378 |
||
379 |
/*! |
|
380 |
Returns true if the document is empty; otherwise returns false. |
|
381 |
*/ |
|
382 |
bool QTextDocument::isEmpty() const |
|
383 |
{ |
|
384 |
Q_D(const QTextDocument); |
|
385 |
/* because if we're empty we still have one single paragraph as |
|
386 |
* one single fragment */ |
|
387 |
return d->length() <= 1; |
|
388 |
} |
|
389 |
||
390 |
/*! |
|
391 |
Clears the document. |
|
392 |
*/ |
|
393 |
void QTextDocument::clear() |
|
394 |
{ |
|
395 |
Q_D(QTextDocument); |
|
396 |
d->clear(); |
|
397 |
d->resources.clear(); |
|
398 |
} |
|
399 |
||
400 |
/*! |
|
401 |
\since 4.2 |
|
402 |
||
403 |
Undoes the last editing operation on the document if undo is |
|
404 |
available. The provided \a cursor is positioned at the end of the |
|
405 |
location where the edition operation was undone. |
|
406 |
||
407 |
See the \l {Overview of Qt's Undo Framework}{Qt Undo Framework} |
|
408 |
documentation for details. |
|
409 |
||
410 |
\sa undoAvailable(), isUndoRedoEnabled() |
|
411 |
*/ |
|
412 |
void QTextDocument::undo(QTextCursor *cursor) |
|
413 |
{ |
|
414 |
Q_D(QTextDocument); |
|
415 |
const int pos = d->undoRedo(true); |
|
416 |
if (cursor && pos >= 0) { |
|
417 |
*cursor = QTextCursor(this); |
|
418 |
cursor->setPosition(pos); |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
/*! |
|
423 |
\since 4.2 |
|
424 |
Redoes the last editing operation on the document if \link |
|
425 |
QTextDocument::isRedoAvailable() redo is available\endlink. |
|
426 |
||
427 |
The provided \a cursor is positioned at the end of the location where |
|
428 |
the edition operation was redone. |
|
429 |
*/ |
|
430 |
void QTextDocument::redo(QTextCursor *cursor) |
|
431 |
{ |
|
432 |
Q_D(QTextDocument); |
|
433 |
const int pos = d->undoRedo(false); |
|
434 |
if (cursor && pos >= 0) { |
|
435 |
*cursor = QTextCursor(this); |
|
436 |
cursor->setPosition(pos); |
|
437 |
} |
|
438 |
} |
|
439 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
440 |
/*! \enum QTextDocument::Stacks |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
441 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
442 |
\value UndoStack The undo stack. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
443 |
\value RedoStack The redo stack. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
444 |
\value UndoAndRedoStacks Both the undo and redo stacks. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
445 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
446 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
447 |
/*! |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
448 |
\since 4.7 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
449 |
Clears the stacks specified by \a stacksToClear. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
450 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
451 |
This method clears any commands on the undo stack, the redo stack, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
452 |
or both (the default). If commands are cleared, the appropriate |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
453 |
signals are emitted, QTextDocument::undoAvailable() or |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
454 |
QTextDocument::redoAvailable(). |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
455 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
456 |
\sa QTextDocument::undoAvailable() QTextDocument::redoAvailable() |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
457 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
458 |
void QTextDocument::clearUndoRedoStacks(Stacks stacksToClear) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
459 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
460 |
Q_D(QTextDocument); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
461 |
d->clearUndoRedoStacks(stacksToClear, true); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
462 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
463 |
|
0 | 464 |
/*! |
465 |
\overload |
|
466 |
||
467 |
*/ |
|
468 |
void QTextDocument::undo() |
|
469 |
{ |
|
470 |
Q_D(QTextDocument); |
|
471 |
d->undoRedo(true); |
|
472 |
} |
|
473 |
||
474 |
/*! |
|
475 |
\overload |
|
476 |
Redoes the last editing operation on the document if \link |
|
477 |
QTextDocument::isRedoAvailable() redo is available\endlink. |
|
478 |
*/ |
|
479 |
void QTextDocument::redo() |
|
480 |
{ |
|
481 |
Q_D(QTextDocument); |
|
482 |
d->undoRedo(false); |
|
483 |
} |
|
484 |
||
485 |
/*! |
|
486 |
\internal |
|
487 |
||
488 |
Appends a custom undo \a item to the undo stack. |
|
489 |
*/ |
|
490 |
void QTextDocument::appendUndoItem(QAbstractUndoItem *item) |
|
491 |
{ |
|
492 |
Q_D(QTextDocument); |
|
493 |
d->appendUndoItem(item); |
|
494 |
} |
|
495 |
||
496 |
/*! |
|
497 |
\property QTextDocument::undoRedoEnabled |
|
498 |
\brief whether undo/redo are enabled for this document |
|
499 |
||
500 |
This defaults to true. If disabled, the undo stack is cleared and |
|
501 |
no items will be added to it. |
|
502 |
*/ |
|
503 |
void QTextDocument::setUndoRedoEnabled(bool enable) |
|
504 |
{ |
|
505 |
Q_D(QTextDocument); |
|
506 |
d->enableUndoRedo(enable); |
|
507 |
} |
|
508 |
||
509 |
bool QTextDocument::isUndoRedoEnabled() const |
|
510 |
{ |
|
511 |
Q_D(const QTextDocument); |
|
512 |
return d->isUndoRedoEnabled(); |
|
513 |
} |
|
514 |
||
515 |
/*! |
|
516 |
\property QTextDocument::maximumBlockCount |
|
517 |
\since 4.2 |
|
518 |
\brief Specifies the limit for blocks in the document. |
|
519 |
||
520 |
Specifies the maximum number of blocks the document may have. If there are |
|
521 |
more blocks in the document that specified with this property blocks are removed |
|
522 |
from the beginning of the document. |
|
523 |
||
524 |
A negative or zero value specifies that the document may contain an unlimited |
|
525 |
amount of blocks. |
|
526 |
||
527 |
The default value is 0. |
|
528 |
||
529 |
Note that setting this property will apply the limit immediately to the document |
|
530 |
contents. |
|
531 |
||
532 |
Setting this property also disables the undo redo history. |
|
533 |
||
534 |
This property is undefined in documents with tables or frames. |
|
535 |
*/ |
|
536 |
int QTextDocument::maximumBlockCount() const |
|
537 |
{ |
|
538 |
Q_D(const QTextDocument); |
|
539 |
return d->maximumBlockCount; |
|
540 |
} |
|
541 |
||
542 |
void QTextDocument::setMaximumBlockCount(int maximum) |
|
543 |
{ |
|
544 |
Q_D(QTextDocument); |
|
545 |
d->maximumBlockCount = maximum; |
|
546 |
d->ensureMaximumBlockCount(); |
|
547 |
setUndoRedoEnabled(false); |
|
548 |
} |
|
549 |
||
550 |
/*! |
|
551 |
\since 4.3 |
|
552 |
||
553 |
The default text option is used on all QTextLayout objects in the document. |
|
554 |
This allows setting global properties for the document such as the default |
|
555 |
word wrap mode. |
|
556 |
*/ |
|
557 |
QTextOption QTextDocument::defaultTextOption() const |
|
558 |
{ |
|
559 |
Q_D(const QTextDocument); |
|
560 |
return d->defaultTextOption; |
|
561 |
} |
|
562 |
||
563 |
/*! |
|
564 |
\since 4.3 |
|
565 |
||
566 |
Sets the default text option. |
|
567 |
*/ |
|
568 |
void QTextDocument::setDefaultTextOption(const QTextOption &option) |
|
569 |
{ |
|
570 |
Q_D(QTextDocument); |
|
571 |
d->defaultTextOption = option; |
|
572 |
if (d->lout) |
|
573 |
d->lout->documentChanged(0, 0, d->length()); |
|
574 |
} |
|
575 |
||
576 |
/*! |
|
577 |
\fn void QTextDocument::markContentsDirty(int position, int length) |
|
578 |
||
579 |
Marks the contents specified by the given \a position and \a length |
|
580 |
as "dirty", informing the document that it needs to be laid out |
|
581 |
again. |
|
582 |
*/ |
|
583 |
void QTextDocument::markContentsDirty(int from, int length) |
|
584 |
{ |
|
585 |
Q_D(QTextDocument); |
|
586 |
d->documentChange(from, length); |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
587 |
if (!d->inContentsChange) { |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
588 |
d->lout->documentChanged(d->docChangeFrom, d->docChangeOldLength, d->docChangeLength); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
589 |
d->docChangeFrom = -1; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
590 |
} |
0 | 591 |
} |
592 |
||
593 |
/*! |
|
594 |
\property QTextDocument::useDesignMetrics |
|
595 |
\since 4.1 |
|
596 |
\brief whether the document uses design metrics of fonts to improve the accuracy of text layout |
|
597 |
||
598 |
If this property is set to true, the layout will use design metrics. |
|
599 |
Otherwise, the metrics of the paint device as set on |
|
600 |
QAbstractTextDocumentLayout::setPaintDevice() will be used. |
|
601 |
||
602 |
Using design metrics makes a layout have a width that is no longer dependent on hinting |
|
603 |
and pixel-rounding. This means that WYSIWYG text layout becomes possible because the width |
|
604 |
scales much more linearly based on paintdevice metrics than it would otherwise. |
|
605 |
||
606 |
By default, this property is false. |
|
607 |
*/ |
|
608 |
||
609 |
void QTextDocument::setUseDesignMetrics(bool b) |
|
610 |
{ |
|
611 |
Q_D(QTextDocument); |
|
612 |
if (b == d->defaultTextOption.useDesignMetrics()) |
|
613 |
return; |
|
614 |
d->defaultTextOption.setUseDesignMetrics(b); |
|
615 |
if (d->lout) |
|
616 |
d->lout->documentChanged(0, 0, d->length()); |
|
617 |
} |
|
618 |
||
619 |
bool QTextDocument::useDesignMetrics() const |
|
620 |
{ |
|
621 |
Q_D(const QTextDocument); |
|
622 |
return d->defaultTextOption.useDesignMetrics(); |
|
623 |
} |
|
624 |
||
625 |
/*! |
|
626 |
\since 4.2 |
|
627 |
||
628 |
Draws the content of the document with painter \a p, clipped to \a rect. |
|
629 |
If \a rect is a null rectangle (default) then the document is painted unclipped. |
|
630 |
*/ |
|
631 |
void QTextDocument::drawContents(QPainter *p, const QRectF &rect) |
|
632 |
{ |
|
633 |
p->save(); |
|
634 |
QAbstractTextDocumentLayout::PaintContext ctx; |
|
635 |
if (rect.isValid()) { |
|
636 |
p->setClipRect(rect); |
|
637 |
ctx.clip = rect; |
|
638 |
} |
|
639 |
documentLayout()->draw(p, ctx); |
|
640 |
p->restore(); |
|
641 |
} |
|
642 |
||
643 |
/*! |
|
644 |
\property QTextDocument::textWidth |
|
645 |
\since 4.2 |
|
646 |
||
647 |
The text width specifies the preferred width for text in the document. If |
|
648 |
the text (or content in general) is wider than the specified with it is broken |
|
649 |
into multiple lines and grows vertically. If the text cannot be broken into multiple |
|
650 |
lines to fit into the specified text width it will be larger and the size() and the |
|
651 |
idealWidth() property will reflect that. |
|
652 |
||
653 |
If the text width is set to -1 then the text will not be broken into multiple lines |
|
654 |
unless it is enforced through an explicit line break or a new paragraph. |
|
655 |
||
656 |
The default value is -1. |
|
657 |
||
658 |
Setting the text width will also set the page height to -1, causing the document to |
|
659 |
grow or shrink vertically in a continuous way. If you want the document layout to break |
|
660 |
the text into multiple pages then you have to set the pageSize property instead. |
|
661 |
||
662 |
\sa size(), idealWidth(), pageSize() |
|
663 |
*/ |
|
664 |
void QTextDocument::setTextWidth(qreal width) |
|
665 |
{ |
|
666 |
Q_D(QTextDocument); |
|
667 |
QSizeF sz = d->pageSize; |
|
668 |
sz.setWidth(width); |
|
669 |
sz.setHeight(-1); |
|
670 |
setPageSize(sz); |
|
671 |
} |
|
672 |
||
673 |
qreal QTextDocument::textWidth() const |
|
674 |
{ |
|
675 |
Q_D(const QTextDocument); |
|
676 |
return d->pageSize.width(); |
|
677 |
} |
|
678 |
||
679 |
/*! |
|
680 |
\since 4.2 |
|
681 |
||
682 |
Returns the ideal width of the text document. The ideal width is the actually used width |
|
683 |
of the document without optional alignments taken into account. It is always <= size().width(). |
|
684 |
||
685 |
\sa adjustSize(), textWidth |
|
686 |
*/ |
|
687 |
qreal QTextDocument::idealWidth() const |
|
688 |
{ |
|
689 |
if (QTextDocumentLayout *lout = qobject_cast<QTextDocumentLayout *>(documentLayout())) |
|
690 |
return lout->idealWidth(); |
|
691 |
return textWidth(); |
|
692 |
} |
|
693 |
||
694 |
/*! |
|
695 |
\property QTextDocument::documentMargin |
|
696 |
\since 4.5 |
|
697 |
||
698 |
The margin around the document. The default is 4. |
|
699 |
*/ |
|
700 |
qreal QTextDocument::documentMargin() const |
|
701 |
{ |
|
702 |
Q_D(const QTextDocument); |
|
703 |
return d->documentMargin; |
|
704 |
} |
|
705 |
||
706 |
void QTextDocument::setDocumentMargin(qreal margin) |
|
707 |
{ |
|
708 |
Q_D(QTextDocument); |
|
709 |
if (d->documentMargin != margin) { |
|
710 |
d->documentMargin = margin; |
|
711 |
||
712 |
QTextFrame* root = rootFrame(); |
|
713 |
QTextFrameFormat format = root->frameFormat(); |
|
714 |
format.setMargin(margin); |
|
715 |
root->setFrameFormat(format); |
|
716 |
||
717 |
if (d->lout) |
|
718 |
d->lout->documentChanged(0, 0, d->length()); |
|
719 |
} |
|
720 |
} |
|
721 |
||
722 |
||
723 |
/*! |
|
724 |
\property QTextDocument::indentWidth |
|
725 |
\since 4.4 |
|
726 |
||
727 |
Returns the width used for text list and text block indenting. |
|
728 |
||
729 |
The indent properties of QTextListFormat and QTextBlockFormat specify |
|
730 |
multiples of this value. The default indent width is 40. |
|
731 |
*/ |
|
732 |
qreal QTextDocument::indentWidth() const |
|
733 |
{ |
|
734 |
Q_D(const QTextDocument); |
|
735 |
return d->indentWidth; |
|
736 |
} |
|
737 |
||
738 |
||
739 |
/*! |
|
740 |
\since 4.4 |
|
741 |
||
742 |
Sets the \a width used for text list and text block indenting. |
|
743 |
||
744 |
The indent properties of QTextListFormat and QTextBlockFormat specify |
|
745 |
multiples of this value. The default indent width is 40 . |
|
746 |
||
747 |
\sa indentWidth() |
|
748 |
*/ |
|
749 |
void QTextDocument::setIndentWidth(qreal width) |
|
750 |
{ |
|
751 |
Q_D(QTextDocument); |
|
752 |
if (d->indentWidth != width) { |
|
753 |
d->indentWidth = width; |
|
754 |
if (d->lout) |
|
755 |
d->lout->documentChanged(0, 0, d->length()); |
|
756 |
} |
|
757 |
} |
|
758 |
||
759 |
||
760 |
||
761 |
||
762 |
/*! |
|
763 |
\since 4.2 |
|
764 |
||
765 |
Adjusts the document to a reasonable size. |
|
766 |
||
767 |
\sa idealWidth(), textWidth, size |
|
768 |
*/ |
|
769 |
void QTextDocument::adjustSize() |
|
770 |
{ |
|
771 |
// Pull this private function in from qglobal.cpp |
|
772 |
QFont f = defaultFont(); |
|
773 |
QFontMetrics fm(f); |
|
774 |
int mw = fm.width(QLatin1Char('x')) * 80; |
|
775 |
int w = mw; |
|
776 |
setTextWidth(w); |
|
777 |
QSizeF size = documentLayout()->documentSize(); |
|
778 |
if (size.width() != 0) { |
|
779 |
w = qt_int_sqrt((uint)(5 * size.height() * size.width() / 3)); |
|
780 |
setTextWidth(qMin(w, mw)); |
|
781 |
||
782 |
size = documentLayout()->documentSize(); |
|
783 |
if (w*3 < 5*size.height()) { |
|
784 |
w = qt_int_sqrt((uint)(2 * size.height() * size.width())); |
|
785 |
setTextWidth(qMin(w, mw)); |
|
786 |
} |
|
787 |
} |
|
788 |
setTextWidth(idealWidth()); |
|
789 |
} |
|
790 |
||
791 |
/*! |
|
792 |
\property QTextDocument::size |
|
793 |
\since 4.2 |
|
794 |
||
795 |
Returns the actual size of the document. |
|
796 |
This is equivalent to documentLayout()->documentSize(); |
|
797 |
||
798 |
The size of the document can be changed either by setting |
|
799 |
a text width or setting an entire page size. |
|
800 |
||
801 |
Note that the width is always >= pageSize().width(). |
|
802 |
||
803 |
By default, for a newly-created, empty document, this property contains |
|
804 |
a configuration-dependent size. |
|
805 |
||
806 |
\sa setTextWidth(), setPageSize(), idealWidth() |
|
807 |
*/ |
|
808 |
QSizeF QTextDocument::size() const |
|
809 |
{ |
|
810 |
return documentLayout()->documentSize(); |
|
811 |
} |
|
812 |
||
813 |
/*! |
|
814 |
\property QTextDocument::blockCount |
|
815 |
\since 4.2 |
|
816 |
||
817 |
Returns the number of text blocks in the document. |
|
818 |
||
819 |
The value of this property is undefined in documents with tables or frames. |
|
820 |
||
821 |
By default, if defined, this property contains a value of 1. |
|
822 |
\sa lineCount(), characterCount() |
|
823 |
*/ |
|
824 |
int QTextDocument::blockCount() const |
|
825 |
{ |
|
826 |
Q_D(const QTextDocument); |
|
827 |
return d->blockMap().numNodes(); |
|
828 |
} |
|
829 |
||
830 |
||
831 |
/*! |
|
832 |
\since 4.5 |
|
833 |
||
834 |
Returns the number of lines of this document (if the layout supports |
|
835 |
this). Otherwise, this is identical to the number of blocks. |
|
836 |
||
837 |
\sa blockCount(), characterCount() |
|
838 |
*/ |
|
839 |
int QTextDocument::lineCount() const |
|
840 |
{ |
|
841 |
Q_D(const QTextDocument); |
|
842 |
return d->blockMap().length(2); |
|
843 |
} |
|
844 |
||
845 |
/*! |
|
846 |
\since 4.5 |
|
847 |
||
848 |
Returns the number of characters of this document. |
|
849 |
||
850 |
\sa blockCount(), characterAt() |
|
851 |
*/ |
|
852 |
int QTextDocument::characterCount() const |
|
853 |
{ |
|
854 |
Q_D(const QTextDocument); |
|
855 |
return d->length(); |
|
856 |
} |
|
857 |
||
858 |
/*! |
|
859 |
\since 4.5 |
|
860 |
||
861 |
Returns the character at position \a pos, or a null character if the |
|
862 |
position is out of range. |
|
863 |
||
864 |
\sa characterCount() |
|
865 |
*/ |
|
866 |
QChar QTextDocument::characterAt(int pos) const |
|
867 |
{ |
|
868 |
Q_D(const QTextDocument); |
|
869 |
if (pos < 0 || pos >= d->length()) |
|
870 |
return QChar(); |
|
871 |
QTextDocumentPrivate::FragmentIterator fragIt = d->find(pos); |
|
872 |
const QTextFragmentData * const frag = fragIt.value(); |
|
873 |
const int offsetInFragment = qMax(0, pos - fragIt.position()); |
|
874 |
return d->text.at(frag->stringPosition + offsetInFragment); |
|
875 |
} |
|
876 |
||
877 |
||
878 |
/*! |
|
879 |
\property QTextDocument::defaultStyleSheet |
|
880 |
\since 4.2 |
|
881 |
||
882 |
The default style sheet is applied to all newly HTML formatted text that is |
|
883 |
inserted into the document, for example using setHtml() or QTextCursor::insertHtml(). |
|
884 |
||
885 |
The style sheet needs to be compliant to CSS 2.1 syntax. |
|
886 |
||
887 |
\bold{Note:} Changing the default style sheet does not have any effect to the existing content |
|
888 |
of the document. |
|
889 |
||
890 |
\sa {Supported HTML Subset} |
|
891 |
*/ |
|
892 |
||
893 |
#ifndef QT_NO_CSSPARSER |
|
894 |
void QTextDocument::setDefaultStyleSheet(const QString &sheet) |
|
895 |
{ |
|
896 |
Q_D(QTextDocument); |
|
897 |
d->defaultStyleSheet = sheet; |
|
898 |
QCss::Parser parser(sheet); |
|
899 |
d->parsedDefaultStyleSheet = QCss::StyleSheet(); |
|
900 |
d->parsedDefaultStyleSheet.origin = QCss::StyleSheetOrigin_UserAgent; |
|
901 |
parser.parse(&d->parsedDefaultStyleSheet); |
|
902 |
} |
|
903 |
||
904 |
QString QTextDocument::defaultStyleSheet() const |
|
905 |
{ |
|
906 |
Q_D(const QTextDocument); |
|
907 |
return d->defaultStyleSheet; |
|
908 |
} |
|
909 |
#endif // QT_NO_CSSPARSER |
|
910 |
||
911 |
/*! |
|
912 |
\fn void QTextDocument::contentsChanged() |
|
913 |
||
914 |
This signal is emitted whenever the document's content changes; for |
|
915 |
example, when text is inserted or deleted, or when formatting is applied. |
|
916 |
||
917 |
\sa contentsChange() |
|
918 |
*/ |
|
919 |
||
920 |
/*! |
|
921 |
\fn void QTextDocument::contentsChange(int position, int charsRemoved, int charsAdded) |
|
922 |
||
923 |
This signal is emitted whenever the document's content changes; for |
|
924 |
example, when text is inserted or deleted, or when formatting is applied. |
|
925 |
||
926 |
Information is provided about the \a position of the character in the |
|
927 |
document where the change occurred, the number of characters removed |
|
928 |
(\a charsRemoved), and the number of characters added (\a charsAdded). |
|
929 |
||
930 |
The signal is emitted before the document's layout manager is notified |
|
931 |
about the change. This hook allows you to implement syntax highlighting |
|
932 |
for the document. |
|
933 |
||
934 |
\sa QAbstractTextDocumentLayout::documentChanged(), contentsChanged() |
|
935 |
*/ |
|
936 |
||
937 |
||
938 |
/*! |
|
939 |
\fn QTextDocument::undoAvailable(bool available); |
|
940 |
||
941 |
This signal is emitted whenever undo operations become available |
|
942 |
(\a available is true) or unavailable (\a available is false). |
|
943 |
||
944 |
See the \l {Overview of Qt's Undo Framework}{Qt Undo Framework} |
|
945 |
documentation for details. |
|
946 |
||
947 |
\sa undo(), isUndoRedoEnabled() |
|
948 |
*/ |
|
949 |
||
950 |
/*! |
|
951 |
\fn QTextDocument::redoAvailable(bool available); |
|
952 |
||
953 |
This signal is emitted whenever redo operations become available |
|
954 |
(\a available is true) or unavailable (\a available is false). |
|
955 |
*/ |
|
956 |
||
957 |
/*! |
|
958 |
\fn QTextDocument::cursorPositionChanged(const QTextCursor &cursor); |
|
959 |
||
960 |
This signal is emitted whenever the position of a cursor changed |
|
961 |
due to an editing operation. The cursor that changed is passed in |
|
962 |
\a cursor. If you need a signal when the cursor is moved with the |
|
963 |
arrow keys you can use the \l{QTextEdit::}{cursorPositionChanged()} signal in |
|
964 |
QTextEdit. |
|
965 |
*/ |
|
966 |
||
967 |
/*! |
|
968 |
\fn QTextDocument::blockCountChanged(int newBlockCount); |
|
969 |
\since 4.3 |
|
970 |
||
971 |
This signal is emitted when the total number of text blocks in the |
|
972 |
document changes. The value passed in \a newBlockCount is the new |
|
973 |
total. |
|
974 |
*/ |
|
975 |
||
976 |
/*! |
|
977 |
\fn QTextDocument::documentLayoutChanged(); |
|
978 |
\since 4.4 |
|
979 |
||
980 |
This signal is emitted when a new document layout is set. |
|
981 |
||
982 |
\sa setDocumentLayout() |
|
983 |
||
984 |
*/ |
|
985 |
||
986 |
||
987 |
/*! |
|
988 |
Returns true if undo is available; otherwise returns false. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
989 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
990 |
\sa isRedoAvailable(), availableUndoSteps() |
0 | 991 |
*/ |
992 |
bool QTextDocument::isUndoAvailable() const |
|
993 |
{ |
|
994 |
Q_D(const QTextDocument); |
|
995 |
return d->isUndoAvailable(); |
|
996 |
} |
|
997 |
||
998 |
/*! |
|
999 |
Returns true if redo is available; otherwise returns false. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1000 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1001 |
\sa isUndoAvailable(), availableRedoSteps() |
0 | 1002 |
*/ |
1003 |
bool QTextDocument::isRedoAvailable() const |
|
1004 |
{ |
|
1005 |
Q_D(const QTextDocument); |
|
1006 |
return d->isRedoAvailable(); |
|
1007 |
} |
|
1008 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1009 |
/*! \since 4.6 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1010 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1011 |
Returns the number of available undo steps. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1012 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1013 |
\sa isUndoAvailable() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1014 |
*/ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1015 |
int QTextDocument::availableUndoSteps() const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1016 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1017 |
Q_D(const QTextDocument); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1018 |
return d->availableUndoSteps(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1019 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1020 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1021 |
/*! \since 4.6 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1022 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1023 |
Returns the number of available redo steps. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1024 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1025 |
\sa isRedoAvailable() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1026 |
*/ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1027 |
int QTextDocument::availableRedoSteps() const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1028 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1029 |
Q_D(const QTextDocument); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1030 |
return d->availableRedoSteps(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1031 |
} |
0 | 1032 |
|
1033 |
/*! \since 4.4 |
|
1034 |
||
1035 |
Returns the document's revision (if undo is enabled). |
|
1036 |
||
1037 |
The revision is guaranteed to increase when a document that is not |
|
1038 |
modified is edited. |
|
1039 |
||
1040 |
\sa QTextBlock::revision(), isModified() |
|
1041 |
*/ |
|
1042 |
int QTextDocument::revision() const |
|
1043 |
{ |
|
1044 |
Q_D(const QTextDocument); |
|
1045 |
return d->revision; |
|
1046 |
} |
|
1047 |
||
1048 |
||
1049 |
||
1050 |
/*! |
|
1051 |
Sets the document to use the given \a layout. The previous layout |
|
1052 |
is deleted. |
|
1053 |
||
1054 |
\sa documentLayoutChanged() |
|
1055 |
*/ |
|
1056 |
void QTextDocument::setDocumentLayout(QAbstractTextDocumentLayout *layout) |
|
1057 |
{ |
|
1058 |
Q_D(QTextDocument); |
|
1059 |
d->setLayout(layout); |
|
1060 |
} |
|
1061 |
||
1062 |
/*! |
|
1063 |
Returns the document layout for this document. |
|
1064 |
*/ |
|
1065 |
QAbstractTextDocumentLayout *QTextDocument::documentLayout() const |
|
1066 |
{ |
|
1067 |
Q_D(const QTextDocument); |
|
1068 |
if (!d->lout) { |
|
1069 |
QTextDocument *that = const_cast<QTextDocument *>(this); |
|
1070 |
that->d_func()->setLayout(new QTextDocumentLayout(that)); |
|
1071 |
} |
|
1072 |
return d->lout; |
|
1073 |
} |
|
1074 |
||
1075 |
||
1076 |
/*! |
|
1077 |
Returns meta information about the document of the type specified by |
|
1078 |
\a info. |
|
1079 |
||
1080 |
\sa setMetaInformation() |
|
1081 |
*/ |
|
1082 |
QString QTextDocument::metaInformation(MetaInformation info) const |
|
1083 |
{ |
|
1084 |
Q_D(const QTextDocument); |
|
1085 |
switch (info) { |
|
1086 |
case DocumentTitle: |
|
1087 |
return d->title; |
|
1088 |
case DocumentUrl: |
|
1089 |
return d->url; |
|
1090 |
} |
|
1091 |
return QString(); |
|
1092 |
} |
|
1093 |
||
1094 |
/*! |
|
1095 |
Sets the document's meta information of the type specified by \a info |
|
1096 |
to the given \a string. |
|
1097 |
||
1098 |
\sa metaInformation() |
|
1099 |
*/ |
|
1100 |
void QTextDocument::setMetaInformation(MetaInformation info, const QString &string) |
|
1101 |
{ |
|
1102 |
Q_D(QTextDocument); |
|
1103 |
switch (info) { |
|
1104 |
case DocumentTitle: |
|
1105 |
d->title = string; |
|
1106 |
break; |
|
1107 |
case DocumentUrl: |
|
1108 |
d->url = string; |
|
1109 |
break; |
|
1110 |
} |
|
1111 |
} |
|
1112 |
||
1113 |
/*! |
|
1114 |
Returns the plain text contained in the document. If you want |
|
1115 |
formatting information use a QTextCursor instead. |
|
1116 |
||
1117 |
\sa toHtml() |
|
1118 |
*/ |
|
1119 |
QString QTextDocument::toPlainText() const |
|
1120 |
{ |
|
1121 |
Q_D(const QTextDocument); |
|
1122 |
QString txt = d->plainText(); |
|
1123 |
||
1124 |
QChar *uc = txt.data(); |
|
1125 |
QChar *e = uc + txt.size(); |
|
1126 |
||
1127 |
for (; uc != e; ++uc) { |
|
1128 |
switch (uc->unicode()) { |
|
1129 |
case 0xfdd0: // QTextBeginningOfFrame |
|
1130 |
case 0xfdd1: // QTextEndOfFrame |
|
1131 |
case QChar::ParagraphSeparator: |
|
1132 |
case QChar::LineSeparator: |
|
1133 |
*uc = QLatin1Char('\n'); |
|
1134 |
break; |
|
1135 |
case QChar::Nbsp: |
|
1136 |
*uc = QLatin1Char(' '); |
|
1137 |
break; |
|
1138 |
default: |
|
1139 |
; |
|
1140 |
} |
|
1141 |
} |
|
1142 |
return txt; |
|
1143 |
} |
|
1144 |
||
1145 |
/*! |
|
1146 |
Replaces the entire contents of the document with the given plain |
|
1147 |
\a text. |
|
1148 |
||
1149 |
\sa setHtml() |
|
1150 |
*/ |
|
1151 |
void QTextDocument::setPlainText(const QString &text) |
|
1152 |
{ |
|
1153 |
Q_D(QTextDocument); |
|
1154 |
bool previousState = d->isUndoRedoEnabled(); |
|
1155 |
d->enableUndoRedo(false); |
|
1156 |
d->beginEditBlock(); |
|
1157 |
d->clear(); |
|
1158 |
QTextCursor(this).insertText(text); |
|
1159 |
d->endEditBlock(); |
|
1160 |
d->enableUndoRedo(previousState); |
|
1161 |
} |
|
1162 |
||
1163 |
/*! |
|
1164 |
Replaces the entire contents of the document with the given |
|
1165 |
HTML-formatted text in the \a html string. |
|
1166 |
||
1167 |
The HTML formatting is respected as much as possible; for example, |
|
1168 |
"<b>bold</b> text" will produce text where the first word has a font |
|
1169 |
weight that gives it a bold appearance: "\bold{bold} text". |
|
1170 |
||
1171 |
\note It is the responsibility of the caller to make sure that the |
|
1172 |
text is correctly decoded when a QString containing HTML is created |
|
1173 |
and passed to setHtml(). |
|
1174 |
||
1175 |
\sa setPlainText(), {Supported HTML Subset} |
|
1176 |
*/ |
|
1177 |
||
1178 |
#ifndef QT_NO_TEXTHTMLPARSER |
|
1179 |
||
1180 |
void QTextDocument::setHtml(const QString &html) |
|
1181 |
{ |
|
1182 |
Q_D(QTextDocument); |
|
1183 |
bool previousState = d->isUndoRedoEnabled(); |
|
1184 |
d->enableUndoRedo(false); |
|
1185 |
d->beginEditBlock(); |
|
1186 |
d->clear(); |
|
1187 |
QTextHtmlImporter(this, html, QTextHtmlImporter::ImportToDocument).import(); |
|
1188 |
d->endEditBlock(); |
|
1189 |
d->enableUndoRedo(previousState); |
|
1190 |
} |
|
1191 |
||
1192 |
#endif // QT_NO_TEXTHTMLPARSER |
|
1193 |
||
1194 |
/*! |
|
1195 |
\enum QTextDocument::FindFlag |
|
1196 |
||
1197 |
This enum describes the options available to QTextDocument's find function. The options |
|
1198 |
can be OR-ed together from the following list: |
|
1199 |
||
1200 |
\value FindBackward Search backwards instead of forwards. |
|
1201 |
\value FindCaseSensitively By default find works case insensitive. Specifying this option |
|
1202 |
changes the behaviour to a case sensitive find operation. |
|
1203 |
\value FindWholeWords Makes find match only complete words. |
|
1204 |
*/ |
|
1205 |
||
1206 |
/*! |
|
1207 |
\enum QTextDocument::MetaInformation |
|
1208 |
||
1209 |
This enum describes the different types of meta information that can be |
|
1210 |
added to a document. |
|
1211 |
||
1212 |
\value DocumentTitle The title of the document. |
|
1213 |
\value DocumentUrl The url of the document. The loadResource() function uses |
|
1214 |
this url as the base when loading relative resources. |
|
1215 |
||
1216 |
\sa metaInformation(), setMetaInformation() |
|
1217 |
*/ |
|
1218 |
||
1219 |
/*! |
|
1220 |
\fn QTextCursor QTextDocument::find(const QString &subString, int position, FindFlags options) const |
|
1221 |
||
1222 |
\overload |
|
1223 |
||
1224 |
Finds the next occurrence of the string, \a subString, in the document. |
|
1225 |
The search starts at the given \a position, and proceeds forwards |
|
1226 |
through the document unless specified otherwise in the search options. |
|
1227 |
The \a options control the type of search performed. |
|
1228 |
||
1229 |
Returns a cursor with the match selected if \a subString |
|
1230 |
was found; otherwise returns a null cursor. |
|
1231 |
||
1232 |
If the \a position is 0 (the default) the search begins from the beginning |
|
1233 |
of the document; otherwise it begins at the specified position. |
|
1234 |
*/ |
|
1235 |
QTextCursor QTextDocument::find(const QString &subString, int from, FindFlags options) const |
|
1236 |
{ |
|
1237 |
QRegExp expr(subString); |
|
1238 |
expr.setPatternSyntax(QRegExp::FixedString); |
|
1239 |
expr.setCaseSensitivity((options & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); |
|
1240 |
||
1241 |
return find(expr, from, options); |
|
1242 |
} |
|
1243 |
||
1244 |
/*! |
|
1245 |
\fn QTextCursor QTextDocument::find(const QString &subString, const QTextCursor &cursor, FindFlags options) const |
|
1246 |
||
1247 |
Finds the next occurrence of the string, \a subString, in the document. |
|
1248 |
The search starts at the position of the given \a cursor, and proceeds |
|
1249 |
forwards through the document unless specified otherwise in the search |
|
1250 |
options. The \a options control the type of search performed. |
|
1251 |
||
1252 |
Returns a cursor with the match selected if \a subString was found; otherwise |
|
1253 |
returns a null cursor. |
|
1254 |
||
1255 |
If the given \a cursor has a selection, the search begins after the |
|
1256 |
selection; otherwise it begins at the cursor's position. |
|
1257 |
||
1258 |
By default the search is case-sensitive, and can match text anywhere in the |
|
1259 |
document. |
|
1260 |
*/ |
|
1261 |
QTextCursor QTextDocument::find(const QString &subString, const QTextCursor &from, FindFlags options) const |
|
1262 |
{ |
|
1263 |
int pos = 0; |
|
1264 |
if (!from.isNull()) { |
|
1265 |
if (options & QTextDocument::FindBackward) |
|
1266 |
pos = from.selectionStart(); |
|
1267 |
else |
|
1268 |
pos = from.selectionEnd(); |
|
1269 |
} |
|
1270 |
QRegExp expr(subString); |
|
1271 |
expr.setPatternSyntax(QRegExp::FixedString); |
|
1272 |
expr.setCaseSensitivity((options & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); |
|
1273 |
||
1274 |
return find(expr, pos, options); |
|
1275 |
} |
|
1276 |
||
1277 |
||
1278 |
static bool findInBlock(const QTextBlock &block, const QRegExp &expression, int offset, |
|
1279 |
QTextDocument::FindFlags options, QTextCursor &cursor) |
|
1280 |
{ |
|
1281 |
const QRegExp expr(expression); |
|
1282 |
QString text = block.text(); |
|
1283 |
text.replace(QChar::Nbsp, QLatin1Char(' ')); |
|
1284 |
||
1285 |
int idx = -1; |
|
1286 |
while (offset >=0 && offset <= text.length()) { |
|
1287 |
idx = (options & QTextDocument::FindBackward) ? |
|
1288 |
expr.lastIndexIn(text, offset) : expr.indexIn(text, offset); |
|
1289 |
if (idx == -1) |
|
1290 |
return false; |
|
1291 |
||
1292 |
if (options & QTextDocument::FindWholeWords) { |
|
1293 |
const int start = idx; |
|
1294 |
const int end = start + expr.matchedLength(); |
|
1295 |
if ((start != 0 && text.at(start - 1).isLetterOrNumber()) |
|
1296 |
|| (end != text.length() && text.at(end).isLetterOrNumber())) { |
|
1297 |
//if this is not a whole word, continue the search in the string |
|
1298 |
offset = (options & QTextDocument::FindBackward) ? idx-1 : end+1; |
|
1299 |
idx = -1; |
|
1300 |
continue; |
|
1301 |
} |
|
1302 |
} |
|
1303 |
//we have a hit, return the cursor for that. |
|
1304 |
break; |
|
1305 |
} |
|
1306 |
if (idx == -1) |
|
1307 |
return false; |
|
1308 |
cursor = QTextCursor(block.docHandle(), block.position() + idx); |
|
1309 |
cursor.setPosition(cursor.position() + expr.matchedLength(), QTextCursor::KeepAnchor); |
|
1310 |
return true; |
|
1311 |
} |
|
1312 |
||
1313 |
/*! |
|
1314 |
\fn QTextCursor QTextDocument::find(const QRegExp & expr, int position, FindFlags options) const |
|
1315 |
||
1316 |
\overload |
|
1317 |
||
1318 |
Finds the next occurrence, matching the regular expression, \a expr, in the document. |
|
1319 |
The search starts at the given \a position, and proceeds forwards |
|
1320 |
through the document unless specified otherwise in the search options. |
|
1321 |
The \a options control the type of search performed. The FindCaseSensitively |
|
1322 |
option is ignored for this overload, use QRegExp::caseSensitivity instead. |
|
1323 |
||
1324 |
Returns a cursor with the match selected if a match was found; otherwise |
|
1325 |
returns a null cursor. |
|
1326 |
||
1327 |
If the \a position is 0 (the default) the search begins from the beginning |
|
1328 |
of the document; otherwise it begins at the specified position. |
|
1329 |
*/ |
|
1330 |
QTextCursor QTextDocument::find(const QRegExp & expr, int from, FindFlags options) const |
|
1331 |
{ |
|
1332 |
Q_D(const QTextDocument); |
|
1333 |
||
1334 |
if (expr.isEmpty()) |
|
1335 |
return QTextCursor(); |
|
1336 |
||
1337 |
int pos = from; |
|
1338 |
//the cursor is positioned between characters, so for a backward search |
|
1339 |
//do not include the character given in the position. |
|
1340 |
if (options & FindBackward) { |
|
1341 |
--pos ; |
|
1342 |
if(pos < 0) |
|
1343 |
return QTextCursor(); |
|
1344 |
} |
|
1345 |
||
1346 |
QTextCursor cursor; |
|
1347 |
QTextBlock block = d->blocksFind(pos); |
|
1348 |
||
1349 |
if (!(options & FindBackward)) { |
|
1350 |
int blockOffset = qMax(0, pos - block.position()); |
|
1351 |
while (block.isValid()) { |
|
1352 |
if (findInBlock(block, expr, blockOffset, options, cursor)) |
|
1353 |
return cursor; |
|
1354 |
blockOffset = 0; |
|
1355 |
block = block.next(); |
|
1356 |
} |
|
1357 |
} else { |
|
1358 |
int blockOffset = pos - block.position(); |
|
1359 |
while (block.isValid()) { |
|
1360 |
if (findInBlock(block, expr, blockOffset, options, cursor)) |
|
1361 |
return cursor; |
|
1362 |
block = block.previous(); |
|
1363 |
blockOffset = block.length() - 1; |
|
1364 |
} |
|
1365 |
} |
|
1366 |
||
1367 |
return QTextCursor(); |
|
1368 |
} |
|
1369 |
||
1370 |
/*! |
|
1371 |
\fn QTextCursor QTextDocument::find(const QRegExp &expr, const QTextCursor &cursor, FindFlags options) const |
|
1372 |
||
1373 |
Finds the next occurrence, matching the regular expression, \a expr, in the document. |
|
1374 |
The search starts at the position of the given \a cursor, and proceeds |
|
1375 |
forwards through the document unless specified otherwise in the search |
|
1376 |
options. The \a options control the type of search performed. The FindCaseSensitively |
|
1377 |
option is ignored for this overload, use QRegExp::caseSensitivity instead. |
|
1378 |
||
1379 |
Returns a cursor with the match selected if a match was found; otherwise |
|
1380 |
returns a null cursor. |
|
1381 |
||
1382 |
If the given \a cursor has a selection, the search begins after the |
|
1383 |
selection; otherwise it begins at the cursor's position. |
|
1384 |
||
1385 |
By default the search is case-sensitive, and can match text anywhere in the |
|
1386 |
document. |
|
1387 |
*/ |
|
1388 |
QTextCursor QTextDocument::find(const QRegExp &expr, const QTextCursor &from, FindFlags options) const |
|
1389 |
{ |
|
1390 |
int pos = 0; |
|
1391 |
if (!from.isNull()) { |
|
1392 |
if (options & QTextDocument::FindBackward) |
|
1393 |
pos = from.selectionStart(); |
|
1394 |
else |
|
1395 |
pos = from.selectionEnd(); |
|
1396 |
} |
|
1397 |
return find(expr, pos, options); |
|
1398 |
} |
|
1399 |
||
1400 |
||
1401 |
/*! |
|
1402 |
\fn QTextObject *QTextDocument::createObject(const QTextFormat &format) |
|
1403 |
||
1404 |
Creates and returns a new document object (a QTextObject), based |
|
1405 |
on the given \a format. |
|
1406 |
||
1407 |
QTextObjects will always get created through this method, so you |
|
1408 |
must reimplement it if you use custom text objects inside your document. |
|
1409 |
*/ |
|
1410 |
QTextObject *QTextDocument::createObject(const QTextFormat &f) |
|
1411 |
{ |
|
1412 |
QTextObject *obj = 0; |
|
1413 |
if (f.isListFormat()) |
|
1414 |
obj = new QTextList(this); |
|
1415 |
else if (f.isTableFormat()) |
|
1416 |
obj = new QTextTable(this); |
|
1417 |
else if (f.isFrameFormat()) |
|
1418 |
obj = new QTextFrame(this); |
|
1419 |
||
1420 |
return obj; |
|
1421 |
} |
|
1422 |
||
1423 |
/*! |
|
1424 |
\internal |
|
1425 |
||
1426 |
Returns the frame that contains the text cursor position \a pos. |
|
1427 |
*/ |
|
1428 |
QTextFrame *QTextDocument::frameAt(int pos) const |
|
1429 |
{ |
|
1430 |
Q_D(const QTextDocument); |
|
1431 |
return d->frameAt(pos); |
|
1432 |
} |
|
1433 |
||
1434 |
/*! |
|
1435 |
Returns the document's root frame. |
|
1436 |
*/ |
|
1437 |
QTextFrame *QTextDocument::rootFrame() const |
|
1438 |
{ |
|
1439 |
Q_D(const QTextDocument); |
|
1440 |
return d->rootFrame(); |
|
1441 |
} |
|
1442 |
||
1443 |
/*! |
|
1444 |
Returns the text object associated with the given \a objectIndex. |
|
1445 |
*/ |
|
1446 |
QTextObject *QTextDocument::object(int objectIndex) const |
|
1447 |
{ |
|
1448 |
Q_D(const QTextDocument); |
|
1449 |
return d->objectForIndex(objectIndex); |
|
1450 |
} |
|
1451 |
||
1452 |
/*! |
|
1453 |
Returns the text object associated with the format \a f. |
|
1454 |
*/ |
|
1455 |
QTextObject *QTextDocument::objectForFormat(const QTextFormat &f) const |
|
1456 |
{ |
|
1457 |
Q_D(const QTextDocument); |
|
1458 |
return d->objectForFormat(f); |
|
1459 |
} |
|
1460 |
||
1461 |
||
1462 |
/*! |
|
1463 |
Returns the text block that contains the \a{pos}-th character. |
|
1464 |
*/ |
|
1465 |
QTextBlock QTextDocument::findBlock(int pos) const |
|
1466 |
{ |
|
1467 |
Q_D(const QTextDocument); |
|
1468 |
return QTextBlock(docHandle(), d->blockMap().findNode(pos)); |
|
1469 |
} |
|
1470 |
||
1471 |
/*! |
|
1472 |
\since 4.4 |
|
1473 |
Returns the text block with the specified \a blockNumber. |
|
1474 |
||
1475 |
\sa QTextBlock::blockNumber() |
|
1476 |
*/ |
|
1477 |
QTextBlock QTextDocument::findBlockByNumber(int blockNumber) const |
|
1478 |
{ |
|
1479 |
Q_D(const QTextDocument); |
|
1480 |
return QTextBlock(docHandle(), d->blockMap().findNode(blockNumber, 1)); |
|
1481 |
} |
|
1482 |
||
1483 |
/*! |
|
1484 |
\since 4.5 |
|
1485 |
Returns the text block that contains the specified \a lineNumber. |
|
1486 |
||
1487 |
\sa QTextBlock::firstLineNumber() |
|
1488 |
*/ |
|
1489 |
QTextBlock QTextDocument::findBlockByLineNumber(int lineNumber) const |
|
1490 |
{ |
|
1491 |
Q_D(const QTextDocument); |
|
1492 |
return QTextBlock(docHandle(), d->blockMap().findNode(lineNumber, 2)); |
|
1493 |
} |
|
1494 |
||
1495 |
/*! |
|
1496 |
Returns the document's first text block. |
|
1497 |
||
1498 |
\sa firstBlock() |
|
1499 |
*/ |
|
1500 |
QTextBlock QTextDocument::begin() const |
|
1501 |
{ |
|
1502 |
Q_D(const QTextDocument); |
|
1503 |
return QTextBlock(docHandle(), d->blockMap().begin().n); |
|
1504 |
} |
|
1505 |
||
1506 |
/*! |
|
1507 |
This function returns a block to test for the end of the document |
|
1508 |
while iterating over it. |
|
1509 |
||
1510 |
\snippet doc/src/snippets/textdocumentendsnippet.cpp 0 |
|
1511 |
||
1512 |
The block returned is invalid and represents the block after the |
|
1513 |
last block in the document. You can use lastBlock() to retrieve the |
|
1514 |
last valid block of the document. |
|
1515 |
||
1516 |
\sa lastBlock() |
|
1517 |
*/ |
|
1518 |
QTextBlock QTextDocument::end() const |
|
1519 |
{ |
|
1520 |
return QTextBlock(docHandle(), 0); |
|
1521 |
} |
|
1522 |
||
1523 |
/*! |
|
1524 |
\since 4.4 |
|
1525 |
Returns the document's first text block. |
|
1526 |
*/ |
|
1527 |
QTextBlock QTextDocument::firstBlock() const |
|
1528 |
{ |
|
1529 |
Q_D(const QTextDocument); |
|
1530 |
return QTextBlock(docHandle(), d->blockMap().begin().n); |
|
1531 |
} |
|
1532 |
||
1533 |
/*! |
|
1534 |
\since 4.4 |
|
1535 |
Returns the document's last (valid) text block. |
|
1536 |
*/ |
|
1537 |
QTextBlock QTextDocument::lastBlock() const |
|
1538 |
{ |
|
1539 |
Q_D(const QTextDocument); |
|
1540 |
return QTextBlock(docHandle(), d->blockMap().last().n); |
|
1541 |
} |
|
1542 |
||
1543 |
/*! |
|
1544 |
\property QTextDocument::pageSize |
|
1545 |
\brief the page size that should be used for laying out the document |
|
1546 |
||
1547 |
By default, for a newly-created, empty document, this property contains |
|
1548 |
an undefined size. |
|
1549 |
||
1550 |
\sa modificationChanged() |
|
1551 |
*/ |
|
1552 |
||
1553 |
void QTextDocument::setPageSize(const QSizeF &size) |
|
1554 |
{ |
|
1555 |
Q_D(QTextDocument); |
|
1556 |
d->pageSize = size; |
|
1557 |
if (d->lout) |
|
1558 |
d->lout->documentChanged(0, 0, d->length()); |
|
1559 |
} |
|
1560 |
||
1561 |
QSizeF QTextDocument::pageSize() const |
|
1562 |
{ |
|
1563 |
Q_D(const QTextDocument); |
|
1564 |
return d->pageSize; |
|
1565 |
} |
|
1566 |
||
1567 |
/*! |
|
1568 |
returns the number of pages in this document. |
|
1569 |
*/ |
|
1570 |
int QTextDocument::pageCount() const |
|
1571 |
{ |
|
1572 |
return documentLayout()->pageCount(); |
|
1573 |
} |
|
1574 |
||
1575 |
/*! |
|
1576 |
Sets the default \a font to use in the document layout. |
|
1577 |
*/ |
|
1578 |
void QTextDocument::setDefaultFont(const QFont &font) |
|
1579 |
{ |
|
1580 |
Q_D(QTextDocument); |
|
1581 |
d->setDefaultFont(font); |
|
1582 |
if (d->lout) |
|
1583 |
d->lout->documentChanged(0, 0, d->length()); |
|
1584 |
} |
|
1585 |
||
1586 |
/*! |
|
1587 |
Returns the default font to be used in the document layout. |
|
1588 |
*/ |
|
1589 |
QFont QTextDocument::defaultFont() const |
|
1590 |
{ |
|
1591 |
Q_D(const QTextDocument); |
|
1592 |
return d->defaultFont(); |
|
1593 |
} |
|
1594 |
||
1595 |
/*! |
|
1596 |
\fn QTextDocument::modificationChanged(bool changed) |
|
1597 |
||
1598 |
This signal is emitted whenever the content of the document |
|
1599 |
changes in a way that affects the modification state. If \a |
|
1600 |
changed is true, the document has been modified; otherwise it is |
|
1601 |
false. |
|
1602 |
||
1603 |
For example, calling setModified(false) on a document and then |
|
1604 |
inserting text causes the signal to get emitted. If you undo that |
|
1605 |
operation, causing the document to return to its original |
|
1606 |
unmodified state, the signal will get emitted again. |
|
1607 |
*/ |
|
1608 |
||
1609 |
/*! |
|
1610 |
\property QTextDocument::modified |
|
1611 |
\brief whether the document has been modified by the user |
|
1612 |
||
1613 |
By default, this property is false. |
|
1614 |
||
1615 |
\sa modificationChanged() |
|
1616 |
*/ |
|
1617 |
||
1618 |
bool QTextDocument::isModified() const |
|
1619 |
{ |
|
1620 |
return docHandle()->isModified(); |
|
1621 |
} |
|
1622 |
||
1623 |
void QTextDocument::setModified(bool m) |
|
1624 |
{ |
|
1625 |
docHandle()->setModified(m); |
|
1626 |
} |
|
1627 |
||
1628 |
#ifndef QT_NO_PRINTER |
|
1629 |
static void printPage(int index, QPainter *painter, const QTextDocument *doc, const QRectF &body, const QPointF &pageNumberPos) |
|
1630 |
{ |
|
1631 |
painter->save(); |
|
1632 |
painter->translate(body.left(), body.top() - (index - 1) * body.height()); |
|
1633 |
QRectF view(0, (index - 1) * body.height(), body.width(), body.height()); |
|
1634 |
||
1635 |
QAbstractTextDocumentLayout *layout = doc->documentLayout(); |
|
1636 |
QAbstractTextDocumentLayout::PaintContext ctx; |
|
1637 |
||
1638 |
painter->setClipRect(view); |
|
1639 |
ctx.clip = view; |
|
1640 |
||
1641 |
// don't use the system palette text as default text color, on HP/UX |
|
1642 |
// for example that's white, and white text on white paper doesn't |
|
1643 |
// look that nice |
|
1644 |
ctx.palette.setColor(QPalette::Text, Qt::black); |
|
1645 |
||
1646 |
layout->draw(painter, ctx); |
|
1647 |
||
1648 |
if (!pageNumberPos.isNull()) { |
|
1649 |
painter->setClipping(false); |
|
1650 |
painter->setFont(QFont(doc->defaultFont())); |
|
1651 |
const QString pageString = QString::number(index); |
|
1652 |
||
1653 |
painter->drawText(qRound(pageNumberPos.x() - painter->fontMetrics().width(pageString)), |
|
1654 |
qRound(pageNumberPos.y() + view.top()), |
|
1655 |
pageString); |
|
1656 |
} |
|
1657 |
||
1658 |
painter->restore(); |
|
1659 |
} |
|
1660 |
||
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1661 |
Q_GUI_EXPORT extern int qt_defaultDpi(); |
0 | 1662 |
|
1663 |
/*! |
|
1664 |
Prints the document to the given \a printer. The QPrinter must be |
|
1665 |
set up before being used with this function. |
|
1666 |
||
1667 |
This is only a convenience method to print the whole document to the printer. |
|
1668 |
||
1669 |
If the document is already paginated through a specified height in the pageSize() |
|
1670 |
property it is printed as-is. |
|
1671 |
||
1672 |
If the document is not paginated, like for example a document used in a QTextEdit, |
|
1673 |
then a temporary copy of the document is created and the copy is broken into |
|
1674 |
multiple pages according to the size of the QPrinter's paperRect(). By default |
|
1675 |
a 2 cm margin is set around the document contents. In addition the current page |
|
1676 |
number is printed at the bottom of each page. |
|
1677 |
||
1678 |
Note that QPrinter::Selection is not supported as print range with this function since |
|
1679 |
the selection is a property of QTextCursor. If you have a QTextEdit associated with |
|
1680 |
your QTextDocument then you can use QTextEdit's print() function because QTextEdit has |
|
1681 |
access to the user's selection. |
|
1682 |
||
1683 |
\sa QTextEdit::print() |
|
1684 |
*/ |
|
1685 |
||
1686 |
void QTextDocument::print(QPrinter *printer) const |
|
1687 |
{ |
|
1688 |
Q_D(const QTextDocument); |
|
1689 |
||
1690 |
if (!printer || !printer->isValid()) |
|
1691 |
return; |
|
1692 |
||
1693 |
if (!d->title.isEmpty()) |
|
1694 |
printer->setDocName(d->title); |
|
1695 |
||
1696 |
bool documentPaginated = d->pageSize.isValid() && !d->pageSize.isNull() |
|
1697 |
&& d->pageSize.height() != INT_MAX; |
|
1698 |
||
1699 |
if (!documentPaginated && !printer->fullPage() && !printer->d_func()->hasCustomPageMargins) |
|
1700 |
printer->setPageMargins(23.53, 23.53, 23.53, 23.53, QPrinter::Millimeter); |
|
1701 |
||
1702 |
QPainter p(printer); |
|
1703 |
||
1704 |
// Check that there is a valid device to print to. |
|
1705 |
if (!p.isActive()) |
|
1706 |
return; |
|
1707 |
||
1708 |
const QTextDocument *doc = this; |
|
25
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
1709 |
QScopedPointer<QTextDocument> clonedDoc; |
0 | 1710 |
(void)doc->documentLayout(); // make sure that there is a layout |
1711 |
||
1712 |
QRectF body = QRectF(QPointF(0, 0), d->pageSize); |
|
1713 |
QPointF pageNumberPos; |
|
1714 |
||
1715 |
if (documentPaginated) { |
|
1716 |
qreal sourceDpiX = qt_defaultDpi(); |
|
1717 |
qreal sourceDpiY = sourceDpiX; |
|
1718 |
||
1719 |
QPaintDevice *dev = doc->documentLayout()->paintDevice(); |
|
1720 |
if (dev) { |
|
1721 |
sourceDpiX = dev->logicalDpiX(); |
|
1722 |
sourceDpiY = dev->logicalDpiY(); |
|
1723 |
} |
|
1724 |
||
1725 |
const qreal dpiScaleX = qreal(printer->logicalDpiX()) / sourceDpiX; |
|
1726 |
const qreal dpiScaleY = qreal(printer->logicalDpiY()) / sourceDpiY; |
|
1727 |
||
1728 |
// scale to dpi |
|
1729 |
p.scale(dpiScaleX, dpiScaleY); |
|
1730 |
||
1731 |
QSizeF scaledPageSize = d->pageSize; |
|
1732 |
scaledPageSize.rwidth() *= dpiScaleX; |
|
1733 |
scaledPageSize.rheight() *= dpiScaleY; |
|
1734 |
||
1735 |
const QSizeF printerPageSize(printer->pageRect().size()); |
|
1736 |
||
1737 |
// scale to page |
|
1738 |
p.scale(printerPageSize.width() / scaledPageSize.width(), |
|
1739 |
printerPageSize.height() / scaledPageSize.height()); |
|
1740 |
} else { |
|
1741 |
doc = clone(const_cast<QTextDocument *>(this)); |
|
25
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
1742 |
clonedDoc.reset(const_cast<QTextDocument *>(doc)); |
0 | 1743 |
|
1744 |
for (QTextBlock srcBlock = firstBlock(), dstBlock = clonedDoc->firstBlock(); |
|
1745 |
srcBlock.isValid() && dstBlock.isValid(); |
|
1746 |
srcBlock = srcBlock.next(), dstBlock = dstBlock.next()) { |
|
1747 |
dstBlock.layout()->setAdditionalFormats(srcBlock.layout()->additionalFormats()); |
|
1748 |
} |
|
1749 |
||
1750 |
QAbstractTextDocumentLayout *layout = doc->documentLayout(); |
|
1751 |
layout->setPaintDevice(p.device()); |
|
1752 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1753 |
// copy the custom object handlers |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1754 |
layout->d_func()->handlers = documentLayout()->d_func()->handlers; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1755 |
|
0 | 1756 |
int dpiy = p.device()->logicalDpiY(); |
1757 |
int margin = 0; |
|
1758 |
if (printer->fullPage() && !printer->d_func()->hasCustomPageMargins) { |
|
1759 |
// for compatibility |
|
1760 |
margin = (int) ((2/2.54)*dpiy); // 2 cm margins |
|
1761 |
QTextFrameFormat fmt = doc->rootFrame()->frameFormat(); |
|
1762 |
fmt.setMargin(margin); |
|
1763 |
doc->rootFrame()->setFrameFormat(fmt); |
|
1764 |
} |
|
1765 |
||
1766 |
QRectF pageRect(printer->pageRect()); |
|
1767 |
body = QRectF(0, 0, pageRect.width(), pageRect.height()); |
|
1768 |
pageNumberPos = QPointF(body.width() - margin, |
|
1769 |
body.height() - margin |
|
1770 |
+ QFontMetrics(doc->defaultFont(), p.device()).ascent() |
|
1771 |
+ 5 * dpiy / 72.0); |
|
1772 |
clonedDoc->setPageSize(body.size()); |
|
1773 |
} |
|
1774 |
||
1775 |
int docCopies; |
|
1776 |
int pageCopies; |
|
1777 |
if (printer->collateCopies() == true){ |
|
1778 |
docCopies = 1; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
1779 |
pageCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount(); |
0 | 1780 |
} else { |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
1781 |
docCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount(); |
0 | 1782 |
pageCopies = 1; |
1783 |
} |
|
1784 |
||
1785 |
int fromPage = printer->fromPage(); |
|
1786 |
int toPage = printer->toPage(); |
|
1787 |
bool ascending = true; |
|
1788 |
||
1789 |
if (fromPage == 0 && toPage == 0) { |
|
1790 |
fromPage = 1; |
|
1791 |
toPage = doc->pageCount(); |
|
1792 |
} |
|
1793 |
// paranoia check |
|
1794 |
fromPage = qMax(1, fromPage); |
|
1795 |
toPage = qMin(doc->pageCount(), toPage); |
|
1796 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1797 |
if (toPage < fromPage) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1798 |
// if the user entered a page range outside the actual number |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1799 |
// of printable pages, just return |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1800 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1801 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1802 |
|
0 | 1803 |
if (printer->pageOrder() == QPrinter::LastPageFirst) { |
1804 |
int tmp = fromPage; |
|
1805 |
fromPage = toPage; |
|
1806 |
toPage = tmp; |
|
1807 |
ascending = false; |
|
1808 |
} |
|
1809 |
||
1810 |
for (int i = 0; i < docCopies; ++i) { |
|
1811 |
||
1812 |
int page = fromPage; |
|
1813 |
while (true) { |
|
1814 |
for (int j = 0; j < pageCopies; ++j) { |
|
1815 |
if (printer->printerState() == QPrinter::Aborted |
|
1816 |
|| printer->printerState() == QPrinter::Error) |
|
25
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
1817 |
return; |
0 | 1818 |
printPage(page, &p, doc, body, pageNumberPos); |
1819 |
if (j < pageCopies - 1) |
|
1820 |
printer->newPage(); |
|
1821 |
} |
|
1822 |
||
1823 |
if (page == toPage) |
|
1824 |
break; |
|
1825 |
||
1826 |
if (ascending) |
|
1827 |
++page; |
|
1828 |
else |
|
1829 |
--page; |
|
1830 |
||
1831 |
printer->newPage(); |
|
1832 |
} |
|
1833 |
||
1834 |
if ( i < docCopies - 1) |
|
1835 |
printer->newPage(); |
|
1836 |
} |
|
1837 |
} |
|
1838 |
#endif |
|
1839 |
||
1840 |
/*! |
|
1841 |
\enum QTextDocument::ResourceType |
|
1842 |
||
1843 |
This enum describes the types of resources that can be loaded by |
|
1844 |
QTextDocument's loadResource() function. |
|
1845 |
||
1846 |
\value HtmlResource The resource contains HTML. |
|
1847 |
\value ImageResource The resource contains image data. |
|
1848 |
Currently supported data types are QVariant::Pixmap and |
|
1849 |
QVariant::Image. If the corresponding variant is of type |
|
1850 |
QVariant::ByteArray then Qt attempts to load the image using |
|
1851 |
QImage::loadFromData. QVariant::Icon is currently not supported. |
|
1852 |
The icon needs to be converted to one of the supported types first, |
|
1853 |
for example using QIcon::pixmap. |
|
1854 |
\value StyleSheetResource The resource contains CSS. |
|
1855 |
\value UserResource The first available value for user defined |
|
1856 |
resource types. |
|
1857 |
||
1858 |
\sa loadResource() |
|
1859 |
*/ |
|
1860 |
||
1861 |
/*! |
|
1862 |
Returns data of the specified \a type from the resource with the |
|
1863 |
given \a name. |
|
1864 |
||
1865 |
This function is called by the rich text engine to request data that isn't |
|
1866 |
directly stored by QTextDocument, but still associated with it. For example, |
|
1867 |
images are referenced indirectly by the name attribute of a QTextImageFormat |
|
1868 |
object. |
|
1869 |
||
1870 |
Resources are cached internally in the document. If a resource can |
|
1871 |
not be found in the cache, loadResource is called to try to load |
|
1872 |
the resource. loadResource should then use addResource to add the |
|
1873 |
resource to the cache. |
|
1874 |
||
1875 |
\sa QTextDocument::ResourceType |
|
1876 |
*/ |
|
1877 |
QVariant QTextDocument::resource(int type, const QUrl &name) const |
|
1878 |
{ |
|
1879 |
Q_D(const QTextDocument); |
|
1880 |
QVariant r = d->resources.value(name); |
|
1881 |
if (!r.isValid()) { |
|
1882 |
r = d->cachedResources.value(name); |
|
1883 |
if (!r.isValid()) |
|
1884 |
r = const_cast<QTextDocument *>(this)->loadResource(type, name); |
|
1885 |
} |
|
1886 |
return r; |
|
1887 |
} |
|
1888 |
||
1889 |
/*! |
|
1890 |
Adds the resource \a resource to the resource cache, using \a |
|
1891 |
type and \a name as identifiers. \a type should be a value from |
|
1892 |
QTextDocument::ResourceType. |
|
1893 |
||
1894 |
For example, you can add an image as a resource in order to reference it |
|
1895 |
from within the document: |
|
1896 |
||
1897 |
\snippet snippets/textdocument-resources/main.cpp Adding a resource |
|
1898 |
||
1899 |
The image can be inserted into the document using the QTextCursor API: |
|
1900 |
||
1901 |
\snippet snippets/textdocument-resources/main.cpp Inserting an image with a cursor |
|
1902 |
||
1903 |
Alternatively, you can insert images using the HTML \c img tag: |
|
1904 |
||
1905 |
\snippet snippets/textdocument-resources/main.cpp Inserting an image using HTML |
|
1906 |
*/ |
|
1907 |
void QTextDocument::addResource(int type, const QUrl &name, const QVariant &resource) |
|
1908 |
{ |
|
1909 |
Q_UNUSED(type); |
|
1910 |
Q_D(QTextDocument); |
|
1911 |
d->resources.insert(name, resource); |
|
1912 |
} |
|
1913 |
||
1914 |
/*! |
|
1915 |
Loads data of the specified \a type from the resource with the |
|
1916 |
given \a name. |
|
1917 |
||
1918 |
This function is called by the rich text engine to request data that isn't |
|
1919 |
directly stored by QTextDocument, but still associated with it. For example, |
|
1920 |
images are referenced indirectly by the name attribute of a QTextImageFormat |
|
1921 |
object. |
|
1922 |
||
1923 |
When called by Qt, \a type is one of the values of |
|
1924 |
QTextDocument::ResourceType. |
|
1925 |
||
1926 |
If the QTextDocument is a child object of a QTextEdit, QTextBrowser, |
|
1927 |
or a QTextDocument itself then the default implementation tries |
|
1928 |
to retrieve the data from the parent. |
|
1929 |
*/ |
|
1930 |
QVariant QTextDocument::loadResource(int type, const QUrl &name) |
|
1931 |
{ |
|
1932 |
Q_D(QTextDocument); |
|
1933 |
QVariant r; |
|
1934 |
||
1935 |
QTextDocument *doc = qobject_cast<QTextDocument *>(parent()); |
|
1936 |
if (doc) { |
|
1937 |
r = doc->loadResource(type, name); |
|
1938 |
} |
|
1939 |
#ifndef QT_NO_TEXTEDIT |
|
1940 |
else if (QTextEdit *edit = qobject_cast<QTextEdit *>(parent())) { |
|
1941 |
QUrl resolvedName = edit->d_func()->resolveUrl(name); |
|
1942 |
r = edit->loadResource(type, resolvedName); |
|
1943 |
} |
|
1944 |
#endif |
|
1945 |
#ifndef QT_NO_TEXTCONTROL |
|
1946 |
else if (QTextControl *control = qobject_cast<QTextControl *>(parent())) { |
|
1947 |
r = control->loadResource(type, name); |
|
1948 |
} |
|
1949 |
#endif |
|
1950 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
1951 |
// handle data: URLs |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
1952 |
if (r.isNull() && name.scheme().compare(QLatin1String("data"), Qt::CaseInsensitive) == 0) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
1953 |
r = qDecodeDataUrl(name).second; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
25
diff
changeset
|
1954 |
|
0 | 1955 |
// if resource was not loaded try to load it here |
1956 |
if (!doc && r.isNull() && name.isRelative()) { |
|
1957 |
QUrl currentURL = d->url; |
|
1958 |
QUrl resourceUrl = name; |
|
1959 |
||
1960 |
// For the second case QUrl can merge "#someanchor" with "foo.html" |
|
1961 |
// correctly to "foo.html#someanchor" |
|
1962 |
if (!(currentURL.isRelative() |
|
1963 |
|| (currentURL.scheme() == QLatin1String("file") |
|
1964 |
&& !QFileInfo(currentURL.toLocalFile()).isAbsolute())) |
|
1965 |
|| (name.hasFragment() && name.path().isEmpty())) { |
|
1966 |
resourceUrl = currentURL.resolved(name); |
|
1967 |
} else { |
|
1968 |
// this is our last resort when current url and new url are both relative |
|
1969 |
// we try to resolve against the current working directory in the local |
|
1970 |
// file system. |
|
1971 |
QFileInfo fi(currentURL.toLocalFile()); |
|
1972 |
if (fi.exists()) { |
|
1973 |
resourceUrl = |
|
1974 |
QUrl::fromLocalFile(fi.absolutePath() + QDir::separator()).resolved(name); |
|
1975 |
} |
|
1976 |
} |
|
1977 |
||
1978 |
QString s = resourceUrl.toLocalFile(); |
|
1979 |
QFile f(s); |
|
1980 |
if (!s.isEmpty() && f.open(QFile::ReadOnly)) { |
|
1981 |
r = f.readAll(); |
|
1982 |
f.close(); |
|
1983 |
} |
|
1984 |
} |
|
1985 |
||
1986 |
if (!r.isNull()) { |
|
1987 |
if (type == ImageResource && r.type() == QVariant::ByteArray) { |
|
1988 |
if (qApp->thread() != QThread::currentThread()) { |
|
1989 |
// must use images in non-GUI threads |
|
1990 |
QImage image; |
|
1991 |
image.loadFromData(r.toByteArray()); |
|
1992 |
if (!image.isNull()) |
|
1993 |
r = image; |
|
1994 |
} else { |
|
1995 |
QPixmap pm; |
|
1996 |
pm.loadFromData(r.toByteArray()); |
|
1997 |
if (!pm.isNull()) |
|
1998 |
r = pm; |
|
1999 |
} |
|
2000 |
} |
|
2001 |
d->cachedResources.insert(name, r); |
|
2002 |
} |
|
2003 |
return r; |
|
2004 |
} |
|
2005 |
||
2006 |
static QTextFormat formatDifference(const QTextFormat &from, const QTextFormat &to) |
|
2007 |
{ |
|
2008 |
QTextFormat diff = to; |
|
2009 |
||
2010 |
const QMap<int, QVariant> props = to.properties(); |
|
2011 |
for (QMap<int, QVariant>::ConstIterator it = props.begin(), end = props.end(); |
|
2012 |
it != end; ++it) |
|
2013 |
if (it.value() == from.property(it.key())) |
|
2014 |
diff.clearProperty(it.key()); |
|
2015 |
||
2016 |
return diff; |
|
2017 |
} |
|
2018 |
||
2019 |
QTextHtmlExporter::QTextHtmlExporter(const QTextDocument *_doc) |
|
2020 |
: doc(_doc), fragmentMarkers(false) |
|
2021 |
{ |
|
2022 |
const QFont defaultFont = doc->defaultFont(); |
|
2023 |
defaultCharFormat.setFont(defaultFont); |
|
2024 |
// don't export those for the default font since we cannot turn them off with CSS |
|
2025 |
defaultCharFormat.clearProperty(QTextFormat::FontUnderline); |
|
2026 |
defaultCharFormat.clearProperty(QTextFormat::FontOverline); |
|
2027 |
defaultCharFormat.clearProperty(QTextFormat::FontStrikeOut); |
|
2028 |
defaultCharFormat.clearProperty(QTextFormat::TextUnderlineStyle); |
|
2029 |
} |
|
2030 |
||
2031 |
/*! |
|
2032 |
Returns the document in HTML format. The conversion may not be |
|
2033 |
perfect, especially for complex documents, due to the limitations |
|
2034 |
of HTML. |
|
2035 |
*/ |
|
2036 |
QString QTextHtmlExporter::toHtml(const QByteArray &encoding, ExportMode mode) |
|
2037 |
{ |
|
2038 |
html = QLatin1String("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" " |
|
2039 |
"\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" |
|
2040 |
"<html><head><meta name=\"qrichtext\" content=\"1\" />"); |
|
2041 |
html.reserve(doc->docHandle()->length()); |
|
2042 |
||
2043 |
fragmentMarkers = (mode == ExportFragment); |
|
2044 |
||
2045 |
if (!encoding.isEmpty()) |
|
2046 |
html += QString::fromLatin1("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%1\" />").arg(QString::fromAscii(encoding)); |
|
2047 |
||
2048 |
QString title = doc->metaInformation(QTextDocument::DocumentTitle); |
|
2049 |
if (!title.isEmpty()) |
|
2050 |
html += QString::fromLatin1("<title>") + title + QString::fromLatin1("</title>"); |
|
2051 |
html += QLatin1String("<style type=\"text/css\">\n"); |
|
2052 |
html += QLatin1String("p, li { white-space: pre-wrap; }\n"); |
|
2053 |
html += QLatin1String("</style>"); |
|
2054 |
html += QLatin1String("</head><body"); |
|
2055 |
||
2056 |
if (mode == ExportEntireDocument) { |
|
2057 |
html += QLatin1String(" style=\""); |
|
2058 |
||
2059 |
emitFontFamily(defaultCharFormat.fontFamily()); |
|
2060 |
||
2061 |
if (defaultCharFormat.hasProperty(QTextFormat::FontPointSize)) { |
|
2062 |
html += QLatin1String(" font-size:"); |
|
2063 |
html += QString::number(defaultCharFormat.fontPointSize()); |
|
2064 |
html += QLatin1String("pt;"); |
|
2065 |
} |
|
2066 |
||
2067 |
html += QLatin1String(" font-weight:"); |
|
2068 |
html += QString::number(defaultCharFormat.fontWeight() * 8); |
|
2069 |
html += QLatin1Char(';'); |
|
2070 |
||
2071 |
html += QLatin1String(" font-style:"); |
|
2072 |
html += (defaultCharFormat.fontItalic() ? QLatin1String("italic") : QLatin1String("normal")); |
|
2073 |
html += QLatin1Char(';'); |
|
2074 |
||
2075 |
// do not set text-decoration on the default font since those values are /always/ propagated |
|
2076 |
// and cannot be turned off with CSS |
|
2077 |
||
2078 |
html += QLatin1Char('\"'); |
|
2079 |
||
2080 |
const QTextFrameFormat fmt = doc->rootFrame()->frameFormat(); |
|
2081 |
emitBackgroundAttribute(fmt); |
|
2082 |
||
2083 |
} else { |
|
2084 |
defaultCharFormat = QTextCharFormat(); |
|
2085 |
} |
|
2086 |
html += QLatin1Char('>'); |
|
2087 |
||
2088 |
QTextFrameFormat rootFmt = doc->rootFrame()->frameFormat(); |
|
2089 |
rootFmt.clearProperty(QTextFormat::BackgroundBrush); |
|
2090 |
||
2091 |
QTextFrameFormat defaultFmt; |
|
2092 |
defaultFmt.setMargin(doc->documentMargin()); |
|
2093 |
||
2094 |
if (rootFmt == defaultFmt) |
|
2095 |
emitFrame(doc->rootFrame()->begin()); |
|
2096 |
else |
|
2097 |
emitTextFrame(doc->rootFrame()); |
|
2098 |
||
2099 |
html += QLatin1String("</body></html>"); |
|
2100 |
return html; |
|
2101 |
} |
|
2102 |
||
2103 |
void QTextHtmlExporter::emitAttribute(const char *attribute, const QString &value) |
|
2104 |
{ |
|
2105 |
html += QLatin1Char(' '); |
|
2106 |
html += QLatin1String(attribute); |
|
2107 |
html += QLatin1String("=\""); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2108 |
html += Qt::escape(value); |
0 | 2109 |
html += QLatin1Char('"'); |
2110 |
} |
|
2111 |
||
2112 |
bool QTextHtmlExporter::emitCharFormatStyle(const QTextCharFormat &format) |
|
2113 |
{ |
|
2114 |
bool attributesEmitted = false; |
|
2115 |
||
2116 |
{ |
|
2117 |
const QString family = format.fontFamily(); |
|
2118 |
if (!family.isEmpty() && family != defaultCharFormat.fontFamily()) { |
|
2119 |
emitFontFamily(family); |
|
2120 |
attributesEmitted = true; |
|
2121 |
} |
|
2122 |
} |
|
2123 |
||
2124 |
if (format.hasProperty(QTextFormat::FontPointSize) |
|
2125 |
&& format.fontPointSize() != defaultCharFormat.fontPointSize()) { |
|
2126 |
html += QLatin1String(" font-size:"); |
|
2127 |
html += QString::number(format.fontPointSize()); |
|
2128 |
html += QLatin1String("pt;"); |
|
2129 |
attributesEmitted = true; |
|
2130 |
} else if (format.hasProperty(QTextFormat::FontSizeAdjustment)) { |
|
2131 |
static const char * const sizeNames[] = { |
|
2132 |
"small", "medium", "large", "x-large", "xx-large" |
|
2133 |
}; |
|
2134 |
const char *name = 0; |
|
2135 |
const int idx = format.intProperty(QTextFormat::FontSizeAdjustment) + 1; |
|
2136 |
if (idx >= 0 && idx <= 4) { |
|
2137 |
name = sizeNames[idx]; |
|
2138 |
} |
|
2139 |
if (name) { |
|
2140 |
html += QLatin1String(" font-size:"); |
|
2141 |
html += QLatin1String(name); |
|
2142 |
html += QLatin1Char(';'); |
|
2143 |
attributesEmitted = true; |
|
2144 |
} |
|
2145 |
} |
|
2146 |
||
2147 |
if (format.hasProperty(QTextFormat::FontWeight) |
|
2148 |
&& format.fontWeight() != defaultCharFormat.fontWeight()) { |
|
2149 |
html += QLatin1String(" font-weight:"); |
|
2150 |
html += QString::number(format.fontWeight() * 8); |
|
2151 |
html += QLatin1Char(';'); |
|
2152 |
attributesEmitted = true; |
|
2153 |
} |
|
2154 |
||
2155 |
if (format.hasProperty(QTextFormat::FontItalic) |
|
2156 |
&& format.fontItalic() != defaultCharFormat.fontItalic()) { |
|
2157 |
html += QLatin1String(" font-style:"); |
|
2158 |
html += (format.fontItalic() ? QLatin1String("italic") : QLatin1String("normal")); |
|
2159 |
html += QLatin1Char(';'); |
|
2160 |
attributesEmitted = true; |
|
2161 |
} |
|
2162 |
||
2163 |
QLatin1String decorationTag(" text-decoration:"); |
|
2164 |
html += decorationTag; |
|
2165 |
bool hasDecoration = false; |
|
2166 |
bool atLeastOneDecorationSet = false; |
|
2167 |
||
2168 |
if ((format.hasProperty(QTextFormat::FontUnderline) || format.hasProperty(QTextFormat::TextUnderlineStyle)) |
|
2169 |
&& format.fontUnderline() != defaultCharFormat.fontUnderline()) { |
|
2170 |
hasDecoration = true; |
|
2171 |
if (format.fontUnderline()) { |
|
2172 |
html += QLatin1String(" underline"); |
|
2173 |
atLeastOneDecorationSet = true; |
|
2174 |
} |
|
2175 |
} |
|
2176 |
||
2177 |
if (format.hasProperty(QTextFormat::FontOverline) |
|
2178 |
&& format.fontOverline() != defaultCharFormat.fontOverline()) { |
|
2179 |
hasDecoration = true; |
|
2180 |
if (format.fontOverline()) { |
|
2181 |
html += QLatin1String(" overline"); |
|
2182 |
atLeastOneDecorationSet = true; |
|
2183 |
} |
|
2184 |
} |
|
2185 |
||
2186 |
if (format.hasProperty(QTextFormat::FontStrikeOut) |
|
2187 |
&& format.fontStrikeOut() != defaultCharFormat.fontStrikeOut()) { |
|
2188 |
hasDecoration = true; |
|
2189 |
if (format.fontStrikeOut()) { |
|
2190 |
html += QLatin1String(" line-through"); |
|
2191 |
atLeastOneDecorationSet = true; |
|
2192 |
} |
|
2193 |
} |
|
2194 |
||
2195 |
if (hasDecoration) { |
|
2196 |
if (!atLeastOneDecorationSet) |
|
2197 |
html += QLatin1String("none"); |
|
2198 |
html += QLatin1Char(';'); |
|
2199 |
attributesEmitted = true; |
|
2200 |
} else { |
|
2201 |
html.chop(qstrlen(decorationTag.latin1())); |
|
2202 |
} |
|
2203 |
||
2204 |
if (format.foreground() != defaultCharFormat.foreground() |
|
2205 |
&& format.foreground().style() != Qt::NoBrush) { |
|
2206 |
html += QLatin1String(" color:"); |
|
2207 |
html += format.foreground().color().name(); |
|
2208 |
html += QLatin1Char(';'); |
|
2209 |
attributesEmitted = true; |
|
2210 |
} |
|
2211 |
||
2212 |
if (format.background() != defaultCharFormat.background() |
|
2213 |
&& format.background().style() == Qt::SolidPattern) { |
|
2214 |
html += QLatin1String(" background-color:"); |
|
2215 |
html += format.background().color().name(); |
|
2216 |
html += QLatin1Char(';'); |
|
2217 |
attributesEmitted = true; |
|
2218 |
} |
|
2219 |
||
2220 |
if (format.verticalAlignment() != defaultCharFormat.verticalAlignment() |
|
2221 |
&& format.verticalAlignment() != QTextCharFormat::AlignNormal) |
|
2222 |
{ |
|
2223 |
html += QLatin1String(" vertical-align:"); |
|
2224 |
||
2225 |
QTextCharFormat::VerticalAlignment valign = format.verticalAlignment(); |
|
2226 |
if (valign == QTextCharFormat::AlignSubScript) |
|
2227 |
html += QLatin1String("sub"); |
|
2228 |
else if (valign == QTextCharFormat::AlignSuperScript) |
|
2229 |
html += QLatin1String("super"); |
|
2230 |
else if (valign == QTextCharFormat::AlignMiddle) |
|
2231 |
html += QLatin1String("middle"); |
|
2232 |
else if (valign == QTextCharFormat::AlignTop) |
|
2233 |
html += QLatin1String("top"); |
|
2234 |
else if (valign == QTextCharFormat::AlignBottom) |
|
2235 |
html += QLatin1String("bottom"); |
|
2236 |
||
2237 |
html += QLatin1Char(';'); |
|
2238 |
attributesEmitted = true; |
|
2239 |
} |
|
2240 |
||
2241 |
if (format.fontCapitalization() != QFont::MixedCase) { |
|
2242 |
const QFont::Capitalization caps = format.fontCapitalization(); |
|
2243 |
if (caps == QFont::AllUppercase) |
|
2244 |
html += QLatin1String(" text-transform:uppercase;"); |
|
2245 |
else if (caps == QFont::AllLowercase) |
|
2246 |
html += QLatin1String(" text-transform:lowercase;"); |
|
2247 |
else if (caps == QFont::SmallCaps) |
|
2248 |
html += QLatin1String(" font-variant:small-caps;"); |
|
2249 |
attributesEmitted = true; |
|
2250 |
} |
|
2251 |
||
2252 |
if (format.fontWordSpacing() != 0.0) { |
|
2253 |
html += QLatin1String(" word-spacing:"); |
|
2254 |
html += QString::number(format.fontWordSpacing()); |
|
2255 |
html += QLatin1String("px;"); |
|
2256 |
attributesEmitted = true; |
|
2257 |
} |
|
2258 |
||
2259 |
return attributesEmitted; |
|
2260 |
} |
|
2261 |
||
2262 |
void QTextHtmlExporter::emitTextLength(const char *attribute, const QTextLength &length) |
|
2263 |
{ |
|
2264 |
if (length.type() == QTextLength::VariableLength) // default |
|
2265 |
return; |
|
2266 |
||
2267 |
html += QLatin1Char(' '); |
|
2268 |
html += QLatin1String(attribute); |
|
2269 |
html += QLatin1String("=\""); |
|
2270 |
html += QString::number(length.rawValue()); |
|
2271 |
||
2272 |
if (length.type() == QTextLength::PercentageLength) |
|
2273 |
html += QLatin1String("%\""); |
|
2274 |
else |
|
2275 |
html += QLatin1Char('\"'); |
|
2276 |
} |
|
2277 |
||
2278 |
void QTextHtmlExporter::emitAlignment(Qt::Alignment align) |
|
2279 |
{ |
|
2280 |
if (align & Qt::AlignLeft) |
|
2281 |
return; |
|
2282 |
else if (align & Qt::AlignRight) |
|
2283 |
html += QLatin1String(" align=\"right\""); |
|
2284 |
else if (align & Qt::AlignHCenter) |
|
2285 |
html += QLatin1String(" align=\"center\""); |
|
2286 |
else if (align & Qt::AlignJustify) |
|
2287 |
html += QLatin1String(" align=\"justify\""); |
|
2288 |
} |
|
2289 |
||
2290 |
void QTextHtmlExporter::emitFloatStyle(QTextFrameFormat::Position pos, StyleMode mode) |
|
2291 |
{ |
|
2292 |
if (pos == QTextFrameFormat::InFlow) |
|
2293 |
return; |
|
2294 |
||
2295 |
if (mode == EmitStyleTag) |
|
2296 |
html += QLatin1String(" style=\"float:"); |
|
2297 |
else |
|
2298 |
html += QLatin1String(" float:"); |
|
2299 |
||
2300 |
if (pos == QTextFrameFormat::FloatLeft) |
|
2301 |
html += QLatin1String(" left;"); |
|
2302 |
else if (pos == QTextFrameFormat::FloatRight) |
|
2303 |
html += QLatin1String(" right;"); |
|
2304 |
else |
|
2305 |
Q_ASSERT_X(0, "QTextHtmlExporter::emitFloatStyle()", "pos should be a valid enum type"); |
|
2306 |
||
2307 |
if (mode == EmitStyleTag) |
|
2308 |
html += QLatin1Char('\"'); |
|
2309 |
} |
|
2310 |
||
2311 |
void QTextHtmlExporter::emitBorderStyle(QTextFrameFormat::BorderStyle style) |
|
2312 |
{ |
|
2313 |
Q_ASSERT(style <= QTextFrameFormat::BorderStyle_Outset); |
|
2314 |
||
2315 |
html += QLatin1String(" border-style:"); |
|
2316 |
||
2317 |
switch (style) { |
|
2318 |
case QTextFrameFormat::BorderStyle_None: |
|
2319 |
html += QLatin1String("none"); |
|
2320 |
break; |
|
2321 |
case QTextFrameFormat::BorderStyle_Dotted: |
|
2322 |
html += QLatin1String("dotted"); |
|
2323 |
break; |
|
2324 |
case QTextFrameFormat::BorderStyle_Dashed: |
|
2325 |
html += QLatin1String("dashed"); |
|
2326 |
break; |
|
2327 |
case QTextFrameFormat::BorderStyle_Solid: |
|
2328 |
html += QLatin1String("solid"); |
|
2329 |
break; |
|
2330 |
case QTextFrameFormat::BorderStyle_Double: |
|
2331 |
html += QLatin1String("double"); |
|
2332 |
break; |
|
2333 |
case QTextFrameFormat::BorderStyle_DotDash: |
|
2334 |
html += QLatin1String("dot-dash"); |
|
2335 |
break; |
|
2336 |
case QTextFrameFormat::BorderStyle_DotDotDash: |
|
2337 |
html += QLatin1String("dot-dot-dash"); |
|
2338 |
break; |
|
2339 |
case QTextFrameFormat::BorderStyle_Groove: |
|
2340 |
html += QLatin1String("groove"); |
|
2341 |
break; |
|
2342 |
case QTextFrameFormat::BorderStyle_Ridge: |
|
2343 |
html += QLatin1String("ridge"); |
|
2344 |
break; |
|
2345 |
case QTextFrameFormat::BorderStyle_Inset: |
|
2346 |
html += QLatin1String("inset"); |
|
2347 |
break; |
|
2348 |
case QTextFrameFormat::BorderStyle_Outset: |
|
2349 |
html += QLatin1String("outset"); |
|
2350 |
break; |
|
2351 |
default: |
|
2352 |
Q_ASSERT(false); |
|
2353 |
break; |
|
2354 |
}; |
|
2355 |
||
2356 |
html += QLatin1Char(';'); |
|
2357 |
} |
|
2358 |
||
2359 |
void QTextHtmlExporter::emitPageBreakPolicy(QTextFormat::PageBreakFlags policy) |
|
2360 |
{ |
|
2361 |
if (policy & QTextFormat::PageBreak_AlwaysBefore) |
|
2362 |
html += QLatin1String(" page-break-before:always;"); |
|
2363 |
||
2364 |
if (policy & QTextFormat::PageBreak_AlwaysAfter) |
|
2365 |
html += QLatin1String(" page-break-after:always;"); |
|
2366 |
} |
|
2367 |
||
2368 |
void QTextHtmlExporter::emitFontFamily(const QString &family) |
|
2369 |
{ |
|
2370 |
html += QLatin1String(" font-family:"); |
|
2371 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2372 |
QLatin1String quote("\'"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2373 |
if (family.contains(QLatin1Char('\''))) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2374 |
quote = QLatin1String("""); |
0 | 2375 |
|
2376 |
html += quote; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2377 |
html += Qt::escape(family); |
0 | 2378 |
html += quote; |
2379 |
html += QLatin1Char(';'); |
|
2380 |
} |
|
2381 |
||
2382 |
void QTextHtmlExporter::emitMargins(const QString &top, const QString &bottom, const QString &left, const QString &right) |
|
2383 |
{ |
|
2384 |
html += QLatin1String(" margin-top:"); |
|
2385 |
html += top; |
|
2386 |
html += QLatin1String("px;"); |
|
2387 |
||
2388 |
html += QLatin1String(" margin-bottom:"); |
|
2389 |
html += bottom; |
|
2390 |
html += QLatin1String("px;"); |
|
2391 |
||
2392 |
html += QLatin1String(" margin-left:"); |
|
2393 |
html += left; |
|
2394 |
html += QLatin1String("px;"); |
|
2395 |
||
2396 |
html += QLatin1String(" margin-right:"); |
|
2397 |
html += right; |
|
2398 |
html += QLatin1String("px;"); |
|
2399 |
} |
|
2400 |
||
2401 |
void QTextHtmlExporter::emitFragment(const QTextFragment &fragment) |
|
2402 |
{ |
|
2403 |
const QTextCharFormat format = fragment.charFormat(); |
|
2404 |
||
2405 |
bool closeAnchor = false; |
|
2406 |
||
2407 |
if (format.isAnchor()) { |
|
2408 |
const QString name = format.anchorName(); |
|
2409 |
if (!name.isEmpty()) { |
|
2410 |
html += QLatin1String("<a name=\""); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2411 |
html += Qt::escape(name); |
0 | 2412 |
html += QLatin1String("\"></a>"); |
2413 |
} |
|
2414 |
const QString href = format.anchorHref(); |
|
2415 |
if (!href.isEmpty()) { |
|
2416 |
html += QLatin1String("<a href=\""); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2417 |
html += Qt::escape(href); |
0 | 2418 |
html += QLatin1String("\">"); |
2419 |
closeAnchor = true; |
|
2420 |
} |
|
2421 |
} |
|
2422 |
||
2423 |
QString txt = fragment.text(); |
|
2424 |
const bool isObject = txt.contains(QChar::ObjectReplacementCharacter); |
|
2425 |
const bool isImage = isObject && format.isImageFormat(); |
|
2426 |
||
2427 |
QLatin1String styleTag("<span style=\""); |
|
2428 |
html += styleTag; |
|
2429 |
||
2430 |
bool attributesEmitted = false; |
|
2431 |
if (!isImage) |
|
2432 |
attributesEmitted = emitCharFormatStyle(format); |
|
2433 |
if (attributesEmitted) |
|
2434 |
html += QLatin1String("\">"); |
|
2435 |
else |
|
2436 |
html.chop(qstrlen(styleTag.latin1())); |
|
2437 |
||
2438 |
if (isObject) { |
|
2439 |
for (int i = 0; isImage && i < txt.length(); ++i) { |
|
2440 |
QTextImageFormat imgFmt = format.toImageFormat(); |
|
2441 |
||
2442 |
html += QLatin1String("<img"); |
|
2443 |
||
2444 |
if (imgFmt.hasProperty(QTextFormat::ImageName)) |
|
2445 |
emitAttribute("src", imgFmt.name()); |
|
2446 |
||
2447 |
if (imgFmt.hasProperty(QTextFormat::ImageWidth)) |
|
2448 |
emitAttribute("width", QString::number(imgFmt.width())); |
|
2449 |
||
2450 |
if (imgFmt.hasProperty(QTextFormat::ImageHeight)) |
|
2451 |
emitAttribute("height", QString::number(imgFmt.height())); |
|
2452 |
||
2453 |
if (imgFmt.verticalAlignment() == QTextCharFormat::AlignMiddle) |
|
2454 |
html += QLatin1String(" style=\"vertical-align: middle;\""); |
|
2455 |
else if (imgFmt.verticalAlignment() == QTextCharFormat::AlignTop) |
|
2456 |
html += QLatin1String(" style=\"vertical-align: top;\""); |
|
2457 |
||
2458 |
if (QTextFrame *imageFrame = qobject_cast<QTextFrame *>(doc->objectForFormat(imgFmt))) |
|
2459 |
emitFloatStyle(imageFrame->frameFormat().position()); |
|
2460 |
||
2461 |
html += QLatin1String(" />"); |
|
2462 |
} |
|
2463 |
} else { |
|
2464 |
Q_ASSERT(!txt.contains(QChar::ObjectReplacementCharacter)); |
|
2465 |
||
2466 |
txt = Qt::escape(txt); |
|
2467 |
||
2468 |
// split for [\n{LineSeparator}] |
|
2469 |
QString forcedLineBreakRegExp = QString::fromLatin1("[\\na]"); |
|
2470 |
forcedLineBreakRegExp[3] = QChar::LineSeparator; |
|
2471 |
||
2472 |
const QStringList lines = txt.split(QRegExp(forcedLineBreakRegExp)); |
|
2473 |
for (int i = 0; i < lines.count(); ++i) { |
|
2474 |
if (i > 0) |
|
2475 |
html += QLatin1String("<br />"); // space on purpose for compatibility with Netscape, Lynx & Co. |
|
2476 |
html += lines.at(i); |
|
2477 |
} |
|
2478 |
} |
|
2479 |
||
2480 |
if (attributesEmitted) |
|
2481 |
html += QLatin1String("</span>"); |
|
2482 |
||
2483 |
if (closeAnchor) |
|
2484 |
html += QLatin1String("</a>"); |
|
2485 |
} |
|
2486 |
||
2487 |
static bool isOrderedList(int style) |
|
2488 |
{ |
|
2489 |
return style == QTextListFormat::ListDecimal || style == QTextListFormat::ListLowerAlpha |
|
2490 |
|| style == QTextListFormat::ListUpperAlpha |
|
2491 |
|| style == QTextListFormat::ListUpperRoman |
|
2492 |
|| style == QTextListFormat::ListLowerRoman |
|
2493 |
; |
|
2494 |
} |
|
2495 |
||
2496 |
void QTextHtmlExporter::emitBlockAttributes(const QTextBlock &block) |
|
2497 |
{ |
|
2498 |
QTextBlockFormat format = block.blockFormat(); |
|
2499 |
emitAlignment(format.alignment()); |
|
2500 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
2501 |
// assume default to not bloat the html too much |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
2502 |
// html += QLatin1String(" dir='ltr'"); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
2503 |
if (block.textDirection() == Qt::RightToLeft) |
0 | 2504 |
html += QLatin1String(" dir='rtl'"); |
2505 |
||
2506 |
QLatin1String style(" style=\""); |
|
2507 |
html += style; |
|
2508 |
||
2509 |
const bool emptyBlock = block.begin().atEnd(); |
|
2510 |
if (emptyBlock) { |
|
2511 |
html += QLatin1String("-qt-paragraph-type:empty;"); |
|
2512 |
} |
|
2513 |
||
2514 |
emitMargins(QString::number(format.topMargin()), |
|
2515 |
QString::number(format.bottomMargin()), |
|
2516 |
QString::number(format.leftMargin()), |
|
2517 |
QString::number(format.rightMargin())); |
|
2518 |
||
2519 |
html += QLatin1String(" -qt-block-indent:"); |
|
2520 |
html += QString::number(format.indent()); |
|
2521 |
html += QLatin1Char(';'); |
|
2522 |
||
2523 |
html += QLatin1String(" text-indent:"); |
|
2524 |
html += QString::number(format.textIndent()); |
|
2525 |
html += QLatin1String("px;"); |
|
2526 |
||
2527 |
if (block.userState() != -1) { |
|
2528 |
html += QLatin1String(" -qt-user-state:"); |
|
2529 |
html += QString::number(block.userState()); |
|
2530 |
html += QLatin1Char(';'); |
|
2531 |
} |
|
2532 |
||
2533 |
emitPageBreakPolicy(format.pageBreakPolicy()); |
|
2534 |
||
2535 |
QTextCharFormat diff; |
|
2536 |
if (emptyBlock) { // only print character properties when we don't expect them to be repeated by actual text in the parag |
|
2537 |
const QTextCharFormat blockCharFmt = block.charFormat(); |
|
2538 |
diff = formatDifference(defaultCharFormat, blockCharFmt).toCharFormat(); |
|
2539 |
} |
|
2540 |
||
2541 |
diff.clearProperty(QTextFormat::BackgroundBrush); |
|
2542 |
if (format.hasProperty(QTextFormat::BackgroundBrush)) { |
|
2543 |
QBrush bg = format.background(); |
|
2544 |
if (bg.style() != Qt::NoBrush) |
|
2545 |
diff.setProperty(QTextFormat::BackgroundBrush, format.property(QTextFormat::BackgroundBrush)); |
|
2546 |
} |
|
2547 |
||
2548 |
if (!diff.properties().isEmpty()) |
|
2549 |
emitCharFormatStyle(diff); |
|
2550 |
||
2551 |
html += QLatin1Char('"'); |
|
2552 |
||
2553 |
} |
|
2554 |
||
2555 |
void QTextHtmlExporter::emitBlock(const QTextBlock &block) |
|
2556 |
{ |
|
2557 |
if (block.begin().atEnd()) { |
|
2558 |
// ### HACK, remove once QTextFrame::Iterator is fixed |
|
2559 |
int p = block.position(); |
|
2560 |
if (p > 0) |
|
2561 |
--p; |
|
2562 |
QTextDocumentPrivate::FragmentIterator frag = doc->docHandle()->find(p); |
|
2563 |
QChar ch = doc->docHandle()->buffer().at(frag->stringPosition); |
|
2564 |
if (ch == QTextBeginningOfFrame |
|
2565 |
|| ch == QTextEndOfFrame) |
|
2566 |
return; |
|
2567 |
} |
|
2568 |
||
2569 |
html += QLatin1Char('\n'); |
|
2570 |
||
2571 |
// save and later restore, in case we 'change' the default format by |
|
2572 |
// emitting block char format information |
|
2573 |
QTextCharFormat oldDefaultCharFormat = defaultCharFormat; |
|
2574 |
||
2575 |
QTextList *list = block.textList(); |
|
2576 |
if (list) { |
|
2577 |
if (list->itemNumber(block) == 0) { // first item? emit <ul> or appropriate |
|
2578 |
const QTextListFormat format = list->format(); |
|
2579 |
const int style = format.style(); |
|
2580 |
switch (style) { |
|
2581 |
case QTextListFormat::ListDecimal: html += QLatin1String("<ol"); break; |
|
2582 |
case QTextListFormat::ListDisc: html += QLatin1String("<ul"); break; |
|
2583 |
case QTextListFormat::ListCircle: html += QLatin1String("<ul type=\"circle\""); break; |
|
2584 |
case QTextListFormat::ListSquare: html += QLatin1String("<ul type=\"square\""); break; |
|
2585 |
case QTextListFormat::ListLowerAlpha: html += QLatin1String("<ol type=\"a\""); break; |
|
2586 |
case QTextListFormat::ListUpperAlpha: html += QLatin1String("<ol type=\"A\""); break; |
|
2587 |
case QTextListFormat::ListLowerRoman: html += QLatin1String("<ol type=\"i\""); break; |
|
2588 |
case QTextListFormat::ListUpperRoman: html += QLatin1String("<ol type=\"I\""); break; |
|
2589 |
default: html += QLatin1String("<ul"); // ### should not happen |
|
2590 |
} |
|
2591 |
||
2592 |
html += QLatin1String(" style=\"margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px;"); |
|
2593 |
||
2594 |
if (format.hasProperty(QTextFormat::ListIndent)) { |
|
2595 |
html += QLatin1String(" -qt-list-indent: "); |
|
2596 |
html += QString::number(format.indent()); |
|
2597 |
html += QLatin1Char(';'); |
|
2598 |
} |
|
2599 |
||
2600 |
html += QLatin1String("\">"); |
|
2601 |
} |
|
2602 |
||
2603 |
html += QLatin1String("<li"); |
|
2604 |
||
2605 |
const QTextCharFormat blockFmt = formatDifference(defaultCharFormat, block.charFormat()).toCharFormat(); |
|
2606 |
if (!blockFmt.properties().isEmpty()) { |
|
2607 |
html += QLatin1String(" style=\""); |
|
2608 |
emitCharFormatStyle(blockFmt); |
|
2609 |
html += QLatin1Char('\"'); |
|
2610 |
||
2611 |
defaultCharFormat.merge(block.charFormat()); |
|
2612 |
} |
|
2613 |
} |
|
2614 |
||
2615 |
const QTextBlockFormat blockFormat = block.blockFormat(); |
|
2616 |
if (blockFormat.hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth)) { |
|
2617 |
html += QLatin1String("<hr"); |
|
2618 |
||
2619 |
QTextLength width = blockFormat.lengthProperty(QTextFormat::BlockTrailingHorizontalRulerWidth); |
|
2620 |
if (width.type() != QTextLength::VariableLength) |
|
2621 |
emitTextLength("width", width); |
|
2622 |
else |
|
2623 |
html += QLatin1Char(' '); |
|
2624 |
||
2625 |
html += QLatin1String("/>"); |
|
2626 |
return; |
|
2627 |
} |
|
2628 |
||
2629 |
const bool pre = blockFormat.nonBreakableLines(); |
|
2630 |
if (pre) { |
|
2631 |
if (list) |
|
2632 |
html += QLatin1Char('>'); |
|
2633 |
html += QLatin1String("<pre"); |
|
2634 |
} else if (!list) { |
|
2635 |
html += QLatin1String("<p"); |
|
2636 |
} |
|
2637 |
||
2638 |
emitBlockAttributes(block); |
|
2639 |
||
2640 |
html += QLatin1Char('>'); |
|
2641 |
||
2642 |
QTextBlock::Iterator it = block.begin(); |
|
2643 |
if (fragmentMarkers && !it.atEnd() && block == doc->begin()) |
|
2644 |
html += QLatin1String("<!--StartFragment-->"); |
|
2645 |
||
2646 |
for (; !it.atEnd(); ++it) |
|
2647 |
emitFragment(it.fragment()); |
|
2648 |
||
2649 |
if (fragmentMarkers && block.position() + block.length() == doc->docHandle()->length()) |
|
2650 |
html += QLatin1String("<!--EndFragment-->"); |
|
2651 |
||
2652 |
if (pre) |
|
2653 |
html += QLatin1String("</pre>"); |
|
2654 |
else if (list) |
|
2655 |
html += QLatin1String("</li>"); |
|
2656 |
else |
|
2657 |
html += QLatin1String("</p>"); |
|
2658 |
||
2659 |
if (list) { |
|
2660 |
if (list->itemNumber(block) == list->count() - 1) { // last item? close list |
|
2661 |
if (isOrderedList(list->format().style())) |
|
2662 |
html += QLatin1String("</ol>"); |
|
2663 |
else |
|
2664 |
html += QLatin1String("</ul>"); |
|
2665 |
} |
|
2666 |
} |
|
2667 |
||
2668 |
defaultCharFormat = oldDefaultCharFormat; |
|
2669 |
} |
|
2670 |
||
2671 |
extern bool qHasPixmapTexture(const QBrush& brush); |
|
2672 |
||
2673 |
QString QTextHtmlExporter::findUrlForImage(const QTextDocument *doc, qint64 cacheKey, bool isPixmap) |
|
2674 |
{ |
|
2675 |
QString url; |
|
2676 |
if (!doc) |
|
2677 |
return url; |
|
2678 |
||
2679 |
if (QTextDocument *parent = qobject_cast<QTextDocument *>(doc->parent())) |
|
2680 |
return findUrlForImage(parent, cacheKey, isPixmap); |
|
2681 |
||
2682 |
if (doc && doc->docHandle()) { |
|
2683 |
QTextDocumentPrivate *priv = doc->docHandle(); |
|
2684 |
QMap<QUrl, QVariant>::const_iterator it = priv->cachedResources.constBegin(); |
|
2685 |
for (; it != priv->cachedResources.constEnd(); ++it) { |
|
2686 |
||
2687 |
const QVariant &v = it.value(); |
|
2688 |
if (v.type() == QVariant::Image && !isPixmap) { |
|
2689 |
if (qvariant_cast<QImage>(v).cacheKey() == cacheKey) |
|
2690 |
break; |
|
2691 |
} |
|
2692 |
||
2693 |
if (v.type() == QVariant::Pixmap && isPixmap) { |
|
2694 |
if (qvariant_cast<QPixmap>(v).cacheKey() == cacheKey) |
|
2695 |
break; |
|
2696 |
} |
|
2697 |
} |
|
2698 |
||
2699 |
if (it != priv->cachedResources.constEnd()) |
|
2700 |
url = it.key().toString(); |
|
2701 |
} |
|
2702 |
||
2703 |
return url; |
|
2704 |
} |
|
2705 |
||
2706 |
void QTextDocumentPrivate::mergeCachedResources(const QTextDocumentPrivate *priv) |
|
2707 |
{ |
|
2708 |
if (!priv) |
|
2709 |
return; |
|
2710 |
||
2711 |
cachedResources.unite(priv->cachedResources); |
|
2712 |
} |
|
2713 |
||
2714 |
void QTextHtmlExporter::emitBackgroundAttribute(const QTextFormat &format) |
|
2715 |
{ |
|
2716 |
if (format.hasProperty(QTextFormat::BackgroundImageUrl)) { |
|
2717 |
QString url = format.property(QTextFormat::BackgroundImageUrl).toString(); |
|
2718 |
emitAttribute("background", url); |
|
2719 |
} else { |
|
2720 |
const QBrush &brush = format.background(); |
|
2721 |
if (brush.style() == Qt::SolidPattern) { |
|
2722 |
emitAttribute("bgcolor", brush.color().name()); |
|
2723 |
} else if (brush.style() == Qt::TexturePattern) { |
|
2724 |
const bool isPixmap = qHasPixmapTexture(brush); |
|
2725 |
const qint64 cacheKey = isPixmap ? brush.texture().cacheKey() : brush.textureImage().cacheKey(); |
|
2726 |
||
2727 |
const QString url = findUrlForImage(doc, cacheKey, isPixmap); |
|
2728 |
||
2729 |
if (!url.isEmpty()) |
|
2730 |
emitAttribute("background", url); |
|
2731 |
} |
|
2732 |
} |
|
2733 |
} |
|
2734 |
||
2735 |
void QTextHtmlExporter::emitTable(const QTextTable *table) |
|
2736 |
{ |
|
2737 |
QTextTableFormat format = table->format(); |
|
2738 |
||
2739 |
html += QLatin1String("\n<table"); |
|
2740 |
||
2741 |
if (format.hasProperty(QTextFormat::FrameBorder)) |
|
2742 |
emitAttribute("border", QString::number(format.border())); |
|
2743 |
||
2744 |
emitFrameStyle(format, TableFrame); |
|
2745 |
||
2746 |
emitAlignment(format.alignment()); |
|
2747 |
emitTextLength("width", format.width()); |
|
2748 |
||
2749 |
if (format.hasProperty(QTextFormat::TableCellSpacing)) |
|
2750 |
emitAttribute("cellspacing", QString::number(format.cellSpacing())); |
|
2751 |
if (format.hasProperty(QTextFormat::TableCellPadding)) |
|
2752 |
emitAttribute("cellpadding", QString::number(format.cellPadding())); |
|
2753 |
||
2754 |
emitBackgroundAttribute(format); |
|
2755 |
||
2756 |
html += QLatin1Char('>'); |
|
2757 |
||
2758 |
const int rows = table->rows(); |
|
2759 |
const int columns = table->columns(); |
|
2760 |
||
2761 |
QVector<QTextLength> columnWidths = format.columnWidthConstraints(); |
|
2762 |
if (columnWidths.isEmpty()) { |
|
2763 |
columnWidths.resize(columns); |
|
2764 |
columnWidths.fill(QTextLength()); |
|
2765 |
} |
|
2766 |
Q_ASSERT(columnWidths.count() == columns); |
|
2767 |
||
2768 |
QVarLengthArray<bool> widthEmittedForColumn(columns); |
|
2769 |
for (int i = 0; i < columns; ++i) |
|
2770 |
widthEmittedForColumn[i] = false; |
|
2771 |
||
2772 |
const int headerRowCount = qMin(format.headerRowCount(), rows); |
|
2773 |
if (headerRowCount > 0) |
|
2774 |
html += QLatin1String("<thead>"); |
|
2775 |
||
2776 |
for (int row = 0; row < rows; ++row) { |
|
2777 |
html += QLatin1String("\n<tr>"); |
|
2778 |
||
2779 |
for (int col = 0; col < columns; ++col) { |
|
2780 |
const QTextTableCell cell = table->cellAt(row, col); |
|
2781 |
||
2782 |
// for col/rowspans |
|
2783 |
if (cell.row() != row) |
|
2784 |
continue; |
|
2785 |
||
2786 |
if (cell.column() != col) |
|
2787 |
continue; |
|
2788 |
||
2789 |
html += QLatin1String("\n<td"); |
|
2790 |
||
2791 |
if (!widthEmittedForColumn[col] && cell.columnSpan() == 1) { |
|
2792 |
emitTextLength("width", columnWidths.at(col)); |
|
2793 |
widthEmittedForColumn[col] = true; |
|
2794 |
} |
|
2795 |
||
2796 |
if (cell.columnSpan() > 1) |
|
2797 |
emitAttribute("colspan", QString::number(cell.columnSpan())); |
|
2798 |
||
2799 |
if (cell.rowSpan() > 1) |
|
2800 |
emitAttribute("rowspan", QString::number(cell.rowSpan())); |
|
2801 |
||
2802 |
const QTextTableCellFormat cellFormat = cell.format().toTableCellFormat(); |
|
2803 |
emitBackgroundAttribute(cellFormat); |
|
2804 |
||
2805 |
QTextCharFormat oldDefaultCharFormat = defaultCharFormat; |
|
2806 |
||
2807 |
QTextCharFormat::VerticalAlignment valign = cellFormat.verticalAlignment(); |
|
2808 |
||
2809 |
QString styleString; |
|
2810 |
if (valign >= QTextCharFormat::AlignMiddle && valign <= QTextCharFormat::AlignBottom) { |
|
2811 |
styleString += QLatin1String(" vertical-align:"); |
|
2812 |
switch (valign) { |
|
2813 |
case QTextCharFormat::AlignMiddle: |
|
2814 |
styleString += QLatin1String("middle"); |
|
2815 |
break; |
|
2816 |
case QTextCharFormat::AlignTop: |
|
2817 |
styleString += QLatin1String("top"); |
|
2818 |
break; |
|
2819 |
case QTextCharFormat::AlignBottom: |
|
2820 |
styleString += QLatin1String("bottom"); |
|
2821 |
break; |
|
2822 |
default: |
|
2823 |
break; |
|
2824 |
} |
|
2825 |
styleString += QLatin1Char(';'); |
|
2826 |
||
2827 |
QTextCharFormat temp; |
|
2828 |
temp.setVerticalAlignment(valign); |
|
2829 |
defaultCharFormat.merge(temp); |
|
2830 |
} |
|
2831 |
||
2832 |
if (cellFormat.hasProperty(QTextFormat::TableCellLeftPadding)) |
|
2833 |
styleString += QLatin1String(" padding-left:") + QString::number(cellFormat.leftPadding()) + QLatin1Char(';'); |
|
2834 |
if (cellFormat.hasProperty(QTextFormat::TableCellRightPadding)) |
|
2835 |
styleString += QLatin1String(" padding-right:") + QString::number(cellFormat.rightPadding()) + QLatin1Char(';'); |
|
2836 |
if (cellFormat.hasProperty(QTextFormat::TableCellTopPadding)) |
|
2837 |
styleString += QLatin1String(" padding-top:") + QString::number(cellFormat.topPadding()) + QLatin1Char(';'); |
|
2838 |
if (cellFormat.hasProperty(QTextFormat::TableCellBottomPadding)) |
|
2839 |
styleString += QLatin1String(" padding-bottom:") + QString::number(cellFormat.bottomPadding()) + QLatin1Char(';'); |
|
2840 |
||
2841 |
if (!styleString.isEmpty()) |
|
2842 |
html += QLatin1String(" style=\"") + styleString + QLatin1Char('\"'); |
|
2843 |
||
2844 |
html += QLatin1Char('>'); |
|
2845 |
||
2846 |
emitFrame(cell.begin()); |
|
2847 |
||
2848 |
html += QLatin1String("</td>"); |
|
2849 |
||
2850 |
defaultCharFormat = oldDefaultCharFormat; |
|
2851 |
} |
|
2852 |
||
2853 |
html += QLatin1String("</tr>"); |
|
2854 |
if (headerRowCount > 0 && row == headerRowCount - 1) |
|
2855 |
html += QLatin1String("</thead>"); |
|
2856 |
} |
|
2857 |
||
2858 |
html += QLatin1String("</table>"); |
|
2859 |
} |
|
2860 |
||
2861 |
void QTextHtmlExporter::emitFrame(QTextFrame::Iterator frameIt) |
|
2862 |
{ |
|
2863 |
if (!frameIt.atEnd()) { |
|
2864 |
QTextFrame::Iterator next = frameIt; |
|
2865 |
++next; |
|
2866 |
if (next.atEnd() |
|
2867 |
&& frameIt.currentFrame() == 0 |
|
2868 |
&& frameIt.parentFrame() != doc->rootFrame() |
|
2869 |
&& frameIt.currentBlock().begin().atEnd()) |
|
2870 |
return; |
|
2871 |
} |
|
2872 |
||
2873 |
for (QTextFrame::Iterator it = frameIt; |
|
2874 |
!it.atEnd(); ++it) { |
|
2875 |
if (QTextFrame *f = it.currentFrame()) { |
|
2876 |
if (QTextTable *table = qobject_cast<QTextTable *>(f)) { |
|
2877 |
emitTable(table); |
|
2878 |
} else { |
|
2879 |
emitTextFrame(f); |
|
2880 |
} |
|
2881 |
} else if (it.currentBlock().isValid()) { |
|
2882 |
emitBlock(it.currentBlock()); |
|
2883 |
} |
|
2884 |
} |
|
2885 |
} |
|
2886 |
||
2887 |
void QTextHtmlExporter::emitTextFrame(const QTextFrame *f) |
|
2888 |
{ |
|
2889 |
FrameType frameType = f->parentFrame() ? TextFrame : RootFrame; |
|
2890 |
||
2891 |
html += QLatin1String("\n<table"); |
|
2892 |
QTextFrameFormat format = f->frameFormat(); |
|
2893 |
||
2894 |
if (format.hasProperty(QTextFormat::FrameBorder)) |
|
2895 |
emitAttribute("border", QString::number(format.border())); |
|
2896 |
||
2897 |
emitFrameStyle(format, frameType); |
|
2898 |
||
2899 |
emitTextLength("width", format.width()); |
|
2900 |
emitTextLength("height", format.height()); |
|
2901 |
||
2902 |
// root frame's bcolor goes in the <body> tag |
|
2903 |
if (frameType != RootFrame) |
|
2904 |
emitBackgroundAttribute(format); |
|
2905 |
||
2906 |
html += QLatin1Char('>'); |
|
2907 |
html += QLatin1String("\n<tr>\n<td style=\"border: none;\">"); |
|
2908 |
emitFrame(f->begin()); |
|
2909 |
html += QLatin1String("</td></tr></table>"); |
|
2910 |
} |
|
2911 |
||
2912 |
void QTextHtmlExporter::emitFrameStyle(const QTextFrameFormat &format, FrameType frameType) |
|
2913 |
{ |
|
2914 |
QLatin1String styleAttribute(" style=\""); |
|
2915 |
html += styleAttribute; |
|
2916 |
const int originalHtmlLength = html.length(); |
|
2917 |
||
2918 |
if (frameType == TextFrame) |
|
2919 |
html += QLatin1String("-qt-table-type: frame;"); |
|
2920 |
else if (frameType == RootFrame) |
|
2921 |
html += QLatin1String("-qt-table-type: root;"); |
|
2922 |
||
2923 |
const QTextFrameFormat defaultFormat; |
|
2924 |
||
2925 |
emitFloatStyle(format.position(), OmitStyleTag); |
|
2926 |
emitPageBreakPolicy(format.pageBreakPolicy()); |
|
2927 |
||
2928 |
if (format.borderBrush() != defaultFormat.borderBrush()) { |
|
2929 |
html += QLatin1String(" border-color:"); |
|
2930 |
html += format.borderBrush().color().name(); |
|
2931 |
html += QLatin1Char(';'); |
|
2932 |
} |
|
2933 |
||
2934 |
if (format.borderStyle() != defaultFormat.borderStyle()) |
|
2935 |
emitBorderStyle(format.borderStyle()); |
|
2936 |
||
2937 |
if (format.hasProperty(QTextFormat::FrameMargin) |
|
2938 |
|| format.hasProperty(QTextFormat::FrameLeftMargin) |
|
2939 |
|| format.hasProperty(QTextFormat::FrameRightMargin) |
|
2940 |
|| format.hasProperty(QTextFormat::FrameTopMargin) |
|
2941 |
|| format.hasProperty(QTextFormat::FrameBottomMargin)) |
|
2942 |
emitMargins(QString::number(format.topMargin()), |
|
2943 |
QString::number(format.bottomMargin()), |
|
2944 |
QString::number(format.leftMargin()), |
|
2945 |
QString::number(format.rightMargin())); |
|
2946 |
||
2947 |
if (html.length() == originalHtmlLength) // nothing emitted? |
|
2948 |
html.chop(qstrlen(styleAttribute.latin1())); |
|
2949 |
else |
|
2950 |
html += QLatin1Char('\"'); |
|
2951 |
} |
|
2952 |
||
2953 |
/*! |
|
2954 |
Returns a string containing an HTML representation of the document. |
|
2955 |
||
2956 |
The \a encoding parameter specifies the value for the charset attribute |
|
2957 |
in the html header. For example if 'utf-8' is specified then the |
|
2958 |
beginning of the generated html will look like this: |
|
2959 |
\snippet doc/src/snippets/code/src_gui_text_qtextdocument.cpp 1 |
|
2960 |
||
2961 |
If no encoding is specified then no such meta information is generated. |
|
2962 |
||
2963 |
If you later on convert the returned html string into a byte array for |
|
2964 |
transmission over a network or when saving to disk you should specify |
|
2965 |
the encoding you're going to use for the conversion to a byte array here. |
|
2966 |
||
2967 |
\sa {Supported HTML Subset} |
|
2968 |
*/ |
|
2969 |
#ifndef QT_NO_TEXTHTMLPARSER |
|
2970 |
QString QTextDocument::toHtml(const QByteArray &encoding) const |
|
2971 |
{ |
|
2972 |
return QTextHtmlExporter(this).toHtml(encoding); |
|
2973 |
} |
|
2974 |
#endif // QT_NO_TEXTHTMLPARSER |
|
2975 |
||
2976 |
/*! |
|
2977 |
Returns a vector of text formats for all the formats used in the document. |
|
2978 |
*/ |
|
2979 |
QVector<QTextFormat> QTextDocument::allFormats() const |
|
2980 |
{ |
|
2981 |
Q_D(const QTextDocument); |
|
2982 |
return d->formatCollection()->formats; |
|
2983 |
} |
|
2984 |
||
2985 |
||
2986 |
/*! |
|
2987 |
\internal |
|
2988 |
||
2989 |
So that not all classes have to be friends of each other... |
|
2990 |
*/ |
|
2991 |
QTextDocumentPrivate *QTextDocument::docHandle() const |
|
2992 |
{ |
|
2993 |
Q_D(const QTextDocument); |
|
2994 |
return const_cast<QTextDocumentPrivate *>(d); |
|
2995 |
} |
|
2996 |
||
2997 |
/*! |
|
2998 |
\since 4.4 |
|
2999 |
\fn QTextDocument::undoCommandAdded() |
|
3000 |
||
3001 |
This signal is emitted every time a new level of undo is added to the QTextDocument. |
|
3002 |
*/ |
|
3003 |
||
3004 |
QT_END_NAMESPACE |