~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/widgets/tutorials/addressbook/part4/addressbook.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

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