doc/src/examples/orderform.qdoc
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 /*!
       
    43     \example richtext/orderform
       
    44     \title Order Form Example
       
    45 
       
    46     The Order Form example shows how to generate rich text documents by
       
    47     combining a simple template with data input by the user in a dialog. Data
       
    48     is extracted from a \c DetailsDialog object and displayed on a QTextEdit
       
    49     with a QTextCursor, using various formats. Each form generated is added
       
    50     to a QTabWidget for easy access.
       
    51 
       
    52     \image orderform-example.png
       
    53 
       
    54     \section1 DetailsDialog Definition
       
    55 
       
    56     The \c DetailsDialog class is a subclass of QDialog, implementing a slot
       
    57     \c verify() to allow contents of the \c DetailsDialog to be verified later.
       
    58     This is further explained in \c DetailsDialog Implementation.
       
    59 
       
    60     \snippet examples/richtext/orderform/detailsdialog.h 0
       
    61 
       
    62     The constructor of \c DetailsDialog accepts parameters \a title and
       
    63     \a parent. The class defines four \e{getter} functions: \c orderItems(),
       
    64     \c senderName(), \c senderAddress(), and \c sendOffers() to allow data
       
    65     to be accessed externally.
       
    66 
       
    67     The class definition includes input widgets for the required
       
    68     fields, \c nameEdit and \c addressEdit. Also, a QCheckBox and a
       
    69     QDialogButtonBox are defined; the former to provide the user with the
       
    70     option to receive information on products and offers, and the latter
       
    71     to ensure that buttons used are arranged according to the user's native
       
    72     platform. In addition, a QTableWidget, \c itemsTable, is used to hold
       
    73     order details.
       
    74 
       
    75     The screenshot below shows the \c DetailsDialog we intend to create.
       
    76 
       
    77     \image orderform-example-detailsdialog.png
       
    78 
       
    79     \section1 DetailsDialog Implementation
       
    80 
       
    81     The constructor of \c DetailsDialog instantiates the earlier defined fields
       
    82     and their respective labels. The label for \c offersCheckBox is set and the
       
    83     \c setupItemsTable() function is invoked to setup and populate
       
    84     \c itemsTable. The QDialogButtonBox object, \c buttonBox, is instantiated
       
    85     with \gui OK and \gui Cancel buttons. This \c buttonBox's \c accepted() and
       
    86     \c rejected() signals are connected to the \c verify() and \c reject()
       
    87     slots in \c DetailsDialog.
       
    88 
       
    89     \snippet examples/richtext/orderform/detailsdialog.cpp 0
       
    90 
       
    91     A QGridLayout is used to place all the objects on the \c DetailsDialog.
       
    92 
       
    93     \snippet examples/richtext/orderform/detailsdialog.cpp 1
       
    94 
       
    95     The \c setupItemsTable() function instantiates the QTableWidget object,
       
    96     \c itemsTable, and sets the number of rows based on the QStringList
       
    97     object, \c items, which holds the type of items ordered. The number of
       
    98     columns is set to 2, providing a "name" and "quantity" layout. A \c for
       
    99     loop is used to populate the \c itemsTable and the \c name item's flag
       
   100     is set to Qt::ItemIsEnabled or Qt::ItemIsSelectable. For demonstration
       
   101     purposes, the \c quantity item is set to a 1 and all items in the
       
   102     \c itemsTable have this value for quantity; but this can be modified by
       
   103     editing the contents of the cells at run time.
       
   104 
       
   105     \snippet examples/richtext/orderform/detailsdialog.cpp 2
       
   106 
       
   107     The \c orderItems() function extracts data from the \c itemsTable and
       
   108     returns it in the form of a QList<QPair<QString,int>> where each QPair
       
   109     corresponds to an item and the quantity ordered.
       
   110 
       
   111     \snippet examples/richtext/orderform/detailsdialog.cpp 3
       
   112 
       
   113     The \c senderName() function is used to return the value of the QLineEdit
       
   114     used to store the name field for the order form.
       
   115 
       
   116     \snippet examples/richtext/orderform/detailsdialog.cpp 4
       
   117 
       
   118     The \c senderAddress() function is used to return the value of the
       
   119     QTextEdit containing the address for the order form.
       
   120 
       
   121     \snippet examples/richtext/orderform/detailsdialog.cpp 5
       
   122 
       
   123     The \c sendOffers() function is used to return a \c true or \c false
       
   124     value that is used to determine if the customer in the order form
       
   125     wishes to receive more information on the company's offers and promotions.
       
   126 
       
   127     \snippet examples/richtext/orderform/detailsdialog.cpp 6
       
   128 
       
   129     The \c verify() function is an additionally implemented slot used to
       
   130     verify the details entered by the user into the \c DetailsDialog. If
       
   131     the details entered are incomplete, a QMessageBox is displayed
       
   132     providing the user the option to discard the \c DetailsDialog. Otherwise,
       
   133     the details are accepted and the \c accept() function is invoked.
       
   134 
       
   135     \snippet examples/richtext/orderform/detailsdialog.cpp 7
       
   136 
       
   137     \section1 MainWindow Definition
       
   138 
       
   139     The \c MainWindow class is a subclass of QMainWindow, implementing two
       
   140     slots - \c openDialog() and \c printFile(). It also contains a private
       
   141     instance of QTabWidget, \c letters.
       
   142 
       
   143     \snippet examples/richtext/orderform/mainwindow.h 0
       
   144 
       
   145     \section1 MainWindow Implementation
       
   146 
       
   147     The \c MainWindow constructor sets up the \c fileMenu and the required
       
   148     actions, \c newAction and \c printAction. These actions' \c triggered()
       
   149     signals are connected to the additionally implemented openDialog() slot
       
   150     and the default close() slot. The QTabWidget, \c letters, is
       
   151     instantiated and set as the window's central widget.
       
   152 
       
   153     \snippet examples/richtext/orderform/mainwindow.cpp 0
       
   154 
       
   155     The \c createLetter() function creates a new QTabWidget with a QTextEdit,
       
   156     \c editor, as the parent. This function accepts four parameters that
       
   157     correspond to we obtained through \c DetailsDialog, in order to "fill"
       
   158     the \c editor.
       
   159 
       
   160     \snippet examples/richtext/orderform/mainwindow.cpp 1
       
   161 
       
   162     We then obtain the cursor for the \c editor using QTextEdit::textCursor().
       
   163     The \c cursor is then moved to the start of the document using
       
   164     QTextCursor::Start.
       
   165 
       
   166     \snippet examples/richtext/orderform/mainwindow.cpp 2
       
   167 
       
   168     Recall the structure of a \l{Rich Text Document Structure}
       
   169     {Rich Text Document}, where sequences of frames and
       
   170     tables are always separated by text blocks, some of which may contain no
       
   171     information.
       
   172 
       
   173     In the case of the Order Form Example, the document structure for this portion
       
   174     is described by the table below:
       
   175 
       
   176     \table
       
   177         \row
       
   178             \o {1, 8} frame with \e{referenceFrameFormat}
       
   179         \row
       
   180             \o block \o \c{A company}
       
   181         \row
       
   182             \o block
       
   183         \row
       
   184             \o block \o \c{321 City Street}
       
   185         \row
       
   186             \o block
       
   187         \row
       
   188             \o block \o \c{Industry Park}
       
   189         \row
       
   190             \o block
       
   191         \row
       
   192             \o block \o \c{Another country}
       
   193     \endtable
       
   194 
       
   195     This is accomplished with the following code:
       
   196 
       
   197     \snippet examples/richtext/orderform/mainwindow.cpp 3
       
   198 
       
   199     Note that \c topFrame is the \c {editor}'s top-level frame and is not shown
       
   200     in the document structure.
       
   201 
       
   202     We then set the \c{cursor}'s position back to its last position in
       
   203     \c topFrame and fill in the customer's name (provided by the constructor)
       
   204     and address - using a \c foreach loop to traverse the QString, \c address.
       
   205 
       
   206     \snippet examples/richtext/orderform/mainwindow.cpp 4
       
   207 
       
   208     The \c cursor is now back in \c topFrame and the document structure for
       
   209     the above portion of code is:
       
   210 
       
   211     \table
       
   212         \row
       
   213             \o block \o \c{Donald}
       
   214         \row
       
   215             \o block \o \c{47338 Park Avenue}
       
   216         \row
       
   217             \o block \o \c{Big City}
       
   218     \endtable
       
   219 
       
   220     For spacing purposes, we invoke \l{QTextCursor::insertBlock()}
       
   221     {insertBlock()} twice. The \l{QDate::currentDate()}{currentDate()} is
       
   222     obtained and displayed. We use \l{QTextFrameFormat::setWidth()}
       
   223     {setWidth()} to increase the width of \c bodyFrameFormat and we insert
       
   224     a new frame with that width.
       
   225 
       
   226     \snippet examples/richtext/orderform/mainwindow.cpp 5
       
   227 
       
   228     The following code inserts standard text into the order form.
       
   229 
       
   230     \snippet examples/richtext/orderform/mainwindow.cpp 6
       
   231     \snippet examples/richtext/orderform/mainwindow.cpp 7
       
   232 
       
   233     This part of the document structure now contains the date, a frame with
       
   234     \c bodyFrameFormat, as well as the standard text.
       
   235 
       
   236     \table
       
   237         \row
       
   238             \o block
       
   239         \row
       
   240             \o block
       
   241         \row
       
   242             \o block \o \c{Date: 25 May 2007}
       
   243         \row
       
   244             \o block
       
   245         \row
       
   246             \o {1, 4} frame with \e{bodyFrameFormat}
       
   247         \row
       
   248             \o block \o \c{I would like to place an order for the following items:}
       
   249         \row
       
   250             \o block
       
   251         \row
       
   252             \o block
       
   253     \endtable
       
   254 
       
   255     A QTextTableFormat object, \c orderTableFormat, is used to hold the type
       
   256     of item and the quantity ordered.
       
   257 
       
   258     \snippet examples/richtext/orderform/mainwindow.cpp 8
       
   259 
       
   260     We use \l{QTextTable::cellAt()}{cellAt()} to set the headers for the
       
   261     \c orderTable.
       
   262 
       
   263     \snippet examples/richtext/orderform/mainwindow.cpp 9
       
   264 
       
   265     Then, we iterate through the QList of QPair objects to populate
       
   266     \c orderTable.
       
   267 
       
   268     \snippet examples/richtext/orderform/mainwindow.cpp 10
       
   269 
       
   270     The resulting document structure for this section is:
       
   271 
       
   272     \table
       
   273         \row
       
   274             \o {1, 11} \c{orderTable} with \e{orderTableFormat}
       
   275         \row
       
   276             \o block \o \c{Product}
       
   277         \row
       
   278             \o block \o \c{Quantity}
       
   279         \row
       
   280             \o block \o \c{T-shirt}
       
   281         \row
       
   282             \o block \o \c{4}
       
   283         \row
       
   284             \o block \o \c{Badge}
       
   285         \row
       
   286             \o block \o \c{3}
       
   287         \row
       
   288             \o block \o \c{Reference book}
       
   289         \row
       
   290             \o block \o \c{2}
       
   291         \row
       
   292             \o block \o \c{Coffee cup}
       
   293         \row
       
   294             \o block \o \c{5}
       
   295     \endtable
       
   296 
       
   297     The \c cursor is then moved back to \c{topFrame}'s
       
   298     \l{QTextFrame::lastPosition()}{lastPosition()} and more standard text
       
   299     is inserted.
       
   300 
       
   301     \snippet examples/richtext/orderform/mainwindow.cpp 11
       
   302     \snippet examples/richtext/orderform/mainwindow.cpp 12
       
   303 
       
   304     Another QTextTable is inserted, to display the customer's
       
   305     preference regarding offers.
       
   306 
       
   307     \snippet examples/richtext/orderform/mainwindow.cpp 13
       
   308 
       
   309     The document structure for this portion is:
       
   310 
       
   311     \table
       
   312         \row
       
   313             \o block
       
   314         \row
       
   315             \o block\o \c{Please update my...}
       
   316         \row
       
   317             \o {1, 5} block
       
   318         \row
       
   319             \o {1, 4} \c{offersTable}
       
   320         \row
       
   321             \o block \o \c{I want to receive...}
       
   322         \row
       
   323             \o block \o \c{I do not want to recieve...}
       
   324         \row
       
   325             \o block \o \c{X}
       
   326     \endtable
       
   327 
       
   328     The \c cursor is moved to insert "Sincerely" along with the customer's
       
   329     name. More blocks are inserted for spacing purposes. The \c printAction
       
   330     is enabled to indicate that an order form can now be printed.
       
   331 
       
   332     \snippet examples/richtext/orderform/mainwindow.cpp 14
       
   333 
       
   334     The bottom portion of the document structure is:
       
   335 
       
   336     \table
       
   337         \row
       
   338             \o block
       
   339         \row
       
   340             \o {1, 5} block\o \c{Sincerely,}
       
   341         \row
       
   342             \o block
       
   343         \row
       
   344             \o block
       
   345         \row
       
   346             \o block
       
   347         \row
       
   348             \o block \o \c{Donald}
       
   349     \endtable
       
   350 
       
   351     The \c createSample() function is used for illustration purposes, to create
       
   352     a sample order form.
       
   353 
       
   354     \snippet examples/richtext/orderform/mainwindow.cpp 15
       
   355 
       
   356     The \c openDialog() function opens a \c DetailsDialog object. If the
       
   357     details in \c dialog are accepted, the \c createLetter() function is
       
   358     invoked using the parameters extracted from \c dialog.
       
   359 
       
   360     \snippet examples/richtext/orderform/mainwindow.cpp 16
       
   361 
       
   362     In order to print out the order form, a \c printFile() function is
       
   363     included, as shown below:
       
   364 
       
   365     \snippet examples/richtext/orderform/mainwindow.cpp 17
       
   366 
       
   367     This function also allows the user to print a selected area with
       
   368     QTextCursor::hasSelection(), instead of printing the entire document.
       
   369 
       
   370     \section1 \c main() Function
       
   371 
       
   372     The \c main() function instantiates \c MainWindow and sets its size to
       
   373     640x480 pixels before invoking the \c show() function and
       
   374     \c createSample() function.
       
   375 
       
   376     \snippet examples/richtext/orderform/main.cpp 0
       
   377 
       
   378 */