~ubuntu-branches/ubuntu/trusty/grantlee/trusty

« back to all changes in this revision

Viewing changes to examples/books/bookwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
4
** Contact: Qt Software Information (qt-info@nokia.com)
 
5
**
 
6
** This file is part of the demonstration applications of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** No Commercial Usage
 
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.
 
14
**
 
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.
 
22
**
 
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
 
26
** package.
 
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
**
 
36
** If you are unsure which license is appropriate for your use, please
 
37
** contact the sales department at qt-sales@nokia.com.
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
// krazy:excludeall=qclasses
 
43
 
 
44
#include "bookwindow.h"
 
45
#include "bookwrapper.h"
 
46
#include "bookdelegate.h"
 
47
#include "initdb.h"
 
48
#include "grantlee_paths.h"
 
49
 
 
50
#include <QtSql>
 
51
 
 
52
#include <grantlee_core.h>
 
53
 
 
54
BookWindow::BookWindow()
 
55
{
 
56
    ui.setupUi(this);
 
57
 
 
58
    if (!QSqlDatabase::drivers().contains("QSQLITE"))
 
59
        QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");
 
60
 
 
61
    // initialize the database
 
62
    QSqlError err = initDb();
 
63
    if (err.type() != QSqlError::NoError) {
 
64
        showError(err);
 
65
        return;
 
66
    }
 
67
 
 
68
    // Create the data model
 
69
    model = new QSqlRelationalTableModel(ui.bookTable);
 
70
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
 
71
    model->setTable("books");
 
72
 
 
73
    // Rememeber the indexes of the columns
 
74
    authorIdx = model->fieldIndex("author");
 
75
    genreIdx = model->fieldIndex("genre");
 
76
 
 
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"));
 
80
 
 
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"));
 
87
 
 
88
    // Populate the model
 
89
    if (!model->select()) {
 
90
        showError(model->lastError());
 
91
        return;
 
92
    }
 
93
 
 
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);
 
99
 
 
100
    // Initialize the Author combo box
 
101
    ui.authorEdit->setModel(model->relationModel(authorIdx));
 
102
    ui.authorEdit->setModelColumn(model->relationModel(authorIdx)->fieldIndex("name"));
 
103
 
 
104
    ui.genreEdit->setModel(model->relationModel(genreIdx));
 
105
    ui.genreEdit->setModelColumn(model->relationModel(genreIdx)->fieldIndex("name"));
 
106
 
 
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"));
 
115
 
 
116
    connect(ui.bookTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
 
117
            mapper, SLOT(setCurrentModelIndex(QModelIndex)));
 
118
 
 
119
    ui.bookTable->setCurrentIndex(model->index(0, 0));
 
120
 
 
121
    ui.exportTheme->insertItems(0, QStringList() << "simple" << "coloured" << "simple2" << "coloured2" );
 
122
 
 
123
    connect(ui.exportButton, SIGNAL(pressed()), SLOT(renderBooks()));
 
124
 
 
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);
 
129
 
 
130
    m_engine->setPluginPaths( QStringList() << GRANTLEE_PLUGIN_PATH );
 
131
}
 
132
 
 
133
void BookWindow::showError(const QSqlError &err)
 
134
{
 
135
    QMessageBox::critical(this, "Unable to initialize Database",
 
136
                "Error initializing database: " + err.text());
 
137
}
 
138
 
 
139
void BookWindow::renderBooks()
 
140
{
 
141
    int rows = model->rowCount();
 
142
    QVariantHash mapping;
 
143
    QVariantList bookList;
 
144
    for (int row = 0; row < rows; ++row)
 
145
    {
 
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);
 
153
    }
 
154
    mapping.insert("books", bookList);
 
155
 
 
156
    QString themeName = ui.exportTheme->currentText();
 
157
 
 
158
    Grantlee::Context c(mapping);
 
159
 
 
160
    Grantlee::Template t = m_engine->loadByName( themeName + ".html" );
 
161
    if (!t)
 
162
    {
 
163
      QMessageBox::critical(this, "Unable to load template",
 
164
                QString( "Error loading template: %1" ).arg( themeName + ".html" ) );
 
165
      return;
 
166
    }
 
167
 
 
168
    if ( t->error() )
 
169
    {
 
170
      QMessageBox::critical(this, "Unable to load template",
 
171
                QString( "Error loading template: %1" ).arg( t->errorString() ) );
 
172
      return;
 
173
    }
 
174
 
 
175
    bool ok;
 
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())
 
180
      return;
 
181
 
 
182
    QFile file( text );
 
183
    if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
 
184
      return;
 
185
 
 
186
    QString content = t->render(&c);
 
187
 
 
188
    if ( t->error() )
 
189
    {
 
190
      QMessageBox::critical(this, "Unable render template",
 
191
                QString( "Error rendering template: %1" ).arg( t->errorString() ) );
 
192
      return;
 
193
    }
 
194
 
 
195
    file.write(content.toLocal8Bit());
 
196
    file.close();
 
197
 
 
198
}
 
199
 
 
200
#include "bookwindow.moc"
 
201