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("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 addButton->show();
67 submitButton = new QPushButton(tr("&Submit"));
68 submitButton->hide();
69 cancelButton = new QPushButton(tr("&Cancel"));
70 cancelButton->hide();
71//! [navigation pushbuttons]
72 nextButton = new QPushButton(tr("&Next"));
73 nextButton->setEnabled(false);
74 previousButton = new QPushButton(tr("&Previous"));
75 previousButton->setEnabled(false);
76//! [navigation pushbuttons]
77 connect(addButton, &QPushButton::clicked,
78 this, &AddressBook::addContact);
79 connect(submitButton, &QPushButton::clicked,
80 this, &AddressBook::submitContact);
81 connect(cancelButton, &QPushButton::clicked,
82 this, &AddressBook::cancel);
83//! [connecting navigation signals]
84 connect(nextButton, &QPushButton::clicked,
85 this, &AddressBook::next);
86 connect(previousButton, &QPushButton::clicked,
87 this, &AddressBook::previous);
88//! [connecting navigation signals]
89
90 QVBoxLayout *buttonLayout1 = new QVBoxLayout;
91 buttonLayout1->addWidget(addButton, Qt::AlignTop);
92 buttonLayout1->addWidget(submitButton);
93 buttonLayout1->addWidget(cancelButton);
94 buttonLayout1->addStretch();
95//! [navigation layout]
96 QHBoxLayout *buttonLayout2 = new QHBoxLayout;
97 buttonLayout2->addWidget(previousButton);
98 buttonLayout2->addWidget(nextButton);
99//! [ navigation layout]
100 QGridLayout *mainLayout = new QGridLayout;
101 mainLayout->addWidget(nameLabel, 0, 0);
102 mainLayout->addWidget(nameLine, 0, 1);
103 mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
104 mainLayout->addWidget(addressText, 1, 1);
105 mainLayout->addLayout(buttonLayout1, 1, 2);
106//! [adding navigation layout]
107 mainLayout->addLayout(buttonLayout2, 2, 1);
108//! [adding navigation layout]
109 setLayout(mainLayout);
110 setWindowTitle(tr("Simple Address Book"));
111}
112
113void AddressBook::addContact()
114{
115 oldName = nameLine->text();
116 oldAddress = addressText->toPlainText();
117
118 nameLine->clear();
119 addressText->clear();
120
121 nameLine->setReadOnly(false);
122 nameLine->setFocus(Qt::OtherFocusReason);
123 addressText->setReadOnly(false);
124
125 addButton->setEnabled(false);
126//! [disabling navigation]
127 nextButton->setEnabled(false);
128 previousButton->setEnabled(false);
129//! [disabling navigation]
130 submitButton->show();
131 cancelButton->show();
132}
133
134void AddressBook::submitContact()
135{
136 QString name = nameLine->text();
137 QString address = addressText->toPlainText();
138
139 if (name.isEmpty() || address.isEmpty()) {
140 QMessageBox::information(this, tr("Empty Field"),
141 tr("Please enter a name and address."));
142 return;
143 }
144
145 if (!contacts.contains(name)) {
146 contacts.insert(name, address);
147 QMessageBox::information(this, tr("Add Successful"),
148 tr("\"%1\" has been added to your address book.").arg(name));
149 } else {
150 QMessageBox::information(this, tr("Add Unsuccessful"),
151 tr("Sorry, \"%1\" is already in your address book.").arg(name));
152 }
153
154 if (contacts.isEmpty()) {
155 nameLine->clear();
156 addressText->clear();
157 }
158
159 nameLine->setReadOnly(true);
160 addressText->setReadOnly(true);
161 addButton->setEnabled(true);
162
163//! [enabling navigation]
164 int number = contacts.size();
165 nextButton->setEnabled(number > 1);
166 previousButton->setEnabled(number > 1);
167//! [enabling navigation]
168 submitButton->hide();
169 cancelButton->hide();
170}
171
172void AddressBook::cancel()
173{
174 nameLine->setText(oldName);
175 addressText->setText(oldAddress);
176
177 if (contacts.isEmpty()) {
178 nameLine->clear();
179 addressText->clear();
180 }
181
182 nameLine->setReadOnly(true);
183 addressText->setReadOnly(true);
184 addButton->setEnabled(true);
185
186 int number = contacts.size();
187 nextButton->setEnabled(number > 1);
188 previousButton->setEnabled(number > 1);
189
190 submitButton->hide();
191 cancelButton->hide();
192}
193
194//! [next() function]
195void AddressBook::next()
196{
197 QString name = nameLine->text();
198 QMap<QString, QString>::iterator i = contacts.find(name);
199
200 if (i != contacts.end())
201 i++;
202
203 if (i == contacts.end())
204 i = contacts.begin();
205
206 nameLine->setText(i.key());
207 addressText->setText(i.value());
208}
209//! [next() function]
210//! [previous() function]
211void AddressBook::previous()
212{
213 QString name = nameLine->text();
214 QMap<QString, QString>::iterator i = contacts.find(name);
215
216 if (i == contacts.end()){
217 nameLine->clear();
218 addressText->clear();
219 return;
220 }
221
222 if (i == contacts.begin())
223 i = contacts.end();
224
225 i--;
226 nameLine->setText(i.key());
227 addressText->setText(i.value());
228}
229//! [previous() function]
230