source: trunk/doc/src/frameworks-technologies/richtext.qdoc@ 568

Last change on this file since 568 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 50.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \group richtext-processing
44 \title Rich Text Processing APIs
45*/
46
47/*!
48 \page richtext.html
49 \title Rich Text Processing
50 \brief An overview of Qt's rich text processing, editing and display features.
51
52 \ingroup frameworks-technologies
53
54 \nextpage Rich Text Document Structure
55
56 The Scribe framework provides a set of classes for reading and manipulating
57 structured rich text documents. Unlike previous rich text support in Qt, the
58 new classes are centered around the QTextDocument class rather than raw
59 textual information. This enables the developer to create and modify
60 structured rich text documents without having to prepare content in an
61 intermediate markup format.
62
63 The information within a document can be accessed via two complementary
64 interfaces: A cursor-based interface is used for editing, and a read-only
65 hierarchical interface provides a high level overview of the document
66 structure. The main advantage of the cursor-based interface is that the
67 text can be edited using operations that mimic a user's interaction with
68 an editor, without losing the underlying structure of the document. The
69 read-only hierarchical interface is most useful when performing operations
70 such as searching and document export.
71
72 This document is divided up into chapters for convenient reference:
73
74 \list
75 \i \l{Rich Text Document Structure} outlines
76 the different kinds of elements in a QTextDocument, and describes how
77 they are arranged in a document structure.
78 \i \l{The QTextCursor Interface} explains how rich
79 text documents can be edited using the cursor-based interface.
80 \i \l{Document Layouts} briefly explains the role of document layouts.
81 \i \l{Common Rich Text Editing Tasks} examines some
82 common tasks that involve reading or manipulating rich text documents.
83 \i \l{Advanced Rich Text Processing} examines advanced rich text editing tasks.
84 \i \l{Supported HTML Subset} lists the HTML tags supported by QTextDocument.
85 \endlist
86
87 \section1 Rich Text Processing APIs
88
89 Qt provides an extensive collection of classes for parsing, rendering
90 manipulating and editing rich text.
91
92 \annotatedlist richtext-processing
93*/
94
95/*!
96 \page richtext-structure.html
97 \contentspage richtext.html Contents
98 \previouspage Rich Text Processing
99 \nextpage The QTextCursor Interface
100
101 \title Rich Text Document Structure
102
103 \tableofcontents
104
105 Text documents are represented by the QTextDocument class, which
106 contains information about the document's internal representation, its
107 structure, and keeps track of modifications to provide undo/redo
108 facilities.
109
110 The structured representation of a text document presents its contents as
111 a hierarchy of text blocks, frames, tables, and other objects. These provide
112 a logical structure to the document and describe how their contents will be
113 displayed. Generally, frames and tables are used to group other
114 structures while text blocks contain the actual textual information.
115
116 New elements are created and inserted into the document programmatically
117 \l{richtext-cursor.html}{with a QTextCursor} or by using an editor
118 widget, such as QTextEdit. Elements can be given a particular format when
119 they are created; otherwise they take the cursor's current format for the
120 element.
121
122 \table
123 \row
124 \i \inlineimage richtext-document.png
125 \i \bold{Basic structure}
126
127 The "top level" of a document might be populated in the way shown.
128 Each document always contains a root frame, and this always contains
129 at least one text block.
130
131 For documents with some textual content, the root
132 frame usually contains a sequence of blocks and other elements.
133
134 Sequences of frames and tables are always separated by text blocks in a
135 document, even if the text blocks contain no information. This ensures that
136 new elements can always be inserted between existing structures.
137 \endtable
138
139 In this chapter, we look at each of the structural elements
140 used in a rich text document, outline their features and uses, and show
141 how to examine their contents. Document editing is described in
142 \l{richtext-cursor.html}{The QTextCursor Interface}.
143
144 \section1 Rich Text Documents
145
146 QTextDocument objects contain all the information required to construct
147 rich text documents.
148 Text documents can be accessed in two complementary ways: as a linear
149 buffer for editors to use, and as an object hierarchy that is useful to
150 layout engines.
151 In the hierarchical document model, objects generally correspond to
152 visual elements such as frames, tables, and lists. At a lower level,
153 these elements describe properties such as the text style and alignment.
154 The linear representation of the document is used for editing and
155 manipulation of the document's contents.
156
157 Although QTextEdit makes it easy to display and edit rich text, documents
158 can also be used independently of any editor widget, for example:
159
160 \snippet doc/src/snippets/code/doc_src_richtext.qdoc 0
161
162 Alternatively, they can be extracted from an existing editor:
163
164 \snippet doc/src/snippets/code/doc_src_richtext.qdoc 1
165
166 This flexibility enables applications to handle multiple rich text
167 documents without the overhead of multiple editor widgets, or requiring
168 documents to be stored in some intermediate format.
169
170 An empty document contains a root frame which itself contains a single
171 empty text block. Frames provide logical separation between parts of the document, but
172 also have properties that determine how they will appear when rendered.
173 A table is a specialized type of frame that consists of a number of
174 cells, arranged into rows and columns, each of which can contain
175 further structure and text. Tables provide management and layout
176 features that allow flexible configurations of cells to be created.
177
178 Text blocks contain text fragments, each of which specifies text and
179 character format information. Textual properties are defined both at
180 the character level and at the block level. At the character level,
181 properties such as font family, text color, and font weight can be
182 specified. The block level properties control the higher level
183 appearance and behavior of the text, such as the direction of text
184 flow, alignment, and background color.
185
186 The document structure is not manipulated directly. Editing is
187 performed through a cursor-based interface.
188 The \l{richtext-cursor.html}{text cursor interface}
189 automatically inserts new document elements into the root frame, and
190 ensures that it is padded with empty blocks where necessary.
191
192 We obtain the root frame in the following manner:
193
194 \snippet doc/src/snippets/textdocument-frames/xmlwriter.h 0
195 \snippet doc/src/snippets/textdocument-frames/xmlwriter.cpp 0
196
197 When navigating the document structure, it is useful to begin at the
198 root frame because it provides access to the entire document structure.
199
200
201 \section1 Document Elements
202
203 Rich text documents usually consist of common elements such as paragraphs,
204 frames, tables, and lists. These are represented in a QTextDocument
205 by the QTextBlock, QTextFrame, QTextTable, and QTextList classes.
206 Unlike the other elements in a document, images are represented by
207 specially formatted text fragments. This enables them to be placed
208 formatted inline with the surrounding text.
209
210 The basic structural building blocks in documents are QTextBlock and
211 QTextFrame. Blocks themselves contain fragments of rich text
212 (QTextFragment), but these do not directly influence the high level
213 structure of a document.
214
215 Elements which can group together other document elements are typically
216 subclasses of QTextObject, and fall into two categories: Elements that
217 group together text blocks are subclasses of QTextBlockGroup, and those
218 that group together frames and other elements are subclasses of QTextFrame.
219
220 \section2 Text Blocks
221
222 Text blocks are provided by the QTextBlock class.
223
224 Text blocks group together fragments of text with different character formats,
225 and are used to represent paragraphs in the document. Each block
226 typically contains a number of text fragments with different styles.
227 Fragments are created when text is inserted into the document, and more
228 of them are added when the document is edited. The document splits, merges,
229 and removes fragments to efficiently represent the different styles
230 of text in the block.
231
232 The fragments within a given block can be examined by using a
233 QTextBlock::iterator to traverse the block's internal structure:
234
235 \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 3
236 \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 5
237 \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 6
238
239 Blocks are also used to represent list items. As a result, blocks can
240 define their own character formats which contain information about
241 block-level decoration, such as the type of bullet points used for
242 list items. The formatting for the block itself is described by the
243 QTextBlockFormat class, and describes properties such as text alignment,
244 indentation, and background color.
245
246 Although a given document may contain complex structures, once we have a
247 reference to a valid block in the document, we can navigate between each
248 of the text blocks in the order in which they were written:
249
250 \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 0
251 \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 1
252 \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 2
253
254 This method is useful for when you want to extract just the rich text from a
255 document because it ignores frames, tables, and other types of structure.
256
257 QTextBlock provides comparison operators that make it easier to manipulate
258 blocks: \l{QTextBlock::operator==()}{operator==()} and
259 \l{QTextBlock::operator!=()}{operator!=()} are used to test whether two
260 blocks are the same, and \l{QTextBlock::operator<()}{operator<()} is used
261 to determine which one occurs first in a document.
262
263 \section2 Frames
264
265 Frames are provided by the QTextFrame class.
266
267 Text frames group together blocks of text and child frames, creating
268 document structures that are larger than paragraphs. The format of a frame
269 specifies how it is rendered and positioned on the page. Frames are
270 either inserted into the text flow, or they float on the left or right
271 hand side of the page.
272 Each document contains a root frame that contains all the other document
273 elements. As a result, all frames except the root frame have a parent
274 frame.
275
276 Since text blocks are used to separate other document elements, each
277 frame will always contain at least one text block, and zero or more
278 child frames. We can inspect the contents of a frame by using a
279 QTextFrame::iterator to traverse the frame's child elements:
280
281 \snippet doc/src/snippets/textdocument-frames/xmlwriter.cpp 1
282 \snippet doc/src/snippets/textdocument-frames/xmlwriter.cpp 2
283
284 Note that the iterator selects both frames and blocks, so it is necessary
285 to check which it is referring to. This allows us to navigate the document
286 structure on a frame-by-frame basis yet still access text blocks if
287 required. Both the QTextBlock::iterator and QTextFrame::iterator classes
288 can be used in complementary ways to extract the required structure from
289 a document.
290
291 \section2 Tables
292
293 Tables are provided by the QTextTable class.
294
295 Tables are collections of cells that are arranged in rows and columns.
296 Each table cell is a document element with its own character format, but it
297 can also contain other elements, such as frames and text blocks. Table cells
298 are automatically created when the table is constructed, or when extra rows
299 or columns are added. They can also be moved between tables.
300
301 QTextTable is a subclass of QTextFrame, so tables are treated like frames
302 in the document structure. For each frame that we encounter in the
303 document, we can test whether it represents a table, and deal with it in a
304 different way:
305
306 \snippet doc/src/snippets/textdocument-tables/xmlwriter.cpp 0
307 \snippet doc/src/snippets/textdocument-tables/xmlwriter.cpp 1
308
309 The cells within an existing table can be examined by iterating through
310 the rows and columns.
311
312 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 9
313 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 10
314 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 11
315 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 12
316
317
318 \section2 Lists
319
320 Lists are provided by the QTextList class.
321
322 Lists are sequences of text blocks that are formatted in the usual way, but
323 which also provide the standard list decorations such as bullet points and
324 enumerated items. Lists can be nested, and will be indented if the list's
325 format specifies a non-zero indentation.
326
327 We can refer to each list item by its index in the list:
328
329 \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 0
330 \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 1
331 \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 2
332
333 Since QTextList is a subclass of QTextBlockGroup, it does not group the
334 list items as child elements, but instead provides various functions for
335 managing them. This means that any text block we find when traversing a
336 document may actually be a list item. We can ensure that list items are
337 correctly identified by using the following code:
338
339 \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 3
340 \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 4
341 \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 5
342 \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 6
343 \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 7
344
345
346 \section2 Images
347
348 Images in QTextDocument are represented by text fragments that reference
349 external images via the resource mechanism. Images are created using the
350 cursor interface, and can be modified later by changing the character
351 format of the image's text fragment:
352
353 \snippet doc/src/snippets/textdocument-imageformat/main.cpp 0
354 \snippet doc/src/snippets/textdocument-imageformat/main.cpp 1
355 \snippet doc/src/snippets/textdocument-imageformat/main.cpp 2
356
357 The fragment that represents the image can be found by iterating over
358 the fragments in the text block that contains the image.
359*/
360
361/*!
362 \page richtext-cursor.html
363 \contentspage richtext.html Contents
364 \previouspage Rich Text Document Structure
365 \nextpage Document Layouts
366
367 \title The QTextCursor Interface
368
369 \tableofcontents
370
371 Documents can be edited via the interface provided by the QTextCursor
372 class; cursors are either created using a constructor or obtained from
373 an editor widget. The cursor is used to perform editing operations that
374 correspond exactly to those the user is able to make themselves in an
375 editor. As a result, information about the document structure is also
376 available through the cursor, and this allows the structure to be
377 modified. The use of a cursor-oriented interface for editing makes the
378 process of writing a custom editor simpler for developers, since the
379 editing operations can be easily visualized.
380
381 The QTextCursor class also maintains information about any text it
382 has selected in the document, again following a model that is
383 conceptually similar to the actions made by the user to select text
384 in an editor.
385
386 Rich text documents can have multiple cursors
387 associated with them, and each of these contains information about their
388 position in the document and any selections that they may hold. This
389 cursor-based paradigm makes common operations, such as cutting and pasting
390 text, simple to implement programmatically, yet it also allows more complex
391 editing operations to be performed on the document.
392
393 This chapter describes most of the common editing operations that you
394 will need to perform using a cursor, from basic insertion of text and
395 document elements to more complex manipulation of document structures.
396
397 \section1 Cursor-Based Editing
398
399 At the simplest level, text documents are made up of a string of characters,
400 marked up in some way to represent the block structure of the text within the
401 document. QTextCursor provides a cursor-based interface that allows the
402 contents of a QTextDocument to be manipulated at the character level. Since
403 the elements (blocks, frames, tables, etc.) are also encoded in the character
404 stream, the document structure can itself be changed by the cursor.
405
406 The cursor keeps track of its location within its parent document, and can
407 report information about the surrounding structure, such as the enclosing
408 text block, frame, table, or list. The formats of the enclosing structures
409 can also be directly obtained through the cursor.
410
411 \section2 Using a Cursor
412
413 The main use of a cursor is to insert or modify text within a block.
414 We can use a text editor's cursor to do this:
415
416 \snippet doc/src/snippets/textblock-formats/main.cpp 0
417
418 Alternatively, we can obtain a cursor directly from a document:
419
420 \snippet doc/src/snippets/textdocument-images/main.cpp 0
421
422 The cursor is positioned at the start of the document so that we can write
423 into the first (empty) block in the document.
424
425 \section2 Grouping Cursor Operations
426
427 A series of editing operations can be packaged together so that they can
428 be replayed, or undone together in a single action. This is achieved by
429 using the \c beginEditBlock() and \c endEditBlock() functions in the
430 following way, as in the following example where we select the word that
431 contains the cursor:
432
433 \snippet doc/src/snippets/textdocument-selections/mainwindow.cpp 0
434
435 If editing operations are not grouped, the document automatically records
436 the individual operations so that they can be undone later. Grouping
437 operations into larger packages can make editing more efficient both for
438 the user and for the application, but care has to be taken not to group too
439 many operations together as the user may want find-grained control over the
440 undo process.
441
442 \section2 Multiple Cursors
443
444 Multiple cursors can be used to simultaneously edit the same document,
445 although only one will be visible to the user in a QTextEdit widget.
446 The QTextDocument ensures that each cursor writes text correctly and
447 does not interfere with any of the others.
448
449 \omit
450 \snippet doc/src/snippets/textdocument-cursors/main.cpp 0
451 \snippet doc/src/snippets/textdocument-cursors/main.cpp 1
452 \endomit
453
454 \section1 Inserting Document Elements
455
456 QTextCursor provides several functions that can be used to change the
457 structure of a rich text document. Generally, these functions allow
458 document elements to be created with relevant formatting information,
459 and they are inserted into the document at the cursor's position.
460
461 The first group of functions insert block-level elements, and update the
462 cursor position, but they do not return the element that was inserted:
463
464 \list
465 \i \l{QTextCursor::insertBlock()}{insertBlock()} inserts a new text block
466 (paragraph) into a document at the cursor's position, and moves the
467 cursor to the start of the new block.
468 \i \l{QTextCursor::insertFragment()}{insertFragment()} inserts an existing
469 text fragment into a document at the cursor's position.
470 \i \l{QTextCursor::insertImage()}{insertImage()} inserts an image into a
471 document at the cursor's position.
472 \i \l{QTextCursor::insertText()}{insertText()} inserts text into the
473 document at the cursor's position.
474 \endlist
475
476 You can examine the contents of the element that was inserted through the
477 cursor interface.
478
479 The second group of functions insert elements that provide structure to
480 the document, and return the structure that was inserted:
481
482 \list
483 \i \l{QTextCursor::insertFrame()}{insertFrame()} inserts a frame into the
484 document \e after the cursor's current block, and moves the cursor to
485 the start of the empty block in the new frame.
486 \i \l{QTextCursor::insertList()}{insertList()} inserts a list into the
487 document at the cursor's position, and moves the cursor to the start
488 of the first item in the list.
489 \i \l{QTextCursor::insertTable()}{insertTable()} inserts a table into
490 the document \e after the cursor's current block, and moves the cursor
491 to the start of the block following the table.
492 \endlist
493
494 These elements either contain or group together other elements in the
495 document.
496
497 \section2 Text and Text Fragments
498
499 Text can be inserted into the current block in the current character
500 format, or in a custom format that is specified with the text:
501
502 \snippet doc/src/snippets/textdocument-charformats/main.cpp 0
503
504 Once the character format has been used with a cursor, that format becomes
505 the default format for any text inserted with that cursor until another
506 character format is specified.
507
508 If a cursor is used to insert text without specifying a character format,
509 the text will be given the character format used at that position in the
510 document.
511
512 \section2 Blocks
513
514 Text blocks are inserted into the document with the
515 \l{QTextCursor::insertBlock()}{insertBlock()} function.
516
517 \snippet doc/src/snippets/textblock-formats/main.cpp 1
518
519 The cursor is positioned at the start of the new block.
520
521 \section2 Frames
522
523 Frames are inserted into a document using the cursor, and will be placed
524 within the cursor's current frame \e after the current block.
525 The following code shows how a frame can be inserted between two text
526 blocks in a document's root frame. We begin by finding the cursor's
527 current frame:
528
529 \snippet doc/src/snippets/textdocument-frames/mainwindow.cpp 0
530
531 We insert some text in this frame then set up a frame format for the
532 child frame:
533
534 \snippet doc/src/snippets/textdocument-frames/mainwindow.cpp 1
535
536 The frame format will give the frame an external margin of 32 pixels,
537 internal padding of 8 pixels, and a border that is 4 pixels wide.
538 See the QTextFrameFormat documentation for more information about
539 frame formats.
540
541 The frame is inserted into the document after the preceding text:
542
543 \snippet doc/src/snippets/textdocument-frames/mainwindow.cpp 2
544
545 We add some text to the document immediately after we insert the frame.
546 Since the text cursor is positioned \e{inside the frame} when it is inserted
547 into the document, this text will also be inserted inside the frame.
548
549 Finally, we position the cursor outside the frame by taking the last
550 available cursor position inside the frame we recorded earlier:
551
552 \snippet doc/src/snippets/textdocument-frames/mainwindow.cpp 3
553
554 The text that we add last is inserted after the child frame in the
555 document. Since each frame is padded with text blocks, this ensures that
556 more elements can always be inserted with a cursor.
557
558 \section2 Tables
559
560 Tables are inserted into the document using the cursor, and will be
561 placed within the cursor's current frame \e after the current block:
562
563 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 0
564 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 3
565
566 Tables can be created with a specific format that defines the overall
567 properties of the table, such as its alignment, background color, and
568 the cell spacing used. It can also determine the constraints on each
569 column, allowing each of them to have a fixed width, or resize according
570 to the available space.
571
572 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 2
573
574 The columns in the table created above will each take up a certain
575 percentage of the available width. Note that the table format is
576 optional; if you insert a table without a format, some sensible
577 default values will be used for the table's properties.
578
579 Since cells can contain other document elements, they too can be
580 formatted and styled as necessary.
581
582 Text can be added to the table by navigating to each cell with the cursor
583 and inserting text.
584
585 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 4
586
587 We can create a simple timetable by following this approach:
588
589 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 5
590 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 6
591 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 7
592 \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 8
593
594 \section2 Lists
595
596 Lists of block elements can be automatically created and inserted into the
597 document at the current cursor position. Each list that is created in this
598 way requires a list format to be specified:
599
600 \snippet doc/src/snippets/textdocument-lists/mainwindow.cpp 0
601
602 The above code first checks whether the cursor is within an existing list
603 and, if so, gives the list format for the new list a suitable level of
604 indentation. This allows nested lists to be created with increasing
605 levels of indentation. A more sophisticated implementation would also use
606 different kinds of symbol for the bullet points in each level of the list.
607
608 \section2 Images
609
610 Inline images are added to documents through the cursor in the usual manner.
611 Unlike many other elements, all of the image properties are specified by the
612 image's format. This means that a QTextImageFormat object has to be
613 created before an image can be inserted:
614
615 \snippet doc/src/snippets/textdocument-images/main.cpp 1
616
617 The image name refers to an entry in the application's resource file.
618 The method used to derive this name is described in
619 \l{resources.html}{The Qt Resource System}.
620
621 \section1 Examples
622
623 Rich text is stored in text documents that can either be created by
624 importing HTML from an external source, or generated using a QTextCursor.
625
626 \section2 Manipulating Rich Text
627
628 The easiest way to use a rich text document is through
629 the QTextEdit class, providing an editable view onto a document. The code
630 below imports HTML into a document, and displays the document using a
631 text edit widget.
632
633 \snippet doc/src/snippets/scribe-overview/main.cpp 1
634
635 You can retrieve the document from the text edit using the
636 document() function. The document can then be edited programmatically
637 using the QTextCursor class. This class is modeled after a screen
638 cursor, and editing operations follow the same semantics. The following
639 code changes the first line of the document to a bold font, leaving all
640 other font properties untouched. The editor will be automatically
641 updated to reflect the changes made to the underlying document data.
642
643 \snippet doc/src/snippets/scribe-overview/main.cpp 0
644
645 Note that the cursor was moved from the start of the first line to the
646 end, but that it retained an anchor at the start of the line. This
647 demonstrates the cursor-based selection facilities of the
648 QTextCursor class.
649
650 \section2 Generating a Calendar
651
652 Rich text can be generated very quickly using the cursor-based
653 approach. The following example shows a simple calendar in a
654 QTextEdit widget with bold headers for the days of the week:
655
656 \snippet doc/src/snippets/textdocument-blocks/mainwindow.cpp 0
657 \codeline
658 \snippet doc/src/snippets/textdocument-blocks/mainwindow.cpp 1
659 \snippet doc/src/snippets/textdocument-blocks/mainwindow.cpp 2
660 \snippet doc/src/snippets/textdocument-blocks/mainwindow.cpp 3
661
662 The above example demonstrates how simple it is to quickly generate new
663 rich text documents using a minimum amount of code. Although we have
664 generated a crude fixed-pitch calendar to avoid quoting too much code,
665 Scribe provides much more sophisticated layout and formatting features.
666*/
667
668/*!
669 \page richtext-layouts.html
670 \contentspage richtext.html Contents
671 \previouspage The QTextCursor Interface
672 \nextpage Common Rich Text Editing Tasks
673
674 \title Document Layouts
675
676 \tableofcontents
677
678 The layout of a document is only relevant when it is to be displayed on
679 a device, or when some information is requested that requires a visual
680 representation of the document. Until this occurs, the document does
681 not need to be formatted and prepared for a device.
682
683 \section1 Overview
684
685 Each document's layout is managed by a subclass of the
686 QAbstractTextDocumentLayout class. This class provides a common
687 interface for layout and rendering engines. The default rendering
688 behavior is currently implemented in a private class. This approach
689 makes it possible to create custom layouts, and provides the
690 mechanism used when preparing pages for printing or exporting to
691 Portable Document Format (PDF) files.
692
693 \section1 Example - Shaped Text Layout
694
695 Sometimes it is important to be able to format plain text within an
696 irregularly-shaped region, perhaps when rendering a custom widget, for
697 example. Scribe provides generic features, such as those provided by
698 the QTextLayout class, to help developers perform word-wrapping and
699 layout tasks without the need to create a document first.
700
701 \img plaintext-layout.png
702
703 Formatting and drawing a paragraph of plain text is straightforward.
704 The example below will lay out a paragraph of text, using a single
705 font, around the right hand edge of a circle.
706
707 \snippet doc/src/snippets/plaintextlayout/window.cpp 0
708
709 We create a text layout, specifying the text string we want to display
710 and the font to use. We ensure that the text we supplied is formatted
711 correctly by obtaining text lines from the text format, and wrapping
712 the remaining text using the available space. The lines are positioned
713 as we move down the page.
714
715 The formatted text can be drawn onto a paint device; in the above code,
716 the text is drawn directly onto a widget.
717 */
718
719 /*!
720 \page richtext-common-tasks.html
721 \contentspage richtext.html Contents
722 \previouspage Document Layouts
723 \nextpage Advanced Rich Text Processing
724
725 \title Common Rich Text Editing Tasks
726
727 \tableofcontents
728
729 There are a number of tasks that are often performed by developers
730 when editing and processing text documents using Qt. These include the use
731 of display widgets such as QTextBrowser and QTextEdit, creation of
732 documents with QTextDocument, editing using a QTextCursor, and
733 exporting the document structure.
734 This document outlines some of the more common ways of using the rich
735 text classes to perform these tasks, showing convenient patterns that can
736 be reused in your own applications.
737
738 \section1 Using QTextEdit
739
740 A text editor widget can be constructed and used to display HTML in the
741 following way:
742
743 \snippet doc/src/snippets/code/doc_src_richtext.qdoc 2
744
745 By default, the text editor contains a document with a root frame, inside
746 which is an empty text block. This document can be obtained so that it can
747 be modified directly by the application:
748
749 \snippet doc/src/snippets/code/doc_src_richtext.qdoc 3
750
751 The text editor's cursor may also be used to edit a document:
752
753 \snippet doc/src/snippets/code/doc_src_richtext.qdoc 4
754
755 Although a document can be edited using many cursors at once, a QTextEdit
756 only displays a single cursor at a time. Therefore, if we want to update the
757 editor to display a particular cursor or its selection, we need to set the
758 editor's cursor after we have modified the document:
759
760 \snippet doc/src/snippets/code/doc_src_richtext.qdoc 5
761
762 \section1 Selecting Text
763
764 Text is selected by moving the cursor using operations that are similar to
765 those performed by a user in a text editor. To select text between two
766 points in the document, we need to position the cursor at the first point
767 then move it using a special mode (\l{QTextCursor::MoveMode}) with a
768 move operation (\l{QTextCursor::MoveOperation}).
769 When we select the text, we leave the selection anchor at the old cursor
770 position just as the user might do by holding down the Shift key when
771 selecting text:
772
773 \snippet doc/src/snippets/textdocument-selections/mainwindow.cpp 1
774
775 In the above code, a whole word is selected using this method. QTextCursor
776 provides a number of common move operations for selecting individual
777 characters, words, lines, and whole blocks.
778
779 \section1 Finding Text
780
781 QTextDocument provides a cursor-based interface for searching, making
782 it easy to find and modify text in the style of a text editor. The following
783 code finds all the instances of a particular word in a document, and changes
784 the color of each:
785
786 \snippet doc/src/snippets/textdocument-find/main.cpp 0
787 \snippet doc/src/snippets/textdocument-find/main.cpp 1
788
789 Note that the cursor does not have to be moved after each search and replace
790 operation; it is always positioned at the end of the word that was just
791 replaced.
792
793 \section1 Printing Documents
794
795 QTextEdit is designed for the display of large rich text documents that are
796 read on screen, rendering them in the same way as a web browser. As a result,
797 it does not automatically break the contents of the document into page-sized
798 pieces that are suitable for printing.
799
800 QTextDocument provides a \l{QTextDocument::print()}{print()} function to
801 allow documents to be printed using the QPrinter class. The following code
802 shows how to prepare a document in a QTextEdit for printing with a QPrinter:
803
804 \snippet doc/src/snippets/textdocument-printing/mainwindow.cpp 0
805
806 The document is obtained from the text editor, and a QPrinter is constructed
807 then configured using a QPrintDialog. If the user accepts the printer's
808 configuration then the document is formatted and printed using the
809 \l{QTextDocument::print()}{print()} function.
810*/
811
812/*!
813 \page richtext-advanced-processing.html
814 \contentspage richtext.html Contents
815 \previouspage Common Rich Text Editing Tasks
816 \nextpage Supported HTML Subset
817
818 \title Advanced Rich Text Processing
819
820 \section1 Handling Large Files
821
822 Qt does not limit the size of files that are used for text
823 processing. In most cases, this will not present a problem. For
824 especially large files, however, you might experience that your
825 application will become unresponsive or that you will run out of
826 memory. The size of the files you can load depends on your
827 hardware and on Qt's and your own application's implementation.
828
829 If you are faced with this problem, we recommend that you address the
830 following issues:
831
832 \list
833 \o You should consider breaking up large paragraphs into smaller
834 ones as Qt handles small paragraphs better. You could also
835 insert line breaks at regular intervals, which will look the
836 same as one large paragraph in a QTextEdit.
837 \o You can reduce the amount of blocks in a QTextDocument with
838 \l{QTextDocument::}{maximumBlockCount()}. The document is only
839 as large as the number of blocks as far as QTextEdit is concerned.
840 \o When adding text to a text edit, it is an advantage to add it
841 in an edit block (see example below). The result is that the
842 text edit does not need to build the entire document structure at once.
843 \endlist
844
845 We give an example of the latter technique from the list. We assume that
846 the text edit is visible.
847
848 \snippet doc/src/snippets/code/doc_src_richtext.qdoc 6
849
850 \omit
851 Ideas for other sections:
852
853 * Hiding QTextBlock elements.
854 * Changing the word wrapping mode in QTextEdit. Custom word wrapping?
855 \endomit
856*/
857
858/*!
859 \page richtext-html-subset.html
860 \title Supported HTML Subset
861 \brief Describes the support for HTML markup in text widgets.
862
863 \contentspage richtext.html Contents
864 \previouspage Common Rich Text Editing Tasks
865
866 Qt's text widgets are able to display rich text, specified using a subset of \l{HTML 4}
867 markup. Widgets that use QTextDocument, such as QLabel and QTextEdit, are able to display
868 rich text specified in this way.
869
870 \tableofcontents
871
872 \section1 Using HTML Markup in Text Widgets
873
874 Widgets automatically detect HTML markup and display rich text accordingly. For example,
875 setting a label's \l{QLabel::}{text} property with the string \c{"<b>Hello</b> <i>Qt!</i>"}
876 will result in the label displaying text like this: \bold{Hello} \e{Qt!}
877
878 When HTML markup is used for text, Qt follows the rules defined by the \l{HTML 4}
879 specification. This includes default properties for text layout, such as the
880 direction of the text flow (left-to-right) which can be changed by applying the
881 \l{#Block Attributes}{\c dir} attribute to blocks of text.
882
883 \section1 Supported Tags
884
885 The following table lists the HTML tags supported by Qt's
886 \l{Rich Text Processing}{rich text} engine:
887
888 \table
889 \header \o Tag
890 \o Description
891 \o Comment
892 \row \o \c a
893 \o Anchor or link
894 \o Supports the \c href and \c name attributes.
895 \row \o \c address
896 \o Address
897 \o
898 \row \o \c b
899 \o Bold
900 \o
901 \row \o \c big
902 \o Larger font
903 \o
904 \row \o \c blockquote
905 \o Indented paragraph
906 \o
907 \row \o \c body
908 \o Document body
909 \o Supports the \c bgcolor attribute, which
910 can be a Qt \l{QColor::setNamedColor()}{color name}
911 or a \c #RRGGBB color specification.
912 \row \o \c br
913 \o Line break
914 \o
915 \row \o \c center
916 \o Centered paragraph
917 \o
918 \row \o \c cite
919 \o Inline citation
920 \o Same as \c i.
921 \row \o \c code
922 \o Code
923 \o Same as \c tt.
924 \row \o \c dd
925 \o Definition data
926 \o
927 \row \o \c dfn
928 \o Definition
929 \o Same as \c i.
930 \row \o \c div
931 \o Document division
932 \o Supports the standard \l{block attributes}.
933 \row \o \c dl
934 \o Definition list
935 \o Supports the standard \l{block attributes}.
936 \row \o \c dt
937 \o Definition term
938 \o Supports the standard \l{block attributes}.
939 \row \o \c em
940 \o Emphasized
941 \o Same as \c i.
942 \row \o \c font
943 \o Font size, family, and/or color
944 \o Supports the following attributes:
945 \c size, \c face, and \c color (Qt
946 \l{QColor::setNamedColor()}{color names} or
947 \c #RRGGBB).
948 \row \o \c h1
949 \o Level 1 heading
950 \o Supports the standard \l{block attributes}.
951 \row \o \c h2
952 \o Level 2 heading
953 \o Supports the standard \l{block attributes}.
954 \row \o \c h3
955 \o Level 3 heading
956 \o Supports the standard \l{block attributes}.
957 \row \o \c h4
958 \o Level 4 heading
959 \o Supports the standard \l{block attributes}.
960 \row \o \c h5
961 \o Level 5 heading
962 \o Supports the standard \l{block attributes}.
963 \row \o \c h6
964 \o Level 6 heading
965 \o Supports the standard \l{block attributes}.
966 \row \o \c head
967 \o Document header
968 \o
969 \row \o \c hr
970 \o Horizontal line
971 \o Supports the \c width attribute, which can
972 be specified as an absolute or relative (\c %) value.
973 \row \o \c html
974 \o HTML document
975 \o
976 \row \o \c i
977 \o Italic
978 \o
979 \row \o \c img
980 \o Image
981 \o Supports the \c src, \c source
982 (for Qt 3 compatibility), \c width, and \c height
983 attributes.
984 \row \o \c kbd
985 \o User-entered text
986 \o
987 \row \o \c meta
988 \o Meta-information
989 \o If a text encoding is specified using the \c{meta} tag,
990 it is picked up by Qt::codecForHtml().
991 Likewise, if an encoding is specified to
992 QTextDocument::toHtml(), the encoding is stored using
993 a \c meta tag, for example:
994
995 \snippet doc/src/snippets/code/doc_src_richtext.qdoc 7
996
997 \row \o \c li
998 \o List item
999 \o
1000 \row \o \c nobr
1001 \o Non-breakable text
1002 \o
1003 \row \o \c ol
1004 \o Ordered list
1005 \o Supports the standard \l{list attributes}.
1006 \row \o \c p
1007 \o Paragraph
1008 \o Left-aligned by default. Supports the standard
1009 \l{block attributes}.
1010 \row \o \c pre
1011 \o Preformated text
1012 \o
1013 \row \o \c qt
1014 \o Qt rich-text document
1015 \o Synonym for \c html. Provided for compatibility with
1016 earlier versions of Qt.
1017 \row \o \c s
1018 \o Strikethrough
1019 \o
1020 \row \o \c samp
1021 \o Sample code
1022 \o Same as \c tt.
1023 \row \o \c small
1024 \o Small font
1025 \o
1026 \row \o \c span
1027 \o Grouped elements
1028 \o
1029 \row \o \c strong
1030 \o Strong
1031 \o Same as \c b.
1032 \row \o \c sub
1033 \o Subscript
1034 \o
1035 \row \o \c sup
1036 \o Superscript
1037 \o
1038 \row \o \c table
1039 \o Table
1040 \o Supports the following attributes: \c border,
1041 \c bgcolor (Qt \l{QColor::setNamedColor()}{color names}
1042 or \c #RRGGBB), \c cellspacing, \c cellpadding,
1043 \c width (absolute or relative), and \c height.
1044 \row \o \c tbody
1045 \o Table body
1046 \o Does nothing.
1047 \row \o \c td
1048 \o Table data cell
1049 \o Supports the standard \l{table cell attributes}.
1050 \row \o \c tfoot
1051 \o Table footer
1052 \o Does nothing.
1053 \row \o \c th
1054 \o Table header cell
1055 \o Supports the standard \l{table cell attributes}.
1056 \row \o \c thead
1057 \o Table header
1058 \o If the \c thead tag is specified, it is used when printing tables
1059 that span multiple pages.
1060 \row \o \c title
1061 \o Document title
1062 \o The value specified using the \c
1063 title tag is available through
1064 QTextDocument::metaInformation().
1065 \row \o \c tr
1066 \o Table row
1067 \o Supports the \c bgcolor attribute, which
1068 can be a Qt \l{QColor::setNamedColor()}{color name}
1069 or a \c #RRGGBB color specification.
1070 \row \o \c tt
1071 \o Typewrite font
1072 \o
1073 \row \o \c u
1074 \o Underlined
1075 \o
1076 \row \o \c ul
1077 \o Unordered list
1078 \o Supports the standard \l{list attributes}.
1079 \row \o \c var
1080 \o Variable
1081 \o Same as \c i.
1082 \endtable
1083
1084 \section1 Block Attributes
1085
1086 The following attributes are supported by the \c div, \c dl, \c
1087 dt, \c h1, \c h2, \c h3, \c h4, \c h5, \c h6, \c p tags:
1088
1089 \list
1090 \o \c align (\c left, \c right, \c center, \c justify)
1091 \o \c dir (\c ltr, \c rtl)
1092 \endlist
1093
1094 \section1 List Attributes
1095
1096 The following attribute is supported by the \c ol and \c ul tags:
1097
1098 \list
1099 \o \c type (\c 1, \c a, \c A, \c square, \c disc, \c circle)
1100 \endlist
1101
1102 \section1 Table Cell Attributes
1103
1104 The following attributes are supported by the \c td and \c th
1105 tags:
1106
1107 \list
1108 \o \c width (absolute, relative, or no-value)
1109 \o \c bgcolor (Qt \l{QColor::setNamedColor()}{color names} or \c #RRGGBB)
1110 \o \c colspan
1111 \o \c rowspan
1112 \o \c align (\c left, \c right, \c center, \c justify)
1113 \o \c valign (\c top, \c middle, \c bottom)
1114 \endlist
1115
1116 \section1 CSS Properties
1117 The following table lists the CSS properties supported by Qt's
1118 \l{Rich Text Processing}{rich text} engine:
1119
1120 \table
1121 \header \o Property
1122 \o Values
1123 \o Description
1124 \row
1125 \o \c background-color
1126 \o <color>
1127 \o Background color for elements
1128 \row
1129 \o \c background-image
1130 \o <uri>
1131 \o Background image for elements
1132 \row \o \c color
1133 \o <color>
1134 \o Text foreground color
1135 \row \o \c font-family
1136 \o <family name>
1137 \o Font family name
1138 \row \o \c font-size
1139 \o [ small | medium | large | x-large | xx-large ] | <size>pt | <size>px
1140 \o Font size relative to the document font, or specified in points or pixels
1141 \row \o \c font-style
1142 \o [ normal | italic | oblique ]
1143 \o
1144 \row \o \c font-weight
1145 \o [ normal | bold | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 ]
1146 \o Specifies the font weight used for text, where \c normal and \c bold
1147 are mapped to the corresponding QFont weights. Numeric values are
1148 8 times the equivalent QFont weight values.
1149 \row \o \c text-decoration
1150 \o none | [ underline || overline || line-through ]
1151 \o Additional text effects
1152 \row \o \c font
1153 \o [ [ <'font-style'> || <'font-weight'> ]? <'font-size'> <'font-family'> ]
1154 \o Font shorthand property
1155 \row \o \c text-indent
1156 \o <length>px
1157 \o First line text indentation in pixels
1158 \row \o \c white-space
1159 \o normal | pre | nowrap | pre-wrap
1160 \o Declares how whitespace in HTML is handled.
1161 \row \o \c margin-top
1162 \o <length>px
1163 \o Top paragraph margin in pixels
1164 \row \o \c margin-bottom
1165 \o <length>px
1166 \o Bottom paragraph margin in pixels
1167 \row \o \c margin-left
1168 \o <length>px
1169 \o Left paragraph margin in pixels
1170 \row \o \c margin-right
1171 \o <length>px
1172 \o Right paragraph margin in pixels
1173 \row \o \c padding-top
1174 \o <length>px
1175 \o Top table cell padding in pixels
1176 \row \o \c padding-bottom
1177 \o <length>px
1178 \o Bottom table cell padding in pixels
1179 \row \o \c padding-left
1180 \o <length>px
1181 \o Left table cell padding in pixels
1182 \row \o \c padding-right
1183 \o <length>px
1184 \o Right table cell padding in pixels
1185 \row \o \c padding
1186 \o <length>px
1187 \o Shorthand for setting all the padding properties at once.
1188 \row \o \c vertical-align
1189 \o baseline | sub | super | middle | top | bottom
1190 \o Vertical text alignment. For vertical alignment in text table cells only middle, top, and bottom apply.
1191 \row \o \c border-color
1192 \o <color>
1193 \o Border color for text tables.
1194 \row \o \c border-style
1195 \o none | dotted | dashed | dot-dash | dot-dot-dash | solid | double | groove | ridge | inset | outset
1196 \o Border style for text tables.
1197 \row \o \c background
1198 \o [ <'background-color'> || <'background-image'> ]
1199 \o Background shorthand property
1200 \row \o \c page-break-before
1201 \o [ auto | always ]
1202 \o Make it possible to enforce a page break before the paragraph/table
1203 \row \o \c page-break-after
1204 \o [ auto | always ]
1205 \o Make it possible to enforce a page break after the paragraph/table
1206 \row \o float
1207 \o [ left | right | none ]
1208 \o Specifies where an image or a text will be placed in another element. Note that the \c float property is
1209 only supported for tables and images.
1210 \row \o \c text-transform
1211 \o [ uppercase | lowercase ]
1212 \o Select the transformation that will be performed on the text prior to displaying it.
1213 \row \o \c font-variant
1214 \o small-caps
1215 \o Perform the smallcaps transformation on the text prior to displaying it.
1216 \row \o \c word-spacing
1217 \o <width>px
1218 \o Specifies an alternate spacing between each word.
1219 \endtable
1220
1221 \section1 Supported CSS Selectors
1222
1223 All CSS 2.1 selector classes are supported except pseudo-class selectors such
1224 as \c{:first-child}, \c{:visited} and \c{:hover}.
1225
1226*/
Note: See TracBrowser for help on using the repository browser.