1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
42
#include "addressbook.h"
44
AddressBook::AddressBook(QWidget *parent)
47
QLabel *nameLabel = new QLabel(tr("Name:"));
48
nameLine = new QLineEdit;
49
nameLine->setReadOnly(true);
51
QLabel *addressLabel = new QLabel(tr("Address:"));
52
addressText = new QTextEdit;
53
addressText->setReadOnly(true);
55
addButton = new QPushButton(tr("&Add"));
56
//! [edit and remove buttons]
57
editButton = new QPushButton(tr("&Edit"));
58
editButton->setEnabled(false);
59
removeButton = new QPushButton(tr("&Remove"));
60
removeButton->setEnabled(false);
61
//! [edit and remove buttons]
62
submitButton = new QPushButton(tr("&Submit"));
64
cancelButton = new QPushButton(tr("&Cancel"));
67
nextButton = new QPushButton(tr("&Next"));
68
nextButton->setEnabled(false);
69
previousButton = new QPushButton(tr("&Previous"));
70
previousButton->setEnabled(false);
72
connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
73
connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
74
//! [connecting edit and remove]
75
connect(editButton, SIGNAL(clicked()), this, SLOT(editContact()));
76
connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact()));
77
//! [connecting edit and remove]
78
connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
79
connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
80
connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
82
QVBoxLayout *buttonLayout1 = new QVBoxLayout;
83
buttonLayout1->addWidget(addButton);
84
//! [adding edit and remove to the layout]
85
buttonLayout1->addWidget(editButton);
86
buttonLayout1->addWidget(removeButton);
87
//! [adding edit and remove to the layout]
88
buttonLayout1->addWidget(submitButton);
89
buttonLayout1->addWidget(cancelButton);
90
buttonLayout1->addStretch();
92
QHBoxLayout *buttonLayout2 = new QHBoxLayout;
93
buttonLayout2->addWidget(previousButton);
94
buttonLayout2->addWidget(nextButton);
96
QGridLayout *mainLayout = new QGridLayout;
97
mainLayout->addWidget(nameLabel, 0, 0);
98
mainLayout->addWidget(nameLine, 0, 1);
99
mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
100
mainLayout->addWidget(addressText, 1, 1);
101
mainLayout->addLayout(buttonLayout1, 1, 2);
102
mainLayout->addLayout(buttonLayout2, 2, 1);
104
setLayout(mainLayout);
105
setWindowTitle(tr("Simple Address Book"));
108
void AddressBook::addContact()
110
oldName = nameLine->text();
111
oldAddress = addressText->toPlainText();
114
addressText->clear();
116
updateInterface(AddingMode);
118
//! [editContact() function]
119
void AddressBook::editContact()
121
oldName = nameLine->text();
122
oldAddress = addressText->toPlainText();
124
updateInterface(EditingMode);
126
//! [editContact() function]
127
//! [submitContact() function beginning]
128
void AddressBook::submitContact()
130
//! [submitContact() function beginning]
131
QString name = nameLine->text();
132
QString address = addressText->toPlainText();
134
if (name.isEmpty() || address.isEmpty()) {
135
QMessageBox::information(this, tr("Empty Field"),
136
tr("Please enter a name and address."));
139
//! [submitContact() function part1]
140
if (currentMode == AddingMode) {
142
if (!contacts.contains(name)) {
143
contacts.insert(name, address);
144
QMessageBox::information(this, tr("Add Successful"),
145
tr("\"%1\" has been added to your address book.").arg(name));
147
QMessageBox::information(this, tr("Add Unsuccessful"),
148
tr("Sorry, \"%1\" is already in your address book.").arg(name));
150
//! [submitContact() function part1]
151
//! [submitContact() function part2]
152
} else if (currentMode == EditingMode) {
154
if (oldName != name) {
155
if (!contacts.contains(name)) {
156
QMessageBox::information(this, tr("Edit Successful"),
157
tr("\"%1\" has been edited in your address book.").arg(oldName));
158
contacts.remove(oldName);
159
contacts.insert(name, address);
161
QMessageBox::information(this, tr("Edit Unsuccessful"),
162
tr("Sorry, \"%1\" is already in your address book.").arg(name));
164
} else if (oldAddress != address) {
165
QMessageBox::information(this, tr("Edit Successful"),
166
tr("\"%1\" has been edited in your address book.").arg(name));
167
contacts[name] = address;
171
updateInterface(NavigationMode);
173
//! [submitContact() function part2]
175
void AddressBook::cancel()
177
nameLine->setText(oldName);
178
addressText->setText(oldAddress);
179
updateInterface(NavigationMode);
181
//! [removeContact() function]
182
void AddressBook::removeContact()
184
QString name = nameLine->text();
185
QString address = addressText->toPlainText();
187
if (contacts.contains(name)) {
189
int button = QMessageBox::question(this,
190
tr("Confirm Remove"),
191
tr("Are you sure you want to remove \"%1\"?").arg(name),
192
QMessageBox::Yes | QMessageBox::No);
194
if (button == QMessageBox::Yes) {
197
contacts.remove(name);
199
QMessageBox::information(this, tr("Remove Successful"),
200
tr("\"%1\" has been removed from your address book.").arg(name));
204
updateInterface(NavigationMode);
206
//! [removeContact() function]
207
void AddressBook::next()
209
QString name = nameLine->text();
210
QMap<QString, QString>::iterator i = contacts.find(name);
212
if (i != contacts.end())
215
if (i == contacts.end())
216
i = contacts.begin();
218
nameLine->setText(i.key());
219
addressText->setText(i.value());
222
void AddressBook::previous()
224
QString name = nameLine->text();
225
QMap<QString, QString>::iterator i = contacts.find(name);
227
if (i == contacts.end()) {
229
addressText->clear();
233
if (i == contacts.begin())
237
nameLine->setText(i.key());
238
addressText->setText(i.value());
240
//! [update interface() part 1]
241
void AddressBook::updateInterface(Mode mode)
245
switch (currentMode) {
250
nameLine->setReadOnly(false);
251
nameLine->setFocus(Qt::OtherFocusReason);
252
addressText->setReadOnly(false);
254
addButton->setEnabled(false);
255
editButton->setEnabled(false);
256
removeButton->setEnabled(false);
258
nextButton->setEnabled(false);
259
previousButton->setEnabled(false);
261
submitButton->show();
262
cancelButton->show();
264
//! [update interface() part 1]
265
//! [update interface() part 2]
268
if (contacts.isEmpty()) {
270
addressText->clear();
273
nameLine->setReadOnly(true);
274
addressText->setReadOnly(true);
275
addButton->setEnabled(true);
277
int number = contacts.size();
278
editButton->setEnabled(number >= 1);
279
removeButton->setEnabled(number >= 1);
280
nextButton->setEnabled(number > 1);
281
previousButton->setEnabled(number >1 );
283
submitButton->hide();
284
cancelButton->hide();
288
//! [update interface() part 2]