1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QtWidgets> |
52 | #include "addressbook.h" |
53 | |
54 | AddressBook::AddressBook(QWidget *parent) |
55 | : QWidget(parent) |
56 | { |
57 | QLabel *nameLabel = new QLabel(tr("Name:" )); |
58 | nameLine = new QLineEdit; |
59 | nameLine->setReadOnly(true); |
60 | |
61 | QLabel *addressLabel = new QLabel(tr("Address:" )); |
62 | addressText = new QTextEdit; |
63 | addressText->setReadOnly(true); |
64 | |
65 | addButton = new QPushButton(tr("&Add" )); |
66 | //! [edit and remove buttons] |
67 | editButton = new QPushButton(tr("&Edit" )); |
68 | editButton->setEnabled(false); |
69 | removeButton = new QPushButton(tr("&Remove" )); |
70 | removeButton->setEnabled(false); |
71 | //! [edit and remove buttons] |
72 | submitButton = new QPushButton(tr("&Submit" )); |
73 | submitButton->hide(); |
74 | cancelButton = new QPushButton(tr("&Cancel" )); |
75 | cancelButton->hide(); |
76 | |
77 | nextButton = new QPushButton(tr("&Next" )); |
78 | nextButton->setEnabled(false); |
79 | previousButton = new QPushButton(tr("&Previous" )); |
80 | previousButton->setEnabled(false); |
81 | |
82 | connect(addButton, &QPushButton::clicked, |
83 | this, &AddressBook::addContact); |
84 | connect(submitButton, &QPushButton::clicked, |
85 | this, &AddressBook::submitContact); |
86 | //! [connecting edit and remove] |
87 | connect(editButton, &QPushButton::clicked, |
88 | this, &AddressBook::editContact); |
89 | connect(removeButton, &QPushButton::clicked, |
90 | this, &AddressBook::removeContact); |
91 | //! [connecting edit and remove] |
92 | connect(cancelButton, &QPushButton::clicked, |
93 | this, &AddressBook::cancel); |
94 | connect(nextButton, &QPushButton::clicked, |
95 | this, &AddressBook::next); |
96 | connect(previousButton, &QPushButton::clicked, |
97 | this, &AddressBook::previous); |
98 | |
99 | QVBoxLayout *buttonLayout1 = new QVBoxLayout; |
100 | buttonLayout1->addWidget(addButton); |
101 | //! [adding edit and remove to the layout] |
102 | buttonLayout1->addWidget(editButton); |
103 | buttonLayout1->addWidget(removeButton); |
104 | //! [adding edit and remove to the layout] |
105 | buttonLayout1->addWidget(submitButton); |
106 | buttonLayout1->addWidget(cancelButton); |
107 | buttonLayout1->addStretch(); |
108 | |
109 | QHBoxLayout *buttonLayout2 = new QHBoxLayout; |
110 | buttonLayout2->addWidget(previousButton); |
111 | buttonLayout2->addWidget(nextButton); |
112 | |
113 | QGridLayout *mainLayout = new QGridLayout; |
114 | mainLayout->addWidget(nameLabel, 0, 0); |
115 | mainLayout->addWidget(nameLine, 0, 1); |
116 | mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); |
117 | mainLayout->addWidget(addressText, 1, 1); |
118 | mainLayout->addLayout(buttonLayout1, 1, 2); |
119 | mainLayout->addLayout(buttonLayout2, 2, 1); |
120 | |
121 | setLayout(mainLayout); |
122 | setWindowTitle(tr("Simple Address Book" )); |
123 | } |
124 | |
125 | void AddressBook::addContact() |
126 | { |
127 | oldName = nameLine->text(); |
128 | oldAddress = addressText->toPlainText(); |
129 | |
130 | nameLine->clear(); |
131 | addressText->clear(); |
132 | |
133 | updateInterface(AddingMode); |
134 | } |
135 | //! [editContact() function] |
136 | void AddressBook::editContact() |
137 | { |
138 | oldName = nameLine->text(); |
139 | oldAddress = addressText->toPlainText(); |
140 | |
141 | updateInterface(EditingMode); |
142 | } |
143 | //! [editContact() function] |
144 | //! [submitContact() function beginning] |
145 | void AddressBook::submitContact() |
146 | { |
147 | //! [submitContact() function beginning] |
148 | QString name = nameLine->text(); |
149 | QString address = addressText->toPlainText(); |
150 | |
151 | if (name.isEmpty() || address.isEmpty()) { |
152 | QMessageBox::information(this, tr("Empty Field" ), |
153 | tr("Please enter a name and address." )); |
154 | return; |
155 | } |
156 | //! [submitContact() function part1] |
157 | if (currentMode == AddingMode) { |
158 | |
159 | if (!contacts.contains(name)) { |
160 | contacts.insert(name, address); |
161 | QMessageBox::information(this, tr("Add Successful" ), |
162 | tr("\"%1\" has been added to your address book." ).arg(name)); |
163 | } else { |
164 | QMessageBox::information(this, tr("Add Unsuccessful" ), |
165 | tr("Sorry, \"%1\" is already in your address book." ).arg(name)); |
166 | } |
167 | //! [submitContact() function part1] |
168 | //! [submitContact() function part2] |
169 | } else if (currentMode == EditingMode) { |
170 | |
171 | if (oldName != name) { |
172 | if (!contacts.contains(name)) { |
173 | QMessageBox::information(this, tr("Edit Successful" ), |
174 | tr("\"%1\" has been edited in your address book." ).arg(oldName)); |
175 | contacts.remove(oldName); |
176 | contacts.insert(name, address); |
177 | } else { |
178 | QMessageBox::information(this, tr("Edit Unsuccessful" ), |
179 | tr("Sorry, \"%1\" is already in your address book." ).arg(name)); |
180 | } |
181 | } else if (oldAddress != address) { |
182 | QMessageBox::information(this, tr("Edit Successful" ), |
183 | tr("\"%1\" has been edited in your address book." ).arg(name)); |
184 | contacts[name] = address; |
185 | } |
186 | } |
187 | |
188 | updateInterface(NavigationMode); |
189 | } |
190 | //! [submitContact() function part2] |
191 | |
192 | void AddressBook::cancel() |
193 | { |
194 | nameLine->setText(oldName); |
195 | addressText->setText(oldAddress); |
196 | updateInterface(NavigationMode); |
197 | } |
198 | //! [removeContact() function] |
199 | void AddressBook::removeContact() |
200 | { |
201 | QString name = nameLine->text(); |
202 | QString address = addressText->toPlainText(); |
203 | |
204 | if (contacts.contains(name)) { |
205 | |
206 | int button = QMessageBox::question(this, |
207 | tr("Confirm Remove" ), |
208 | tr("Are you sure you want to remove \"%1\"?" ).arg(name), |
209 | QMessageBox::Yes | QMessageBox::No); |
210 | |
211 | if (button == QMessageBox::Yes) { |
212 | |
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() function] |
224 | void AddressBook::next() |
225 | { |
226 | QString name = nameLine->text(); |
227 | QMap<QString, QString>::iterator i = contacts.find(name); |
228 | |
229 | if (i != contacts.end()) |
230 | i++; |
231 | |
232 | if (i == contacts.end()) |
233 | i = contacts.begin(); |
234 | |
235 | nameLine->setText(i.key()); |
236 | addressText->setText(i.value()); |
237 | } |
238 | |
239 | void AddressBook::previous() |
240 | { |
241 | QString name = nameLine->text(); |
242 | QMap<QString, QString>::iterator i = contacts.find(name); |
243 | |
244 | if (i == contacts.end()) { |
245 | nameLine->clear(); |
246 | addressText->clear(); |
247 | return; |
248 | } |
249 | |
250 | if (i == contacts.begin()) |
251 | i = contacts.end(); |
252 | |
253 | i--; |
254 | nameLine->setText(i.key()); |
255 | addressText->setText(i.value()); |
256 | } |
257 | //! [update interface() part 1] |
258 | void AddressBook::updateInterface(Mode mode) |
259 | { |
260 | currentMode = mode; |
261 | |
262 | switch (currentMode) { |
263 | |
264 | case AddingMode: |
265 | case EditingMode: |
266 | |
267 | nameLine->setReadOnly(false); |
268 | nameLine->setFocus(Qt::OtherFocusReason); |
269 | addressText->setReadOnly(false); |
270 | |
271 | addButton->setEnabled(false); |
272 | editButton->setEnabled(false); |
273 | removeButton->setEnabled(false); |
274 | |
275 | nextButton->setEnabled(false); |
276 | previousButton->setEnabled(false); |
277 | |
278 | submitButton->show(); |
279 | cancelButton->show(); |
280 | break; |
281 | //! [update interface() part 1] |
282 | //! [update interface() part 2] |
283 | case NavigationMode: |
284 | |
285 | if (contacts.isEmpty()) { |
286 | nameLine->clear(); |
287 | addressText->clear(); |
288 | } |
289 | |
290 | nameLine->setReadOnly(true); |
291 | addressText->setReadOnly(true); |
292 | addButton->setEnabled(true); |
293 | |
294 | int number = contacts.size(); |
295 | editButton->setEnabled(number >= 1); |
296 | removeButton->setEnabled(number >= 1); |
297 | nextButton->setEnabled(number > 1); |
298 | previousButton->setEnabled(number >1 ); |
299 | |
300 | submitButton->hide(); |
301 | cancelButton->hide(); |
302 | break; |
303 | } |
304 | } |
305 | //! [update interface() part 2] |
306 | |