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
54AddressBook::AddressBook(QWidget *parent)
55 : QWidget(parent)
56{
57 QLabel *nameLabel = new QLabel(tr(s: "Name:"));
58 nameLine = new QLineEdit;
59 nameLine->setReadOnly(true);
60
61 QLabel *addressLabel = new QLabel(tr(s: "Address:"));
62 addressText = new QTextEdit;
63 addressText->setReadOnly(true);
64
65 addButton = new QPushButton(tr(s: "&Add"));
66//! [edit and remove buttons]
67 editButton = new QPushButton(tr(s: "&Edit"));
68 editButton->setEnabled(false);
69 removeButton = new QPushButton(tr(s: "&Remove"));
70 removeButton->setEnabled(false);
71//! [edit and remove buttons]
72 submitButton = new QPushButton(tr(s: "&Submit"));
73 submitButton->hide();
74 cancelButton = new QPushButton(tr(s: "&Cancel"));
75 cancelButton->hide();
76
77 nextButton = new QPushButton(tr(s: "&Next"));
78 nextButton->setEnabled(false);
79 previousButton = new QPushButton(tr(s: "&Previous"));
80 previousButton->setEnabled(false);
81
82 connect(sender: addButton, signal: &QPushButton::clicked,
83 receiver: this, slot: &AddressBook::addContact);
84 connect(sender: submitButton, signal: &QPushButton::clicked,
85 receiver: this, slot: &AddressBook::submitContact);
86//! [connecting edit and remove]
87 connect(sender: editButton, signal: &QPushButton::clicked,
88 receiver: this, slot: &AddressBook::editContact);
89 connect(sender: removeButton, signal: &QPushButton::clicked,
90 receiver: this, slot: &AddressBook::removeContact);
91//! [connecting edit and remove]
92 connect(sender: cancelButton, signal: &QPushButton::clicked,
93 receiver: this, slot: &AddressBook::cancel);
94 connect(sender: nextButton, signal: &QPushButton::clicked,
95 receiver: this, slot: &AddressBook::next);
96 connect(sender: previousButton, signal: &QPushButton::clicked,
97 receiver: this, slot: &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, row: 0, column: 0);
115 mainLayout->addWidget(nameLine, row: 0, column: 1);
116 mainLayout->addWidget(addressLabel, row: 1, column: 0, Qt::AlignTop);
117 mainLayout->addWidget(addressText, row: 1, column: 1);
118 mainLayout->addLayout(buttonLayout1, row: 1, column: 2);
119 mainLayout->addLayout(buttonLayout2, row: 2, column: 1);
120
121 setLayout(mainLayout);
122 setWindowTitle(tr(s: "Simple Address Book"));
123}
124
125void AddressBook::addContact()
126{
127 oldName = nameLine->text();
128 oldAddress = addressText->toPlainText();
129
130 nameLine->clear();
131 addressText->clear();
132
133 updateInterface(mode: AddingMode);
134}
135//! [editContact() function]
136void AddressBook::editContact()
137{
138 oldName = nameLine->text();
139 oldAddress = addressText->toPlainText();
140
141 updateInterface(mode: EditingMode);
142}
143//! [editContact() function]
144//! [submitContact() function beginning]
145void 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(parent: this, title: tr(s: "Empty Field"),
153 text: tr(s: "Please enter a name and address."));
154 return;
155 }
156//! [submitContact() function part1]
157 if (currentMode == AddingMode) {
158
159 if (!contacts.contains(akey: name)) {
160 contacts.insert(akey: name, avalue: address);
161 QMessageBox::information(parent: this, title: tr(s: "Add Successful"),
162 text: tr(s: "\"%1\" has been added to your address book.").arg(a: name));
163 } else {
164 QMessageBox::information(parent: this, title: tr(s: "Add Unsuccessful"),
165 text: tr(s: "Sorry, \"%1\" is already in your address book.").arg(a: name));
166 }
167//! [submitContact() function part1]
168//! [submitContact() function part2]
169 } else if (currentMode == EditingMode) {
170
171 if (oldName != name) {
172 if (!contacts.contains(akey: name)) {
173 QMessageBox::information(parent: this, title: tr(s: "Edit Successful"),
174 text: tr(s: "\"%1\" has been edited in your address book.").arg(a: oldName));
175 contacts.remove(akey: oldName);
176 contacts.insert(akey: name, avalue: address);
177 } else {
178 QMessageBox::information(parent: this, title: tr(s: "Edit Unsuccessful"),
179 text: tr(s: "Sorry, \"%1\" is already in your address book.").arg(a: name));
180 }
181 } else if (oldAddress != address) {
182 QMessageBox::information(parent: this, title: tr(s: "Edit Successful"),
183 text: tr(s: "\"%1\" has been edited in your address book.").arg(a: name));
184 contacts[name] = address;
185 }
186 }
187
188 updateInterface(mode: NavigationMode);
189}
190//! [submitContact() function part2]
191
192void AddressBook::cancel()
193{
194 nameLine->setText(oldName);
195 addressText->setText(oldAddress);
196 updateInterface(mode: NavigationMode);
197}
198//! [removeContact() function]
199void AddressBook::removeContact()
200{
201 QString name = nameLine->text();
202 QString address = addressText->toPlainText();
203
204 if (contacts.contains(akey: name)) {
205
206 int button = QMessageBox::question(parent: this,
207 title: tr(s: "Confirm Remove"),
208 text: tr(s: "Are you sure you want to remove \"%1\"?").arg(a: name),
209 buttons: QMessageBox::Yes | QMessageBox::No);
210
211 if (button == QMessageBox::Yes) {
212
213 previous();
214 contacts.remove(akey: name);
215
216 QMessageBox::information(parent: this, title: tr(s: "Remove Successful"),
217 text: tr(s: "\"%1\" has been removed from your address book.").arg(a: name));
218 }
219 }
220
221 updateInterface(mode: NavigationMode);
222}
223//! [removeContact() function]
224void AddressBook::next()
225{
226 QString name = nameLine->text();
227 QMap<QString, QString>::iterator i = contacts.find(akey: 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
239void AddressBook::previous()
240{
241 QString name = nameLine->text();
242 QMap<QString, QString>::iterator i = contacts.find(akey: 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]
258void 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

source code of qtbase/examples/widgets/tutorials/addressbook/part4/addressbook.cpp