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

Last change on this file since 168 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 12.1 KB
Line 
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#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 return;
152 }
153
154 if (currentMode == AddingMode) {
155
156 if (!contacts.contains(name)) {
157 contacts.insert(name, address);
158 QMessageBox::information(this, tr("Add Successful"),
159 tr("\"%1\" has been added to your address book.").arg(name));
160 } else {
161 QMessageBox::information(this, tr("Add Unsuccessful"),
162 tr("Sorry, \"%1\" is already in your address book.").arg(name));
163 return;
164 }
165 } else if (currentMode == EditingMode) {
166
167 if (oldName != name) {
168 if (!contacts.contains(name)) {
169 QMessageBox::information(this, tr("Edit Successful"),
170 tr("\"%1\" has been edited in your address book.").arg(oldName));
171 contacts.remove(oldName);
172 contacts.insert(name, address);
173 } else {
174 QMessageBox::information(this, tr("Edit Unsuccessful"),
175 tr("Sorry, \"%1\" is already in your address book.").arg(name));
176 return;
177 }
178 } else if (oldAddress != address) {
179 QMessageBox::information(this, tr("Edit Successful"),
180 tr("\"%1\" has been edited in your address book.").arg(name));
181 contacts[name] = address;
182 }
183 }
184
185 updateInterface(NavigationMode);
186}
187
188void AddressBook::cancel()
189{
190 nameLine->setText(oldName);
191 addressText->setText(oldAddress);
192 updateInterface(NavigationMode);
193}
194
195void AddressBook::removeContact()
196{
197 QString name = nameLine->text();
198 QString address = addressText->toPlainText();
199
200 if (contacts.contains(name)) {
201
202 int button = QMessageBox::question(this,
203 tr("Confirm Remove"),
204 tr("Are you sure you want to remove \"%1\"?").arg(name),
205 QMessageBox::Yes | QMessageBox::No);
206
207 if (button == QMessageBox::Yes) {
208
209 previous();
210 contacts.remove(name);
211
212 QMessageBox::information(this, tr("Remove Successful"),
213 tr("\"%1\" has been removed from your address book.").arg(name));
214 }
215 }
216
217 updateInterface(NavigationMode);
218}
219
220void AddressBook::next()
221{
222 QString name = nameLine->text();
223 QMap<QString, QString>::iterator i = contacts.find(name);
224
225 if (i != contacts.end())
226 i++;
227
228 if (i == contacts.end())
229 i = contacts.begin();
230
231 nameLine->setText(i.key());
232 addressText->setText(i.value());
233}
234
235void AddressBook::previous()
236{
237 QString name = nameLine->text();
238 QMap<QString, QString>::iterator i = contacts.find(name);
239
240 if (i == contacts.end()) {
241 nameLine->clear();
242 addressText->clear();
243 return;
244 }
245
246 if (i == contacts.begin())
247 i = contacts.end();
248
249 i--;
250 nameLine->setText(i.key());
251 addressText->setText(i.value());
252}
253
254void AddressBook::findContact()
255{
256 dialog->show();
257
258 if (dialog->exec() == 1) {
259 QString contactName = dialog->getFindText();
260
261 if (contacts.contains(contactName)) {
262 nameLine->setText(contactName);
263 addressText->setText(contacts.value(contactName));
264 } else {
265 QMessageBox::information(this, tr("Contact Not Found"),
266 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
267 return;
268 }
269 }
270
271 updateInterface(NavigationMode);
272}
273
274void AddressBook::updateInterface(Mode mode)
275{
276 currentMode = mode;
277
278 switch (currentMode) {
279
280 case AddingMode:
281 case EditingMode:
282
283 nameLine->setReadOnly(false);
284 nameLine->setFocus(Qt::OtherFocusReason);
285 addressText->setReadOnly(false);
286
287 addButton->setEnabled(false);
288 editButton->setEnabled(false);
289 removeButton->setEnabled(false);
290
291 nextButton->setEnabled(false);
292 previousButton->setEnabled(false);
293
294 submitButton->show();
295 cancelButton->show();
296
297 loadButton->setEnabled(false);
298 saveButton->setEnabled(false);
299 break;
300
301 case NavigationMode:
302
303 if (contacts.isEmpty()) {
304 nameLine->clear();
305 addressText->clear();
306 }
307
308 nameLine->setReadOnly(true);
309 addressText->setReadOnly(true);
310 addButton->setEnabled(true);
311
312 int number = contacts.size();
313 editButton->setEnabled(number >= 1);
314 removeButton->setEnabled(number >= 1);
315 findButton->setEnabled(number > 2);
316 nextButton->setEnabled(number > 1);
317 previousButton->setEnabled(number > 1);
318
319 submitButton->hide();
320 cancelButton->hide();
321
322 loadButton->setEnabled(true);
323 saveButton->setEnabled(number >= 1);
324 break;
325 }
326}
327
328//! [saveToFile() function part1]
329void AddressBook::saveToFile()
330{
331 QString fileName = QFileDialog::getSaveFileName(this,
332 tr("Save Address Book"), "",
333 tr("Address Book (*.abk);;All Files (*)"));
334
335//! [saveToFile() function part1]
336//! [saveToFile() function part2]
337 if (fileName.isEmpty())
338 return;
339 else {
340 QFile file(fileName);
341 if (!file.open(QIODevice::WriteOnly)) {
342 QMessageBox::information(this, tr("Unable to open file"),
343 file.errorString());
344 return;
345 }
346
347//! [saveToFile() function part2]
348//! [saveToFile() function part3]
349 QDataStream out(&file);
350 out.setVersion(QDataStream::Qt_4_5);
351 out << contacts;
352 }
353}
354//! [saveToFile() function part3]
355
356//! [loadFromFile() function part1]
357void AddressBook::loadFromFile()
358{
359 QString fileName = QFileDialog::getOpenFileName(this,
360 tr("Open Address Book"), "",
361 tr("Address Book (*.abk);;All Files (*)"));
362//! [loadFromFile() function part1]
363
364//! [loadFromFile() function part2]
365 if (fileName.isEmpty())
366 return;
367 else {
368
369 QFile file(fileName);
370
371 if (!file.open(QIODevice::ReadOnly)) {
372 QMessageBox::information(this, tr("Unable to open file"),
373 file.errorString());
374 return;
375 }
376
377 QDataStream in(&file);
378 in.setVersion(QDataStream::Qt_4_5);
379 contacts.empty(); // empty existing contacts
380 in >> contacts;
381//! [loadFromFile() function part2]
382
383//! [loadFromFile() function part3]
384 if (contacts.isEmpty()) {
385 QMessageBox::information(this, tr("No contacts in file"),
386 tr("The file you are attempting to open contains no contacts."));
387 } else {
388 QMap<QString, QString>::iterator i = contacts.begin();
389 nameLine->setText(i.key());
390 addressText->setText(i.value());
391 }
392 }
393
394 updateInterface(NavigationMode);
395}
396//! [loadFromFile() function part3]
Note: See TracBrowser for help on using the repository browser.