source: trunk/examples/tutorials/addressbook/part5/addressbook.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 9.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42#include "addressbook.h"
43
44AddressBook::AddressBook(QWidget *parent)
45 : QWidget(parent)
46{
47 QLabel *nameLabel = new QLabel(tr("Name:"));
48 nameLine = new QLineEdit;
49 nameLine->setReadOnly(true);
50
51 QLabel *addressLabel = new QLabel(tr("Address:"));
52 addressText = new QTextEdit;
53 addressText->setReadOnly(true);
54
55 addButton = new QPushButton(tr("&Add"));
56
57 editButton = new QPushButton(tr("&Edit"));
58 editButton->setEnabled(false);
59 removeButton = new QPushButton(tr("&Remove"));
60 removeButton->setEnabled(false);
61//! [instantiating findButton]
62 findButton = new QPushButton(tr("&Find"));
63 findButton->setEnabled(false);
64//! [instantiating findButton]
65 submitButton = new QPushButton(tr("&Submit"));
66 submitButton->hide();
67 cancelButton = new QPushButton(tr("&Cancel"));
68 cancelButton->hide();
69
70 nextButton = new QPushButton(tr("&Next"));
71 nextButton->setEnabled(false);
72 previousButton = new QPushButton(tr("&Previous"));
73 previousButton->setEnabled(false);
74
75//! [instantiating FindDialog]
76 dialog = new FindDialog;
77//! [instantiating FindDialog]
78
79 connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
80 connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
81 connect(editButton, SIGNAL(clicked()), this, SLOT(editContact()));
82 connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
83 connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact()));
84//! [signals and slots for find]
85 connect(findButton, SIGNAL(clicked()), this, SLOT(findContact()));
86//! [signals and slots for find]
87 connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
88 connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
89
90 QVBoxLayout *buttonLayout1 = new QVBoxLayout;
91 buttonLayout1->addWidget(addButton);
92 buttonLayout1->addWidget(editButton);
93 buttonLayout1->addWidget(removeButton);
94//! [adding findButton to layout]
95 buttonLayout1->addWidget(findButton);
96//! [adding findButton to layout]
97 buttonLayout1->addWidget(submitButton);
98 buttonLayout1->addWidget(cancelButton);
99 buttonLayout1->addStretch();
100
101 QHBoxLayout *buttonLayout2 = new QHBoxLayout;
102 buttonLayout2->addWidget(previousButton);
103 buttonLayout2->addWidget(nextButton);
104
105 QGridLayout *mainLayout = new QGridLayout;
106 mainLayout->addWidget(nameLabel, 0, 0);
107 mainLayout->addWidget(nameLine, 0, 1);
108 mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
109 mainLayout->addWidget(addressText, 1, 1);
110 mainLayout->addLayout(buttonLayout1, 1, 2);
111 mainLayout->addLayout(buttonLayout2, 2, 1);
112
113 setLayout(mainLayout);
114 setWindowTitle(tr("Simple Address Book"));
115}
116
117void AddressBook::addContact()
118{
119 oldName = nameLine->text();
120 oldAddress = addressText->toPlainText();
121
122 nameLine->clear();
123 addressText->clear();
124
125 updateInterface(AddingMode);
126}
127
128void AddressBook::editContact()
129{
130 oldName = nameLine->text();
131 oldAddress = addressText->toPlainText();
132
133 updateInterface(EditingMode);
134}
135
136void AddressBook::submitContact()
137{
138 QString name = nameLine->text();
139 QString address = addressText->toPlainText();
140
141 if (name.isEmpty() || address.isEmpty()) {
142 QMessageBox::information(this, tr("Empty Field"),
143 tr("Please enter a name and address."));
144 return;
145 }
146
147 if (currentMode == AddingMode) {
148
149 if (!contacts.contains(name)) {
150 contacts.insert(name, address);
151 QMessageBox::information(this, tr("Add Successful"),
152 tr("\"%1\" has been added to your address book.").arg(name));
153 } else {
154 QMessageBox::information(this, tr("Add Unsuccessful"),
155 tr("Sorry, \"%1\" is already in your address book.").arg(name));
156 }
157 } else if (currentMode == EditingMode) {
158
159 if (oldName != name) {
160 if (!contacts.contains(name)) {
161 QMessageBox::information(this, tr("Edit Successful"),
162 tr("\"%1\" has been edited in your address book.").arg(oldName));
163 contacts.remove(oldName);
164 contacts.insert(name, address);
165 } else {
166 QMessageBox::information(this, tr("Edit Unsuccessful"),
167 tr("Sorry, \"%1\" is already in your address book.").arg(name));
168 }
169 } else if (oldAddress != address) {
170 QMessageBox::information(this, tr("Edit Successful"),
171 tr("\"%1\" has been edited in your address book.").arg(name));
172 contacts[name] = address;
173 }
174 }
175
176 updateInterface(NavigationMode);
177}
178
179void AddressBook::cancel()
180{
181 nameLine->setText(oldName);
182 addressText->setText(oldAddress);
183 updateInterface(NavigationMode);
184}
185
186void AddressBook::removeContact()
187{
188 QString name = nameLine->text();
189 QString address = addressText->toPlainText();
190
191 if (contacts.contains(name)) {
192
193 int button = QMessageBox::question(this,
194 tr("Confirm Remove"),
195 tr("Are you sure you want to remove \"%1\"?").arg(name),
196 QMessageBox::Yes | QMessageBox::No);
197
198 if (button == QMessageBox::Yes) {
199
200 previous();
201 contacts.remove(name);
202
203 QMessageBox::information(this, tr("Remove Successful"),
204 tr("\"%1\" has been removed from your address book.").arg(name));
205 }
206 }
207
208 updateInterface(NavigationMode);
209}
210
211void AddressBook::next()
212{
213 QString name = nameLine->text();
214 QMap<QString, QString>::iterator i = contacts.find(name);
215
216 if (i != contacts.end())
217 i++;
218
219 if (i == contacts.end())
220 i = contacts.begin();
221
222 nameLine->setText(i.key());
223 addressText->setText(i.value());
224}
225
226void AddressBook::previous()
227{
228 QString name = nameLine->text();
229 QMap<QString, QString>::iterator i = contacts.find(name);
230
231 if (i == contacts.end()) {
232 nameLine->clear();
233 addressText->clear();
234 return;
235 }
236
237 if (i == contacts.begin())
238 i = contacts.end();
239
240 i--;
241 nameLine->setText(i.key());
242 addressText->setText(i.value());
243}
244//! [findContact() function]
245void AddressBook::findContact()
246{
247 dialog->show();
248
249 if (dialog->exec() == QDialog::Accepted) {
250 QString contactName = dialog->getFindText();
251
252 if (contacts.contains(contactName)) {
253 nameLine->setText(contactName);
254 addressText->setText(contacts.value(contactName));
255 } else {
256 QMessageBox::information(this, tr("Contact Not Found"),
257 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
258 return;
259 }
260 }
261
262 updateInterface(NavigationMode);
263}
264//! [findContact() function]
265
266void AddressBook::updateInterface(Mode mode)
267{
268 currentMode = mode;
269
270 switch (currentMode) {
271
272 case AddingMode:
273 case EditingMode:
274
275 nameLine->setReadOnly(false);
276 nameLine->setFocus(Qt::OtherFocusReason);
277 addressText->setReadOnly(false);
278
279 addButton->setEnabled(false);
280 editButton->setEnabled(false);
281 removeButton->setEnabled(false);
282
283 nextButton->setEnabled(false);
284 previousButton->setEnabled(false);
285
286 submitButton->show();
287 cancelButton->show();
288 break;
289
290 case NavigationMode:
291
292 if (contacts.isEmpty()) {
293 nameLine->clear();
294 addressText->clear();
295 }
296
297 nameLine->setReadOnly(true);
298 addressText->setReadOnly(true);
299 addButton->setEnabled(true);
300
301 int number = contacts.size();
302 editButton->setEnabled(number >= 1);
303 removeButton->setEnabled(number >= 1);
304 findButton->setEnabled(number > 2);
305 nextButton->setEnabled(number > 1);
306 previousButton->setEnabled(number > 1);
307
308 submitButton->hide();
309 cancelButton->hide();
310 break;
311 }
312}
Note: See TracBrowser for help on using the repository browser.