| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "detailsdialog.h"
|
|---|
| 45 | #include "mainwindow.h"
|
|---|
| 46 |
|
|---|
| 47 | //! [0]
|
|---|
| 48 | MainWindow::MainWindow()
|
|---|
| 49 | {
|
|---|
| 50 | QMenu *fileMenu = new QMenu(tr("&File"), this);
|
|---|
| 51 | QAction *newAction = fileMenu->addAction(tr("&New..."));
|
|---|
| 52 | newAction->setShortcut(tr("Ctrl+N"));
|
|---|
| 53 | printAction = fileMenu->addAction(tr("&Print..."), this, SLOT(printFile()));
|
|---|
| 54 | printAction->setShortcut(tr("Ctrl+P"));
|
|---|
| 55 | printAction->setEnabled(false);
|
|---|
| 56 | QAction *quitAction = fileMenu->addAction(tr("E&xit"));
|
|---|
| 57 | quitAction->setShortcut(tr("Ctrl+Q"));
|
|---|
| 58 | menuBar()->addMenu(fileMenu);
|
|---|
| 59 |
|
|---|
| 60 | letters = new QTabWidget;
|
|---|
| 61 |
|
|---|
| 62 | connect(newAction, SIGNAL(triggered()), this, SLOT(openDialog()));
|
|---|
| 63 | connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
|
|---|
| 64 |
|
|---|
| 65 | setCentralWidget(letters);
|
|---|
| 66 | setWindowTitle(tr("Order Form"));
|
|---|
| 67 | }
|
|---|
| 68 | //! [0]
|
|---|
| 69 |
|
|---|
| 70 | //! [1]
|
|---|
| 71 | void MainWindow::createLetter(const QString &name, const QString &address,
|
|---|
| 72 | QList<QPair<QString,int> > orderItems,
|
|---|
| 73 | bool sendOffers)
|
|---|
| 74 | {
|
|---|
| 75 | QTextEdit *editor = new QTextEdit;
|
|---|
| 76 | int tabIndex = letters->addTab(editor, name);
|
|---|
| 77 | letters->setCurrentIndex(tabIndex);
|
|---|
| 78 | //! [1]
|
|---|
| 79 |
|
|---|
| 80 | //! [2]
|
|---|
| 81 | QTextCursor cursor(editor->textCursor());
|
|---|
| 82 | cursor.movePosition(QTextCursor::Start);
|
|---|
| 83 | //! [2] //! [3]
|
|---|
| 84 | QTextFrame *topFrame = cursor.currentFrame();
|
|---|
| 85 | QTextFrameFormat topFrameFormat = topFrame->frameFormat();
|
|---|
| 86 | topFrameFormat.setPadding(16);
|
|---|
| 87 | topFrame->setFrameFormat(topFrameFormat);
|
|---|
| 88 |
|
|---|
| 89 | QTextCharFormat textFormat;
|
|---|
| 90 | QTextCharFormat boldFormat;
|
|---|
| 91 | boldFormat.setFontWeight(QFont::Bold);
|
|---|
| 92 |
|
|---|
| 93 | QTextFrameFormat referenceFrameFormat;
|
|---|
| 94 | referenceFrameFormat.setBorder(1);
|
|---|
| 95 | referenceFrameFormat.setPadding(8);
|
|---|
| 96 | referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);
|
|---|
| 97 | referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 40));
|
|---|
| 98 | cursor.insertFrame(referenceFrameFormat);
|
|---|
| 99 |
|
|---|
| 100 | cursor.insertText("A company", boldFormat);
|
|---|
| 101 | cursor.insertBlock();
|
|---|
| 102 | cursor.insertText("321 City Street");
|
|---|
| 103 | cursor.insertBlock();
|
|---|
| 104 | cursor.insertText("Industry Park");
|
|---|
| 105 | cursor.insertBlock();
|
|---|
| 106 | cursor.insertText("Another country");
|
|---|
| 107 | //! [3]
|
|---|
| 108 |
|
|---|
| 109 | //! [4]
|
|---|
| 110 | cursor.setPosition(topFrame->lastPosition());
|
|---|
| 111 |
|
|---|
| 112 | cursor.insertText(name, textFormat);
|
|---|
| 113 | QString line;
|
|---|
| 114 | foreach (line, address.split("\n")) {
|
|---|
| 115 | cursor.insertBlock();
|
|---|
| 116 | cursor.insertText(line);
|
|---|
| 117 | }
|
|---|
| 118 | //! [4] //! [5]
|
|---|
| 119 | cursor.insertBlock();
|
|---|
| 120 | cursor.insertBlock();
|
|---|
| 121 |
|
|---|
| 122 | QDate date = QDate::currentDate();
|
|---|
| 123 | cursor.insertText(tr("Date: %1").arg(date.toString("d MMMM yyyy")),
|
|---|
| 124 | textFormat);
|
|---|
| 125 | cursor.insertBlock();
|
|---|
| 126 |
|
|---|
| 127 | QTextFrameFormat bodyFrameFormat;
|
|---|
| 128 | bodyFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
|
|---|
| 129 | cursor.insertFrame(bodyFrameFormat);
|
|---|
| 130 | //! [5]
|
|---|
| 131 |
|
|---|
| 132 | //! [6]
|
|---|
| 133 | cursor.insertText(tr("I would like to place an order for the following "
|
|---|
| 134 | "items:"), textFormat);
|
|---|
| 135 | cursor.insertBlock();
|
|---|
| 136 | //! [6] //! [7]
|
|---|
| 137 | cursor.insertBlock();
|
|---|
| 138 | //! [7]
|
|---|
| 139 |
|
|---|
| 140 | //! [8]
|
|---|
| 141 | QTextTableFormat orderTableFormat;
|
|---|
| 142 | orderTableFormat.setAlignment(Qt::AlignHCenter);
|
|---|
| 143 | QTextTable *orderTable = cursor.insertTable(1, 2, orderTableFormat);
|
|---|
| 144 |
|
|---|
| 145 | QTextFrameFormat orderFrameFormat = cursor.currentFrame()->frameFormat();
|
|---|
| 146 | orderFrameFormat.setBorder(1);
|
|---|
| 147 | cursor.currentFrame()->setFrameFormat(orderFrameFormat);
|
|---|
| 148 | //! [8]
|
|---|
| 149 |
|
|---|
| 150 | //! [9]
|
|---|
| 151 | cursor = orderTable->cellAt(0, 0).firstCursorPosition();
|
|---|
| 152 | cursor.insertText(tr("Product"), boldFormat);
|
|---|
| 153 | cursor = orderTable->cellAt(0, 1).firstCursorPosition();
|
|---|
| 154 | cursor.insertText(tr("Quantity"), boldFormat);
|
|---|
| 155 | //! [9]
|
|---|
| 156 |
|
|---|
| 157 | //! [10]
|
|---|
| 158 | for (int i = 0; i < orderItems.count(); ++i) {
|
|---|
| 159 | QPair<QString,int> item = orderItems[i];
|
|---|
| 160 | int row = orderTable->rows();
|
|---|
| 161 |
|
|---|
| 162 | orderTable->insertRows(row, 1);
|
|---|
| 163 | cursor = orderTable->cellAt(row, 0).firstCursorPosition();
|
|---|
| 164 | cursor.insertText(item.first, textFormat);
|
|---|
| 165 | cursor = orderTable->cellAt(row, 1).firstCursorPosition();
|
|---|
| 166 | cursor.insertText(QString("%1").arg(item.second), textFormat);
|
|---|
| 167 | }
|
|---|
| 168 | //! [10]
|
|---|
| 169 |
|
|---|
| 170 | //! [11]
|
|---|
| 171 | cursor.setPosition(topFrame->lastPosition());
|
|---|
| 172 |
|
|---|
| 173 | cursor.insertBlock();
|
|---|
| 174 | //! [11] //! [12]
|
|---|
| 175 | cursor.insertText(tr("Please update my records to take account of the "
|
|---|
| 176 | "following privacy information:"));
|
|---|
| 177 | cursor.insertBlock();
|
|---|
| 178 | //! [12]
|
|---|
| 179 |
|
|---|
| 180 | //! [13]
|
|---|
| 181 | QTextTable *offersTable = cursor.insertTable(2, 2);
|
|---|
| 182 |
|
|---|
| 183 | cursor = offersTable->cellAt(0, 1).firstCursorPosition();
|
|---|
| 184 | cursor.insertText(tr("I want to receive more information about your "
|
|---|
| 185 | "company's products and special offers."), textFormat);
|
|---|
| 186 | cursor = offersTable->cellAt(1, 1).firstCursorPosition();
|
|---|
| 187 | cursor.insertText(tr("I do not want to receive any promotional information "
|
|---|
| 188 | "from your company."), textFormat);
|
|---|
| 189 |
|
|---|
| 190 | if (sendOffers)
|
|---|
| 191 | cursor = offersTable->cellAt(0, 0).firstCursorPosition();
|
|---|
| 192 | else
|
|---|
| 193 | cursor = offersTable->cellAt(1, 0).firstCursorPosition();
|
|---|
| 194 |
|
|---|
| 195 | cursor.insertText("X", boldFormat);
|
|---|
| 196 | //! [13]
|
|---|
| 197 |
|
|---|
| 198 | //! [14]
|
|---|
| 199 | cursor.setPosition(topFrame->lastPosition());
|
|---|
| 200 | cursor.insertBlock();
|
|---|
| 201 | cursor.insertText(tr("Sincerely,"), textFormat);
|
|---|
| 202 | cursor.insertBlock();
|
|---|
| 203 | cursor.insertBlock();
|
|---|
| 204 | cursor.insertBlock();
|
|---|
| 205 | cursor.insertText(name);
|
|---|
| 206 |
|
|---|
| 207 | printAction->setEnabled(true);
|
|---|
| 208 | }
|
|---|
| 209 | //! [14]
|
|---|
| 210 |
|
|---|
| 211 | //! [15]
|
|---|
| 212 | void MainWindow::createSample()
|
|---|
| 213 | {
|
|---|
| 214 | DetailsDialog dialog("Dialog with default values", this);
|
|---|
| 215 | createLetter("Mr. Smith", "12 High Street\nSmall Town\nThis country",
|
|---|
| 216 | dialog.orderItems(), true);
|
|---|
| 217 | }
|
|---|
| 218 | //! [15]
|
|---|
| 219 |
|
|---|
| 220 | //! [16]
|
|---|
| 221 | void MainWindow::openDialog()
|
|---|
| 222 | {
|
|---|
| 223 | DetailsDialog dialog(tr("Enter Customer Details"), this);
|
|---|
| 224 |
|
|---|
| 225 | if (dialog.exec() == QDialog::Accepted)
|
|---|
| 226 | createLetter(dialog.senderName(), dialog.senderAddress(),
|
|---|
| 227 | dialog.orderItems(), dialog.sendOffers());
|
|---|
| 228 | }
|
|---|
| 229 | //! [16]
|
|---|
| 230 |
|
|---|
| 231 | //! [17]
|
|---|
| 232 | void MainWindow::printFile()
|
|---|
| 233 | {
|
|---|
| 234 | #ifndef QT_NO_PRINTER
|
|---|
| 235 | QTextEdit *editor = static_cast<QTextEdit*>(letters->currentWidget());
|
|---|
| 236 | //! [18]
|
|---|
| 237 | QPrinter printer;
|
|---|
| 238 |
|
|---|
| 239 | QPrintDialog *dialog = new QPrintDialog(&printer, this);
|
|---|
| 240 | dialog->setWindowTitle(tr("Print Document"));
|
|---|
| 241 | if (editor->textCursor().hasSelection())
|
|---|
| 242 | dialog->addEnabledOption(QAbstractPrintDialog::PrintSelection);
|
|---|
| 243 | if (dialog->exec() != QDialog::Accepted)
|
|---|
| 244 | return;
|
|---|
| 245 | //! [18]
|
|---|
| 246 |
|
|---|
| 247 | editor->print(&printer);
|
|---|
| 248 | #endif
|
|---|
| 249 | }
|
|---|
| 250 | //! [17]
|
|---|