| [556] | 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| [846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| [556] | 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| [846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
|---|
| [556] | 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
|
|---|
| [846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
|---|
| 14 | ** written agreement between you and Nokia.
|
|---|
| [556] | 15 | **
|
|---|
| [846] | 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.
|
|---|
| [556] | 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 | \page qt4-scribe.html
|
|---|
| 30 | \title The Scribe Classes
|
|---|
| 31 |
|
|---|
| 32 | \contentspage {What's New in Qt 4}{Home}
|
|---|
| 33 | \previouspage The Arthur Paint System
|
|---|
| 34 | \nextpage The Qt 4 Main Window Classes
|
|---|
| 35 |
|
|---|
| 36 | \keyword Scribe
|
|---|
| 37 |
|
|---|
| 38 | Scribe introduces a set of text layout classes to Qt 4. These classes
|
|---|
| 39 | replace the old rich text engine found in Qt 3, and provide new features
|
|---|
| 40 | for processing and laying out both plain and rich text.
|
|---|
| 41 |
|
|---|
| 42 | \tableofcontents
|
|---|
| 43 |
|
|---|
| 44 | For more details about how to use the Scribe classes, see the
|
|---|
| 45 | \l{richtext.html}{Rich Text Processing} document.
|
|---|
| 46 |
|
|---|
| 47 | \section1 Overview of Scribe
|
|---|
| 48 |
|
|---|
| 49 | Support for text rendering and layout in Qt 4 has been redesigned
|
|---|
| 50 | around a system that allows textual content to be represented in a more
|
|---|
| 51 | flexible way than was possible with Qt 3. Qt 4 also provides a more
|
|---|
| 52 | convenient programming interface for editing documents. These
|
|---|
| 53 | improvements are made available through a reimplementation of the
|
|---|
| 54 | existing text rendering engine, and the introduction of several new
|
|---|
| 55 | classes.
|
|---|
| 56 |
|
|---|
| 57 | The following sections provide a brief overview of the main concepts
|
|---|
| 58 | behind Scribe.
|
|---|
| 59 |
|
|---|
| 60 | \section2 The Document Interface
|
|---|
| 61 |
|
|---|
| 62 | Text documents are represented by the QTextDocument class, rather
|
|---|
| 63 | than by QString objects. Each QTextDocument object contains
|
|---|
| 64 | information about the document's internal representation, its
|
|---|
| 65 | structure, and keeps track of modifications to provide undo/redo
|
|---|
| 66 | facilities.
|
|---|
| 67 | This approach allows features such as layout management to be
|
|---|
| 68 | delegated to specialized classes, but also provides a focus for the
|
|---|
| 69 | framework.
|
|---|
| 70 |
|
|---|
| 71 | Documents are either converted from external sources or created from
|
|---|
| 72 | scratch using Qt. The creation process can done by an editor widget,
|
|---|
| 73 | such as QTextEdit, or by explicit calls to the Scribe API.
|
|---|
| 74 |
|
|---|
| 75 | Text documents can be accessed in two complementary ways: as a linear
|
|---|
| 76 | buffer for editors to use, and as an object hierarchy that is useful to
|
|---|
| 77 | layout engines.
|
|---|
| 78 | In the hierarchical document model, objects generally correspond to
|
|---|
| 79 | visual elements such as frames, tables, and lists. At a lower level,
|
|---|
| 80 | these elements describe properties such as the text style and alignment.
|
|---|
| 81 | The linear representation of the document is used for editing and
|
|---|
| 82 | manipulation of the document's contents.
|
|---|
| 83 |
|
|---|
| 84 | \section2 Document Structure
|
|---|
| 85 |
|
|---|
| 86 | Each document contains a root frame into which all other structural
|
|---|
| 87 | elements are placed. This frame contains other structural elements,
|
|---|
| 88 | including tables, text blocks, and other frames; these can be nested to
|
|---|
| 89 | an arbitrary depth.
|
|---|
| 90 |
|
|---|
| 91 | Frames provide logical separation between parts of the document, but
|
|---|
| 92 | also have properties that determine how they will appear when rendered.
|
|---|
| 93 | A table is a specialized type of frame that consists of a number of
|
|---|
| 94 | cells, arranged into rows and columns, each of which can contain
|
|---|
| 95 | further structure and text. Tables provide management and layout
|
|---|
| 96 | features that allow flexible configurations of cells to be created.
|
|---|
| 97 |
|
|---|
| 98 | Text blocks contain text fragments, each of which specifies text and
|
|---|
| 99 | character format information. Textual properties are defined both at
|
|---|
| 100 | the character level and at the block level. At the character level,
|
|---|
| 101 | properties such as font family, text color, and font weight can be
|
|---|
| 102 | specified. The block level properties control the higher level
|
|---|
| 103 | appearance and behavior of the text, such as the direction of text
|
|---|
| 104 | flow, alignment, and background color.
|
|---|
| 105 |
|
|---|
| 106 | The document structure is not manipulated directly. Editing is
|
|---|
| 107 | performed through a cursor-based interface.
|
|---|
| 108 |
|
|---|
| 109 | \section2 Editing and Content Creation
|
|---|
| 110 |
|
|---|
| 111 | Documents can be edited via the interface provided by the QTextCursor
|
|---|
| 112 | class; cursors are either created using a constructor or obtained from
|
|---|
| 113 | an editor widget. The cursor is used to perform editing operations that
|
|---|
| 114 | correspond exactly to those the user is able to make themselves in an
|
|---|
| 115 | editor. As a result, information about the document structure is also
|
|---|
| 116 | available through the cursor, and this allows the structure to be
|
|---|
| 117 | modified. The use of a cursor-oriented interface for editing makes the
|
|---|
| 118 | process of writing a custom editor simpler for developers, since the
|
|---|
| 119 | editing operations can be easily visualized.
|
|---|
| 120 |
|
|---|
| 121 | The QTextCursor class also maintains information about any text it
|
|---|
| 122 | has selected in the document, again following a model that is
|
|---|
| 123 | conceptually similar to the actions made by the user to select text
|
|---|
| 124 | in an editor.
|
|---|
| 125 |
|
|---|
| 126 | \section2 Document Layout
|
|---|
| 127 |
|
|---|
| 128 | The layout of a document is only relevant when it is to be displayed on
|
|---|
| 129 | a device, or when some information is requested that requires a visual
|
|---|
| 130 | representation of the document. Until this occurs, the document does
|
|---|
| 131 | not need to be formatted and prepared for a device.
|
|---|
| 132 |
|
|---|
| 133 | Each document's layout is managed by a subclass of the
|
|---|
| 134 | QAbstractTextDocumentLayout class. This class provides a common
|
|---|
| 135 | interface for layout and rendering engines. The default rendering
|
|---|
| 136 | behavior is currently implemented in a private class. This approach
|
|---|
| 137 | makes it possible to create custom layouts, and provides the
|
|---|
| 138 | mechanism used when preparing pages for printing or exporting to
|
|---|
| 139 | Portable Document Format (PDF) files.
|
|---|
| 140 |
|
|---|
| 141 | \section1 Example Code
|
|---|
| 142 |
|
|---|
| 143 | Here we present two different ways in which the Scribe classes can be
|
|---|
| 144 | used: for creating and manipulating rich text, and for laying out
|
|---|
| 145 | plain text.
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 | \section2 Manipulating Rich Text
|
|---|
| 149 |
|
|---|
| 150 | Rich text is stored in text documents that can either be created by
|
|---|
| 151 | importing HTML from an external source, or generated using a
|
|---|
| 152 | QTextCursor. The easiest way to use a rich text document is through
|
|---|
| 153 | the QTextEdit class, providing an editable view onto a document. The code
|
|---|
| 154 | below imports HTML into a document, and displays the document using a
|
|---|
| 155 | text edit widget.
|
|---|
| 156 |
|
|---|
| 157 | \snippet doc/src/snippets/scribe-overview/main.cpp 1
|
|---|
| 158 |
|
|---|
| 159 | You can retrieve the document from the text edit using the
|
|---|
| 160 | document() function. The document can then be edited programmatically
|
|---|
| 161 | using the QTextCursor class. This class is modeled after a screen
|
|---|
| 162 | cursor, and editing operations follow the same semantics. The following
|
|---|
| 163 | code changes the first line of the document to a bold font, leaving all
|
|---|
| 164 | other font properties untouched. The editor will be automatically
|
|---|
| 165 | updated to reflect the changes made to the underlying document data.
|
|---|
| 166 |
|
|---|
| 167 | \snippet doc/src/snippets/scribe-overview/main.cpp 0
|
|---|
| 168 |
|
|---|
| 169 | Note that the cursor was moved from the start of the first line to the
|
|---|
| 170 | end, but that it retained an anchor at the start of the line. This
|
|---|
| 171 | demonstrates the cursor-based selection facilities of the
|
|---|
| 172 | QTextCursor class.
|
|---|
| 173 |
|
|---|
| 174 | Rich text can be generated very quickly using the cursor-based
|
|---|
| 175 | approach. The following example shows a simple calendar in a
|
|---|
| 176 | QTextEdit widget with bold headers for the days of the week:
|
|---|
| 177 |
|
|---|
| 178 | \snippet doc/src/snippets/textdocument-blocks/mainwindow.cpp 0
|
|---|
| 179 | \codeline
|
|---|
| 180 | \snippet doc/src/snippets/textdocument-blocks/mainwindow.cpp 1
|
|---|
| 181 | \snippet doc/src/snippets/textdocument-blocks/mainwindow.cpp 2
|
|---|
| 182 | \snippet doc/src/snippets/textdocument-blocks/mainwindow.cpp 3
|
|---|
| 183 |
|
|---|
| 184 | The above example demonstrates how simple it is to quickly generate new
|
|---|
| 185 | rich text documents using a minimum amount of code. Although we have
|
|---|
| 186 | generated a crude fixed-pitch calendar to avoid quoting too much code,
|
|---|
| 187 | Scribe provides much more sophisticated layout and formatting features.
|
|---|
| 188 |
|
|---|
| 189 | \section2 Plain Text Layout
|
|---|
| 190 |
|
|---|
| 191 | Sometimes it is important to be able to format plain text within an
|
|---|
| 192 | irregularly-shaped region, perhaps when rendering a custom widget, for
|
|---|
| 193 | example. Scribe provides generic features, such as those provided by
|
|---|
| 194 | the QTextLayout class, to help developers perform word-wrapping and
|
|---|
| 195 | layout tasks without the need to create a document first.
|
|---|
| 196 |
|
|---|
| 197 | \img plaintext-layout.png
|
|---|
| 198 |
|
|---|
| 199 | Formatting and drawing a paragraph of plain text is straightforward.
|
|---|
| 200 | The example below will lay out a paragraph of text, using a single
|
|---|
| 201 | font, around the right hand edge of a circle.
|
|---|
| 202 |
|
|---|
| 203 | \snippet doc/src/snippets/plaintextlayout/window.cpp 0
|
|---|
| 204 |
|
|---|
| 205 | We create a text layout, specifying the text string we want to display
|
|---|
| 206 | and the font to use. We ensure that the text we supplied is formatted
|
|---|
| 207 | correctly by obtaining text lines from the text format, and wrapping
|
|---|
| 208 | the remaining text using the available space. The lines are positioned
|
|---|
| 209 | as we move down the page.
|
|---|
| 210 |
|
|---|
| 211 | The formatted text can be drawn onto a paint device; in the above code,
|
|---|
| 212 | the text is drawn directly onto a widget.
|
|---|
| 213 |
|
|---|
| 214 | \section2 Printing Features
|
|---|
| 215 |
|
|---|
| 216 | The layout system used to display rich text documents also supports
|
|---|
| 217 | paged layout of documents, and this is used by Qt to generate output for
|
|---|
| 218 | printing. The printing process is performed by QPrinter and controlled by
|
|---|
| 219 | the user via options displayed in a QPrintDialog:
|
|---|
| 220 |
|
|---|
| 221 | \snippet doc/src/snippets/textdocument-printing/mainwindow.cpp 0
|
|---|
| 222 |
|
|---|
| 223 | Rich text documents can also be exported as PDF files using QPrinter and
|
|---|
| 224 | the appropriate print engine:
|
|---|
| 225 |
|
|---|
| 226 | \snippet demos/textedit/textedit.cpp 0
|
|---|
| 227 |
|
|---|
| 228 | \section1 Comparison with Qt 3
|
|---|
| 229 |
|
|---|
| 230 | The cursor-based editing features, combined with the structural document
|
|---|
| 231 | model, provide a powerful set of tools for manipulating and displaying
|
|---|
| 232 | rich text documents. These provide features that were unavailable in
|
|---|
| 233 | Qt 3's public API. The engine used is a complete rewrite and does not
|
|---|
| 234 | use the rich text engine supplied with Qt 3.
|
|---|
| 235 |
|
|---|
| 236 | The QTextEdit class in Qt 4 has also been completely rewritten with an
|
|---|
| 237 | API that is quite different from its Qt 3 counterpart. Some compatibility
|
|---|
| 238 | methods have been added to allow the widget to be used, for basic cases,
|
|---|
| 239 | in a way that is familiar to users of Qt 3. This class is provided as a
|
|---|
| 240 | working example of an editor widget that uses the new API, showing that
|
|---|
| 241 | it is possible to completely implement a document editor based on the
|
|---|
| 242 | QTextCursor editing interface.
|
|---|
| 243 | */
|
|---|