demos/books/bookwindow.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the demonstration applications of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "bookwindow.h"
00025 #include "bookdelegate.h"
00026 #include "initdb.h"
00027 
00028 #include <QtSql>
00029 
00030 BookWindow::BookWindow()
00031 {
00032     ui.setupUi(this);
00033 
00034     if (!QSqlDatabase::drivers().contains("QSQLITE"))
00035         QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");
00036 
00037     // initialize the database
00038     QSqlError err = initDb();
00039     if (err.type() != QSqlError::NoError) {
00040         showError(err);
00041         return;
00042     }
00043 
00044     // Create the data model
00045     model = new QSqlRelationalTableModel(ui.bookTable);
00046     model->setEditStrategy(QSqlTableModel::OnManualSubmit);
00047     model->setTable("books");
00048 
00049     // Remeber the indexes of the columns
00050     authorIdx = model->fieldIndex("author");
00051     genreIdx = model->fieldIndex("genre");
00052 
00053     // Set the relations to the other database tables
00054     model->setRelation(authorIdx, QSqlRelation("authors", "id", "name"));
00055     model->setRelation(genreIdx, QSqlRelation("genres", "id", "name"));
00056 
00057     // Set the localized header captions
00058     model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name"));
00059     model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre"));
00060     model->setHeaderData(model->fieldIndex("title"), Qt::Horizontal, tr("Title"));
00061     model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year"));
00062     model->setHeaderData(model->fieldIndex("rating"), Qt::Horizontal, tr("Rating"));
00063 
00064     // Populate the model
00065     if (!model->select()) {
00066         showError(model->lastError());
00067         return;
00068     }
00069 
00070     // Set the model and hide the ID column
00071     ui.bookTable->setModel(model);
00072     ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable));
00073     ui.bookTable->setColumnHidden(model->fieldIndex("id"), true);
00074 
00075     // Initialize the Author combo box
00076     ui.authorEdit->setModel(model->relationModel(authorIdx));
00077     ui.authorEdit->setModelColumn(model->relationModel(authorIdx)->fieldIndex("name"));
00078 
00079     ui.genreEdit->setModel(model->relationModel(genreIdx));
00080     ui.genreEdit->setModelColumn(model->relationModel(genreIdx)->fieldIndex("name"));
00081 
00082     QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
00083     mapper->setModel(model);
00084     mapper->setItemDelegate(new BookDelegate(this));
00085     mapper->addMapping(ui.titleEdit, model->fieldIndex("title"));
00086     mapper->addMapping(ui.yearEdit, model->fieldIndex("year"));
00087     mapper->addMapping(ui.authorEdit, authorIdx);
00088     mapper->addMapping(ui.genreEdit, genreIdx);
00089     mapper->addMapping(ui.ratingEdit, model->fieldIndex("rating"));
00090 
00091     connect(ui.bookTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
00092             mapper, SLOT(setCurrentModelIndex(QModelIndex)));
00093 
00094     ui.bookTable->setCurrentIndex(model->index(0, 0));
00095 }
00096 
00097 void BookWindow::showError(const QSqlError &err)
00098 {
00099     QMessageBox::critical(this, "Unable to initialize Database",
00100                 "Error initializing database: " + err.text());
00101 }
00102 

Generated on Thu Mar 15 11:52:28 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1