1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: Qt Software Information (qt-info@nokia.com)
6
** This file is part of the demonstration applications of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
10
** This file contains pre-release code and may not be distributed.
11
** You may use this file in accordance with the terms and conditions
12
** contained in the either Technology Preview License Agreement or the
13
** Beta Release License Agreement.
15
** GNU Lesser General Public License Usage
16
** Alternatively, this file may be used under the terms of the GNU Lesser
17
** General Public License version 2.1 as published by the Free Software
18
** Foundation and appearing in the file LICENSE.LGPL included in the
19
** packaging of this file. Please review the following information to
20
** ensure the GNU Lesser General Public License version 2.1 requirements
21
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
** In addition, as a special exception, Nokia gives you certain
24
** additional rights. These rights are described in the Nokia Qt LGPL
25
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
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.
36
** If you are unsure which license is appropriate for your use, please
37
** contact the sales department at qt-sales@nokia.com.
40
****************************************************************************/
42
// krazy:excludeall=qclasses
44
#include "bookwindow.h"
45
#include "bookwrapper.h"
46
#include "bookdelegate.h"
48
#include "grantlee_paths.h"
52
#include <grantlee_core.h>
54
BookWindow::BookWindow()
58
if (!QSqlDatabase::drivers().contains("QSQLITE"))
59
QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");
61
// initialize the database
62
QSqlError err = initDb();
63
if (err.type() != QSqlError::NoError) {
68
// Create the data model
69
model = new QSqlRelationalTableModel(ui.bookTable);
70
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
71
model->setTable("books");
73
// Rememeber the indexes of the columns
74
authorIdx = model->fieldIndex("author");
75
genreIdx = model->fieldIndex("genre");
77
// Set the relations to the other database tables
78
model->setRelation(authorIdx, QSqlRelation("authors", "id", "name"));
79
model->setRelation(genreIdx, QSqlRelation("genres", "id", "name"));
81
// Set the localized header captions
82
model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name"));
83
model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre"));
84
model->setHeaderData(model->fieldIndex("title"), Qt::Horizontal, tr("Title"));
85
model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year"));
86
model->setHeaderData(model->fieldIndex("rating"), Qt::Horizontal, tr("Rating"));
89
if (!model->select()) {
90
showError(model->lastError());
94
// Set the model and hide the ID column
95
ui.bookTable->setModel(model);
96
ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable));
97
ui.bookTable->setColumnHidden(model->fieldIndex("id"), true);
98
ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection);
100
// Initialize the Author combo box
101
ui.authorEdit->setModel(model->relationModel(authorIdx));
102
ui.authorEdit->setModelColumn(model->relationModel(authorIdx)->fieldIndex("name"));
104
ui.genreEdit->setModel(model->relationModel(genreIdx));
105
ui.genreEdit->setModelColumn(model->relationModel(genreIdx)->fieldIndex("name"));
107
QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
108
mapper->setModel(model);
109
mapper->setItemDelegate(new BookDelegate(this));
110
mapper->addMapping(ui.titleEdit, model->fieldIndex("title"));
111
mapper->addMapping(ui.yearEdit, model->fieldIndex("year"));
112
mapper->addMapping(ui.authorEdit, authorIdx);
113
mapper->addMapping(ui.genreEdit, genreIdx);
114
mapper->addMapping(ui.ratingEdit, model->fieldIndex("rating"));
116
connect(ui.bookTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
117
mapper, SLOT(setCurrentModelIndex(QModelIndex)));
119
ui.bookTable->setCurrentIndex(model->index(0, 0));
121
ui.exportTheme->insertItems(0, QStringList() << "simple" << "coloured" << "simple2" << "coloured2" );
123
connect(ui.exportButton, SIGNAL(pressed()), SLOT(renderBooks()));
125
m_engine = new Grantlee::Engine();
126
Grantlee::FileSystemTemplateLoader::Ptr loader = Grantlee::FileSystemTemplateLoader::Ptr( new Grantlee::FileSystemTemplateLoader() );
127
loader->setTemplateDirs( QStringList() << GRANTLEE_TEMPLATE_PATH );
128
m_engine->addTemplateLoader(loader);
130
m_engine->setPluginPaths( QStringList() << GRANTLEE_PLUGIN_PATH );
133
void BookWindow::showError(const QSqlError &err)
135
QMessageBox::critical(this, "Unable to initialize Database",
136
"Error initializing database: " + err.text());
139
void BookWindow::renderBooks()
141
int rows = model->rowCount();
142
QVariantHash mapping;
143
QVariantList bookList;
144
for (int row = 0; row < rows; ++row)
146
QString title = model->index(row, 1).data().toString();
147
QString author = model->index(row, 2).data().toString();
148
QString genre = model->index(row, 3).data().toString();
149
int rating = model->index(row, 5).data().toInt();
150
QObject *book = new BookWrapper(author, title, genre, rating, this);
151
QVariant var = QVariant::fromValue(book);
152
bookList.append(var);
154
mapping.insert("books", bookList);
156
QString themeName = ui.exportTheme->currentText();
158
Grantlee::Context c(mapping);
160
Grantlee::Template t = m_engine->loadByName( themeName + ".html" );
163
QMessageBox::critical(this, "Unable to load template",
164
QString( "Error loading template: %1" ).arg( themeName + ".html" ) );
170
QMessageBox::critical(this, "Unable to load template",
171
QString( "Error loading template: %1" ).arg( t->errorString() ) );
176
QString text = QInputDialog::getText(this, tr("Export Location"),
177
tr("file name:"), QLineEdit::Normal,
178
QDir::home().absolutePath() + "/book_export.html", &ok);
179
if (!ok || text.isEmpty())
183
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
186
QString content = t->render(&c);
190
QMessageBox::critical(this, "Unable render template",
191
QString( "Error rendering template: %1" ).arg( t->errorString() ) );
195
file.write(content.toLocal8Bit());
200
#include "bookwindow.moc"