source: trunk/examples/tutorials/addressbook-fr/part6/addressbook.cpp@ 651

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

trunk: Merged in qt 4.6.2 sources.

File size: 12.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include "addressbook.h"
44
45AddressBook::AddressBook(QWidget *parent)
46 : QWidget(parent)
47{
48 QLabel *nameLabel = new QLabel(tr("Name:"));
49 nameLine = new QLineEdit;
50 nameLine->setReadOnly(true);
51
52 QLabel *addressLabel = new QLabel(tr("Address:"));
53 addressText = new QTextEdit;
54 addressText->setReadOnly(true);
55
56 addButton = new QPushButton(tr("&Add"));
57
58 editButton = new QPushButton(tr("&Edit"));
59 editButton->setEnabled(false);
60 removeButton = new QPushButton(tr("&Remove"));
61 removeButton->setEnabled(false);
62 findButton = new QPushButton(tr("&Find"));
63 findButton->setEnabled(false);
64 submitButton = new QPushButton(tr("&Submit"));
65 submitButton->hide();
66 cancelButton = new QPushButton(tr("&Cancel"));
67 cancelButton->hide();
68
69 nextButton = new QPushButton(tr("&Next"));
70 nextButton->setEnabled(false);
71 previousButton = new QPushButton(tr("&Previous"));
72 previousButton->setEnabled(false);
73
74 loadButton = new QPushButton(tr("&Load..."));
75//! [tooltip 1]
76 loadButton->setToolTip(tr("Load contacts from a file"));
77//! [tooltip 1]
78 saveButton = new QPushButton(tr("Sa&ve..."));
79//! [tooltip 2]
80 saveButton->setToolTip(tr("Save contacts to a file"));
81//! [tooltip 2]
82 saveButton->setEnabled(false);
83
84 dialog = new FindDialog;
85
86 connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
87 connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
88 connect(editButton, SIGNAL(clicked()), this, SLOT(editContact()));
89 connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
90 connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact()));
91 connect(findButton, SIGNAL(clicked()), this, SLOT(findContact()));
92 connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
93 connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
94 connect(loadButton, SIGNAL(clicked()), this, SLOT(loadFromFile()));
95 connect(saveButton, SIGNAL(clicked()), this, SLOT(saveToFile()));
96
97 QVBoxLayout *buttonLayout1 = new QVBoxLayout;
98 buttonLayout1->addWidget(addButton);
99 buttonLayout1->addWidget(editButton);
100 buttonLayout1->addWidget(removeButton);
101 buttonLayout1->addWidget(findButton);
102 buttonLayout1->addWidget(submitButton);
103 buttonLayout1->addWidget(cancelButton);
104 buttonLayout1->addWidget(loadButton);
105 buttonLayout1->addWidget(saveButton);
106 buttonLayout1->addStretch();
107
108 QHBoxLayout *buttonLayout2 = new QHBoxLayout;
109 buttonLayout2->addWidget(previousButton);
110 buttonLayout2->addWidget(nextButton);
111
112 QGridLayout *mainLayout = new QGridLayout;
113 mainLayout->addWidget(nameLabel, 0, 0);
114 mainLayout->addWidget(nameLine, 0, 1);
115 mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
116 mainLayout->addWidget(addressText, 1, 1);
117 mainLayout->addLayout(buttonLayout1, 1, 2);
118 mainLayout->addLayout(buttonLayout2, 2, 1);
119
120 setLayout(mainLayout);
121 setWindowTitle(tr("Simple Address Book"));
122}
123
124void AddressBook::addContact()
125{
126 oldName = nameLine->text();
127 oldAddress = addressText->toPlainText();
128
129 nameLine->clear();
130 addressText->clear();
131
132 updateInterface(AddingMode);
133}
134
135void AddressBook::editContact()
136{
137 oldName = nameLine->text();
138 oldAddress = addressText->toPlainText();
139
140 updateInterface(EditingMode);
141}
142
143void AddressBook::submitContact()
144{
145 QString name = nameLine->text();
146 QString address = addressText->toPlainText();
147
148 if (name == "" || address == "") {
149 QMessageBox::information(this, tr("Empty Field"),
150 tr("Please enter a name and address."));
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.