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 addButton->show();
67 submitButton = new QPushButton(tr(s: "&Submit"));
68 submitButton->hide();
69 cancelButton = new QPushButton(tr(s: "&Cancel"));
70 cancelButton->hide();
71//! [navigation pushbuttons]
72 nextButton = new QPushButton(tr(s: "&Next"));
73 nextButton->setEnabled(false);
74 previousButton = new QPushButton(tr(s: "&Previous"));
75 previousButton->setEnabled(false);
76//! [navigation pushbuttons]
77 connect(sender: addButton, signal: &QPushButton::clicked,
78 receiver: this, slot: &AddressBook::addContact);
79 connect(sender: submitButton, signal: &QPushButton::clicked,
80 receiver: this, slot: &AddressBook::submitContact);
81 connect(sender: cancelButton, signal: &QPushButton::clicked,
82 receiver: this, slot: &AddressBook::cancel);
83//! [connecting navigation signals]
84 connect(sender: nextButton, signal: &QPushButton::clicked,
85 receiver: this, slot: &AddressBook::next);
86 connect(sender: previousButton, signal: &QPushButton::clicked,
87 receiver: this, slot: &AddressBook::previous);
88//! [connecting navigation signals]
89
90 QVBoxLayout *buttonLayout1 = new QVBoxLayout;
91 buttonLayout1->addWidget(addButton, stretch: 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, row: 0, column: 0);
102 mainLayout->addWidget(nameLine, row: 0, column: 1);
103 mainLayout->addWidget(addressLabel, row: 1, column: 0, Qt::AlignTop);
104 mainLayout->addWidget(addressText, row: 1, column: 1);
105 mainLayout->addLayout(buttonLayout1, row: 1, column: 2);
106//! [adding navigation layout]
107 mainLayout->addLayout(buttonLayout2, row: 2, column: 1);
108//! [adding navigation layout]
109 setLayout(mainLayout);
110 setWindowTitle(tr(s: "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(parent: this, title: tr(s: "Empty Field"),
141 text: tr(s: "Please enter a name and address."));
142 return;
143 }
144
145 if (!contacts.contains(akey: name)) {
146 contacts.insert(akey: name, avalue: address);
147 QMessageBox::information(parent: this, title: tr(s: "Add Successful"),
148 text: tr(s: "\"%1\" has been added to your address book.").arg(a: name));
149 } else {
150 QMessageBox::information(parent: this, title: tr(s: "Add Unsuccessful"),
151 text: tr(s: "Sorry, \"%1\" is already in your address book.").arg(a: 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(akey: 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(akey: 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

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