| 
									
										
										
										
											2009-06-03 17:00:30 +02:00
										 |  |  | #include "addressbook.h"
 | 
					
						
							|  |  |  | #include "ui_addressbook.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AddressBook::AddressBook(QWidget *parent) | 
					
						
							| 
									
										
										
										
											2009-06-04 13:53:32 +02:00
										 |  |  |     : QWidget(parent), ui(new Ui::AddressBook) | 
					
						
							| 
									
										
										
										
											2009-06-03 17:00:30 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-05 12:07:34 +02:00
										 |  |  | //! [extract objects]
 | 
					
						
							|  |  |  |     nextButton = new QPushButton; | 
					
						
							|  |  |  |     nextButton = ui->nextButton; | 
					
						
							| 
									
										
										
										
											2009-06-05 12:31:14 +02:00
										 |  |  |     nextButton->setEnabled(false); | 
					
						
							| 
									
										
										
										
											2009-06-05 12:07:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     previousButton = new QPushButton; | 
					
						
							|  |  |  |     previousButton = ui->previousButton; | 
					
						
							| 
									
										
										
										
											2009-06-05 12:31:14 +02:00
										 |  |  |     nextButton->setEnabled(false); | 
					
						
							| 
									
										
										
										
											2009-06-05 12:07:34 +02:00
										 |  |  | //! [extract objects]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-03 17:00:30 +02:00
										 |  |  |     connect(addButton, SIGNAL(clicked()), this, | 
					
						
							|  |  |  |                 SLOT(addContact())); | 
					
						
							|  |  |  |     connect(submitButton, SIGNAL(clicked()), this, | 
					
						
							|  |  |  |                 SLOT(submitContact())); | 
					
						
							|  |  |  |     connect(cancelButton, SIGNAL(clicked()), this, | 
					
						
							|  |  |  |                 SLOT(cancel())); | 
					
						
							| 
									
										
										
										
											2009-06-05 12:07:34 +02:00
										 |  |  | //! [signal slot]
 | 
					
						
							|  |  |  |     connect(nextButton, SIGNAL(clicked()), this, | 
					
						
							|  |  |  |                 SLOT(next())); | 
					
						
							|  |  |  |     connect(previousButton, SIGNAL(clicked()), this, | 
					
						
							|  |  |  |                 SLOT(previous())); | 
					
						
							|  |  |  | //! [signal slot]
 | 
					
						
							| 
									
										
										
										
											2009-06-03 17:00:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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); | 
					
						
							| 
									
										
										
										
											2009-06-05 12:31:14 +02:00
										 |  |  | //! [disable navigation]
 | 
					
						
							|  |  |  |     nextButton->setEnabled(false); | 
					
						
							|  |  |  |     previousButton->setEnabled(false); | 
					
						
							|  |  |  | //! [disable navigation]
 | 
					
						
							| 
									
										
										
										
											2009-06-03 17:00:30 +02:00
										 |  |  |     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); | 
					
						
							| 
									
										
										
										
											2009-06-05 12:31:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | //! [enable navigation]
 | 
					
						
							|  |  |  |     int number = contacts.size(); | 
					
						
							|  |  |  |     nextButton->setEnabled(number > 1); | 
					
						
							|  |  |  |     previousButton->setEnabled(number > 1); | 
					
						
							|  |  |  | //! [enable navigation]
 | 
					
						
							| 
									
										
										
										
											2009-06-03 17:00:30 +02:00
										 |  |  |     submitButton->hide(); | 
					
						
							|  |  |  |     cancelButton->hide(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AddressBook::cancel() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     nameLine->setText(oldName); | 
					
						
							|  |  |  |     nameLine->setReadOnly(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     addressText->setText(oldAddress); | 
					
						
							|  |  |  |     addressText->setReadOnly(true); | 
					
						
							|  |  |  |     addButton->setEnabled(true); | 
					
						
							| 
									
										
										
										
											2009-06-05 12:31:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int number = contacts.size(); | 
					
						
							|  |  |  |     nextButton->setEnabled(number > 1); | 
					
						
							|  |  |  |     previousButton->setEnabled(number > 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-03 17:00:30 +02:00
										 |  |  |     submitButton->hide(); | 
					
						
							|  |  |  |     cancelButton->hide(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-06-05 12:07:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | //! [next]
 | 
					
						
							|  |  |  | void AddressBook::next() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QString name = nameLine->text(); | 
					
						
							|  |  |  |     QMap<QString, QString>::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()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | //! [next]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //! [previous]
 | 
					
						
							|  |  |  | void AddressBook::previous() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QString name = nameLine->text(); | 
					
						
							|  |  |  |     QMap<QString, QString>::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()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | //! [previous]
 | 
					
						
							|  |  |  | 
 |