examples/tutorials/addressbook/part2/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 //! [setting readonly 1]
       
    51     nameLine->setReadOnly(true);
       
    52 //! [setting readonly 1]
       
    53     QLabel *addressLabel = new QLabel(tr("Address:"));
       
    54     addressText = new QTextEdit;
       
    55 //! [setting readonly 2]    
       
    56     addressText->setReadOnly(true);
       
    57 //! [setting readonly 2]
       
    58    
       
    59 //! [pushbutton declaration]
       
    60     addButton = new QPushButton(tr("&Add"));
       
    61     addButton->show();
       
    62     submitButton = new QPushButton(tr("&Submit"));
       
    63     submitButton->hide();
       
    64     cancelButton = new QPushButton(tr("&Cancel"));
       
    65     cancelButton->hide();
       
    66 //! [pushbutton declaration]
       
    67 //! [connecting signals and slots]
       
    68     connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
       
    69     connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
       
    70     connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
       
    71 //! [connecting signals and slots]
       
    72 //! [vertical layout]
       
    73     QVBoxLayout *buttonLayout1 = new QVBoxLayout;
       
    74     buttonLayout1->addWidget(addButton, Qt::AlignTop);
       
    75     buttonLayout1->addWidget(submitButton);
       
    76     buttonLayout1->addWidget(cancelButton);
       
    77     buttonLayout1->addStretch();
       
    78 //! [vertical layout]
       
    79 //! [grid layout]
       
    80     QGridLayout *mainLayout = new QGridLayout;
       
    81     mainLayout->addWidget(nameLabel, 0, 0);
       
    82     mainLayout->addWidget(nameLine, 0, 1);
       
    83     mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
       
    84     mainLayout->addWidget(addressText, 1, 1);
       
    85     mainLayout->addLayout(buttonLayout1, 1, 2);
       
    86 //! [grid layout]
       
    87     setLayout(mainLayout);
       
    88     setWindowTitle(tr("Simple Address Book"));
       
    89 }
       
    90 //! [addContact]
       
    91 void AddressBook::addContact()
       
    92 {
       
    93     oldName = nameLine->text();
       
    94     oldAddress = addressText->toPlainText();
       
    95 
       
    96     nameLine->clear();
       
    97     addressText->clear();
       
    98     
       
    99     nameLine->setReadOnly(false);
       
   100     nameLine->setFocus(Qt::OtherFocusReason);
       
   101     addressText->setReadOnly(false);
       
   102 
       
   103     addButton->setEnabled(false);
       
   104     submitButton->show();
       
   105     cancelButton->show();
       
   106 }
       
   107 //! [addContact]
       
   108 
       
   109 //! [submitContact part1]
       
   110 void AddressBook::submitContact()
       
   111 {
       
   112     QString name = nameLine->text();
       
   113     QString address = addressText->toPlainText();
       
   114     
       
   115     if (name == "" || address == "") {
       
   116         QMessageBox::information(this, tr("Empty Field"),
       
   117             tr("Please enter a name and address."));
       
   118         return;
       
   119     }
       
   120 //! [submitContact part1]
       
   121 //! [submitContact part2]
       
   122     if (!contacts.contains(name)) {
       
   123         contacts.insert(name, address);
       
   124         QMessageBox::information(this, tr("Add Successful"),
       
   125             tr("\"%1\" has been added to your address book.").arg(name));
       
   126     } else {
       
   127         QMessageBox::information(this, tr("Add Unsuccessful"),
       
   128             tr("Sorry, \"%1\" is already in your address book.").arg(name));
       
   129         return;
       
   130     }
       
   131 //! [submitContact part2]
       
   132 //! [submitContact part3]
       
   133     if (contacts.isEmpty()) {
       
   134         nameLine->clear();
       
   135         addressText->clear();
       
   136     }
       
   137 
       
   138     nameLine->setReadOnly(true);
       
   139     addressText->setReadOnly(true);
       
   140     addButton->setEnabled(true);
       
   141     submitButton->hide();
       
   142     cancelButton->hide();
       
   143 }
       
   144 //! [submitContact part3]
       
   145 //! [cancel]
       
   146 void AddressBook::cancel()
       
   147 {
       
   148     nameLine->setText(oldName);
       
   149     nameLine->setReadOnly(true);
       
   150 
       
   151     addressText->setText(oldAddress);
       
   152     addressText->setReadOnly(true);
       
   153 
       
   154     addButton->setEnabled(true);
       
   155     submitButton->hide();
       
   156     cancelButton->hide();    
       
   157 }
       
   158 //! [cancel]