addressbook.cpp 2.63 KB
Newer Older
1 2 3 4
#include "addressbook.h"
#include "ui_addressbook.h"

AddressBook::AddressBook(QWidget *parent)
5
    : QWidget(parent), ui(new Ui::AddressBook)
6 7
{
    ui->setupUi(this);
8

9 10 11 12 13 14
    //! [setup fields]
    ui->nameLine->setReadOnly(true);
    ui->addressText->setReadOnly(true);
    ui->submitButton->hide();
    ui->cancelButton->hide();
    //! [setup fields]
15 16

    //! [signal slot]
17
    connect(ui->addButton, SIGNAL(clicked()), this,
18
                SLOT(addContact()));
19
    connect(ui->submitButton, SIGNAL(clicked()), this,
20
                SLOT(submitContact()));
21
    connect(ui->cancelButton, SIGNAL(clicked()), this,
22 23 24
                SLOT(cancel()));
    //! [signal slot]

25
    //! [window title]
26
    setWindowTitle(tr("Simple Address Book"));
27
    //! [window title]
28 29 30 31 32 33
}

AddressBook::~AddressBook()
{
    delete ui;
}
34

35
//! [addContact]
36 37
void AddressBook::addContact()
{
38 39
    oldName = ui->nameLine->text();
    oldAddress = ui->addressText->toPlainText();
40

41 42
    ui->nameLine->clear();
    ui->addressText->clear();
43

44 45 46
    ui->nameLine->setReadOnly(false);
    ui->nameLine->setFocus(Qt::OtherFocusReason);
    ui->addressText->setReadOnly(false);
47

48 49 50
    ui->addButton->setEnabled(false);
    ui->submitButton->show();
    ui->cancelButton->show();
51
}
52
//! [addContact]
53

54
//! [submitContact part1]
55 56
void AddressBook::submitContact()
{
57 58
    QString name = ui->nameLine->text();
    QString address = ui->addressText->toPlainText();
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

    if (name == "" || address == "") {
        QMessageBox::information(this, tr("Empty Field"),
            tr("Please enter a name and address."));
        return;
    }
//! [submitContact part1]

//! [submitContact part2]
    if (!contacts.contains(name)) {
        contacts.insert(name, address);
        QMessageBox::information(this, tr("Add Successful"),
            tr("\"%1\" has been added to your address book.").arg(name));
    } else {
        QMessageBox::information(this, tr("Add Unsuccessful"),
            tr("Sorry, \"%1\" is already in your address book.").arg(name));
        return;
    }
//! [submitContact part2]

//! [submitContact part3]
    if (contacts.isEmpty()) {
81 82
        ui->nameLine->clear();
        ui->addressText->clear();
83 84
    }

85 86 87 88 89
    ui->nameLine->setReadOnly(true);
    ui->addressText->setReadOnly(true);
    ui->addButton->setEnabled(true);
    ui->submitButton->hide();
    ui->cancelButton->hide();
90
}
91
//! [submitContact part3]
92

93
//! [cancel]
94 95
void AddressBook::cancel()
{
96 97
    ui->nameLine->setText(oldName);
    ui->nameLine->setReadOnly(true);
98

99 100
    ui->addressText->setText(oldAddress);
    ui->addressText->setReadOnly(true);
101

102 103 104
    ui->addButton->setEnabled(true);
    ui->submitButton->hide();
    ui->cancelButton->hide();
105
}
106
//! [cancel]