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("&File"), this);
67 QAction *newAction = fileMenu->addAction(tr("&New..."));
68 newAction->setShortcuts(QKeySequence::New);
69 printAction = fileMenu->addAction(tr("&Print..."), this, &MainWindow::printFile);
70 printAction->setShortcuts(QKeySequence::Print);
71 printAction->setEnabled(false);
72 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
73 quitAction->setShortcuts(QKeySequence::Quit);
74 menuBar()->addMenu(fileMenu);
75
76 letters = new QTabWidget;
77
78 connect(newAction, &QAction::triggered, this, &MainWindow::openDialog);
79 connect(quitAction, &QAction::triggered, this, &MainWindow::close);
80
81 setCentralWidget(letters);
82 setWindowTitle(tr("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(editor, name);
93 letters->setCurrentIndex(tabIndex);
94//! [1]
95
96//! [2]
97 QTextCursor cursor(editor->textCursor());
98 cursor.movePosition(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(referenceFrameFormat);
115
116 cursor.insertText("A company", boldFormat);
117 cursor.insertBlock();
118 cursor.insertText("321 City Street");
119 cursor.insertBlock();
120 cursor.insertText("Industry Park");
121 cursor.insertBlock();
122 cursor.insertText("Another country");
123//! [3]
124
125//! [4]
126 cursor.setPosition(topFrame->lastPosition());
127
128 cursor.insertText(name, textFormat);
129 const QStringList lines = address.split('\n');
130 for (const QString &line : lines) {
131 cursor.insertBlock();
132 cursor.insertText(line);
133 }
134//! [4] //! [5]
135 cursor.insertBlock();
136 cursor.insertBlock();
137
138 QDate date = QDate::currentDate();
139 cursor.insertText(tr("Date: %1").arg(date.toString("d MMMM yyyy")),
140 textFormat);
141 cursor.insertBlock();
142
143 QTextFrameFormat bodyFrameFormat;
144 bodyFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
145 cursor.insertFrame(bodyFrameFormat);
146//! [5]
147
148//! [6]
149 cursor.insertText(tr("I would like to place an order for the following "
150 "items:"), 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(1, 2, 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(0, 0).firstCursorPosition();
168 cursor.insertText(tr("Product"), boldFormat);
169 cursor = orderTable->cellAt(0, 1).firstCursorPosition();
170 cursor.insertText(tr("Quantity"), 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(row, 1);
179 cursor = orderTable->cellAt(row, 0).firstCursorPosition();
180 cursor.insertText(item.first, textFormat);
181 cursor = orderTable->cellAt(row, 1).firstCursorPosition();
182 cursor.insertText(QString("%1").arg(item.second), textFormat);
183 }
184//! [10]
185
186//! [11]
187 cursor.setPosition(topFrame->lastPosition());
188
189 cursor.insertBlock();
190//! [11] //! [12]
191 cursor.insertText(tr("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(2, 2);
198
199 cursor = offersTable->cellAt(0, 1).firstCursorPosition();
200 cursor.insertText(tr("I want to receive more information about your "
201 "company's products and special offers."), textFormat);
202 cursor = offersTable->cellAt(1, 1).firstCursorPosition();
203 cursor.insertText(tr("I do not want to receive any promotional information "
204 "from your company."), textFormat);
205
206 if (sendOffers)
207 cursor = offersTable->cellAt(0, 0).firstCursorPosition();
208 else
209 cursor = offersTable->cellAt(1, 0).firstCursorPosition();
210
211 cursor.insertText("X", boldFormat);
212//! [13]
213
214//! [14]
215 cursor.setPosition(topFrame->lastPosition());
216 cursor.insertBlock();
217 cursor.insertText(tr("Sincerely,"), textFormat);
218 cursor.insertBlock();
219 cursor.insertBlock();
220 cursor.insertBlock();
221 cursor.insertText(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("Mr. Smith", "12 High Street\nSmall Town\nThis country",
232 dialog.orderItems(), true);
233}
234//! [15]
235
236//! [16]
237void MainWindow::openDialog()
238{
239 DetailsDialog dialog(tr("Enter Customer Details"), this);
240
241 if (dialog.exec() == QDialog::Accepted) {
242 createLetter(dialog.senderName(), dialog.senderAddress(),
243 dialog.orderItems(), 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("Print Document"));
258 if (editor->textCursor().hasSelection())
259 dialog.setOption(QAbstractPrintDialog::PrintSelection);
260 if (dialog.exec() != QDialog::Accepted) {
261 return;
262 }
263//! [18]
264
265 editor->print(&printer);
266#endif
267}
268//! [17]
269