3
#include "bookdelegate.h"
4
#include "bookwindow.h"
7
BookWindow::BookWindow()
11
if (!QSqlDatabase::drivers().contains("QSQLITE"))
12
QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");
14
// initialize the database
15
QSqlError err = initDb();
16
if (err.type() != QSqlError::NoError) {
21
// Create the data model
22
model = new QSqlRelationalTableModel(ui.bookTable);
23
model->setTable("books");
25
// Remeber the indexes of the columns
26
authorIdx = model->fieldIndex("author");
27
genreIdx = model->fieldIndex("genre");
29
// Set the relations to the other database tables
30
model->setRelation(authorIdx, QSqlRelation("authors", "id", "name"));
31
model->setRelation(genreIdx, QSqlRelation("genres", "id", "name"));
33
// Set the localized header captions
34
model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name"));
35
model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre"));
36
model->setHeaderData(model->fieldIndex("title"), Qt::Horizontal, tr("Title"));
37
model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year"));
38
model->setHeaderData(model->fieldIndex("rating"), Qt::Horizontal, tr("Rating"));
41
if (!model->select()) {
42
showError(model->lastError());
46
// Set the model and hide the ID column
47
ui.bookTable->setModel(model);
48
ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable));
49
ui.bookTable->setColumnHidden(model->fieldIndex("id"), true);
51
// Initialize the Author combo box
52
ui.authorEdit->setModel(model->relationModel(authorIdx));
53
ui.authorEdit->setModelColumn(model->relationModel(authorIdx)->fieldIndex("name"));
55
ui.genreEdit->setModel(model->relationModel(genreIdx));
56
ui.genreEdit->setModelColumn(model->relationModel(genreIdx)->fieldIndex("name"));
58
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
59
this, SLOT(dataChanged(QModelIndex)));
60
connect(ui.bookTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
61
this, SLOT(currentBookChanged(QModelIndex)));
64
void BookWindow::showError(const QSqlError &err)
66
QMessageBox::critical(this, "Unable to initialize Database",
67
"Error initializing database: " + err.text());
70
void BookWindow::currentBookChanged(const QModelIndex &index)
72
bool enabled = index.isValid();
73
ui.titleEdit->setEnabled(enabled);
74
ui.yearEdit->setEnabled(enabled);
75
ui.authorEdit->setEnabled(enabled);
76
ui.genreEdit->setEnabled(enabled);
77
ui.ratingEdit->setEnabled(enabled);
79
QSqlRecord rec = model->record(index.row());
80
ui.titleEdit->setText(rec.value("title").toString());
81
ui.yearEdit->setValue(rec.value("year").toInt());
82
ui.authorEdit->setCurrentIndex(ui.authorEdit->findText(rec.value(authorIdx).toString()));
83
ui.genreEdit->setCurrentIndex(ui.genreEdit->findText(rec.value(genreIdx).toString()));
84
ui.ratingEdit->setCurrentIndex(rec.value("rating").toInt());
87
void BookWindow::on_authorEdit_activated(const QString &text)
89
QModelIndex currentIndex = model->index(ui.bookTable->currentIndex().row(), authorIdx);
90
if (model->data(currentIndex).toString() != text)
91
ui.bookTable->itemDelegate()->setModelData(ui.authorEdit, model, currentIndex);
94
void BookWindow::on_genreEdit_activated(const QString &text)
96
QModelIndex currentIndex = model->index(ui.bookTable->currentIndex().row(), genreIdx);
97
if (model->data(currentIndex).toString() != text)
98
ui.bookTable->itemDelegate()->setModelData(ui.genreEdit, model, currentIndex);
101
void BookWindow::on_ratingEdit_activated(int value)
103
QModelIndex currentIndex = model->index(ui.bookTable->currentIndex().row(),
104
model->fieldIndex("rating"));
105
if (model->data(currentIndex).toInt() != value)
106
model->setData(currentIndex, value);
109
void BookWindow::on_titleEdit_textChanged(const QString &text)
111
QModelIndex currentIndex = model->index(ui.bookTable->currentIndex().row(),
112
model->fieldIndex("title"));
113
if (model->data(currentIndex).toString() != text)
114
model->setData(currentIndex, text);
117
void BookWindow::on_yearEdit_valueChanged(int value)
119
QModelIndex currentIndex = model->index(ui.bookTable->currentIndex().row(),
120
model->fieldIndex("year"));
121
if (model->data(currentIndex).toInt() != value)
122
model->setData(currentIndex, value);
125
void BookWindow::dataChanged(const QModelIndex &index)
127
if (index.row() == ui.bookTable->currentIndex().row())
128
currentBookChanged(index);