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

source code of qtbase/examples/widgets/richtext/orderform/mainwindow.cpp