examples/tutorials/addressbook/part3/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     addButton->show();
       
    58     submitButton = new QPushButton(tr("&Submit"));
       
    59     submitButton->hide();
       
    60     cancelButton = new QPushButton(tr("&Cancel"));
       
    61     cancelButton->hide();
       
    62 //! [navigation pushbuttons]
       
    63     nextButton = new QPushButton(tr("&Next"));
       
    64     nextButton->setEnabled(false);
       
    65     previousButton = new QPushButton(tr("&Previous"));
       
    66     previousButton->setEnabled(false);
       
    67 //! [navigation pushbuttons]
       
    68 
       
    69     connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
       
    70     connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
       
    71     connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
       
    72 //! [connecting navigation signals]    
       
    73     connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
       
    74     connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
       
    75 //! [connecting navigation signals]
       
    76 
       
    77     QVBoxLayout *buttonLayout1 = new QVBoxLayout;
       
    78     buttonLayout1->addWidget(addButton, Qt::AlignTop);
       
    79     buttonLayout1->addWidget(submitButton);
       
    80     buttonLayout1->addWidget(cancelButton);
       
    81     buttonLayout1->addStretch();
       
    82 //! [navigation layout]
       
    83     QHBoxLayout *buttonLayout2 = new QHBoxLayout;
       
    84     buttonLayout2->addWidget(previousButton);
       
    85     buttonLayout2->addWidget(nextButton);
       
    86 //! [ navigation layout]
       
    87     QGridLayout *mainLayout = new QGridLayout;
       
    88     mainLayout->addWidget(nameLabel, 0, 0);
       
    89     mainLayout->addWidget(nameLine, 0, 1);
       
    90     mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
       
    91     mainLayout->addWidget(addressText, 1, 1);
       
    92     mainLayout->addLayout(buttonLayout1, 1, 2);
       
    93 //! [adding navigation layout]
       
    94     mainLayout->addLayout(buttonLayout2, 3, 1);
       
    95 //! [adding navigation layout]
       
    96     setLayout(mainLayout);
       
    97     setWindowTitle(tr("Simple Address Book"));
       
    98 }
       
    99 
       
   100 void AddressBook::addContact()
       
   101 {
       
   102     oldName = nameLine->text();
       
   103     oldAddress = addressText->toPlainText();
       
   104 
       
   105     nameLine->clear();
       
   106     addressText->clear();
       
   107 
       
   108     nameLine->setReadOnly(false);
       
   109     nameLine->setFocus(Qt::OtherFocusReason);
       
   110     addressText->setReadOnly(false);
       
   111     
       
   112     addButton->setEnabled(false);
       
   113 //! [disabling navigation]
       
   114     nextButton->setEnabled(false);
       
   115     previousButton->setEnabled(false);
       
   116 //! [disabling navigation]
       
   117     submitButton->show();
       
   118     cancelButton->show();
       
   119 }
       
   120 
       
   121 void AddressBook::submitContact()
       
   122 {
       
   123     QString name = nameLine->text();
       
   124     QString address = addressText->toPlainText();
       
   125     
       
   126     if (name == "" || address == "") {
       
   127         QMessageBox::information(this, tr("Empty Field"),
       
   128             tr("Please enter a name and address."));
       
   129     }
       
   130 
       
   131     if (!contacts.contains(name)) {
       
   132         contacts.insert(name, address);
       
   133         QMessageBox::information(this, tr("Add Successful"),
       
   134             tr("\"%1\" has been added to your address book.").arg(name));
       
   135     } else {
       
   136         QMessageBox::information(this, tr("Add Unsuccessful"),
       
   137             tr("Sorry, \"%1\" is already in your address book.").arg(name));
       
   138     }
       
   139 
       
   140     if (contacts.isEmpty()) {
       
   141         nameLine->clear();
       
   142         addressText->clear();
       
   143     }
       
   144 
       
   145     nameLine->setReadOnly(true);
       
   146     addressText->setReadOnly(true);
       
   147     addButton->setEnabled(true);
       
   148 
       
   149 //! [enabling navigation]
       
   150     int number = contacts.size();
       
   151     nextButton->setEnabled(number > 1);
       
   152     previousButton->setEnabled(number > 1);
       
   153 //! [enabling navigation]
       
   154     submitButton->hide();
       
   155     cancelButton->hide();
       
   156 }
       
   157 
       
   158 void AddressBook::cancel()
       
   159 {
       
   160     nameLine->setText(oldName);
       
   161     addressText->setText(oldAddress);
       
   162    
       
   163     if (contacts.isEmpty()) {
       
   164         nameLine->clear();
       
   165         addressText->clear();
       
   166     }
       
   167     
       
   168     nameLine->setReadOnly(true);
       
   169     addressText->setReadOnly(true);
       
   170     addButton->setEnabled(true);
       
   171     
       
   172     int number = contacts.size();
       
   173     nextButton->setEnabled(number > 1);
       
   174     previousButton->setEnabled(number > 1);
       
   175     
       
   176     submitButton->hide();
       
   177     cancelButton->hide();
       
   178 }
       
   179 
       
   180 //! [next() function]
       
   181 void AddressBook::next()
       
   182 {
       
   183     QString name = nameLine->text();
       
   184     QMap<QString, QString>::iterator i = contacts.find(name);
       
   185 
       
   186     if (i != contacts.end())
       
   187         i++;
       
   188 
       
   189     if (i == contacts.end())
       
   190         i = contacts.begin();
       
   191 
       
   192     nameLine->setText(i.key());
       
   193     addressText->setText(i.value());
       
   194 }
       
   195 //! [next() function]
       
   196 //! [previous() function]
       
   197 void AddressBook::previous()
       
   198 {
       
   199     QString name = nameLine->text();
       
   200     QMap<QString, QString>::iterator i = contacts.find(name);
       
   201 
       
   202     if (i == contacts.end()){
       
   203         nameLine->clear();
       
   204         addressText->clear();
       
   205         return;
       
   206     }
       
   207 
       
   208     if (i == contacts.begin())
       
   209         i = contacts.end();
       
   210 
       
   211     i--;
       
   212     nameLine->setText(i.key());
       
   213     addressText->setText(i.value());
       
   214 }
       
   215 //! [previous() function]