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
53#include "detailsdialog.h"
54
55//! [0]
56DetailsDialog::DetailsDialog(const QString &title, QWidget *parent)
57 : QDialog(parent)
58{
59 nameLabel = new QLabel(tr(s: "Name:"));
60 addressLabel = new QLabel(tr(s: "Address:"));
61 addressLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
62
63 nameEdit = new QLineEdit;
64 addressEdit = new QTextEdit;
65
66 offersCheckBox = new QCheckBox(tr(s: "Send information about products and "
67 "special offers"));
68
69 setupItemsTable();
70
71 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
72 | QDialogButtonBox::Cancel);
73
74 connect(sender: buttonBox, signal: &QDialogButtonBox::accepted, receiver: this, slot: &DetailsDialog::verify);
75 connect(sender: buttonBox, signal: &QDialogButtonBox::rejected, receiver: this, slot: &DetailsDialog::reject);
76//! [0]
77
78//! [1]
79 QGridLayout *mainLayout = new QGridLayout;
80 mainLayout->addWidget(nameLabel, row: 0, column: 0);
81 mainLayout->addWidget(nameEdit, row: 0, column: 1);
82 mainLayout->addWidget(addressLabel, row: 1, column: 0);
83 mainLayout->addWidget(addressEdit, row: 1, column: 1);
84 mainLayout->addWidget(itemsTable, row: 0, column: 2, rowSpan: 2, columnSpan: 1);
85 mainLayout->addWidget(offersCheckBox, row: 2, column: 1, rowSpan: 1, columnSpan: 2);
86 mainLayout->addWidget(buttonBox, row: 3, column: 0, rowSpan: 1, columnSpan: 3);
87 setLayout(mainLayout);
88
89 setWindowTitle(title);
90}
91//! [1]
92
93//! [2]
94void DetailsDialog::setupItemsTable()
95{
96 items << tr(s: "T-shirt") << tr(s: "Badge") << tr(s: "Reference book")
97 << tr(s: "Coffee cup");
98
99 itemsTable = new QTableWidget(items.count(), 2);
100
101 for (int row = 0; row < items.count(); ++row) {
102 QTableWidgetItem *name = new QTableWidgetItem(items[row]);
103 name->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
104 itemsTable->setItem(row, column: 0, item: name);
105 QTableWidgetItem *quantity = new QTableWidgetItem("1");
106 itemsTable->setItem(row, column: 1, item: quantity);
107 }
108}
109//! [2]
110
111//! [3]
112QList<QPair<QString, int> > DetailsDialog::orderItems()
113{
114 QList<QPair<QString, int> > orderList;
115
116 for (int row = 0; row < items.count(); ++row) {
117 QPair<QString, int> item;
118 item.first = itemsTable->item(row, column: 0)->text();
119 int quantity = itemsTable->item(row, column: 1)->data(role: Qt::DisplayRole).toInt();
120 item.second = qMax(a: 0, b: quantity);
121 orderList.append(t: item);
122 }
123
124 return orderList;
125}
126//! [3]
127
128//! [4]
129QString DetailsDialog::senderName() const
130{
131 return nameEdit->text();
132}
133//! [4]
134
135//! [5]
136QString DetailsDialog::senderAddress() const
137{
138 return addressEdit->toPlainText();
139}
140//! [5]
141
142//! [6]
143bool DetailsDialog::sendOffers()
144{
145 return offersCheckBox->isChecked();
146}
147//! [6]
148
149//! [7]
150void DetailsDialog::verify()
151{
152 if (!nameEdit->text().isEmpty() && !addressEdit->toPlainText().isEmpty()) {
153 accept();
154 return;
155 }
156
157 QMessageBox::StandardButton answer;
158 answer = QMessageBox::warning(parent: this, title: tr(s: "Incomplete Form"),
159 text: tr(s: "The form does not contain all the necessary information.\n"
160 "Do you want to discard it?"),
161 buttons: QMessageBox::Yes | QMessageBox::No);
162
163 if (answer == QMessageBox::Yes)
164 reject();
165}
166//! [7]
167

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