diff --git a/doc/examples/addressbook-sdk/part6/addressbook.cpp b/doc/examples/addressbook-sdk/part6/addressbook.cpp index de35f5ac738..50c92d33872 100644 --- a/doc/examples/addressbook-sdk/part6/addressbook.cpp +++ b/doc/examples/addressbook-sdk/part6/addressbook.cpp @@ -5,9 +5,261 @@ AddressBook::AddressBook(QWidget *parent) : QWidget(parent), ui(new Ui::AddressBook) { ui->setupUi(this); + + nameLine = new QLineEdit; + nameLine = ui->nameLine; + nameLine->setReadOnly(true); + + addressText = new QTextEdit; + addressText = ui->addressText; + addressText->setReadOnly(true); + + addButton = new QPushButton; + addButton = ui->addButton; + + submitButton = new QPushButton; + submitButton = ui->submitButton; + submitButton->hide(); + + cancelButton = new QPushButton; + cancelButton = ui->cancelButton; + cancelButton->hide(); + + nextButton = new QPushButton; + nextButton = ui->nextButton; + nextButton->setEnabled(false); + + previousButton = new QPushButton; + previousButton = ui->previousButton; + previousButton->setEnabled(false); + + editButton = new QPushButton; + editButton = ui->editButton; + editButton->setEnabled(false); + + removeButton = new QPushButton; + removeButton = ui->removeButton; + removeButton->setEnabled(false); + + findButton = new QPushButton; + findButton = ui->findButton; + + dialog = new FindDialog; + + connect(addButton, SIGNAL(clicked()), this, + SLOT(addContact())); + connect(submitButton, SIGNAL(clicked()), this, + SLOT(submitContact())); + connect(cancelButton, SIGNAL(clicked()), this, + SLOT(cancel())); + connect(nextButton, SIGNAL(clicked()), this, + SLOT(next())); + connect(previousButton, SIGNAL(clicked()), this, + SLOT(previous())); + connect(editButton, SIGNAL(clicked()), this, + SLOT(editContact())); + connect(removeButton, SIGNAL(clicked()), this, + SLOT(removeContact())); + connect(findButton, SIGNAL(clicked()), this, + SLOT(findContact())); + + setWindowTitle(tr("Simple Address Book")); } AddressBook::~AddressBook() { delete ui; } + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + updateInterface(AddingMode); +} + +void AddressBook::submitContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (name == "" || address == "") { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name and address.")); + } + + if (currentMode == AddingMode) { + + 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)); + } + + } else if (currentMode == EditingMode) { + + if (oldName != name) { + if (!contacts.contains(name)) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(oldName)); + contacts.remove(oldName); + contacts.insert(name, address); + } else { + QMessageBox::information(this, tr("Edit Unsuccessful"), + tr("Sorry, \"%1\" is already in your address book.").arg(name)); + } + } else if (oldAddress != address) { + QMessageBox::information(this, tr("Edit Successful"), + tr("\"%1\" has been edited in your address book.").arg(name)); + contacts[name] = address; + } + } + updateInterface(NavigationMode); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + nameLine->setReadOnly(true); + + updateInterface(NavigationMode); +} + +void AddressBook::next() +{ + QString name = nameLine->text(); + QMap::iterator i = contacts.find(name); + + if (i != contacts.end()) + i++; + if (i == contacts.end()) + i = contacts.begin(); + + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::previous() +{ + QString name = nameLine->text(); + QMap::iterator i = contacts.find(name); + + if (i == contacts.end()) { + nameLine->clear(); + addressText->clear(); + return; + } + + if (i == contacts.begin()) + i = contacts.end(); + + i--; + nameLine->setText(i.key()); + addressText->setText(i.value()); +} + +void AddressBook::editContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + updateInterface(EditingMode); +} + +void AddressBook::removeContact() +{ + QString name = nameLine->text(); + QString address = addressText->toPlainText(); + + if (contacts.contains(name)) { + int button = QMessageBox::question(this, + tr("Confirm Remove"), + tr("Are you sure you want to remove \"%1\"?").arg(name), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + previous(); + contacts.remove(name); + + QMessageBox::information(this, tr("Remove Successful"), + tr("\"%1\" has been removed from your address book.").arg(name)); + } + } + + updateInterface(NavigationMode); +} + +void AddressBook::updateInterface(Mode mode) +{ + currentMode = mode; + + switch (currentMode) { + + case AddingMode: + case EditingMode: + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + editButton->setEnabled(false); + removeButton->setEnabled(false); + + nextButton->setEnabled(false); + previousButton->setEnabled(false); + + submitButton->show(); + cancelButton->show(); + break; + + case NavigationMode: + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + editButton->setEnabled(number >= 1); + removeButton->setEnabled(number >= 1); + findButton->setEnabled(number > 2); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number >1); + + submitButton->hide(); + cancelButton->hide(); + break; + } +} + +void AddressBook::findContact() +{ + dialog->show(); + + if (dialog->exec() == QDialog::Accepted) { + QString contactName = dialog->getFindText(); + + if (contacts.contains(contactName)) { + nameLine->setText(contactName); + addressText->setText(contacts.value(contactName)); + } else { + QMessageBox::information(this, tr("Contact Not Found"), + tr("Sorry, \"%1\" is not in your address book.").arg(contactName)); + return; + } + } + + updateInterface(NavigationMode); +} diff --git a/doc/examples/addressbook-sdk/part6/addressbook.h b/doc/examples/addressbook-sdk/part6/addressbook.h index b85a0acddbf..3b193e65280 100644 --- a/doc/examples/addressbook-sdk/part6/addressbook.h +++ b/doc/examples/addressbook-sdk/part6/addressbook.h @@ -2,6 +2,11 @@ #define ADDRESSBOOK_H #include +#include +#include +#include +#include +#include "finddialog.h" namespace Ui { @@ -14,10 +19,39 @@ class AddressBook : public QWidget public: AddressBook(QWidget *parent = 0); + enum Mode { NavigationMode, AddingMode, EditingMode }; ~AddressBook(); +public slots: + void addContact(); + void submitContact(); + void cancel(); + void editContact(); + void removeContact(); + void next(); + void previous(); + void findContact(); + private: Ui::AddressBook *ui; + + void updateInterface(Mode mode); + QPushButton *addButton; + QPushButton *submitButton; + QPushButton *cancelButton; + QPushButton *editButton; + QPushButton *removeButton; + QPushButton *nextButton; + QPushButton *previousButton; + QPushButton *findButton; + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap contacts; + QString oldName; + QString oldAddress; + Mode currentMode; + FindDialog *dialog; }; #endif // ADDRESSBOOK_H diff --git a/doc/examples/addressbook-sdk/part6/addressbook.ui b/doc/examples/addressbook-sdk/part6/addressbook.ui index 7b771cc37b5..9d822ecb47b 100644 --- a/doc/examples/addressbook-sdk/part6/addressbook.ui +++ b/doc/examples/addressbook-sdk/part6/addressbook.ui @@ -1,20 +1,126 @@ + AddressBook - - + + 0 0 - 600 - 400 + 444 + 288 - + AddressBook + + + + + + + Name: + + + + + + + + + + Address: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + + + + + + Add + + + + + + + Submit + + + + + + + Cancel + + + + + + + Edit + + + + + + + Remove + + + + + + + Find + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + Previous + + + + + + + Next + + + + + + + + - - + diff --git a/doc/examples/addressbook-sdk/part6/finddialog.cpp b/doc/examples/addressbook-sdk/part6/finddialog.cpp new file mode 100644 index 00000000000..478ab9e9cd8 --- /dev/null +++ b/doc/examples/addressbook-sdk/part6/finddialog.cpp @@ -0,0 +1,46 @@ +#include "finddialog.h" +#include "ui_finddialog.h" +#include + +FindDialog::FindDialog(QWidget *parent) : + QDialog(parent), + m_ui(new Ui::FindDialog) +{ + m_ui->setupUi(this); + lineEdit = new QLineEdit; + lineEdit = m_ui->lineEdit; + + findButton = new QPushButton; + findButton = m_ui->findButton; + + findText = ""; + + connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); + + setWindowTitle(tr("Find a Contact")); +} + +FindDialog::~FindDialog() +{ + delete m_ui; +} + +void FindDialog::findClicked() +{ + QString text = lineEdit->text(); + + if (text.isEmpty()) { + QMessageBox::information(this, tr("Empty Field"), + tr("Please enter a name.")); + return; + } else { + findText = text; + lineEdit->clear(); + hide(); + } +} + +QString FindDialog::getFindText() +{ + return findText; +} diff --git a/doc/examples/addressbook-sdk/part6/finddialog.h b/doc/examples/addressbook-sdk/part6/finddialog.h new file mode 100644 index 00000000000..95bdaea5061 --- /dev/null +++ b/doc/examples/addressbook-sdk/part6/finddialog.h @@ -0,0 +1,29 @@ +#ifndef FINDDIALOG_H +#define FINDDIALOG_H + +#include +#include +#include + +namespace Ui { + class FindDialog; +} + +class FindDialog : public QDialog { + Q_OBJECT +public: + FindDialog(QWidget *parent = 0); + ~FindDialog(); + QString getFindText(); + +public slots: + void findClicked(); + +private: + Ui::FindDialog *m_ui; + QPushButton *findButton; + QLineEdit *lineEdit; + QString findText; +}; + +#endif // FINDDIALOG_H diff --git a/doc/examples/addressbook-sdk/part6/finddialog.ui b/doc/examples/addressbook-sdk/part6/finddialog.ui new file mode 100644 index 00000000000..09333e18cab --- /dev/null +++ b/doc/examples/addressbook-sdk/part6/finddialog.ui @@ -0,0 +1,42 @@ + + + FindDialog + + + + 0 + 0 + 475 + 71 + + + + Dialog + + + + + + + + Enter the name of a contact: + + + + + + + + + + Find + + + + + + + + + + diff --git a/doc/examples/addressbook-sdk/part6/part6.pro b/doc/examples/addressbook-sdk/part6/part6.pro index 5394461fe94..3daaf7f8462 100644 --- a/doc/examples/addressbook-sdk/part6/part6.pro +++ b/doc/examples/addressbook-sdk/part6/part6.pro @@ -1,16 +1,12 @@ -#------------------------------------------------- -# +# ------------------------------------------------- # Project created by QtCreator 2009-07-01T16:46:33 -# -#------------------------------------------------- - +# ------------------------------------------------- TARGET = part6 TEMPLATE = app - - -SOURCES += main.cpp\ - addressbook.cpp - -HEADERS += addressbook.h - -FORMS += addressbook.ui +SOURCES += main.cpp \ + addressbook.cpp \ + finddialog.cpp +HEADERS += addressbook.h \ + finddialog.h +FORMS += addressbook.ui \ + finddialog.ui