examples/tutorials/addressbook-fr/part4/addressbook.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 #include "addressbook.h"
       
    44 
       
    45 AddressBook::AddressBook(QWidget *parent)
       
    46     : QWidget(parent)
       
    47 {
       
    48     QLabel *nameLabel = new QLabel(tr("Name:"));
       
    49     nameLine = new QLineEdit;
       
    50     nameLine->setReadOnly(true);
       
    51 
       
    52     QLabel *addressLabel = new QLabel(tr("Address:"));
       
    53     addressText = new QTextEdit;
       
    54     addressText->setReadOnly(true);
       
    55 
       
    56     addButton = new QPushButton(tr("&Add"));
       
    57 //! [edit and remove buttons] 
       
    58     editButton = new QPushButton(tr("&Edit"));
       
    59     editButton->setEnabled(false);
       
    60     removeButton = new QPushButton(tr("&Remove"));
       
    61     removeButton->setEnabled(false);
       
    62 //! [edit and remove buttons] 
       
    63     submitButton = new QPushButton(tr("&Submit"));
       
    64     submitButton->hide();
       
    65     cancelButton = new QPushButton(tr("&Cancel"));
       
    66     cancelButton->hide();
       
    67     
       
    68     nextButton = new QPushButton(tr("&Next"));
       
    69     nextButton->setEnabled(false);
       
    70     previousButton = new QPushButton(tr("&Previous"));
       
    71     previousButton->setEnabled(false);
       
    72 
       
    73     connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
       
    74     connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
       
    75 //! [connecting edit and remove] 
       
    76     connect(editButton, SIGNAL(clicked()), this, SLOT(editContact()));
       
    77     connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact()));
       
    78 //! [connecting edit and remove] 
       
    79     connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
       
    80     connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
       
    81     connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
       
    82 
       
    83     QVBoxLayout *buttonLayout1 = new QVBoxLayout;
       
    84     buttonLayout1->addWidget(addButton);
       
    85 //! [adding edit and remove to the layout]     
       
    86     buttonLayout1->addWidget(editButton);
       
    87     buttonLayout1->addWidget(removeButton);
       
    88 //! [adding edit and remove to the layout]         
       
    89     buttonLayout1->addWidget(submitButton);
       
    90     buttonLayout1->addWidget(cancelButton);
       
    91     buttonLayout1->addStretch();
       
    92 
       
    93     QHBoxLayout *buttonLayout2 = new QHBoxLayout;
       
    94     buttonLayout2->addWidget(previousButton);
       
    95     buttonLayout2->addWidget(nextButton);
       
    96 
       
    97     QGridLayout *mainLayout = new QGridLayout;
       
    98     mainLayout->addWidget(nameLabel, 0, 0);
       
    99     mainLayout->addWidget(nameLine, 0, 1);
       
   100     mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
       
   101     mainLayout->addWidget(addressText, 1, 1);
       
   102     mainLayout->addLayout(buttonLayout1, 1, 2);
       
   103     mainLayout->addLayout(buttonLayout2, 3, 1);
       
   104 
       
   105     setLayout(mainLayout);
       
   106     setWindowTitle(tr("Simple Address Book"));
       
   107 }
       
   108 
       
   109 void AddressBook::addContact()
       
   110 {
       
   111     oldName = nameLine->text();
       
   112     oldAddress = addressText->toPlainText();
       
   113 
       
   114     nameLine->clear();
       
   115     addressText->clear();
       
   116 
       
   117     updateInterface(AddingMode);
       
   118 }
       
   119 //! [editContact() function]
       
   120 void AddressBook::editContact()
       
   121 {
       
   122     oldName = nameLine->text();
       
   123     oldAddress = addressText->toPlainText();
       
   124 
       
   125     updateInterface(EditingMode);
       
   126 }
       
   127 //! [editContact() function]
       
   128 //! [submitContact() function beginning]
       
   129 void AddressBook::submitContact()
       
   130 {
       
   131 //! [submitContact() function beginning]
       
   132     QString name = nameLine->text();
       
   133     QString address = addressText->toPlainText();
       
   134 
       
   135     if (name == "" || address == "") {
       
   136         QMessageBox::information(this, tr("Empty Field"),
       
   137             tr("Please enter a name and address."));
       
   138     }
       
   139 //! [submitContact() function part1]
       
   140     if (currentMode == AddingMode) {
       
   141         
       
   142         if (!contacts.contains(name)) {
       
   143             contacts.insert(name, address);
       
   144             QMessageBox::information(this, tr("Add Successful"),
       
   145                 tr("\"%1\" has been added to your address book.").arg(name));
       
   146         } else {
       
   147             QMessageBox::information(this, tr("Add Unsuccessful"),
       
   148                 tr("Sorry, \"%1\" is already in your address book.").arg(name));
       
   149         }
       
   150 //! [submitContact() function part1]
       
   151 //! [submitContact() function part2]
       
   152     } else if (currentMode == EditingMode) {
       
   153         
       
   154         if (oldName != name) {
       
   155             if (!contacts.contains(name)) {
       
   156                 QMessageBox::information(this, tr("Edit Successful"),
       
   157                     tr("\"%1\" has been edited in your address book.").arg(oldName));
       
   158                 contacts.remove(oldName);
       
   159                 contacts.insert(name, address);
       
   160             } else {
       
   161                 QMessageBox::information(this, tr("Edit Unsuccessful"),
       
   162                     tr("Sorry, \"%1\" is already in your address book.").arg(name));
       
   163             }
       
   164         } else if (oldAddress != address) {
       
   165             QMessageBox::information(this, tr("Edit Successful"),
       
   166                 tr("\"%1\" has been edited in your address book.").arg(name));
       
   167             contacts[name] = address;        
       
   168         }
       
   169     }
       
   170 
       
   171     updateInterface(NavigationMode);
       
   172 }
       
   173 //! [submitContact() function part2]
       
   174 
       
   175 void AddressBook::cancel()
       
   176 {
       
   177     nameLine->setText(oldName);
       
   178     addressText->setText(oldAddress);
       
   179     updateInterface(NavigationMode);
       
   180 }
       
   181 //! [removeContact() function]
       
   182 void AddressBook::removeContact()
       
   183 {
       
   184     QString name = nameLine->text();
       
   185     QString address = addressText->toPlainText();
       
   186 
       
   187     if (contacts.contains(name)) {
       
   188 
       
   189         int button = QMessageBox::question(this,
       
   190             tr("Confirm Remove"),
       
   191             tr("Are you sure you want to remove \"%1\"?").arg(name),
       
   192             QMessageBox::Yes | QMessageBox::No);
       
   193 
       
   194         if (button == QMessageBox::Yes) {
       
   195             
       
   196             previous();
       
   197             contacts.remove(name);
       
   198 
       
   199             QMessageBox::information(this, tr("Remove Successful"),
       
   200                 tr("\"%1\" has been removed from your address book.").arg(name));
       
   201         }
       
   202     }
       
   203 
       
   204     updateInterface(NavigationMode);
       
   205 }
       
   206 //! [removeContact() function]
       
   207 void AddressBook::next()
       
   208 {
       
   209     QString name = nameLine->text();
       
   210     QMap<QString, QString>::iterator i = contacts.find(name);
       
   211 
       
   212     if (i != contacts.end())
       
   213         i++;
       
   214 
       
   215     if (i == contacts.end())
       
   216         i = contacts.begin();
       
   217 
       
   218     nameLine->setText(i.key());
       
   219     addressText->setText(i.value());
       
   220 }
       
   221 
       
   222 void AddressBook::previous()
       
   223 {
       
   224     QString name = nameLine->text();
       
   225     QMap<QString, QString>::iterator i = contacts.find(name);
       
   226 
       
   227     if (i == contacts.end()) {
       
   228         nameLine->clear();
       
   229         addressText->clear();
       
   230         return;
       
   231     }
       
   232 
       
   233     if (i == contacts.begin())
       
   234         i = contacts.end();
       
   235 
       
   236     i--;
       
   237     nameLine->setText(i.key());
       
   238     addressText->setText(i.value());
       
   239 }
       
   240 //! [update interface() part 1]
       
   241 void AddressBook::updateInterface(Mode mode)
       
   242 {
       
   243     currentMode = mode;
       
   244 
       
   245     switch (currentMode) {
       
   246         
       
   247     case AddingMode:
       
   248     case EditingMode:
       
   249 
       
   250         nameLine->setReadOnly(false);
       
   251         nameLine->setFocus(Qt::OtherFocusReason);
       
   252         addressText->setReadOnly(false);
       
   253 
       
   254         addButton->setEnabled(false);
       
   255         editButton->setEnabled(false);
       
   256         removeButton->setEnabled(false);
       
   257 
       
   258         nextButton->setEnabled(false);
       
   259         previousButton->setEnabled(false);
       
   260 
       
   261         submitButton->show();
       
   262         cancelButton->show();
       
   263         break;
       
   264 //! [update interface() part 1]
       
   265 //! [update interface() part 2]
       
   266     case NavigationMode:
       
   267 
       
   268         if (contacts.isEmpty()) {
       
   269             nameLine->clear();
       
   270             addressText->clear();
       
   271         }
       
   272 
       
   273         nameLine->setReadOnly(true);
       
   274         addressText->setReadOnly(true);
       
   275         addButton->setEnabled(true);
       
   276 
       
   277         int number = contacts.size();
       
   278         editButton->setEnabled(number >= 1);
       
   279         removeButton->setEnabled(number >= 1);
       
   280         nextButton->setEnabled(number > 1);
       
   281         previousButton->setEnabled(number >1 );
       
   282 
       
   283         submitButton->hide();
       
   284         cancelButton->hide();
       
   285         break;
       
   286     }
       
   287 }
       
   288 //! [update interface() part 2]