File indexing completed on 2024-12-22 03:46:48
0001 /**************************************************************************** 0002 ** 0003 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 0004 ** Contact: Qt Software Information (qt-info@nokia.com) 0005 ** 0006 ** This file is part of the demonstration applications of the Qt Toolkit. 0007 ** 0008 ** $QT_BEGIN_LICENSE:LGPL$ 0009 ** No Commercial Usage 0010 ** This file contains pre-release code and may not be distributed. 0011 ** You may use this file in accordance with the terms and conditions 0012 ** contained in the either Technology Preview License Agreement or the 0013 ** Beta Release License Agreement. 0014 ** 0015 ** GNU Lesser General Public License Usage 0016 ** Alternatively, this file may be used under the terms of the GNU Lesser 0017 ** General Public License version 2.1 as published by the Free Software 0018 ** Foundation and appearing in the file LICENSE.LGPL included in the 0019 ** packaging of this file. Please review the following information to 0020 ** ensure the GNU Lesser General Public License version 2.1 requirements 0021 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 0022 ** 0023 ** In addition, as a special exception, Nokia gives you certain 0024 ** additional rights. These rights are described in the Nokia Qt LGPL 0025 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 0026 ** package. 0027 ** 0028 ** GNU General Public License Usage 0029 ** Alternatively, this file may be used under the terms of the GNU 0030 ** General Public License version 3.0 as published by the Free Software 0031 ** Foundation and appearing in the file LICENSE.GPL included in the 0032 ** packaging of this file. Please review the following information to 0033 ** ensure the GNU General Public License version 3.0 requirements will be 0034 ** met: http://www.gnu.org/copyleft/gpl.html. 0035 ** 0036 ** If you are unsure which license is appropriate for your use, please 0037 ** contact the sales department at qt-sales@nokia.com. 0038 ** $QT_END_LICENSE$ 0039 ** 0040 ****************************************************************************/ 0041 0042 // krazy:excludeall=qclasses 0043 0044 #include "bookwindow.h" 0045 #include "bookdelegate.h" 0046 #include "bookwrapper.h" 0047 #include "initdb.h" 0048 #include "ktexttemplate_paths.h" 0049 0050 #include <QDataWidgetMapper> 0051 #include <QInputDialog> 0052 #include <QMessageBox> 0053 0054 #include <KTextTemplate/Context> 0055 #include <KTextTemplate/Engine> 0056 #include <KTextTemplate/FileSystemTemplateLoader> 0057 0058 BookWindow::BookWindow() 0059 { 0060 ui.setupUi(this); 0061 0062 if (!QSqlDatabase::drivers().contains("QSQLITE")) 0063 QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver"); 0064 0065 // initialize the database 0066 QSqlError err = initDb(); 0067 if (err.type() != QSqlError::NoError) { 0068 showError(err); 0069 return; 0070 } 0071 0072 // Create the data model 0073 model = new QSqlRelationalTableModel(ui.bookTable); 0074 model->setEditStrategy(QSqlTableModel::OnManualSubmit); 0075 model->setTable("books"); 0076 0077 // Rememeber the indexes of the columns 0078 authorIdx = model->fieldIndex("author"); 0079 genreIdx = model->fieldIndex("genre"); 0080 0081 // Set the relations to the other database tables 0082 model->setRelation(authorIdx, QSqlRelation("authors", "id", "name")); 0083 model->setRelation(genreIdx, QSqlRelation("genres", "id", "name")); 0084 0085 // Set the localized header captions 0086 model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name")); 0087 model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre")); 0088 model->setHeaderData(model->fieldIndex("title"), Qt::Horizontal, tr("Title")); 0089 model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year")); 0090 model->setHeaderData(model->fieldIndex("rating"), Qt::Horizontal, tr("Rating")); 0091 0092 // Populate the model 0093 if (!model->select()) { 0094 showError(model->lastError()); 0095 return; 0096 } 0097 0098 // Set the model and hide the ID column 0099 ui.bookTable->setModel(model); 0100 ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable)); 0101 ui.bookTable->setColumnHidden(model->fieldIndex("id"), true); 0102 ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection); 0103 0104 // Initialize the Author combo box 0105 ui.authorEdit->setModel(model->relationModel(authorIdx)); 0106 ui.authorEdit->setModelColumn(model->relationModel(authorIdx)->fieldIndex("name")); 0107 0108 ui.genreEdit->setModel(model->relationModel(genreIdx)); 0109 ui.genreEdit->setModelColumn(model->relationModel(genreIdx)->fieldIndex("name")); 0110 0111 QDataWidgetMapper *mapper = new QDataWidgetMapper(this); 0112 mapper->setModel(model); 0113 mapper->setItemDelegate(new BookDelegate(this)); 0114 mapper->addMapping(ui.titleEdit, model->fieldIndex("title")); 0115 mapper->addMapping(ui.yearEdit, model->fieldIndex("year")); 0116 mapper->addMapping(ui.authorEdit, authorIdx); 0117 mapper->addMapping(ui.genreEdit, genreIdx); 0118 mapper->addMapping(ui.ratingEdit, model->fieldIndex("rating")); 0119 0120 connect(ui.bookTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex, QModelIndex)), mapper, SLOT(setCurrentModelIndex(QModelIndex))); 0121 0122 ui.bookTable->setCurrentIndex(model->index(0, 0)); 0123 0124 ui.exportTheme->insertItems(0, 0125 QStringList() << "simple" 0126 << "coloured" 0127 << "simple2" 0128 << "coloured2"); 0129 0130 connect(ui.exportButton, SIGNAL(pressed()), SLOT(renderBooks())); 0131 0132 m_engine = new KTextTemplate::Engine(); 0133 QSharedPointer<KTextTemplate::FileSystemTemplateLoader> loader = 0134 QSharedPointer<KTextTemplate::FileSystemTemplateLoader>(new KTextTemplate::FileSystemTemplateLoader()); 0135 loader->setTemplateDirs(QStringList() << KTEXTTEMPLATE_TEMPLATE_PATH); 0136 m_engine->addTemplateLoader(loader); 0137 0138 m_engine->setPluginPaths(QStringList() << KTEXTTEMPLATE_PLUGIN_PATH); 0139 } 0140 0141 void BookWindow::showError(const QSqlError &err) 0142 { 0143 QMessageBox::critical(this, "Unable to initialize Database", "Error initializing database: " + err.text()); 0144 } 0145 0146 void BookWindow::renderBooks() const 0147 { 0148 int rows = model->rowCount(); 0149 QVariantHash mapping; 0150 QVariantList bookList; 0151 for (int row = 0; row < rows; ++row) { 0152 QString title = model->index(row, 1).data().toString(); 0153 QString author = model->index(row, 2).data().toString(); 0154 QString genre = model->index(row, 3).data().toString(); 0155 int rating = model->index(row, 5).data().toInt(); 0156 QObject *book = new BookWrapper(author, title, genre, rating, const_cast<BookWindow *>(this)); 0157 QVariant var = QVariant::fromValue(book); 0158 bookList.append(var); 0159 } 0160 mapping.insert("books", bookList); 0161 0162 QString themeName = ui.exportTheme->currentText(); 0163 0164 KTextTemplate::Context c(mapping); 0165 0166 KTextTemplate::Template t = m_engine->loadByName(themeName + ".html"); 0167 if (!t) { 0168 QMessageBox::critical(const_cast<BookWindow *>(this), "Unable to load template", QString("Error loading template: %1").arg(themeName + ".html")); 0169 return; 0170 } 0171 0172 if (t->error()) { 0173 QMessageBox::critical(const_cast<BookWindow *>(this), "Unable to load template", QString("Error loading template: %1").arg(t->errorString())); 0174 return; 0175 } 0176 0177 bool ok; 0178 QString text = QInputDialog::getText(const_cast<BookWindow *>(this), 0179 tr("Export Location"), 0180 tr("file name:"), 0181 QLineEdit::Normal, 0182 QDir::home().absolutePath() + "/book_export.html", 0183 &ok); 0184 if (!ok || text.isEmpty()) 0185 return; 0186 0187 QFile file(text); 0188 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) 0189 return; 0190 0191 QString content = t->render(&c); 0192 0193 if (t->error()) { 0194 QMessageBox::critical(const_cast<BookWindow *>(this), "Unable render template", QString("Error rendering template: %1").arg(t->errorString())); 0195 return; 0196 } 0197 0198 file.write(content.toLocal8Bit()); 0199 file.close(); 0200 } 0201 0202 #include "moc_bookwindow.cpp"