diff --git a/doc/addressbook-sdk.qdoc b/doc/addressbook-sdk.qdoc index c06bd73f0dbfa595967930afa637a9f4708d5318..044166679964ca716781ae3feae142adde941349 100644 --- a/doc/addressbook-sdk.qdoc +++ b/doc/addressbook-sdk.qdoc @@ -881,10 +881,13 @@ \gui{Save...} respectively. Ideally, it would be more user-friendly to set the push buttons' labels to \gui{Load contacts from file} and \gui{Save contacts to file}. However, due to the size of our push buttons, - we set the labels to \gui{Load...} and \gui{Save...} instead. Fortunately, - Qt Creator's \QD plugin provides a simple way to set tooltips with the - \c toolTip property. To test your tooltip, use \key{Ctrl+Alt+R} and hover - your mouse cursor on the push buttons. + we set the labels to \gui{Load...} and \gui{Save...} instead. + + Fortunately, Qt Creator's \QD plugin provides a simple way to set tooltips + with the \c toolTip property. So, we set our buttons' tooltips to + \gui{Load contacts from file} and \gui{Save contacts to file} respectively. + To test your tooltip, use \key{Ctrl+Alt+R} and hover your mouse cursor on + the push buttons. # screenshot of property editor @@ -894,7 +897,7 @@ We declare two public slots, \c saveToFile() and \c loadFromFile(). - # code + \snippet examples/addressbook-sdk/part6/addressbook.h slot definition Now lets look at the \c saveToFile() and \c loadFromFile() functions in detail. @@ -988,7 +991,8 @@ \section1 Placing Widgets on The Form We add a push button into our layout by dragging and dropping it in, with - \c exportButton as its \c objectName. + \c exportButton as its \c objectName. The \c toolTip property is set to + \gui{Export as vCard}. # screenshot diff --git a/doc/examples/addressbook-sdk/part7/addressbook.cpp b/doc/examples/addressbook-sdk/part7/addressbook.cpp index 3a09fa66e896ff2640540b6399c49ac449564294..5761030f4ab37756ae688b719235772b9c406f6b 100644 --- a/doc/examples/addressbook-sdk/part7/addressbook.cpp +++ b/doc/examples/addressbook-sdk/part7/addressbook.cpp @@ -31,6 +31,8 @@ AddressBook::AddressBook(QWidget *parent) SLOT(removeContact())); connect(ui->findButton, SIGNAL(clicked()), this, SLOT(findContact())); + connect(ui->exportButton, SIGNAL(clicked()), this, + SLOT(exportAsVCard())); setWindowTitle(tr("Simple Address Book")); } @@ -281,7 +283,7 @@ void AddressBook::loadFromFile() in >> contacts; if (contacts.isEmpty()) { - QMessagebox::information(this, tr("No contacts in file"), + QMessageBox::information(this, tr("No contacts in file"), tr("The file you are attempting to open contains no contacts.")); } else { QMap<QString, QString>::iterator i = contacts.begin(); @@ -292,3 +294,59 @@ void AddressBook::loadFromFile() updateInterface(NavigationMode); } + +void AddressBook::exportAsVCard() +{ + QString name = ui->nameLine->text(); + QString address = ui->addressText->toPlainText(); + QString firstName; + QString lastName; + QStringList nameList; + + int index = name.indexOf(" "); + + if (index != -1) { + nameList = name.split(QRegExp("\\s+"), QString::SkipEmptyParts); + firstName = nameList.first(); + lastName = nameList.last(); + } else { + firstName = name; + lastName = ""; + } + + QString fileName = QFileDialog::getSaveFileName(this, + tr("Export Coontact"), "", + tr("vCard files (*.vcf);;All Files (*)")); + + if (fileName.isEmpty()) + return; + + QFile file(fileName); + + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::information(this, tr("Unable to open file"), + file.errorString()); + return; + } + + QTextStream out(&file); + + out << "BEGIN:VCARD" << "\n"; + out << "VERSION:2.1" << "\n"; + out << "N:" << lastName << ";" << firstName << "\n"; + + if (!nameList.isEmpty()) + out << "FN:" << nameList.join(" ") << "\n"; + else + out << "FN:" << firstName << "\n"; + + address.replace(";", "\\;", Qt::CaseInsensitive); + address.replace("\n", ";", Qt::CaseInsensitive); + address.replace(",", " ", Qt::CaseInsensitive); + + out << "ADR;HOME:;" << address << "\n"; + out << "END;VCARD" << "\n"; + + QMessageBox::information(this, tr("Export Successful"), + tr("\%1\" has been exported as a vCard.").arg(name)); +} diff --git a/doc/examples/addressbook-sdk/part7/addressbook.h b/doc/examples/addressbook-sdk/part7/addressbook.h index 2dd06cb9aeaada07497fffff7c550766673aed47..2c3b6d5e5d7342d7300ff0b0ff7105ff237bc34a 100644 --- a/doc/examples/addressbook-sdk/part7/addressbook.h +++ b/doc/examples/addressbook-sdk/part7/addressbook.h @@ -29,10 +29,11 @@ public slots: void next(); void previous(); void findContact(); -//! [slot definition] void saveToFile(); void loadFromFile(); //! [slot definition] + void exportAsVCard(); +//! [slot definition] private: Ui::AddressBook *ui; diff --git a/doc/examples/addressbook-sdk/part7/addressbook.ui b/doc/examples/addressbook-sdk/part7/addressbook.ui index f46c9c396976369327bc8f3ae9ad0c43d03511e5..00f09db0dee5a6ce8cafeb82c6d9491dcfb13f58 100644 --- a/doc/examples/addressbook-sdk/part7/addressbook.ui +++ b/doc/examples/addressbook-sdk/part7/addressbook.ui @@ -103,6 +103,16 @@ </property> </widget> </item> + <item> + <widget class="QPushButton" name="exportButton"> + <property name="toolTip"> + <string>Export as vCard</string> + </property> + <property name="text"> + <string>Export</string> + </property> + </widget> + </item> <item> <spacer name="verticalSpacer"> <property name="orientation">