1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
29 \example richtext/orderform
30 \title Order Form Example
32 \brief The Order Form example shows how to generate rich text documents by
33 combining a simple template with data input by the user in a dialog. Data
34 is extracted from a \c DetailsDialog object and displayed on a QTextEdit
35 with a QTextCursor, using various formats. Each form generated is added
36 to a QTabWidget for easy access.
38 \image orderform-example.png
40 \section1 DetailsDialog Definition
42 The \c DetailsDialog class is a subclass of QDialog, implementing a slot
43 \c verify() to allow contents of the \c DetailsDialog to be verified later.
44 This is further explained in \c DetailsDialog Implementation.
46 \snippet richtext/orderform/detailsdialog.h 0
48 The constructor of \c DetailsDialog accepts parameters \a title and
49 \a parent. The class defines four \e{getter} functions: \c orderItems(),
50 \c senderName(), \c senderAddress(), and \c sendOffers() to allow data
51 to be accessed externally.
53 The class definition includes input widgets for the required
54 fields, \c nameEdit and \c addressEdit. Also, a QCheckBox and a
55 QDialogButtonBox are defined; the former to provide the user with the
56 option to receive information on products and offers, and the latter
57 to ensure that buttons used are arranged according to the user's native
58 platform. In addition, a QTableWidget, \c itemsTable, is used to hold
61 The screenshot below shows the \c DetailsDialog we intend to create.
63 \image orderform-example-detailsdialog.png
65 \section1 DetailsDialog Implementation
67 The constructor of \c DetailsDialog instantiates the earlier defined fields
68 and their respective labels. The label for \c offersCheckBox is set and the
69 \c setupItemsTable() function is invoked to setup and populate
70 \c itemsTable. The QDialogButtonBox object, \c buttonBox, is instantiated
71 with \uicontrol OK and \uicontrol Cancel buttons. This \c buttonBox's \c accepted() and
72 \c rejected() signals are connected to the \c verify() and \c reject()
73 slots in \c DetailsDialog.
75 \snippet richtext/orderform/detailsdialog.cpp 0
77 A QGridLayout is used to place all the objects on the \c DetailsDialog.
79 \snippet richtext/orderform/detailsdialog.cpp 1
81 The \c setupItemsTable() function instantiates the QTableWidget object,
82 \c itemsTable, and sets the number of rows based on the QStringList
83 object, \c items, which holds the type of items ordered. The number of
84 columns is set to 2, providing a "name" and "quantity" layout. A \c for
85 loop is used to populate the \c itemsTable and the \c name item's flag
86 is set to Qt::ItemIsEnabled or Qt::ItemIsSelectable. For demonstration
87 purposes, the \c quantity item is set to a 1 and all items in the
88 \c itemsTable have this value for quantity; but this can be modified by
89 editing the contents of the cells at run time.
91 \snippet richtext/orderform/detailsdialog.cpp 2
93 The \c orderItems() function extracts data from the \c itemsTable and
94 returns it in the form of a QList<QPair<QString,int>> where each QPair
95 corresponds to an item and the quantity ordered.
97 \snippet richtext/orderform/detailsdialog.cpp 3
99 The \c senderName() function is used to return the value of the QLineEdit
100 used to store the name field for the order form.
102 \snippet richtext/orderform/detailsdialog.cpp 4
104 The \c senderAddress() function is used to return the value of the
105 QTextEdit containing the address for the order form.
107 \snippet richtext/orderform/detailsdialog.cpp 5
109 The \c sendOffers() function is used to return a \c true or \c false
110 value that is used to determine if the customer in the order form
111 wishes to receive more information on the company's offers and promotions.
113 \snippet richtext/orderform/detailsdialog.cpp 6
115 The \c verify() function is an additionally implemented slot used to
116 verify the details entered by the user into the \c DetailsDialog. If
117 the details entered are incomplete, a QMessageBox is displayed
118 providing the user the option to discard the \c DetailsDialog. Otherwise,
119 the details are accepted and the \c accept() function is invoked.
121 \snippet richtext/orderform/detailsdialog.cpp 7
123 \section1 MainWindow Definition
125 The \c MainWindow class is a subclass of QMainWindow, implementing two
126 slots - \c openDialog() and \c printFile(). It also contains a private
127 instance of QTabWidget, \c letters.
129 \snippet richtext/orderform/mainwindow.h 0
131 \section1 MainWindow Implementation
133 The \c MainWindow constructor sets up the \c fileMenu and the required
134 actions, \c newAction and \c printAction. These actions' \c triggered()
135 signals are connected to the additionally implemented openDialog() slot
136 and the default close() slot. The QTabWidget, \c letters, is
137 instantiated and set as the window's central widget.
139 \snippet richtext/orderform/mainwindow.cpp 0
141 The \c createLetter() function creates a new QTabWidget with a QTextEdit,
142 \c editor, as the parent. This function accepts four parameters that
143 correspond to we obtained through \c DetailsDialog, in order to "fill"
146 \snippet richtext/orderform/mainwindow.cpp 1
148 We then obtain the cursor for the \c editor using QTextEdit::textCursor().
149 The \c cursor is then moved to the start of the document using
152 \snippet richtext/orderform/mainwindow.cpp 2
154 Recall the structure of a \l{Rich Text Document Structure}
155 {Rich Text Document}, where sequences of frames and
156 tables are always separated by text blocks, some of which may contain no
159 In the case of the Order Form Example, the document structure for this portion
160 is described by the table below:
164 \li {1, 8} frame with \e{referenceFrameFormat}
166 \li block \li \c{A company}
170 \li block \li \c{321 City Street}
174 \li block \li \c{Industry Park}
178 \li block \li \c{Another country}
181 This is accomplished with the following code:
183 \snippet richtext/orderform/mainwindow.cpp 3
185 Note that \c topFrame is the \c {editor}'s top-level frame and is not shown
186 in the document structure.
188 We then set the \c{cursor}'s position back to its last position in
189 \c topFrame and fill in the customer's name (provided by the constructor)
190 and address - using a \c foreach loop to traverse the QString, \c address.
192 \snippet richtext/orderform/mainwindow.cpp 4
194 The \c cursor is now back in \c topFrame and the document structure for
195 the above portion of code is:
199 \li block \li \c{Donald}
201 \li block \li \c{47338 Park Avenue}
203 \li block \li \c{Big City}
206 For spacing purposes, we invoke \l{QTextCursor::insertBlock()}
207 {insertBlock()} twice. The \l{QDate::currentDate()}{currentDate()} is
208 obtained and displayed. We use \l{QTextFrameFormat::setWidth()}
209 {setWidth()} to increase the width of \c bodyFrameFormat and we insert
210 a new frame with that width.
212 \snippet richtext/orderform/mainwindow.cpp 5
214 The following code inserts standard text into the order form.
216 \snippet richtext/orderform/mainwindow.cpp 6
217 \snippet richtext/orderform/mainwindow.cpp 7
219 This part of the document structure now contains the date, a frame with
220 \c bodyFrameFormat, as well as the standard text.
228 \li block \li \c{Date: 25 May 2007}
232 \li {1, 4} frame with \e{bodyFrameFormat}
234 \li block \li \c{I would like to place an order for the following items:}
241 A QTextTableFormat object, \c orderTableFormat, is used to hold the type
242 of item and the quantity ordered.
244 \snippet richtext/orderform/mainwindow.cpp 8
246 We use \l{QTextTable::cellAt()}{cellAt()} to set the headers for the
249 \snippet richtext/orderform/mainwindow.cpp 9
251 Then, we iterate through the QList of QPair objects to populate
254 \snippet richtext/orderform/mainwindow.cpp 10
256 The resulting document structure for this section is:
260 \li {1, 11} \c{orderTable} with \e{orderTableFormat}
262 \li block \li \c{Product}
264 \li block \li \c{Quantity}
266 \li block \li \c{T-shirt}
270 \li block \li \c{Badge}
274 \li block \li \c{Reference book}
278 \li block \li \c{Coffee cup}
283 The \c cursor is then moved back to \c{topFrame}'s
284 \l{QTextFrame::lastPosition()}{lastPosition()} and more standard text
287 \snippet richtext/orderform/mainwindow.cpp 11
288 \snippet richtext/orderform/mainwindow.cpp 12
290 Another QTextTable is inserted, to display the customer's
291 preference regarding offers.
293 \snippet richtext/orderform/mainwindow.cpp 13
295 The document structure for this portion is:
301 \li block\li \c{Please update my...}
305 \li {1, 4} \c{offersTable}
307 \li block \li \c{I want to receive...}
309 \li block \li \c{I do not want to receive...}
314 The \c cursor is moved to insert "Sincerely" along with the customer's
315 name. More blocks are inserted for spacing purposes. The \c printAction
316 is enabled to indicate that an order form can now be printed.
318 \snippet richtext/orderform/mainwindow.cpp 14
320 The bottom portion of the document structure is:
326 \li {1, 5} block\li \c{Sincerely,}
334 \li block \li \c{Donald}
337 The \c createSample() function is used for illustration purposes, to create
340 \snippet richtext/orderform/mainwindow.cpp 15
342 The \c openDialog() function opens a \c DetailsDialog object. If the
343 details in \c dialog are accepted, the \c createLetter() function is
344 invoked using the parameters extracted from \c dialog.
346 \snippet richtext/orderform/mainwindow.cpp 16
348 In order to print out the order form, a \c printFile() function is
349 included, as shown below:
351 \snippet richtext/orderform/mainwindow.cpp 17
353 This function also allows the user to print a selected area with
354 QTextCursor::hasSelection(), instead of printing the entire document.
356 \section1 \c main() Function
358 The \c main() function instantiates \c MainWindow and sets its size to
359 640x480 pixels before invoking the \c show() function and
360 \c createSample() function.
362 \snippet richtext/orderform/main.cpp 0