1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
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."
37 **************************************************************************/
39 #include "addressbook.h"
40 #include "ui_addressbook.h"
42 AddressBook::AddressBook(QWidget *parent)
43 : QWidget(parent), ui(new Ui::AddressBook)
47 ui->nameLine->setReadOnly(true);
48 ui->addressText->setReadOnly(true);
49 ui->submitButton->hide();
50 ui->cancelButton->hide();
51 ui->nextButton->setEnabled(false);
52 ui->previousButton->setEnabled(false);
55 ui->editButton->setEnabled(false);
56 ui->removeButton->setEnabled(false);
59 connect(ui->addButton, SIGNAL(clicked()), this,
61 connect(ui->submitButton, SIGNAL(clicked()), this,
62 SLOT(submitContact()));
63 connect(ui->cancelButton, SIGNAL(clicked()), this,
65 connect(ui->nextButton, SIGNAL(clicked()), this,
67 connect(ui->previousButton, SIGNAL(clicked()), this,
70 connect(ui->editButton, SIGNAL(clicked()), this,
72 connect(ui->removeButton, SIGNAL(clicked()), this,
73 SLOT(removeContact()));
76 setWindowTitle(tr("Simple Address Book"));
79 AddressBook::~AddressBook()
85 void AddressBook::addContact()
87 oldName = ui->nameLine->text();
88 oldAddress = ui->addressText->toPlainText();
90 ui->nameLine->clear();
91 ui->addressText->clear();
93 updateInterface(AddingMode);
97 //! [submitContact part1]
98 void AddressBook::submitContact()
100 //! [submitContact part1]
101 QString name = ui->nameLine->text();
102 QString address = ui->addressText->toPlainText();
104 if (name == "" || address == "") {
105 QMessageBox::information(this, tr("Empty Field"),
106 tr("Please enter a name and address."));
109 //! [submitContact part2]
110 if (currentMode == AddingMode) {
112 if (!contacts.contains(name)) {
113 contacts.insert(name, address);
114 QMessageBox::information(this, tr("Add Successful"),
115 tr("\"%1\" has been added to your address book.").arg(name));
117 QMessageBox::information(this, tr("Add Unsuccessful"),
118 tr("Sorry, \"%1\" is already in your address book.").arg(name));
121 //! [submitContact part2]
123 //! [submitContact part3]
124 } else if (currentMode == EditingMode) {
126 if (oldName != name) {
127 if (!contacts.contains(name)) {
128 QMessageBox::information(this, tr("Edit Successful"),
129 tr("\"%1\" has been edited in your address book.").arg(oldName));
130 contacts.remove(oldName);
131 contacts.insert(name, address);
133 QMessageBox::information(this, tr("Edit Unsuccessful"),
134 tr("Sorry, \"%1\" is already in your address book.").arg(name));
137 } else if (oldAddress != address) {
138 QMessageBox::information(this, tr("Edit Successful"),
139 tr("\"%1\" has been edited in your address book.").arg(name));
140 contacts[name] = address;
143 updateInterface(NavigationMode);
145 //! [submitContact part3]
148 void AddressBook::cancel()
150 ui->nameLine->setText(oldName);
151 ui->nameLine->setReadOnly(true);
153 updateInterface(NavigationMode);
157 void AddressBook::next()
159 QString name = ui->nameLine->text();
160 QMap<QString, QString>::iterator i = contacts.find(name);
162 if (i != contacts.end())
164 if (i == contacts.end())
165 i = contacts.begin();
167 ui->nameLine->setText(i.key());
168 ui->addressText->setText(i.value());
171 void AddressBook::previous()
173 QString name = ui->nameLine->text();
174 QMap<QString, QString>::iterator i = contacts.find(name);
176 if (i == contacts.end()) {
177 ui->nameLine->clear();
178 ui->addressText->clear();
182 if (i == contacts.begin())
186 ui->nameLine->setText(i.key());
187 ui->addressText->setText(i.value());
191 void AddressBook::editContact()
193 oldName = ui->nameLine->text();
194 oldAddress = ui->addressText->toPlainText();
196 updateInterface(EditingMode);
201 void AddressBook::removeContact()
203 QString name = ui->nameLine->text();
204 QString address = ui->addressText->toPlainText();
206 if (contacts.contains(name)) {
207 int button = QMessageBox::question(this,
208 tr("Confirm Remove"),
209 tr("Are you sure you want to remove \"%1\"?").arg(name),
210 QMessageBox::Yes | QMessageBox::No);
212 if (button == QMessageBox::Yes) {
214 contacts.remove(name);
216 QMessageBox::information(this, tr("Remove Successful"),
217 tr("\"%1\" has been removed from your address book.").arg(name));
221 updateInterface(NavigationMode);
225 //! [updateInterface part1]
226 void AddressBook::updateInterface(Mode mode)
230 switch (currentMode) {
235 ui->nameLine->setReadOnly(false);
236 ui->nameLine->setFocus(Qt::OtherFocusReason);
237 ui->addressText->setReadOnly(false);
239 ui->addButton->setEnabled(false);
240 ui->editButton->setEnabled(false);
241 ui->removeButton->setEnabled(false);
243 ui->nextButton->setEnabled(false);
244 ui->previousButton->setEnabled(false);
246 ui->submitButton->show();
247 ui->cancelButton->show();
249 //! [updateInterface part1]
251 //! [updateInterface part2]
254 if (contacts.isEmpty()) {
255 ui->nameLine->clear();
256 ui->addressText->clear();
259 ui->nameLine->setReadOnly(true);
260 ui->addressText->setReadOnly(true);
261 ui->addButton->setEnabled(true);
263 int number = contacts.size();
264 ui->editButton->setEnabled(number >= 1);
265 ui->removeButton->setEnabled(number >= 1);
266 ui->nextButton->setEnabled(number > 1);
267 ui->previousButton->setEnabled(number >1);
269 ui->submitButton->hide();
270 ui->cancelButton->hide();
274 //! [updateInterface part2]