~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to demos/books/bookwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <QtSql>
 
2
 
 
3
#include "bookdelegate.h"
 
4
#include "bookwindow.h"
 
5
#include "initdb.h"
 
6
 
 
7
BookWindow::BookWindow()
 
8
{
 
9
    ui.setupUi(this);
 
10
 
 
11
    if (!QSqlDatabase::drivers().contains("QSQLITE"))
 
12
        QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");
 
13
 
 
14
    // initialize the database
 
15
    QSqlError err = initDb();
 
16
    if (err.type() != QSqlError::NoError) {
 
17
        showError(err);
 
18
        return;
 
19
    }
 
20
 
 
21
    // Create the data model
 
22
    model = new QSqlRelationalTableModel(ui.bookTable);
 
23
    model->setTable("books");
 
24
 
 
25
    // Remeber the indexes of the columns
 
26
    authorIdx = model->fieldIndex("author");
 
27
    genreIdx = model->fieldIndex("genre");
 
28
 
 
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"));
 
32
 
 
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"));
 
39
 
 
40
    // Populate the model
 
41
    if (!model->select()) {
 
42
        showError(model->lastError());
 
43
        return;
 
44
    }
 
45
 
 
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);
 
50
 
 
51
    // Initialize the Author combo box
 
52
    ui.authorEdit->setModel(model->relationModel(authorIdx));
 
53
    ui.authorEdit->setModelColumn(model->relationModel(authorIdx)->fieldIndex("name"));
 
54
 
 
55
    ui.genreEdit->setModel(model->relationModel(genreIdx));
 
56
    ui.genreEdit->setModelColumn(model->relationModel(genreIdx)->fieldIndex("name"));
 
57
 
 
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)));
 
62
}
 
63
 
 
64
void BookWindow::showError(const QSqlError &err)
 
65
{
 
66
    QMessageBox::critical(this, "Unable to initialize Database",
 
67
                "Error initializing database: " + err.text());
 
68
}
 
69
 
 
70
void BookWindow::currentBookChanged(const QModelIndex &index)
 
71
{
 
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);
 
78
 
 
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());
 
85
}
 
86
 
 
87
void BookWindow::on_authorEdit_activated(const QString &text)
 
88
{
 
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);
 
92
}
 
93
 
 
94
void BookWindow::on_genreEdit_activated(const QString &text)
 
95
{
 
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);
 
99
}
 
100
 
 
101
void BookWindow::on_ratingEdit_activated(int value)
 
102
{
 
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);
 
107
}
 
108
 
 
109
void BookWindow::on_titleEdit_textChanged(const QString &text)
 
110
{
 
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);
 
115
}
 
116
 
 
117
void BookWindow::on_yearEdit_valueChanged(int value)
 
118
{
 
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);
 
123
}
 
124
 
 
125
void BookWindow::dataChanged(const QModelIndex &index)
 
126
{
 
127
    if (index.row() == ui.bookTable->currentIndex().row())
 
128
        currentBookChanged(index);
 
129
}
 
130