1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 | #include "addressbook.h"
|
---|
44 |
|
---|
45 | AddressBook::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 | addButton->show();
|
---|
58 | submitButton = new QPushButton(tr("&Submit"));
|
---|
59 | submitButton->hide();
|
---|
60 | cancelButton = new QPushButton(tr("&Cancel"));
|
---|
61 | cancelButton->hide();
|
---|
62 | //! [navigation pushbuttons]
|
---|
63 | nextButton = new QPushButton(tr("&Next"));
|
---|
64 | nextButton->setEnabled(false);
|
---|
65 | previousButton = new QPushButton(tr("&Previous"));
|
---|
66 | previousButton->setEnabled(false);
|
---|
67 | //! [navigation pushbuttons]
|
---|
68 |
|
---|
69 | connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
|
---|
70 | connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
|
---|
71 | connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
|
---|
72 | //! [connecting navigation signals]
|
---|
73 | connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
|
---|
74 | connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
|
---|
75 | //! [connecting navigation signals]
|
---|
76 |
|
---|
77 | QVBoxLayout *buttonLayout1 = new QVBoxLayout;
|
---|
78 | buttonLayout1->addWidget(addButton, Qt::AlignTop);
|
---|
79 | buttonLayout1->addWidget(submitButton);
|
---|
80 | buttonLayout1->addWidget(cancelButton);
|
---|
81 | buttonLayout1->addStretch();
|
---|
82 | //! [navigation layout]
|
---|
83 | QHBoxLayout *buttonLayout2 = new QHBoxLayout;
|
---|
84 | buttonLayout2->addWidget(previousButton);
|
---|
85 | buttonLayout2->addWidget(nextButton);
|
---|
86 | //! [ navigation layout]
|
---|
87 | QGridLayout *mainLayout = new QGridLayout;
|
---|
88 | mainLayout->addWidget(nameLabel, 0, 0);
|
---|
89 | mainLayout->addWidget(nameLine, 0, 1);
|
---|
90 | mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
|
---|
91 | mainLayout->addWidget(addressText, 1, 1);
|
---|
92 | mainLayout->addLayout(buttonLayout1, 1, 2);
|
---|
93 | //! [adding navigation layout]
|
---|
94 | mainLayout->addLayout(buttonLayout2, 3, 1);
|
---|
95 | //! [adding navigation layout]
|
---|
96 | setLayout(mainLayout);
|
---|
97 | setWindowTitle(tr("Simple Address Book"));
|
---|
98 | }
|
---|
99 |
|
---|
100 | void AddressBook::addContact()
|
---|
101 | {
|
---|
102 | oldName = nameLine->text();
|
---|
103 | oldAddress = addressText->toPlainText();
|
---|
104 |
|
---|
105 | nameLine->clear();
|
---|
106 | addressText->clear();
|
---|
107 |
|
---|
108 | nameLine->setReadOnly(false);
|
---|
109 | nameLine->setFocus(Qt::OtherFocusReason);
|
---|
110 | addressText->setReadOnly(false);
|
---|
111 |
|
---|
112 | addButton->setEnabled(false);
|
---|
113 | //! [disabling navigation]
|
---|
114 | nextButton->setEnabled(false);
|
---|
115 | previousButton->setEnabled(false);
|
---|
116 | //! [disabling navigation]
|
---|
117 | submitButton->show();
|
---|
118 | cancelButton->show();
|
---|
119 | }
|
---|
120 |
|
---|
121 | void AddressBook::submitContact()
|
---|
122 | {
|
---|
123 | QString name = nameLine->text();
|
---|
124 | QString address = addressText->toPlainText();
|
---|
125 |
|
---|
126 | if (name == "" || address == "") {
|
---|
127 | QMessageBox::information(this, tr("Empty Field"),
|
---|
128 | tr("Please enter a name and adderss."));
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (!contacts.contains(name)) {
|
---|
133 | contacts.insert(name, address);
|
---|
134 | QMessageBox::information(this, tr("Add Successful"),
|
---|
135 | tr("\"%1\" has been added to your address book.").arg(name));
|
---|
136 | } else {
|
---|
137 | QMessageBox::information(this, tr("Add Unsuccessful"),
|
---|
138 | tr("Sorry, \"%1\" is already in your address book.").arg(name));
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (contacts.isEmpty()) {
|
---|
143 | nameLine->clear();
|
---|
144 | addressText->clear();
|
---|
145 | }
|
---|
146 |
|
---|
147 | nameLine->setReadOnly(true);
|
---|
148 | addressText->setReadOnly(true);
|
---|
149 | addButton->setEnabled(true);
|
---|
150 |
|
---|
151 | //! [enabling navigation]
|
---|
152 | int number = contacts.size();
|
---|
153 | nextButton->setEnabled(number > 1);
|
---|
154 | previousButton->setEnabled(number > 1);
|
---|
155 | //! [enabling navigation]
|
---|
156 | submitButton->hide();
|
---|
157 | cancelButton->hide();
|
---|
158 | }
|
---|
159 |
|
---|
160 | void AddressBook::cancel()
|
---|
161 | {
|
---|
162 | nameLine->setText(oldName);
|
---|
163 | addressText->setText(oldAddress);
|
---|
164 |
|
---|
165 | if (contacts.isEmpty()) {
|
---|
166 | nameLine->clear();
|
---|
167 | addressText->clear();
|
---|
168 | }
|
---|
169 |
|
---|
170 | nameLine->setReadOnly(true);
|
---|
171 | addressText->setReadOnly(true);
|
---|
172 | addButton->setEnabled(true);
|
---|
173 |
|
---|
174 | int number = contacts.size();
|
---|
175 | nextButton->setEnabled(number > 1);
|
---|
176 | previousButton->setEnabled(number > 1);
|
---|
177 |
|
---|
178 | submitButton->hide();
|
---|
179 | cancelButton->hide();
|
---|
180 | }
|
---|
181 |
|
---|
182 | //! [next() function]
|
---|
183 | void AddressBook::next()
|
---|
184 | {
|
---|
185 | QString name = nameLine->text();
|
---|
186 | QMap<QString, QString>::iterator i = contacts.find(name);
|
---|
187 |
|
---|
188 | if (i != contacts.end())
|
---|
189 | i++;
|
---|
190 |
|
---|
191 | if (i == contacts.end())
|
---|
192 | i = contacts.begin();
|
---|
193 |
|
---|
194 | nameLine->setText(i.key());
|
---|
195 | addressText->setText(i.value());
|
---|
196 | }
|
---|
197 | //! [next() function]
|
---|
198 | //! [previous() function]
|
---|
199 | void AddressBook::previous()
|
---|
200 | {
|
---|
201 | QString name = nameLine->text();
|
---|
202 | QMap<QString, QString>::iterator i = contacts.find(name);
|
---|
203 |
|
---|
204 | if (i == contacts.end()){
|
---|
205 | nameLine->clear();
|
---|
206 | addressText->clear();
|
---|
207 | return;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (i == contacts.begin())
|
---|
211 | i = contacts.end();
|
---|
212 |
|
---|
213 | i--;
|
---|
214 | nameLine->setText(i.key());
|
---|
215 | addressText->setText(i.value());
|
---|
216 | }
|
---|
217 | //! [previous() function]
|
---|