From 38a6806ee187b8953c01586209a82063dc031d04 Mon Sep 17 00:00:00 2001 From: Kavindra Devi Palaraja Date: Fri, 5 Jun 2009 16:25:31 +0200 Subject: [PATCH] Doc - Starting with Part 4 Reviewed-by: TrustMe --- doc/addressbook-sdk.qdoc | 55 ++++++ doc/examples/addressbook-sdk/part3/main.cpp | 2 - .../addressbook-sdk/part4/addressbook.cpp | 161 ++++++++++++++++++ .../addressbook-sdk/part4/addressbook.h | 58 +++++++ .../addressbook-sdk/part4/addressbook.ui | 111 ++++++++++++ doc/examples/addressbook-sdk/part4/main.cpp | 10 ++ doc/examples/addressbook-sdk/part4/part4.pro | 16 ++ 7 files changed, 411 insertions(+), 2 deletions(-) create mode 100644 doc/examples/addressbook-sdk/part4/addressbook.cpp create mode 100644 doc/examples/addressbook-sdk/part4/addressbook.h create mode 100644 doc/examples/addressbook-sdk/part4/addressbook.ui create mode 100644 doc/examples/addressbook-sdk/part4/main.cpp create mode 100644 doc/examples/addressbook-sdk/part4/part4.pro diff --git a/doc/addressbook-sdk.qdoc b/doc/addressbook-sdk.qdoc index 572149c78be..2ca6e9746d1 100644 --- a/doc/addressbook-sdk.qdoc +++ b/doc/addressbook-sdk.qdoc @@ -542,3 +542,58 @@ Again, we display the contents of the current object in \c contacts. */ + +/*! + \page tutorials-addressbook-sdk-part4.html + \previouspage Address Book 3 - Navigating between Entries + \contentspage {Address Book Tutorial}{Contents} + \nextpage \l{examples/addressbook-sdk/part5}{Chapter 5} + \example examples/addressbook-sdk/part4 + \title Address Book 4 - Editing and Removing Addresses} + + In this chapter, we look at ways to modify the contents of contacts stored + in the address book application. + + #screenshot + + We now have an address book that not only holds contacts in an organized + manner, but also allows navigation. It would be convenient to include edit + and remove functions so that a contact's details can be changed when + needed. However, this requires a little improvement, in the form of enums. + + In our previous chapters, we had two modes: \c AddingMode and + \c NavigationMode - but they were not defined as enums. Instead, we enabled + and disabled the corresponding buttons manually, resulting in multiple + lines of repeated code. + + In this chapter, we define the \c Mode enum with three different values: + \list + \o \c{NavigationMode}, + \o \c{AddingMode}, and + \o \c{EditingMode}. + \endlist + + \section1 Placing Widgets on the Form + + \section1 The AddressBook Class + + We update the header file to contain the \c Mode enum: + + \snippet examples/addressbook-sdk/part4/addressbook.h enum + + We also add two new slots, \c editContact() and \c removeContact(), to our + current list of public slots. + + \snippet examples/addressbook-sdk/part4/addressbook.h slot definition + +*/ + +/*! + \page tutorials-addressbook-sdk-part5.html + \previouspage Address Book 4 - Editing and Removing Addresses + \contentspage {Address Book Tutorial}{Contents} + \nextpage \l{examples/addressbook-sdk/part6}{Chapter 6} + \example examples/addressbook-sdk/part5 + \title Address Book 5 - Adding a Find Function} + +*/ diff --git a/doc/examples/addressbook-sdk/part3/main.cpp b/doc/examples/addressbook-sdk/part3/main.cpp index 3378b4adce4..437a1c8352a 100644 --- a/doc/examples/addressbook-sdk/part3/main.cpp +++ b/doc/examples/addressbook-sdk/part3/main.cpp @@ -1,4 +1,3 @@ -//! [main function] #include #include "addressbook.h" @@ -9,4 +8,3 @@ int main(int argc, char *argv[]) w.show(); return a.exec(); } -//! [main function] diff --git a/doc/examples/addressbook-sdk/part4/addressbook.cpp b/doc/examples/addressbook-sdk/part4/addressbook.cpp new file mode 100644 index 00000000000..e14c86f4260 --- /dev/null +++ b/doc/examples/addressbook-sdk/part4/addressbook.cpp @@ -0,0 +1,161 @@ +#include "addressbook.h" +#include "ui_addressbook.h" + +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; + nextButton->setEnabled(false); + + 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())); + + setWindowTitle(tr("Simple Address Book")); +} + +AddressBook::~AddressBook() +{ + delete ui; +} + +void AddressBook::addContact() +{ + oldName = nameLine->text(); + oldAddress = addressText->toPlainText(); + + nameLine->clear(); + addressText->clear(); + + nameLine->setReadOnly(false); + nameLine->setFocus(Qt::OtherFocusReason); + addressText->setReadOnly(false); + + addButton->setEnabled(false); + nextButton->setEnabled(false); + previousButton->setEnabled(false); + submitButton->show(); + cancelButton->show(); +} + +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.")); + return; + } + + 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; + } + + if (contacts.isEmpty()) { + nameLine->clear(); + addressText->clear(); + } + + nameLine->setReadOnly(true); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + submitButton->hide(); + cancelButton->hide(); +} + +void AddressBook::cancel() +{ + nameLine->setText(oldName); + nameLine->setReadOnly(true); + + addressText->setText(oldAddress); + addressText->setReadOnly(true); + addButton->setEnabled(true); + + int number = contacts.size(); + nextButton->setEnabled(number > 1); + previousButton->setEnabled(number > 1); + + submitButton->hide(); + cancelButton->hide(); +} + +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()); +} + diff --git a/doc/examples/addressbook-sdk/part4/addressbook.h b/doc/examples/addressbook-sdk/part4/addressbook.h new file mode 100644 index 00000000000..7bc76f45f73 --- /dev/null +++ b/doc/examples/addressbook-sdk/part4/addressbook.h @@ -0,0 +1,58 @@ +//! [class definition] +#ifndef ADDRESSBOOK_H +#define ADDRESSBOOK_H + +#include +#include +#include +#include +#include + + +namespace Ui +{ + class AddressBook; +} + +class AddressBook : public QWidget +{ + Q_OBJECT + +public: + AddressBook(QWidget *parent = 0); +//! [enum] + enum Mode { NavigationMode, AddingMode, EditingMode }; +//! [enum] + ~AddressBook(); + +public slots: + void addContact(); + void submitContact(); + void cancel(); +//! [slot definition] + void editContact(); + void removeContact(); +//! [slot definition] + void next(); + void previous(); + +private: + Ui::AddressBook *ui; + + QPushButton *addButton; + QPushButton *submitButton; + QPushButton *cancelButton; +//! [members] + QPushButton *nextButton; + QPushButton *previousButton; +//! [members] + QLineEdit *nameLine; + QTextEdit *addressText; + + QMap contacts; + QString oldName; + QString oldAddress; +}; + +#endif // ADDRESSBOOK_H +//! [class definition] diff --git a/doc/examples/addressbook-sdk/part4/addressbook.ui b/doc/examples/addressbook-sdk/part4/addressbook.ui new file mode 100644 index 00000000000..ee9bbe4d65b --- /dev/null +++ b/doc/examples/addressbook-sdk/part4/addressbook.ui @@ -0,0 +1,111 @@ + + + AddressBook + + + + 0 + 0 + 600 + 400 + + + + AddressBook + + + + + 10 + 10 + 413 + 260 + + + + + + + Name: + + + + + + + + + + Address: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + + + + + + Add + + + + + + + Submit + + + + + + + Cancel + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + Previous + + + + + + + Next + + + + + + + + + + + + diff --git a/doc/examples/addressbook-sdk/part4/main.cpp b/doc/examples/addressbook-sdk/part4/main.cpp new file mode 100644 index 00000000000..437a1c8352a --- /dev/null +++ b/doc/examples/addressbook-sdk/part4/main.cpp @@ -0,0 +1,10 @@ +#include +#include "addressbook.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + AddressBook w; + w.show(); + return a.exec(); +} diff --git a/doc/examples/addressbook-sdk/part4/part4.pro b/doc/examples/addressbook-sdk/part4/part4.pro new file mode 100644 index 00000000000..8c34931ed32 --- /dev/null +++ b/doc/examples/addressbook-sdk/part4/part4.pro @@ -0,0 +1,16 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2009-06-05T16:03:06 +# +#------------------------------------------------- + +TARGET = part4 +TEMPLATE = app + + +SOURCES += main.cpp\ + addressbook.cpp + +HEADERS += addressbook.h + +FORMS += addressbook.ui