author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** |
|
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. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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 |
||
67 |
// Remeber the indexes of the columns |
|
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 |