1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include "addressbook.h"
|
---|
43 |
|
---|
44 | AddressBook::AddressBook(QWidget *parent)
|
---|
45 | : QWidget(parent)
|
---|
46 | {
|
---|
47 | QLabel *nameLabel = new QLabel(tr("Name:"));
|
---|
48 | nameLine = new QLineEdit;
|
---|
49 | nameLine->setReadOnly(true);
|
---|
50 |
|
---|
51 | QLabel *addressLabel = new QLabel(tr("Address:"));
|
---|
52 | addressText = new QTextEdit;
|
---|
53 | addressText->setReadOnly(true);
|
---|
54 |
|
---|
55 | addButton = new QPushButton(tr("&Add"));
|
---|
56 | addButton->show();
|
---|
57 | submitButton = new QPushButton(tr("&Submit"));
|
---|
58 | submitButton->hide();
|
---|
59 | cancelButton = new QPushButton(tr("&Cancel"));
|
---|
60 | cancelButton->hide();
|
---|
61 | //! [navigation pushbuttons]
|
---|
62 | nextButton = new QPushButton(tr("&Next"));
|
---|
63 | nextButton->setEnabled(false);
|
---|
64 | previousButton = new QPushButton(tr("&Previous"));
|
---|
65 | previousButton->setEnabled(false);
|
---|
66 | //! [navigation pushbuttons]
|
---|
67 |
|
---|
68 | connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
|
---|
69 | connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
|
---|
70 | connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
|
---|
71 | //! [connecting navigation signals]
|
---|
72 | connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
|
---|
73 | connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
|
---|
74 | //! [connecting navigation signals]
|
---|
75 |
|
---|
76 | QVBoxLayout *buttonLayout1 = new QVBoxLayout;
|
---|
77 | buttonLayout1->addWidget(addButton, Qt::AlignTop);
|
---|
78 | buttonLayout1->addWidget(submitButton);
|
---|
79 | buttonLayout1->addWidget(cancelButton);
|
---|
80 | buttonLayout1->addStretch();
|
---|
81 | //! [navigation layout]
|
---|
82 | QHBoxLayout *buttonLayout2 = new QHBoxLayout;
|
---|
83 | buttonLayout2->addWidget(previousButton);
|
---|
84 | buttonLayout2->addWidget(nextButton);
|
---|
85 | //! [ navigation layout]
|
---|
86 | QGridLayout *mainLayout = new QGridLayout;
|
---|
87 | mainLayout->addWidget(nameLabel, 0, 0);
|
---|
88 | mainLayout->addWidget(nameLine, 0, 1);
|
---|
89 | mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
|
---|
90 | mainLayout->addWidget(addressText, 1, 1);
|
---|
91 | mainLayout->addLayout(buttonLayout1, 1, 2);
|
---|
92 | //! [adding navigation layout]
|
---|
93 | mainLayout->addLayout(buttonLayout2, 3, 1);
|
---|
94 | //! [adding navigation layout]
|
---|
95 | setLayout(mainLayout);
|
---|
96 | setWindowTitle(tr("Simple Address Book"));
|
---|
97 | }
|
---|
98 |
|
---|
99 | void AddressBook::addContact()
|
---|
100 | {
|
---|
101 | oldName = nameLine->text();
|
---|
102 | oldAddress = addressText->toPlainText();
|
---|
103 |
|
---|
104 | nameLine->clear();
|
---|
105 | addressText->clear();
|
---|
106 |
|
---|
107 | nameLine->setReadOnly(false);
|
---|
108 | nameLine->setFocus(Qt::OtherFocusReason);
|
---|
109 | addressText->setReadOnly(false);
|
---|
110 |
|
---|
111 | addButton->setEnabled(false);
|
---|
112 | //! [disabling navigation]
|
---|
113 | nextButton->setEnabled(false);
|
---|
114 | previousButton->setEnabled(false);
|
---|
115 | //! [disabling navigation]
|
---|
116 | submitButton->show();
|
---|
117 | cancelButton->show();
|
---|
118 | }
|
---|
119 |
|
---|
120 | void AddressBook::submitContact()
|
---|
121 | {
|
---|
122 | QString name = nameLine->text();
|
---|
123 | QString address = addressText->toPlainText();
|
---|
124 |
|
---|
125 | if (name.isEmpty() || address.isEmpty()) {
|
---|
126 | QMessageBox::information(this, tr("Empty Field"),
|
---|
127 | tr("Please enter a name and address."));
|
---|
128 | return;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (!contacts.contains(name)) {
|
---|
132 | contacts.insert(name, address);
|
---|
133 | QMessageBox::information(this, tr("Add Successful"),
|
---|
134 | tr("\"%1\" has been added to your address book.").arg(name));
|
---|
135 | } else {
|
---|
136 | QMessageBox::information(this, tr("Add Unsuccessful"),
|
---|
137 | tr("Sorry, \"%1\" is already in your address book.").arg(name));
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (contacts.isEmpty()) {
|
---|
141 | nameLine->clear();
|
---|
142 | addressText->clear();
|
---|
143 | }
|
---|
144 |
|
---|
145 | nameLine->setReadOnly(true);
|
---|
146 | addressText->setReadOnly(true);
|
---|
147 | addButton->setEnabled(true);
|
---|
148 |
|
---|
149 | //! [enabling navigation]
|
---|
150 | int number = contacts.size();
|
---|
151 | nextButton->setEnabled(number > 1);
|
---|
152 | previousButton->setEnabled(number > 1);
|
---|
153 | //! [enabling navigation]
|
---|
154 | submitButton->hide();
|
---|
155 | cancelButton->hide();
|
---|
156 | }
|
---|
157 |
|
---|
158 | void AddressBook::cancel()
|
---|
159 | {
|
---|
160 | nameLine->setText(oldName);
|
---|
161 | addressText->setText(oldAddress);
|
---|
162 |
|
---|
163 | if (contacts.isEmpty()) {
|
---|
164 | nameLine->clear();
|
---|
165 | addressText->clear();
|
---|
166 | }
|
---|
167 |
|
---|
168 | nameLine->setReadOnly(true);
|
---|
169 | addressText->setReadOnly(true);
|
---|
170 | addButton->setEnabled(true);
|
---|
171 |
|
---|
172 | int number = contacts.size();
|
---|
173 | nextButton->setEnabled(number > 1);
|
---|
174 | previousButton->setEnabled(number > 1);
|
---|
175 |
|
---|
176 | submitButton->hide();
|
---|
177 | cancelButton->hide();
|
---|
178 | }
|
---|
179 |
|
---|
180 | //! [next() function]
|
---|
181 | void AddressBook::next()
|
---|
182 | {
|
---|
183 | QString name = nameLine->text();
|
---|
184 | QMap<QString, QString>::iterator i = contacts.find(name);
|
---|
185 |
|
---|
186 | if (i != contacts.end())
|
---|
187 | i++;
|
---|
188 |
|
---|
189 | if (i == contacts.end())
|
---|
190 | i = contacts.begin();
|
---|
191 |
|
---|
192 | nameLine->setText(i.key());
|
---|
193 | addressText->setText(i.value());
|
---|
194 | }
|
---|
195 | //! [next() function]
|
---|
196 | //! [previous() function]
|
---|
197 | void AddressBook::previous()
|
---|
198 | {
|
---|
199 | QString name = nameLine->text();
|
---|
200 | QMap<QString, QString>::iterator i = contacts.find(name);
|
---|
201 |
|
---|
202 | if (i == contacts.end()){
|
---|
203 | nameLine->clear();
|
---|
204 | addressText->clear();
|
---|
205 | return;
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (i == contacts.begin())
|
---|
209 | i = contacts.end();
|
---|
210 |
|
---|
211 | i--;
|
---|
212 | nameLine->setText(i.key());
|
---|
213 | addressText->setText(i.value());
|
---|
214 | }
|
---|
215 | //! [previous() function]
|
---|