source: trunk/examples/tutorials/addressbook/part6/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: 12.1 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 findButton = new QPushButton(tr("&Find"));
62 findButton->setEnabled(false);
63 submitButton = new QPushButton(tr("&Submit"));
64 submitButton->hide();
65 cancelButton = new QPushButton(tr("&Cancel"));
66 cancelButton->hide();
67
68 nextButton = new QPushButton(tr("&Next"));
69 nextButton->setEnabled(false);
70 previousButton = new QPushButton(tr("&Previous"));
71 previousButton->setEnabled(false);
72
73 loadButton = new QPushButton(tr("&Load..."));
74//! [tooltip 1]
75 loadButton->setToolTip(tr("Load contacts from a file"));
76//! [tooltip 1]
77 saveButton = new QPushButton(tr("Sa&ve..."));
78//! [tooltip 2]
79 saveButton->setToolTip(tr("Save contacts to a file"));
80//! [tooltip 2]
81 saveButton->setEnabled(false);
82
83 dialog = new FindDialog;
84
85 connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
86 connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
87 connect(editButton, SIGNAL(clicked()), this, SLOT(editContact()));
88 connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
89 connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact()));
90 connect(findButton, SIGNAL(clicked()), this, SLOT(findContact()));
91 connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
92 connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
93 connect(loadButton, SIGNAL(clicked()), this, SLOT(loadFromFile()));
94 connect(saveButton, SIGNAL(clicked()), this, SLOT(saveToFile()));
95
96 QVBoxLayout *buttonLayout1 = new QVBoxLayout;
97 buttonLayout1->addWidget(addButton);
98 buttonLayout1->addWidget(editButton);
99 buttonLayout1->addWidget(removeButton);
100 buttonLayout1->addWidget(findButton);
101 buttonLayout1->addWidget(submitButton);
102 buttonLayout1->addWidget(cancelButton);
103 buttonLayout1->addWidget(loadButton);
104 buttonLayout1->addWidget(saveButton);
105 buttonLayout1->addStretch();
106
107 QHBoxLayout *buttonLayout2 = new QHBoxLayout;
108 buttonLayout2->addWidget(previousButton);
109 buttonLayout2->addWidget(nextButton);
110
111 QGridLayout *mainLayout = new QGridLayout;
112 mainLayout->addWidget(nameLabel, 0, 0);
113 mainLayout->addWidget(nameLine, 0, 1);
114 mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
115 mainLayout->addWidget(addressText, 1, 1);
116 mainLayout->addLayout(buttonLayout1, 1, 2);
117 mainLayout->addLayout(buttonLayout2, 2, 1);
118
119 setLayout(mainLayout);
120 setWindowTitle(tr("Simple Address Book"));
121}
122
123void AddressBook::addContact()
124{
125 oldName = nameLine->text();
126 oldAddress = addressText->toPlainText();
127
128 nameLine->clear();
129 addressText->clear();
130
131 updateInterface(AddingMode);
132}
133
134void AddressBook::editContact()
135{
136 oldName = nameLine->text();
137 oldAddress = addressText->toPlainText();
138
139 updateInterface(EditingMode);
140}
141
142void AddressBook::submitContact()
143{
144 QString name = nameLine->text();
145 QString address = addressText->toPlainText();
146
147 if (name.isEmpty() || address.isEmpty()) {
148 QMessageBox::information(this, tr("Empty Field"),
149 tr("Please enter a name and address."));
150 return;
151 }
152
153 if (currentMode == AddingMode) {
154
155 if (!contacts.contains(name)) {
156 contacts.insert(name, address);
157 QMessageBox::information(this, tr("Add Successful"),
158 tr("\"%1\" has been added to your address book.").arg(name));
159 } else {
160 QMessageBox::information(this, tr("Add Unsuccessful"),
161 tr("Sorry, \"%1\" is already in your address book.").arg(name));
162 }
163 } else if (currentMode == EditingMode) {
164
165 if (oldName != name) {
166 if (!contacts.contains(name)) {
167 QMessageBox::information(this, tr("Edit Successful"),
168 tr("\"%1\" has been edited in your address book.").arg(oldName));
169 contacts.remove(oldName);
170 contacts.insert(name, address);
171 } else {
172 QMessageBox::information(this, tr("Edit Unsuccessful"),
173 tr("Sorry, \"%1\" is already in your address book.").arg(name));
174 }
175 } else if (oldAddress != address) {
176 QMessageBox::information(this, tr("Edit Successful"),
177 tr("\"%1\" has been edited in your address book.").arg(name));
178 contacts[name] = address;
179 }
180 }
181
182 updateInterface(NavigationMode);
183}
184
185void AddressBook::cancel()
186{
187 nameLine->setText(oldName);
188 addressText->setText(oldAddress);
189 updateInterface(NavigationMode);
190}
191
192void AddressBook::removeContact()
193{
194 QString name = nameLine->text();
195 QString address = addressText->toPlainText();
196
197 if (contacts.contains(name)) {
198
199 int button = QMessageBox::question(this,
200 tr("Confirm Remove"),
201 tr("Are you sure you want to remove \"%1\"?").arg(name),
202 QMessageBox::Yes | QMessageBox::No);
203
204 if (button == QMessageBox::Yes) {
205
206 previous();
207 contacts.remove(name);
208
209 QMessageBox::information(this, tr("Remove Successful"),
210 tr("\"%1\" has been removed from your address book.").arg(name));
211 }
212 }
213
214 updateInterface(NavigationMode);
215}
216
217void AddressBook::next()
218{
219 QString name = nameLine->text();
220 QMap<QString, QString>::iterator i = contacts.find(name);
221
222 if (i != contacts.end())
223 i++;
224
225 if (i == contacts.end())
226 i = contacts.begin();
227
228 nameLine->setText(i.key());
229 addressText->setText(i.value());
230}
231
232void AddressBook::previous()
233{
234 QString name = nameLine->text();
235 QMap<QString, QString>::iterator i = contacts.find(name);
236
237 if (i == contacts.end()) {
238 nameLine->clear();
239 addressText->clear();
240 return;
241 }
242
243 if (i == contacts.begin())
244 i = contacts.end();
245
246 i--;
247 nameLine->setText(i.key());
248 addressText->setText(i.value());
249}
250
251void AddressBook::findContact()
252{
253 dialog->show();
254
255 if (dialog->exec() == 1) {
256 QString contactName = dialog->getFindText();
257
258 if (contacts.contains(contactName)) {
259 nameLine->setText(contactName);
260 addressText->setText(contacts.value(contactName));
261 } else {
262 QMessageBox::information(this, tr("Contact Not Found"),
263 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
264 return;
265 }
266 }
267
268 updateInterface(NavigationMode);
269}
270
271void AddressBook::updateInterface(Mode mode)
272{
273 currentMode = mode;
274
275 switch (currentMode) {
276
277 case AddingMode:
278 case EditingMode:
279
280 nameLine->setReadOnly(false);
281 nameLine->setFocus(Qt::OtherFocusReason);
282 addressText->setReadOnly(false);
283
284 addButton->setEnabled(false);
285 editButton->setEnabled(false);
286 removeButton->setEnabled(false);
287
288 nextButton->setEnabled(false);
289 previousButton->setEnabled(false);
290
291 submitButton->show();
292 cancelButton->show();
293
294 loadButton->setEnabled(false);
295 saveButton->setEnabled(false);
296 break;
297
298 case NavigationMode:
299
300 if (contacts.isEmpty()) {
301 nameLine->clear();
302 addressText->clear();
303 }
304
305 nameLine->setReadOnly(true);
306 addressText->setReadOnly(true);
307 addButton->setEnabled(true);
308
309 int number = contacts.size();
310 editButton->setEnabled(number >= 1);
311 removeButton->setEnabled(number >= 1);
312 findButton->setEnabled(number > 2);
313 nextButton->setEnabled(number > 1);
314 previousButton->setEnabled(number > 1);
315
316 submitButton->hide();
317 cancelButton->hide();
318
319 loadButton->setEnabled(true);
320 saveButton->setEnabled(number >= 1);
321 break;
322 }
323}
324
325//! [saveToFile() function part1]
326void AddressBook::saveToFile()
327{
328 QString fileName = QFileDialog::getSaveFileName(this,
329 tr("Save Address Book"), "",
330 tr("Address Book (*.abk);;All Files (*)"));
331
332//! [saveToFile() function part1]
333//! [saveToFile() function part2]
334 if (fileName.isEmpty())
335 return;
336 else {
337 QFile file(fileName);
338 if (!file.open(QIODevice::WriteOnly)) {
339 QMessageBox::information(this, tr("Unable to open file"),
340 file.errorString());
341 return;
342 }
343
344//! [saveToFile() function part2]
345//! [saveToFile() function part3]
346 QDataStream out(&file);
347 out.setVersion(QDataStream::Qt_4_5);
348 out << contacts;
349 }
350}
351//! [saveToFile() function part3]
352
353//! [loadFromFile() function part1]
354void AddressBook::loadFromFile()
355{
356 QString fileName = QFileDialog::getOpenFileName(this,
357 tr("Open Address Book"), "",
358 tr("Address Book (*.abk);;All Files (*)"));
359//! [loadFromFile() function part1]
360
361//! [loadFromFile() function part2]
362 if (fileName.isEmpty())
363 return;
364 else {
365
366 QFile file(fileName);
367
368 if (!file.open(QIODevice::ReadOnly)) {
369 QMessageBox::information(this, tr("Unable to open file"),
370 file.errorString());
371 return;
372 }
373
374 QDataStream in(&file);
375 in.setVersion(QDataStream::Qt_4_5);
376 contacts.empty(); // empty existing contacts
377 in >> contacts;
378//! [loadFromFile() function part2]
379
380//! [loadFromFile() function part3]
381 if (contacts.isEmpty()) {
382 QMessageBox::information(this, tr("No contacts in file"),
383 tr("The file you are attempting to open contains no contacts."));
384 } else {
385 QMap<QString, QString>::iterator i = contacts.begin();
386 nameLine->setText(i.key());
387 addressText->setText(i.value());
388 }
389 }
390
391 updateInterface(NavigationMode);
392}
393//! [loadFromFile() function part3]
Note: See TracBrowser for help on using the repository browser.