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