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

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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