Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 6079

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.6: addressbook.cpp Example File (tutorials/addressbook-fr/part4/addressbook.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">addressbook.cpp Example File<br /><span class="small-subtitle">tutorials/addressbook-fr/part4/addressbook.cpp</span>
</h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the examples of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 ** GNU Lesser General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 ** General Public License version 2.1 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License version 3.0 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU General Public License version 3.0 requirements will be
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you have questions regarding the use of this file, please contact
 ** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/</span>

 #include &lt;QtGui&gt;
 #include &quot;addressbook.h&quot;

 AddressBook::AddressBook(QWidget *parent)
     : QWidget(parent)
 {
     QLabel *nameLabel = new QLabel(tr(&quot;Name:&quot;));
     nameLine = new QLineEdit;
     nameLine-&gt;setReadOnly(true);

     QLabel *addressLabel = new QLabel(tr(&quot;Address:&quot;));
     addressText = new QTextEdit;
     addressText-&gt;setReadOnly(true);

     addButton = new QPushButton(tr(&quot;&amp;Add&quot;));
     editButton = new QPushButton(tr(&quot;&amp;Edit&quot;));
     editButton-&gt;setEnabled(false);
     removeButton = new QPushButton(tr(&quot;&amp;Remove&quot;));
     removeButton-&gt;setEnabled(false);
     submitButton = new QPushButton(tr(&quot;&amp;Submit&quot;));
     submitButton-&gt;hide();
     cancelButton = new QPushButton(tr(&quot;&amp;Cancel&quot;));
     cancelButton-&gt;hide();

     nextButton = new QPushButton(tr(&quot;&amp;Next&quot;));
     nextButton-&gt;setEnabled(false);
     previousButton = new QPushButton(tr(&quot;&amp;Previous&quot;));
     previousButton-&gt;setEnabled(false);

     connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
     connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
     connect(editButton, SIGNAL(clicked()), this, SLOT(editContact()));
     connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact()));
     connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
     connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
     connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));

     QVBoxLayout *buttonLayout1 = new QVBoxLayout;
     buttonLayout1-&gt;addWidget(addButton);
     buttonLayout1-&gt;addWidget(editButton);
     buttonLayout1-&gt;addWidget(removeButton);
     buttonLayout1-&gt;addWidget(submitButton);
     buttonLayout1-&gt;addWidget(cancelButton);
     buttonLayout1-&gt;addStretch();

     QHBoxLayout *buttonLayout2 = new QHBoxLayout;
     buttonLayout2-&gt;addWidget(previousButton);
     buttonLayout2-&gt;addWidget(nextButton);

     QGridLayout *mainLayout = new QGridLayout;
     mainLayout-&gt;addWidget(nameLabel, 0, 0);
     mainLayout-&gt;addWidget(nameLine, 0, 1);
     mainLayout-&gt;addWidget(addressLabel, 1, 0, Qt::AlignTop);
     mainLayout-&gt;addWidget(addressText, 1, 1);
     mainLayout-&gt;addLayout(buttonLayout1, 1, 2);
     mainLayout-&gt;addLayout(buttonLayout2, 3, 1);

     setLayout(mainLayout);
     setWindowTitle(tr(&quot;Simple Address Book&quot;));
 }

 void AddressBook::addContact()
 {
     oldName = nameLine-&gt;text();
     oldAddress = addressText-&gt;toPlainText();

     nameLine-&gt;clear();
     addressText-&gt;clear();

     updateInterface(AddingMode);
 }
 void AddressBook::editContact()
 {
     oldName = nameLine-&gt;text();
     oldAddress = addressText-&gt;toPlainText();

     updateInterface(EditingMode);
 }
 void AddressBook::submitContact()
 {
     QString name = nameLine-&gt;text();
     QString address = addressText-&gt;toPlainText();

     if (name == &quot;&quot; || address == &quot;&quot;) {
         QMessageBox::information(this, tr(&quot;Empty Field&quot;),
             tr(&quot;Please enter a name and address.&quot;));
     }
     if (currentMode == AddingMode) {

         if (!contacts.contains(name)) {
             contacts.insert(name, address);
             QMessageBox::information(this, tr(&quot;Add Successful&quot;),
                 tr(&quot;\&quot;%1\&quot; has been added to your address book.&quot;).arg(name));
         } else {
             QMessageBox::information(this, tr(&quot;Add Unsuccessful&quot;),
                 tr(&quot;Sorry, \&quot;%1\&quot; is already in your address book.&quot;).arg(name));
         }
     } else if (currentMode == EditingMode) {

         if (oldName != name) {
             if (!contacts.contains(name)) {
                 QMessageBox::information(this, tr(&quot;Edit Successful&quot;),
                     tr(&quot;\&quot;%1\&quot; has been edited in your address book.&quot;).arg(oldName));
                 contacts.remove(oldName);
                 contacts.insert(name, address);
             } else {
                 QMessageBox::information(this, tr(&quot;Edit Unsuccessful&quot;),
                     tr(&quot;Sorry, \&quot;%1\&quot; is already in your address book.&quot;).arg(name));
             }
         } else if (oldAddress != address) {
             QMessageBox::information(this, tr(&quot;Edit Successful&quot;),
                 tr(&quot;\&quot;%1\&quot; has been edited in your address book.&quot;).arg(name));
             contacts[name] = address;
         }
     }

     updateInterface(NavigationMode);
 }

 void AddressBook::cancel()
 {
     nameLine-&gt;setText(oldName);
     addressText-&gt;setText(oldAddress);
     updateInterface(NavigationMode);
 }
 void AddressBook::removeContact()
 {
     QString name = nameLine-&gt;text();
     QString address = addressText-&gt;toPlainText();

     if (contacts.contains(name)) {

         int button = QMessageBox::question(this,
             tr(&quot;Confirm Remove&quot;),
             tr(&quot;Are you sure you want to remove \&quot;%1\&quot;?&quot;).arg(name),
             QMessageBox::Yes | QMessageBox::No);

         if (button == QMessageBox::Yes) {

             previous();
             contacts.remove(name);

             QMessageBox::information(this, tr(&quot;Remove Successful&quot;),
                 tr(&quot;\&quot;%1\&quot; has been removed from your address book.&quot;).arg(name));
         }
     }

     updateInterface(NavigationMode);
 }
 void AddressBook::next()
 {
     QString name = nameLine-&gt;text();
     QMap&lt;QString, QString&gt;::iterator i = contacts.find(name);

     if (i != contacts.end())
         i++;

     if (i == contacts.end())
         i = contacts.begin();

     nameLine-&gt;setText(i.key());
     addressText-&gt;setText(i.value());
 }

 void AddressBook::previous()
 {
     QString name = nameLine-&gt;text();
     QMap&lt;QString, QString&gt;::iterator i = contacts.find(name);

     if (i == contacts.end()) {
         nameLine-&gt;clear();
         addressText-&gt;clear();
         return;
     }

     if (i == contacts.begin())
         i = contacts.end();

     i--;
     nameLine-&gt;setText(i.key());
     addressText-&gt;setText(i.value());
 }
 void AddressBook::updateInterface(Mode mode)
 {
     currentMode = mode;

     switch (currentMode) {

     case AddingMode:
     case EditingMode:

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

         addButton-&gt;setEnabled(false);
         editButton-&gt;setEnabled(false);
         removeButton-&gt;setEnabled(false);

         nextButton-&gt;setEnabled(false);
         previousButton-&gt;setEnabled(false);

         submitButton-&gt;show();
         cancelButton-&gt;show();
         break;
     case NavigationMode:

         if (contacts.isEmpty()) {
             nameLine-&gt;clear();
             addressText-&gt;clear();
         }

         nameLine-&gt;setReadOnly(true);
         addressText-&gt;setReadOnly(true);
         addButton-&gt;setEnabled(true);

         int number = contacts.size();
         editButton-&gt;setEnabled(number &gt;= 1);
         removeButton-&gt;setEnabled(number &gt;= 1);
         nextButton-&gt;setEnabled(number &gt; 1);
         previousButton-&gt;setEnabled(number &gt;1 );

         submitButton-&gt;hide();
         cancelButton-&gt;hide();
         break;
     }
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>