OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / doc / examples / addressbook-sdk / part4 / 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
54 //! [extract objects]
55     ui->editButton->setEnabled(false);
56     ui->removeButton->setEnabled(false);
57 //! [extract objects]
58
59     connect(ui->addButton, SIGNAL(clicked()), this,
60                 SLOT(addContact()));
61     connect(ui->submitButton, SIGNAL(clicked()), this,
62                 SLOT(submitContact()));
63     connect(ui->cancelButton, SIGNAL(clicked()), this,
64                 SLOT(cancel()));
65     connect(ui->nextButton, SIGNAL(clicked()), this,
66                 SLOT(next()));
67     connect(ui->previousButton, SIGNAL(clicked()), this,
68                 SLOT(previous()));
69 //! [signal slot]
70     connect(ui->editButton, SIGNAL(clicked()), this,
71                 SLOT(editContact()));
72     connect(ui->removeButton, SIGNAL(clicked()), this,
73                 SLOT(removeContact()));
74 //! [signal slot]
75
76     setWindowTitle(tr("Simple Address Book"));
77 }
78
79 AddressBook::~AddressBook()
80 {
81     delete ui;
82 }
83
84 //! [addContact]
85 void AddressBook::addContact()
86 {
87     oldName = ui->nameLine->text();
88     oldAddress = ui->addressText->toPlainText();
89
90     ui->nameLine->clear();
91     ui->addressText->clear();
92
93     updateInterface(AddingMode);
94 }
95 //! [addContact]
96
97 //! [submitContact part1]
98 void AddressBook::submitContact()
99 {
100 //! [submitContact part1]
101     QString name = ui->nameLine->text();
102     QString address = ui->addressText->toPlainText();
103
104     if (name == "" || address == "") {
105         QMessageBox::information(this, tr("Empty Field"),
106             tr("Please enter a name and address."));
107     }
108
109 //! [submitContact part2]
110     if (currentMode == AddingMode) {
111
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));
116         } else {
117             QMessageBox::information(this, tr("Add Unsuccessful"),
118                 tr("Sorry, \"%1\" is already in your address book.").arg(name));
119             return;
120         }
121 //! [submitContact part2]
122
123 //! [submitContact part3]
124     } else if (currentMode == EditingMode) {
125
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);
132             } else  {
133                 QMessageBox::information(this, tr("Edit Unsuccessful"),
134                     tr("Sorry, \"%1\" is already in your address book.").arg(name));
135                 return;
136             }
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;
141         }
142     }
143     updateInterface(NavigationMode);
144 }
145 //! [submitContact part3]
146
147 //! [cancel]
148 void AddressBook::cancel()
149 {
150     ui->nameLine->setText(oldName);
151     ui->nameLine->setReadOnly(true);
152
153     updateInterface(NavigationMode);
154 }
155 //! [cancel]
156
157 void AddressBook::next()
158 {
159     QString name = ui->nameLine->text();
160     QMap<QString, QString>::iterator i = contacts.find(name);
161
162     if (i != contacts.end())
163         i++;
164     if (i == contacts.end())
165         i = contacts.begin();
166
167     ui->nameLine->setText(i.key());
168     ui->addressText->setText(i.value());
169 }
170
171 void AddressBook::previous()
172 {
173     QString name = ui->nameLine->text();
174     QMap<QString, QString>::iterator i = contacts.find(name);
175
176     if (i == contacts.end()) {
177         ui->nameLine->clear();
178         ui->addressText->clear();
179         return;
180     }
181
182     if (i == contacts.begin())
183         i = contacts.end();
184
185     i--;
186     ui->nameLine->setText(i.key());
187     ui->addressText->setText(i.value());
188 }
189
190 //! [editContact]
191 void AddressBook::editContact()
192 {
193     oldName = ui->nameLine->text();
194     oldAddress = ui->addressText->toPlainText();
195
196     updateInterface(EditingMode);
197 }
198 //! [editContact]
199
200 //! [removeContact]
201 void AddressBook::removeContact()
202 {
203     QString name = ui->nameLine->text();
204     QString address = ui->addressText->toPlainText();
205
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);
211
212         if (button == QMessageBox::Yes) {
213             previous();
214             contacts.remove(name);
215
216             QMessageBox::information(this, tr("Remove Successful"),
217                 tr("\"%1\" has been removed from your address book.").arg(name));
218         }
219     }
220
221     updateInterface(NavigationMode);
222 }
223 //! [removeContact]
224
225 //! [updateInterface part1]
226 void AddressBook::updateInterface(Mode mode)
227 {
228     currentMode = mode;
229
230     switch (currentMode) {
231
232     case AddingMode:
233     case EditingMode:
234
235         ui->nameLine->setReadOnly(false);
236         ui->nameLine->setFocus(Qt::OtherFocusReason);
237         ui->addressText->setReadOnly(false);
238
239         ui->addButton->setEnabled(false);
240         ui->editButton->setEnabled(false);
241         ui->removeButton->setEnabled(false);
242
243         ui->nextButton->setEnabled(false);
244         ui->previousButton->setEnabled(false);
245
246         ui->submitButton->show();
247         ui->cancelButton->show();
248         break;
249 //! [updateInterface part1]
250
251 //! [updateInterface part2]
252     case NavigationMode:
253
254         if (contacts.isEmpty()) {
255             ui->nameLine->clear();
256             ui->addressText->clear();
257         }
258
259         ui->nameLine->setReadOnly(true);
260         ui->addressText->setReadOnly(true);
261         ui->addButton->setEnabled(true);
262
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);
268
269         ui->submitButton->hide();
270         ui->cancelButton->hide();
271         break;
272     }
273 }
274 //! [updateInterface part2]