| 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 demonstration applications 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 "bookwindow.h" |
| 52 | #include "bookdelegate.h" |
| 53 | #include "initdb.h" |
| 54 | |
| 55 | #include <QtSql> |
| 56 | |
| 57 | BookWindow::BookWindow() |
| 58 | { |
| 59 | ui.setupUi(this); |
| 60 | |
| 61 | if (!QSqlDatabase::drivers().contains("QSQLITE" )) |
| 62 | QMessageBox::critical( |
| 63 | this, |
| 64 | "Unable to load database" , |
| 65 | "This demo needs the SQLITE driver" |
| 66 | ); |
| 67 | |
| 68 | // Initialize the database: |
| 69 | QSqlError err = initDb(); |
| 70 | if (err.type() != QSqlError::NoError) { |
| 71 | showError(err); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Create the data model: |
| 76 | model = new QSqlRelationalTableModel(ui.bookTable); |
| 77 | model->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 78 | model->setTable("books" ); |
| 79 | |
| 80 | // Remember the indexes of the columns: |
| 81 | authorIdx = model->fieldIndex("author" ); |
| 82 | genreIdx = model->fieldIndex("genre" ); |
| 83 | |
| 84 | // Set the relations to the other database tables: |
| 85 | model->setRelation(authorIdx, QSqlRelation("authors" , "id" , "name" )); |
| 86 | model->setRelation(genreIdx, QSqlRelation("genres" , "id" , "name" )); |
| 87 | |
| 88 | // Set the localized header captions: |
| 89 | model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name" )); |
| 90 | model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre" )); |
| 91 | model->setHeaderData(model->fieldIndex("title" ), |
| 92 | Qt::Horizontal, tr("Title" )); |
| 93 | model->setHeaderData(model->fieldIndex("year" ), Qt::Horizontal, tr("Year" )); |
| 94 | model->setHeaderData(model->fieldIndex("rating" ), |
| 95 | Qt::Horizontal, tr("Rating" )); |
| 96 | |
| 97 | // Populate the model: |
| 98 | if (!model->select()) { |
| 99 | showError(model->lastError()); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Set the model and hide the ID column: |
| 104 | ui.bookTable->setModel(model); |
| 105 | ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable)); |
| 106 | ui.bookTable->setColumnHidden(model->fieldIndex("id" ), true); |
| 107 | ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection); |
| 108 | |
| 109 | // Initialize the Author combo box: |
| 110 | ui.authorEdit->setModel(model->relationModel(authorIdx)); |
| 111 | ui.authorEdit->setModelColumn( |
| 112 | model->relationModel(authorIdx)->fieldIndex("name" )); |
| 113 | |
| 114 | ui.genreEdit->setModel(model->relationModel(genreIdx)); |
| 115 | ui.genreEdit->setModelColumn( |
| 116 | model->relationModel(genreIdx)->fieldIndex("name" )); |
| 117 | |
| 118 | // Lock and prohibit resizing of the width of the rating column: |
| 119 | ui.bookTable->horizontalHeader()->setSectionResizeMode( |
| 120 | model->fieldIndex("rating" ), |
| 121 | QHeaderView::ResizeToContents); |
| 122 | |
| 123 | QDataWidgetMapper *mapper = new QDataWidgetMapper(this); |
| 124 | mapper->setModel(model); |
| 125 | mapper->setItemDelegate(new BookDelegate(this)); |
| 126 | mapper->addMapping(ui.titleEdit, model->fieldIndex("title" )); |
| 127 | mapper->addMapping(ui.yearEdit, model->fieldIndex("year" )); |
| 128 | mapper->addMapping(ui.authorEdit, authorIdx); |
| 129 | mapper->addMapping(ui.genreEdit, genreIdx); |
| 130 | mapper->addMapping(ui.ratingEdit, model->fieldIndex("rating" )); |
| 131 | |
| 132 | connect(ui.bookTable->selectionModel(), |
| 133 | &QItemSelectionModel::currentRowChanged, |
| 134 | mapper, |
| 135 | &QDataWidgetMapper::setCurrentModelIndex |
| 136 | ); |
| 137 | |
| 138 | ui.bookTable->setCurrentIndex(model->index(0, 0)); |
| 139 | createMenuBar(); |
| 140 | } |
| 141 | |
| 142 | void BookWindow::showError(const QSqlError &err) |
| 143 | { |
| 144 | QMessageBox::critical(this, "Unable to initialize Database" , |
| 145 | "Error initializing database: " + err.text()); |
| 146 | } |
| 147 | |
| 148 | void BookWindow::() |
| 149 | { |
| 150 | QAction *quitAction = new QAction(tr("&Quit" ), this); |
| 151 | QAction *aboutAction = new QAction(tr("&About" ), this); |
| 152 | QAction *aboutQtAction = new QAction(tr("&About Qt" ), this); |
| 153 | |
| 154 | QMenu * = menuBar()->addMenu(tr("&File" )); |
| 155 | fileMenu->addAction(quitAction); |
| 156 | |
| 157 | QMenu * = menuBar()->addMenu(tr("&Help" )); |
| 158 | helpMenu->addAction(aboutAction); |
| 159 | helpMenu->addAction(aboutQtAction); |
| 160 | |
| 161 | connect(quitAction, &QAction::triggered, this, &BookWindow::close); |
| 162 | connect(aboutAction, &QAction::triggered, this, &BookWindow::about); |
| 163 | connect(aboutQtAction, &QAction::triggered, qApp, &QApplication::aboutQt); |
| 164 | } |
| 165 | |
| 166 | void BookWindow::about() |
| 167 | { |
| 168 | QMessageBox::about(this, tr("About Books" ), |
| 169 | tr("<p>The <b>Books</b> example shows how to use Qt SQL classes " |
| 170 | "with a model/view framework." )); |
| 171 | } |
| 172 | |