addressbook.cpp 2.86 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 15 16 17 18
    //! [extract objects]
    nameLine = new QLineEdit;
    nameLine = ui->nameLine;
    nameLine->setReadOnly(true);

    addressText = new QTextEdit;
    addressText = ui->addressText;
    addressText->setReadOnly(true);

    addButton = new QPushButton;
19 20
    addButton = ui->addButton;

21
    submitButton = new QPushButton;
22
    submitButton = ui->submitButton;
23
    submitButton->hide();
24

25
    cancelButton = new QPushButton;
26
    cancelButton = ui->cancelButton;
27 28 29 30 31 32 33 34 35 36 37 38
    cancelButton->hide();
    //! [extract objects]

    //! [signal slot]
    connect(addButton, SIGNAL(clicked()), this,
                SLOT(addContact()));
    connect(submitButton, SIGNAL(clicked()), this,
                SLOT(submitContact()));
    connect(cancelButton, SIGNAL(clicked()), this,
                SLOT(cancel()));
    //! [signal slot]

39
    //! [window title]
40
    setWindowTitle(tr("Simple Address Book"));
41
    //! [window title]
42 43 44 45 46 47
}

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

49
//! [addContact]
50 51
void AddressBook::addContact()
{
52
    oldName = nameLine->text();
53
    oldAddress = addressText->toPlainText();
54 55 56 57 58 59 60 61 62 63 64

    nameLine->clear();
    addressText->clear();

    nameLine->setReadOnly(false);
    nameLine->setFocus(Qt::OtherFocusReason);
    addressText->setReadOnly(false);

    addButton->setEnabled(false);
    submitButton->show();
    cancelButton->show();
65
}
66
//! [addContact]
67

68
//! [submitContact part1]
69 70
void AddressBook::submitContact()
{
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    QString name = nameLine->text();
    QString address = addressText->toPlainText();

    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));
        return;
    } 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()) {
        nameLine->clear();
        addressText->clear();
    }

    nameLine->setReadOnly(true);
    addressText->setReadOnly(true);
    addButton->setEnabled(true);
    submitButton->hide();
    cancelButton->hide();
105
}
106
//! [submitContact part3]
107

108
//! [cancel]
109 110
void AddressBook::cancel()
{
111 112 113 114 115 116 117 118 119
    nameLine->setText(oldName);
    nameLine->setReadOnly(true);

    addressText->setText(oldAddress);
    addressText->setReadOnly(true);

    addButton->setEnabled(true);
    submitButton->hide();
    cancelButton->hide();
120
}
121
//! [cancel]