OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / doc / examples / addressbook-sdk / part5 / addressbook.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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
23 **     permission.
24 **
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."
36 **
37 **************************************************************************/
38
39 #include "addressbook.h"
40 #include "ui_addressbook.h"
41
42 AddressBook::AddressBook(QWidget *parent)
43     : QWidget(parent), ui(new Ui::AddressBook)
44 {
45     ui->setupUi(this);
46
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);
53     ui->editButton->setEnabled(false);
54     ui->removeButton->setEnabled(false);
55
56     connect(ui->addButton, SIGNAL(clicked()), this,
57                 SLOT(addContact()));
58     connect(ui->submitButton, SIGNAL(clicked()), this,
59                 SLOT(submitContact()));
60     connect(ui->cancelButton, SIGNAL(clicked()), this,
61                 SLOT(cancel()));
62     connect(ui->nextButton, SIGNAL(clicked()), this,
63                 SLOT(next()));
64     connect(ui->previousButton, SIGNAL(clicked()), this,
65                 SLOT(previous()));
66     connect(ui->editButton, SIGNAL(clicked()), this,
67                 SLOT(editContact()));
68     connect(ui->removeButton, SIGNAL(clicked()), this,
69                 SLOT(removeContact()));
70 //! [signal slot]
71     connect(ui->findButton, SIGNAL(clicked()), this,
72                 SLOT(findContact()));
73 //! [signal slot]
74
75     setWindowTitle(tr("Simple Address Book"));
76 }
77
78 AddressBook::~AddressBook()
79 {
80     delete ui;
81 }
82
83 void AddressBook::addContact()
84 {
85     oldName = ui->nameLine->text();
86     oldAddress = ui->addressText->toPlainText();
87
88     ui->nameLine->clear();
89     ui->addressText->clear();
90
91     updateInterface(AddingMode);
92 }
93
94 void AddressBook::submitContact()
95 {
96     QString name = ui->nameLine->text();
97     QString address = ui->addressText->toPlainText();
98
99     if (name == "" || address == "") {
100         QMessageBox::information(this, tr("Empty Field"),
101             tr("Please enter a name and address."));
102         updateInterface(NavigationMode);
103         return;
104     }
105
106     if (currentMode == AddingMode) {
107
108         if (!contacts.contains(name)) {
109             contacts.insert(name, address);
110             QMessageBox::information(this, tr("Add Successful"),
111                 tr("\"%1\" has been added to your address book.").arg(name));
112         } else {
113             QMessageBox::information(this, tr("Add Unsuccessful"),
114                 tr("Sorry, \"%1\" is already in your address book.").arg(name));
115         }
116
117     } else if (currentMode == EditingMode) {
118
119         if (oldName != name) {
120             if (!contacts.contains(name)) {
121                 QMessageBox::information(this, tr("Edit Successful"),
122                     tr("\"%1\" has been edited in your address book.").arg(oldName));
123                 contacts.remove(oldName);
124                 contacts.insert(name, address);
125             } else  {
126                 QMessageBox::information(this, tr("Edit Unsuccessful"),
127                     tr("Sorry, \"%1\" is already in your address book.").arg(name));
128             }
129         } else if (oldAddress != address) {
130             QMessageBox::information(this, tr("Edit Successful"),
131                 tr("\"%1\" has been edited in your address book.").arg(name));
132             contacts[name] = address;
133         }
134     }
135     updateInterface(NavigationMode);
136 }
137
138 void AddressBook::cancel()
139 {
140     ui->nameLine->setText(oldName);
141     ui->nameLine->setReadOnly(true);
142
143     updateInterface(NavigationMode);
144 }
145
146 void AddressBook::next()
147 {
148     QString name = ui->nameLine->text();
149     QMap<QString, QString>::iterator i = contacts.find(name);
150
151     if (i != contacts.end())
152         i++;
153     if (i == contacts.end())
154         i = contacts.begin();
155
156     ui->nameLine->setText(i.key());
157     ui->addressText->setText(i.value());
158 }
159
160 void AddressBook::previous()
161 {
162     QString name = ui->nameLine->text();
163     QMap<QString, QString>::iterator i = contacts.find(name);
164
165     if (i == contacts.end()) {
166         ui->nameLine->clear();
167         ui->addressText->clear();
168         return;
169     }
170
171     if (i == contacts.begin())
172         i = contacts.end();
173
174     i--;
175     ui->nameLine->setText(i.key());
176     ui->addressText->setText(i.value());
177 }
178
179 void AddressBook::editContact()
180 {
181     oldName = ui->nameLine->text();
182     oldAddress = ui->addressText->toPlainText();
183
184     updateInterface(EditingMode);
185 }
186
187 void AddressBook::removeContact()
188 {
189     QString name = ui->nameLine->text();
190     QString address = ui->addressText->toPlainText();
191
192     if (contacts.contains(name)) {
193         int button = QMessageBox::question(this,
194             tr("Confirm Remove"),
195             tr("Are you sure you want to remove \"%1\"?").arg(name),
196             QMessageBox::Yes | QMessageBox::No);
197
198         if (button == QMessageBox::Yes) {
199             previous();
200             contacts.remove(name);
201
202             QMessageBox::information(this, tr("Remove Successful"),
203                 tr("\"%1\" has been removed from your address book.").arg(name));
204         }
205     }
206
207     updateInterface(NavigationMode);
208 }
209
210 void AddressBook::updateInterface(Mode mode)
211 {
212     currentMode = mode;
213
214     switch (currentMode) {
215
216     case AddingMode:
217     case EditingMode:
218
219         ui->nameLine->setReadOnly(false);
220         ui->nameLine->setFocus(Qt::OtherFocusReason);
221         ui->addressText->setReadOnly(false);
222
223         ui->addButton->setEnabled(false);
224         ui->editButton->setEnabled(false);
225         ui->removeButton->setEnabled(false);
226
227         ui->nextButton->setEnabled(false);
228         ui->previousButton->setEnabled(false);
229
230         ui->submitButton->show();
231         ui->cancelButton->show();
232         break;
233
234     case NavigationMode:
235
236         if (contacts.isEmpty()) {
237             ui->nameLine->clear();
238             ui->addressText->clear();
239         }
240
241         ui->nameLine->setReadOnly(true);
242         ui->addressText->setReadOnly(true);
243         ui->addButton->setEnabled(true);
244
245         int number = contacts.size();
246         ui->editButton->setEnabled(number >= 1);
247         ui->removeButton->setEnabled(number >= 1);
248 //! [enable]
249         ui->findButton->setEnabled(number > 2);
250 //! [enable]
251         ui->nextButton->setEnabled(number > 1);
252         ui->previousButton->setEnabled(number >1);
253
254         ui->submitButton->hide();
255         ui->cancelButton->hide();
256         break;
257     }
258 }
259
260 //! [findContact]
261 void AddressBook::findContact()
262 {
263     FindDialog dialog;
264
265     if (dialog.exec() == QDialog::Accepted) {
266         QString contactName = dialog.findText();
267
268         if (contacts.contains(contactName)) {
269             ui->nameLine->setText(contactName);
270             ui->addressText->setText(contacts.value(contactName));
271         } else {
272             QMessageBox::information(this, tr("Contact Not Found"),
273                 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
274             return;
275         }
276     }
277
278     updateInterface(NavigationMode);
279 }
280 //! [findContact]