| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include <QtWidgets> |
| 52 | #include "addressbook.h" |
| 53 | |
| 54 | AddressBook::AddressBook(QWidget *parent) |
| 55 | : QWidget(parent) |
| 56 | { |
| 57 | QLabel *nameLabel = new QLabel(tr("Name:" )); |
| 58 | nameLine = new QLineEdit; |
| 59 | //! [setting readonly 1] |
| 60 | nameLine->setReadOnly(true); |
| 61 | //! [setting readonly 1] |
| 62 | QLabel *addressLabel = new QLabel(tr("Address:" )); |
| 63 | addressText = new QTextEdit; |
| 64 | //! [setting readonly 2] |
| 65 | addressText->setReadOnly(true); |
| 66 | //! [setting readonly 2] |
| 67 | |
| 68 | //! [pushbutton declaration] |
| 69 | addButton = new QPushButton(tr("&Add" )); |
| 70 | addButton->show(); |
| 71 | submitButton = new QPushButton(tr("&Submit" )); |
| 72 | submitButton->hide(); |
| 73 | cancelButton = new QPushButton(tr("&Cancel" )); |
| 74 | cancelButton->hide(); |
| 75 | //! [pushbutton declaration] |
| 76 | //! [connecting signals and slots] |
| 77 | connect(addButton, &QPushButton::clicked, |
| 78 | this, &AddressBook::addContact); |
| 79 | connect(submitButton, &QPushButton::clicked, |
| 80 | this, &AddressBook::submitContact); |
| 81 | connect(cancelButton, &QPushButton::clicked, |
| 82 | this, &AddressBook::cancel); |
| 83 | //! [connecting signals and slots] |
| 84 | //! [vertical layout] |
| 85 | QVBoxLayout *buttonLayout1 = new QVBoxLayout; |
| 86 | buttonLayout1->addWidget(addButton, Qt::AlignTop); |
| 87 | buttonLayout1->addWidget(submitButton); |
| 88 | buttonLayout1->addWidget(cancelButton); |
| 89 | buttonLayout1->addStretch(); |
| 90 | //! [vertical layout] |
| 91 | //! [grid layout] |
| 92 | QGridLayout *mainLayout = new QGridLayout; |
| 93 | mainLayout->addWidget(nameLabel, 0, 0); |
| 94 | mainLayout->addWidget(nameLine, 0, 1); |
| 95 | mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); |
| 96 | mainLayout->addWidget(addressText, 1, 1); |
| 97 | mainLayout->addLayout(buttonLayout1, 1, 2); |
| 98 | //! [grid layout] |
| 99 | setLayout(mainLayout); |
| 100 | setWindowTitle(tr("Simple Address Book" )); |
| 101 | } |
| 102 | //! [addContact] |
| 103 | void AddressBook::addContact() |
| 104 | { |
| 105 | oldName = nameLine->text(); |
| 106 | oldAddress = addressText->toPlainText(); |
| 107 | |
| 108 | nameLine->clear(); |
| 109 | addressText->clear(); |
| 110 | |
| 111 | nameLine->setReadOnly(false); |
| 112 | nameLine->setFocus(Qt::OtherFocusReason); |
| 113 | addressText->setReadOnly(false); |
| 114 | |
| 115 | addButton->setEnabled(false); |
| 116 | submitButton->show(); |
| 117 | cancelButton->show(); |
| 118 | } |
| 119 | //! [addContact] |
| 120 | |
| 121 | //! [submitContact part1] |
| 122 | void AddressBook::submitContact() |
| 123 | { |
| 124 | QString name = nameLine->text(); |
| 125 | QString address = addressText->toPlainText(); |
| 126 | |
| 127 | if (name.isEmpty() || address.isEmpty()) { |
| 128 | QMessageBox::information(this, tr("Empty Field" ), |
| 129 | tr("Please enter a name and address." )); |
| 130 | return; |
| 131 | } |
| 132 | //! [submitContact part1] |
| 133 | //! [submitContact part2] |
| 134 | if (!contacts.contains(name)) { |
| 135 | contacts.insert(name, address); |
| 136 | QMessageBox::information(this, tr("Add Successful" ), |
| 137 | tr("\"%1\" has been added to your address book." ).arg(name)); |
| 138 | } else { |
| 139 | QMessageBox::information(this, tr("Add Unsuccessful" ), |
| 140 | tr("Sorry, \"%1\" is already in your address book." ).arg(name)); |
| 141 | return; |
| 142 | } |
| 143 | //! [submitContact part2] |
| 144 | //! [submitContact part3] |
| 145 | if (contacts.isEmpty()) { |
| 146 | nameLine->clear(); |
| 147 | addressText->clear(); |
| 148 | } |
| 149 | |
| 150 | nameLine->setReadOnly(true); |
| 151 | addressText->setReadOnly(true); |
| 152 | addButton->setEnabled(true); |
| 153 | submitButton->hide(); |
| 154 | cancelButton->hide(); |
| 155 | } |
| 156 | //! [submitContact part3] |
| 157 | //! [cancel] |
| 158 | void AddressBook::cancel() |
| 159 | { |
| 160 | nameLine->setText(oldName); |
| 161 | nameLine->setReadOnly(true); |
| 162 | |
| 163 | addressText->setText(oldAddress); |
| 164 | addressText->setReadOnly(true); |
| 165 | |
| 166 | addButton->setEnabled(true); |
| 167 | submitButton->hide(); |
| 168 | cancelButton->hide(); |
| 169 | } |
| 170 | //! [cancel] |
| 171 | |