source: trunk/examples/tutorials/addressbook-fr/part5/addressbook.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 9.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia 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//! [instantiating findButton]
63 findButton = new QPushButton(tr("&Find"));
64 findButton->setEnabled(false);
65//! [instantiating findButton]
66 submitButton = new QPushButton(tr("&Submit"));
67 submitButton->hide();
68 cancelButton = new QPushButton(tr("&Cancel"));
69 cancelButton->hide();
70
71 nextButton = new QPushButton(tr("&Next"));
72 nextButton->setEnabled(false);
73 previousButton = new QPushButton(tr("&Previous"));
74 previousButton->setEnabled(false);
75
76//! [instantiating FindDialog]
77 dialog = new FindDialog;
78//! [instantiating FindDialog]
79
80 connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
81 connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
82 connect(editButton, SIGNAL(clicked()), this, SLOT(editContact()));
83 connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
84 connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact()));
85//! [signals and slots for find]
86 connect(findButton, SIGNAL(clicked()), this, SLOT(findContact()));
87//! [signals and slots for find]
88 connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
89 connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
90
91 QVBoxLayout *buttonLayout1 = new QVBoxLayout;
92 buttonLayout1->addWidget(addButton);
93 buttonLayout1->addWidget(editButton);
94 buttonLayout1->addWidget(removeButton);
95//! [adding findButton to layout]
96 buttonLayout1->addWidget(findButton);
97//! [adding findButton to layout]
98 buttonLayout1->addWidget(submitButton);
99 buttonLayout1->addWidget(cancelButton);
100 buttonLayout1->addStretch();
101
102 QHBoxLayout *buttonLayout2 = new QHBoxLayout;
103 buttonLayout2->addWidget(previousButton);
104 buttonLayout2->addWidget(nextButton);
105
106 QGridLayout *mainLayout = new QGridLayout;
107 mainLayout->addWidget(nameLabel, 0, 0);
108 mainLayout->addWidget(nameLine, 0, 1);
109 mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
110 mainLayout->addWidget(addressText, 1, 1);
111 mainLayout->addLayout(buttonLayout1, 1, 2);
112 mainLayout->addLayout(buttonLayout2, 2, 1);
113
114 setLayout(mainLayout);
115 setWindowTitle(tr("Simple Address Book"));
116}
117
118void AddressBook::addContact()
119{
120 oldName = nameLine->text();
121 oldAddress = addressText->toPlainText();
122
123 nameLine->clear();
124 addressText->clear();
125
126 updateInterface(AddingMode);
127}
128
129void AddressBook::editContact()
130{
131 oldName = nameLine->text();
132 oldAddress = addressText->toPlainText();
133
134 updateInterface(EditingMode);
135}
136
137void AddressBook::submitContact()
138{
139 QString name = nameLine->text();
140 QString address = addressText->toPlainText();
141
142 if (name == "" || address == "") {
143 QMessageBox::information(this, tr("Empty Field"),
144 tr("Please enter a name and address."));
145 }
146
147 if (currentMode == AddingMode) {
148
149 if (!contacts.contains(name)) {
150 contacts.insert(name, address);
151 QMessageBox::information(this, tr("Add Successful"),
152 tr("\"%1\" has been added to your address book.").arg(name));
153 } else {
154 QMessageBox::information(this, tr("Add Unsuccessful"),
155 tr("Sorry, \"%1\" is already in your address book.").arg(name));
156 }
157 } else if (currentMode == EditingMode) {
158
159 if (oldName != name) {
160 if (!contacts.contains(name)) {
161 QMessageBox::information(this, tr("Edit Successful"),
162 tr("\"%1\" has been edited in your address book.").arg(oldName));
163 contacts.remove(oldName);
164 contacts.insert(name, address);
165 } else {
166 QMessageBox::information(this, tr("Edit Unsuccessful"),
167 tr("Sorry, \"%1\" is already in your address book.").arg(name));
168 }
169 } else if (oldAddress != address) {
170 QMessageBox::information(this, tr("Edit Successful"),
171 tr("\"%1\" has been edited in your address book.").arg(name));
172 contacts[name] = address;
173 }
174 }
175
176 updateInterface(NavigationMode);
177}
178
179void AddressBook::cancel()
180{
181 nameLine->setText(oldName);
182 addressText->setText(oldAddress);
183 updateInterface(NavigationMode);
184}
185
186void AddressBook::removeContact()
187{
188 QString name = nameLine->text();
189 QString address = addressText->toPlainText();
190
191 if (contacts.contains(name)) {
192
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
200 previous();
201 contacts.remove(name);
202
203 QMessageBox::information(this, tr("Remove Successful"),
204 tr("\"%1\" has been removed from your address book.").arg(name));
205 }
206 }
207
208 updateInterface(NavigationMode);
209}
210
211void AddressBook::next()
212{
213 QString name = nameLine->text();
214 QMap<QString, QString>::iterator i = contacts.find(name);
215
216 if (i != contacts.end())
217 i++;
218
219 if (i == contacts.end())
220 i = contacts.begin();
221
222 nameLine->setText(i.key());
223 addressText->setText(i.value());
224}
225
226void AddressBook::previous()
227{
228 QString name = nameLine->text();
229 QMap<QString, QString>::iterator i = contacts.find(name);
230
231 if (i == contacts.end()) {
232 nameLine->clear();
233 addressText->clear();
234 return;
235 }
236
237 if (i == contacts.begin())
238 i = contacts.end();
239
240 i--;
241 nameLine->setText(i.key());
242 addressText->setText(i.value());
243}
244//! [findContact() function]
245void AddressBook::findContact()
246{
247 dialog->show();
248
249 if (dialog->exec() == QDialog::Accepted) {
250 QString contactName = dialog->getFindText();
251
252 if (contacts.contains(contactName)) {
253 nameLine->setText(contactName);
254 addressText->setText(contacts.value(contactName));
255 } else {
256 QMessageBox::information(this, tr("Contact Not Found"),
257 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
258 return;
259 }
260 }
261
262 updateInterface(NavigationMode);
263}
264//! [findContact() function]
265
266void AddressBook::updateInterface(Mode mode)
267{
268 currentMode = mode;
269
270 switch (currentMode) {
271
272 case AddingMode:
273 case EditingMode:
274
275 nameLine->setReadOnly(false);
276 nameLine->setFocus(Qt::OtherFocusReason);
277 addressText->setReadOnly(false);
278
279 addButton->setEnabled(false);
280 editButton->setEnabled(false);
281 removeButton->setEnabled(false);
282
283 nextButton->setEnabled(false);
284 previousButton->setEnabled(false);
285
286 submitButton->show();
287 cancelButton->show();
288 break;
289
290 case NavigationMode:
291
292 if (contacts.isEmpty()) {
293 nameLine->clear();
294 addressText->clear();
295 }
296
297 nameLine->setReadOnly(true);
298 addressText->setReadOnly(true);
299 addButton->setEnabled(true);
300
301 int number = contacts.size();
302 editButton->setEnabled(number >= 1);
303 removeButton->setEnabled(number >= 1);
304 findButton->setEnabled(number > 2);
305 nextButton->setEnabled(number > 1);
306 previousButton->setEnabled(number > 1);
307
308 submitButton->hide();
309 cancelButton->hide();
310 break;
311 }
312}
Note: See TracBrowser for help on using the repository browser.