| 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 <QtSql> | 
| 53 |  | 
| 54 | #include "window.h" | 
| 55 |  | 
| 56 | //! [Set up widgets] | 
| 57 | Window::Window(QWidget *parent) | 
| 58 |     : QWidget(parent) | 
| 59 | { | 
| 60 |     setupModel(); | 
| 61 |  | 
| 62 |     nameLabel = new QLabel(tr("Na&me:" )); | 
| 63 |     nameEdit = new QLineEdit(); | 
| 64 |     addressLabel = new QLabel(tr("&Address:" )); | 
| 65 |     addressEdit = new QTextEdit(); | 
| 66 |     typeLabel = new QLabel(tr("&Type:" )); | 
| 67 |     typeComboBox = new QComboBox(); | 
| 68 |     nextButton = new QPushButton(tr("&Next" )); | 
| 69 |     previousButton = new QPushButton(tr("&Previous" )); | 
| 70 |  | 
| 71 |     nameLabel->setBuddy(nameEdit); | 
| 72 |     addressLabel->setBuddy(addressEdit); | 
| 73 |     typeLabel->setBuddy(typeComboBox); | 
| 74 | //! [Set up widgets] | 
| 75 |  | 
| 76 | //! [Set up the mapper] | 
| 77 |     QSqlTableModel *relModel = model->relationModel(typeIndex); | 
| 78 |     typeComboBox->setModel(relModel); | 
| 79 |     typeComboBox->setModelColumn(relModel->fieldIndex("description" )); | 
| 80 |  | 
| 81 |     mapper = new QDataWidgetMapper(this); | 
| 82 |     mapper->setModel(model); | 
| 83 |     mapper->setItemDelegate(new QSqlRelationalDelegate(this)); | 
| 84 |     mapper->addMapping(nameEdit, model->fieldIndex("name" )); | 
| 85 |     mapper->addMapping(addressEdit, model->fieldIndex("address" )); | 
| 86 |     mapper->addMapping(typeComboBox, typeIndex); | 
| 87 | //! [Set up the mapper] | 
| 88 |  | 
| 89 | //! [Set up connections and layouts] | 
| 90 |     connect(previousButton, &QPushButton::clicked, | 
| 91 |             mapper, &QDataWidgetMapper::toPrevious); | 
| 92 |     connect(nextButton, &QPushButton::clicked, | 
| 93 |             mapper, &QDataWidgetMapper::toNext); | 
| 94 |     connect(mapper, &QDataWidgetMapper::currentIndexChanged, | 
| 95 |             this, &Window::updateButtons); | 
| 96 |  | 
| 97 |     QGridLayout *layout = new QGridLayout(); | 
| 98 |     layout->addWidget(nameLabel, 0, 0, 1, 1); | 
| 99 |     layout->addWidget(nameEdit, 0, 1, 1, 1); | 
| 100 |     layout->addWidget(previousButton, 0, 2, 1, 1); | 
| 101 |     layout->addWidget(addressLabel, 1, 0, 1, 1); | 
| 102 |     layout->addWidget(addressEdit, 1, 1, 2, 1); | 
| 103 |     layout->addWidget(nextButton, 1, 2, 1, 1); | 
| 104 |     layout->addWidget(typeLabel, 3, 0, 1, 1); | 
| 105 |     layout->addWidget(typeComboBox, 3, 1, 1, 1); | 
| 106 |     setLayout(layout); | 
| 107 |  | 
| 108 |     setWindowTitle(tr("SQL Widget Mapper" )); | 
| 109 |     mapper->toFirst(); | 
| 110 | } | 
| 111 | //! [Set up connections and layouts] | 
| 112 |  | 
| 113 | //! [Set up the main table] | 
| 114 | void Window::setupModel() | 
| 115 | { | 
| 116 |     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE" ); | 
| 117 |     db.setDatabaseName(":memory:" ); | 
| 118 |     if (!db.open()) { | 
| 119 |         QMessageBox::critical(0, tr("Cannot open database" ), | 
| 120 |             tr("Unable to establish a database connection.\n"  | 
| 121 |                "This example needs SQLite support. Please read "  | 
| 122 |                "the Qt SQL driver documentation for information how "  | 
| 123 |                "to build it." ), QMessageBox::Cancel); | 
| 124 |         return; | 
| 125 |     } | 
| 126 |  | 
| 127 |     QSqlQuery query; | 
| 128 |     query.exec("create table person (id int primary key, "  | 
| 129 |                "name varchar(20), address varchar(200), typeid int)" ); | 
| 130 |     query.exec("insert into person values(1, 'Alice', "  | 
| 131 |                "'<qt>123 Main Street<br/>Market Town</qt>', 101)" ); | 
| 132 |     query.exec("insert into person values(2, 'Bob', "  | 
| 133 |                "'<qt>PO Box 32<br/>Mail Handling Service"  | 
| 134 |                "<br/>Service City</qt>', 102)" ); | 
| 135 |     query.exec("insert into person values(3, 'Carol', "  | 
| 136 |                "'<qt>The Lighthouse<br/>Remote Island</qt>', 103)" ); | 
| 137 |     query.exec("insert into person values(4, 'Donald', "  | 
| 138 |                "'<qt>47338 Park Avenue<br/>Big City</qt>', 101)" ); | 
| 139 |     query.exec("insert into person values(5, 'Emma', "  | 
| 140 |                "'<qt>Research Station<br/>Base Camp<br/>"  | 
| 141 |                "Big Mountain</qt>', 103)" ); | 
| 142 | //! [Set up the main table] | 
| 143 |  | 
| 144 | //! [Set up the address type table] | 
| 145 |     query.exec("create table addresstype (id int, description varchar(20))" ); | 
| 146 |     query.exec("insert into addresstype values(101, 'Home')" ); | 
| 147 |     query.exec("insert into addresstype values(102, 'Work')" ); | 
| 148 |     query.exec("insert into addresstype values(103, 'Other')" ); | 
| 149 |  | 
| 150 |     model = new QSqlRelationalTableModel(this); | 
| 151 |     model->setTable("person" ); | 
| 152 |     model->setEditStrategy(QSqlTableModel::OnManualSubmit); | 
| 153 |  | 
| 154 |     typeIndex = model->fieldIndex("typeid" ); | 
| 155 |  | 
| 156 |     model->setRelation(typeIndex, | 
| 157 |            QSqlRelation("addresstype" , "id" , "description" )); | 
| 158 |     model->select(); | 
| 159 | } | 
| 160 | //! [Set up the address type table] | 
| 161 |  | 
| 162 | //! [Slot for updating the buttons] | 
| 163 | void Window::updateButtons(int row) | 
| 164 | { | 
| 165 |     previousButton->setEnabled(row > 0); | 
| 166 |     nextButton->setEnabled(row < model->rowCount() - 1); | 
| 167 | } | 
| 168 | //! [Slot for updating the buttons] | 
| 169 |  |