source: trunk/examples/richtext/orderform/mainwindow.cpp

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

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

File size: 8.2 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 examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "detailsdialog.h"
44#include "mainwindow.h"
45
46//! [0]
47MainWindow::MainWindow()
48{
49 QMenu *fileMenu = new QMenu(tr("&File"), this);
50 QAction *newAction = fileMenu->addAction(tr("&New..."));
51 newAction->setShortcuts(QKeySequence::New);
52 printAction = fileMenu->addAction(tr("&Print..."), this, SLOT(printFile()));
53 printAction->setShortcuts(QKeySequence::Print);
54 printAction->setEnabled(false);
55 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
56 quitAction->setShortcuts(QKeySequence::Quit);
57 menuBar()->addMenu(fileMenu);
58
59 letters = new QTabWidget;
60
61 connect(newAction, SIGNAL(triggered()), this, SLOT(openDialog()));
62 connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
63
64 setCentralWidget(letters);
65 setWindowTitle(tr("Order Form"));
66}
67//! [0]
68
69//! [1]
70void MainWindow::createLetter(const QString &name, const QString &address,
71 QList<QPair<QString,int> > orderItems,
72 bool sendOffers)
73{
74 QTextEdit *editor = new QTextEdit;
75 int tabIndex = letters->addTab(editor, name);
76 letters->setCurrentIndex(tabIndex);
77//! [1]
78
79//! [2]
80 QTextCursor cursor(editor->textCursor());
81 cursor.movePosition(QTextCursor::Start);
82//! [2] //! [3]
83 QTextFrame *topFrame = cursor.currentFrame();
84 QTextFrameFormat topFrameFormat = topFrame->frameFormat();
85 topFrameFormat.setPadding(16);
86 topFrame->setFrameFormat(topFrameFormat);
87
88 QTextCharFormat textFormat;
89 QTextCharFormat boldFormat;
90 boldFormat.setFontWeight(QFont::Bold);
91
92 QTextFrameFormat referenceFrameFormat;
93 referenceFrameFormat.setBorder(1);
94 referenceFrameFormat.setPadding(8);
95 referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);
96 referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 40));
97 cursor.insertFrame(referenceFrameFormat);
98
99 cursor.insertText("A company", boldFormat);
100 cursor.insertBlock();
101 cursor.insertText("321 City Street");
102 cursor.insertBlock();
103 cursor.insertText("Industry Park");
104 cursor.insertBlock();
105 cursor.insertText("Another country");
106//! [3]
107
108//! [4]
109 cursor.setPosition(topFrame->lastPosition());
110
111 cursor.insertText(name, textFormat);
112 QString line;
113 foreach (line, address.split("\n")) {
114 cursor.insertBlock();
115 cursor.insertText(line);
116 }
117//! [4] //! [5]
118 cursor.insertBlock();
119 cursor.insertBlock();
120
121 QDate date = QDate::currentDate();
122 cursor.insertText(tr("Date: %1").arg(date.toString("d MMMM yyyy")),
123 textFormat);
124 cursor.insertBlock();
125
126 QTextFrameFormat bodyFrameFormat;
127 bodyFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
128 cursor.insertFrame(bodyFrameFormat);
129//! [5]
130
131//! [6]
132 cursor.insertText(tr("I would like to place an order for the following "
133 "items:"), textFormat);
134 cursor.insertBlock();
135//! [6] //! [7]
136 cursor.insertBlock();
137//! [7]
138
139//! [8]
140 QTextTableFormat orderTableFormat;
141 orderTableFormat.setAlignment(Qt::AlignHCenter);
142 QTextTable *orderTable = cursor.insertTable(1, 2, orderTableFormat);
143
144 QTextFrameFormat orderFrameFormat = cursor.currentFrame()->frameFormat();
145 orderFrameFormat.setBorder(1);
146 cursor.currentFrame()->setFrameFormat(orderFrameFormat);
147//! [8]
148
149//! [9]
150 cursor = orderTable->cellAt(0, 0).firstCursorPosition();
151 cursor.insertText(tr("Product"), boldFormat);
152 cursor = orderTable->cellAt(0, 1).firstCursorPosition();
153 cursor.insertText(tr("Quantity"), boldFormat);
154//! [9]
155
156//! [10]
157 for (int i = 0; i < orderItems.count(); ++i) {
158 QPair<QString,int> item = orderItems[i];
159 int row = orderTable->rows();
160
161 orderTable->insertRows(row, 1);
162 cursor = orderTable->cellAt(row, 0).firstCursorPosition();
163 cursor.insertText(item.first, textFormat);
164 cursor = orderTable->cellAt(row, 1).firstCursorPosition();
165 cursor.insertText(QString("%1").arg(item.second), textFormat);
166 }
167//! [10]
168
169//! [11]
170 cursor.setPosition(topFrame->lastPosition());
171
172 cursor.insertBlock();
173//! [11] //! [12]
174 cursor.insertText(tr("Please update my records to take account of the "
175 "following privacy information:"));
176 cursor.insertBlock();
177//! [12]
178
179//! [13]
180 QTextTable *offersTable = cursor.insertTable(2, 2);
181
182 cursor = offersTable->cellAt(0, 1).firstCursorPosition();
183 cursor.insertText(tr("I want to receive more information about your "
184 "company's products and special offers."), textFormat);
185 cursor = offersTable->cellAt(1, 1).firstCursorPosition();
186 cursor.insertText(tr("I do not want to receive any promotional information "
187 "from your company."), textFormat);
188
189 if (sendOffers)
190 cursor = offersTable->cellAt(0, 0).firstCursorPosition();
191 else
192 cursor = offersTable->cellAt(1, 0).firstCursorPosition();
193
194 cursor.insertText("X", boldFormat);
195//! [13]
196
197//! [14]
198 cursor.setPosition(topFrame->lastPosition());
199 cursor.insertBlock();
200 cursor.insertText(tr("Sincerely,"), textFormat);
201 cursor.insertBlock();
202 cursor.insertBlock();
203 cursor.insertBlock();
204 cursor.insertText(name);
205
206 printAction->setEnabled(true);
207}
208//! [14]
209
210//! [15]
211void MainWindow::createSample()
212{
213 DetailsDialog dialog("Dialog with default values", this);
214 createLetter("Mr. Smith", "12 High Street\nSmall Town\nThis country",
215 dialog.orderItems(), true);
216}
217//! [15]
218
219//! [16]
220void MainWindow::openDialog()
221{
222 DetailsDialog dialog(tr("Enter Customer Details"), this);
223
224 if (dialog.exec() == QDialog::Accepted)
225 createLetter(dialog.senderName(), dialog.senderAddress(),
226 dialog.orderItems(), dialog.sendOffers());
227}
228//! [16]
229
230//! [17]
231void MainWindow::printFile()
232{
233#ifndef QT_NO_PRINTER
234 QTextEdit *editor = static_cast<QTextEdit*>(letters->currentWidget());
235//! [18]
236 QPrinter printer;
237
238 QPrintDialog *dialog = new QPrintDialog(&printer, this);
239 dialog->setWindowTitle(tr("Print Document"));
240 if (editor->textCursor().hasSelection())
241 dialog->addEnabledOption(QAbstractPrintDialog::PrintSelection);
242 if (dialog->exec() != QDialog::Accepted)
243 return;
244//! [18]
245
246 editor->print(&printer);
247#endif
248}
249//! [17]
Note: See TracBrowser for help on using the repository browser.