[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the demonstration applications of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 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 | **
|
---|
[561] | 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.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at qt-info@nokia.com.
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "bookwindow.h"
|
---|
| 43 | #include "bookdelegate.h"
|
---|
| 44 | #include "initdb.h"
|
---|
| 45 |
|
---|
| 46 | #include <QtSql>
|
---|
| 47 |
|
---|
| 48 | BookWindow::BookWindow()
|
---|
| 49 | {
|
---|
| 50 | ui.setupUi(this);
|
---|
| 51 |
|
---|
| 52 | if (!QSqlDatabase::drivers().contains("QSQLITE"))
|
---|
| 53 | QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");
|
---|
| 54 |
|
---|
| 55 | // initialize the database
|
---|
| 56 | QSqlError err = initDb();
|
---|
| 57 | if (err.type() != QSqlError::NoError) {
|
---|
| 58 | showError(err);
|
---|
| 59 | return;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // Create the data model
|
---|
| 63 | model = new QSqlRelationalTableModel(ui.bookTable);
|
---|
| 64 | model->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
---|
| 65 | model->setTable("books");
|
---|
| 66 |
|
---|
[846] | 67 | // Remember the indexes of the columns
|
---|
[2] | 68 | authorIdx = model->fieldIndex("author");
|
---|
| 69 | genreIdx = model->fieldIndex("genre");
|
---|
| 70 |
|
---|
| 71 | // Set the relations to the other database tables
|
---|
| 72 | model->setRelation(authorIdx, QSqlRelation("authors", "id", "name"));
|
---|
| 73 | model->setRelation(genreIdx, QSqlRelation("genres", "id", "name"));
|
---|
| 74 |
|
---|
| 75 | // Set the localized header captions
|
---|
| 76 | model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name"));
|
---|
| 77 | model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre"));
|
---|
| 78 | model->setHeaderData(model->fieldIndex("title"), Qt::Horizontal, tr("Title"));
|
---|
| 79 | model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year"));
|
---|
| 80 | model->setHeaderData(model->fieldIndex("rating"), Qt::Horizontal, tr("Rating"));
|
---|
| 81 |
|
---|
| 82 | // Populate the model
|
---|
| 83 | if (!model->select()) {
|
---|
| 84 | showError(model->lastError());
|
---|
| 85 | return;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | // Set the model and hide the ID column
|
---|
| 89 | ui.bookTable->setModel(model);
|
---|
| 90 | ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable));
|
---|
| 91 | ui.bookTable->setColumnHidden(model->fieldIndex("id"), true);
|
---|
| 92 | ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection);
|
---|
| 93 |
|
---|
| 94 | // Initialize the Author combo box
|
---|
| 95 | ui.authorEdit->setModel(model->relationModel(authorIdx));
|
---|
| 96 | ui.authorEdit->setModelColumn(model->relationModel(authorIdx)->fieldIndex("name"));
|
---|
| 97 |
|
---|
| 98 | ui.genreEdit->setModel(model->relationModel(genreIdx));
|
---|
| 99 | ui.genreEdit->setModelColumn(model->relationModel(genreIdx)->fieldIndex("name"));
|
---|
| 100 |
|
---|
| 101 | QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
|
---|
| 102 | mapper->setModel(model);
|
---|
| 103 | mapper->setItemDelegate(new BookDelegate(this));
|
---|
| 104 | mapper->addMapping(ui.titleEdit, model->fieldIndex("title"));
|
---|
| 105 | mapper->addMapping(ui.yearEdit, model->fieldIndex("year"));
|
---|
| 106 | mapper->addMapping(ui.authorEdit, authorIdx);
|
---|
| 107 | mapper->addMapping(ui.genreEdit, genreIdx);
|
---|
| 108 | mapper->addMapping(ui.ratingEdit, model->fieldIndex("rating"));
|
---|
| 109 |
|
---|
| 110 | connect(ui.bookTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
|
---|
| 111 | mapper, SLOT(setCurrentModelIndex(QModelIndex)));
|
---|
| 112 |
|
---|
| 113 | ui.bookTable->setCurrentIndex(model->index(0, 0));
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void BookWindow::showError(const QSqlError &err)
|
---|
| 117 | {
|
---|
| 118 | QMessageBox::critical(this, "Unable to initialize Database",
|
---|
| 119 | "Error initializing database: " + err.text());
|
---|
| 120 | }
|
---|
| 121 |
|
---|