xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/database/bookwindow.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /****************************************************************************
2*4882a593Smuzhiyun **
3*4882a593Smuzhiyun ** Copyright (C) 2016 The Qt Company Ltd.
4*4882a593Smuzhiyun ** Contact: https://www.qt.io/licensing/
5*4882a593Smuzhiyun **
6*4882a593Smuzhiyun ** This file is part of the demonstration applications of the Qt Toolkit.
7*4882a593Smuzhiyun **
8*4882a593Smuzhiyun ** $QT_BEGIN_LICENSE:BSD$
9*4882a593Smuzhiyun ** Commercial License Usage
10*4882a593Smuzhiyun ** Licensees holding valid commercial Qt licenses may use this file in
11*4882a593Smuzhiyun ** accordance with the commercial license agreement provided with the
12*4882a593Smuzhiyun ** Software or, alternatively, in accordance with the terms contained in
13*4882a593Smuzhiyun ** a written agreement between you and The Qt Company. For licensing terms
14*4882a593Smuzhiyun ** and conditions see https://www.qt.io/terms-conditions. For further
15*4882a593Smuzhiyun ** information use the contact form at https://www.qt.io/contact-us.
16*4882a593Smuzhiyun **
17*4882a593Smuzhiyun ** BSD License Usage
18*4882a593Smuzhiyun ** Alternatively, you may use this file under the terms of the BSD license
19*4882a593Smuzhiyun ** as follows:
20*4882a593Smuzhiyun **
21*4882a593Smuzhiyun ** "Redistribution and use in source and binary forms, with or without
22*4882a593Smuzhiyun ** modification, are permitted provided that the following conditions are
23*4882a593Smuzhiyun ** met:
24*4882a593Smuzhiyun **   * Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun **     notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun **   * Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun **     notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun **     the documentation and/or other materials provided with the
29*4882a593Smuzhiyun **     distribution.
30*4882a593Smuzhiyun **   * Neither the name of The Qt Company Ltd nor the names of its
31*4882a593Smuzhiyun **     contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun **     from this software without specific prior written permission.
33*4882a593Smuzhiyun **
34*4882a593Smuzhiyun **
35*4882a593Smuzhiyun ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36*4882a593Smuzhiyun ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37*4882a593Smuzhiyun ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38*4882a593Smuzhiyun ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39*4882a593Smuzhiyun ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40*4882a593Smuzhiyun ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41*4882a593Smuzhiyun ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42*4882a593Smuzhiyun ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43*4882a593Smuzhiyun ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44*4882a593Smuzhiyun ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45*4882a593Smuzhiyun ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46*4882a593Smuzhiyun **
47*4882a593Smuzhiyun ** $QT_END_LICENSE$
48*4882a593Smuzhiyun **
49*4882a593Smuzhiyun ****************************************************************************/
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #include "bookwindow.h"
52*4882a593Smuzhiyun #include "bookdelegate.h"
53*4882a593Smuzhiyun #include "initdb.h"
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #include <QtSql>
56*4882a593Smuzhiyun 
BookWindow()57*4882a593Smuzhiyun BookWindow::BookWindow()
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun     ui.setupUi(this);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun     if (!QSqlDatabase::drivers().contains("QSQLITE"))
62*4882a593Smuzhiyun         QMessageBox::critical(
63*4882a593Smuzhiyun                     this,
64*4882a593Smuzhiyun                     "Unable to load database",
65*4882a593Smuzhiyun                     "This demo needs the SQLITE driver"
66*4882a593Smuzhiyun                     );
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun     // Initialize the database:
69*4882a593Smuzhiyun     QSqlError err = initDb();
70*4882a593Smuzhiyun     if (err.type() != QSqlError::NoError) {
71*4882a593Smuzhiyun         showError(err);
72*4882a593Smuzhiyun         return;
73*4882a593Smuzhiyun     }
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun     // Create the data model:
76*4882a593Smuzhiyun     model = new QSqlRelationalTableModel(ui.bookTable);
77*4882a593Smuzhiyun     model->setEditStrategy(QSqlTableModel::OnManualSubmit);
78*4882a593Smuzhiyun     model->setTable("books");
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun     // Remember the indexes of the columns:
81*4882a593Smuzhiyun     authorIdx = model->fieldIndex("author");
82*4882a593Smuzhiyun     genreIdx = model->fieldIndex("genre");
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun     // Set the relations to the other database tables:
85*4882a593Smuzhiyun     model->setRelation(authorIdx, QSqlRelation("authors", "id", "name"));
86*4882a593Smuzhiyun     model->setRelation(genreIdx, QSqlRelation("genres", "id", "name"));
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun     // Set the localized header captions:
89*4882a593Smuzhiyun     model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name"));
90*4882a593Smuzhiyun     model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre"));
91*4882a593Smuzhiyun     model->setHeaderData(model->fieldIndex("title"),
92*4882a593Smuzhiyun                          Qt::Horizontal, tr("Title"));
93*4882a593Smuzhiyun     model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year"));
94*4882a593Smuzhiyun     model->setHeaderData(model->fieldIndex("rating"),
95*4882a593Smuzhiyun                          Qt::Horizontal, tr("Rating"));
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun     // Populate the model:
98*4882a593Smuzhiyun     if (!model->select()) {
99*4882a593Smuzhiyun         showError(model->lastError());
100*4882a593Smuzhiyun         return;
101*4882a593Smuzhiyun     }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun     // Set the model and hide the ID column:
104*4882a593Smuzhiyun     ui.bookTable->setModel(model);
105*4882a593Smuzhiyun     ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable));
106*4882a593Smuzhiyun     ui.bookTable->setColumnHidden(model->fieldIndex("id"), true);
107*4882a593Smuzhiyun     ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun     // Initialize the Author combo box:
110*4882a593Smuzhiyun     ui.authorEdit->setModel(model->relationModel(authorIdx));
111*4882a593Smuzhiyun     ui.authorEdit->setModelColumn(
112*4882a593Smuzhiyun                 model->relationModel(authorIdx)->fieldIndex("name"));
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun     ui.genreEdit->setModel(model->relationModel(genreIdx));
115*4882a593Smuzhiyun     ui.genreEdit->setModelColumn(
116*4882a593Smuzhiyun                 model->relationModel(genreIdx)->fieldIndex("name"));
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun     // Lock and prohibit resizing of the width of the rating column:
119*4882a593Smuzhiyun     ui.bookTable->horizontalHeader()->setSectionResizeMode(
120*4882a593Smuzhiyun                 model->fieldIndex("rating"),
121*4882a593Smuzhiyun                 QHeaderView::ResizeToContents);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun     QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
124*4882a593Smuzhiyun     mapper->setModel(model);
125*4882a593Smuzhiyun     mapper->setItemDelegate(new BookDelegate(this));
126*4882a593Smuzhiyun     mapper->addMapping(ui.titleEdit, model->fieldIndex("title"));
127*4882a593Smuzhiyun     mapper->addMapping(ui.yearEdit, model->fieldIndex("year"));
128*4882a593Smuzhiyun     mapper->addMapping(ui.authorEdit, authorIdx);
129*4882a593Smuzhiyun     mapper->addMapping(ui.genreEdit, genreIdx);
130*4882a593Smuzhiyun     mapper->addMapping(ui.ratingEdit, model->fieldIndex("rating"));
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun     connect(ui.bookTable->selectionModel(),
133*4882a593Smuzhiyun             &QItemSelectionModel::currentRowChanged,
134*4882a593Smuzhiyun             mapper,
135*4882a593Smuzhiyun             &QDataWidgetMapper::setCurrentModelIndex
136*4882a593Smuzhiyun             );
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun     ui.bookTable->setCurrentIndex(model->index(0, 0));
139*4882a593Smuzhiyun   //  createMenuBar();
140*4882a593Smuzhiyun     this->setContentsMargins(0,0,0,0);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
showError(const QSqlError & err)143*4882a593Smuzhiyun void BookWindow::showError(const QSqlError &err)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun     QMessageBox::critical(this, "Unable to initialize Database",
146*4882a593Smuzhiyun                 "Error initializing database: " + err.text());
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
createMenuBar()149*4882a593Smuzhiyun void BookWindow::createMenuBar()
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun     QAction *quitAction = new QAction(tr("&Quit"), this);
152*4882a593Smuzhiyun     QAction *aboutAction = new QAction(tr("&About"), this);
153*4882a593Smuzhiyun     QAction *aboutQtAction = new QAction(tr("&About Qt"), this);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun     QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
156*4882a593Smuzhiyun     fileMenu->addAction(quitAction);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun     QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
159*4882a593Smuzhiyun     helpMenu->addAction(aboutAction);
160*4882a593Smuzhiyun     helpMenu->addAction(aboutQtAction);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun     connect(quitAction, &QAction::triggered, this, &BookWindow::close);
163*4882a593Smuzhiyun     connect(aboutAction, &QAction::triggered, this, &BookWindow::about);
164*4882a593Smuzhiyun     connect(aboutQtAction, &QAction::triggered, qApp, &QApplication::aboutQt);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
about()167*4882a593Smuzhiyun void BookWindow::about()
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun     QMessageBox::about(this, tr("About Books"),
170*4882a593Smuzhiyun             tr("<p>The <b>Books</b> example shows how to use Qt SQL classes "
171*4882a593Smuzhiyun                "with a model/view framework."));
172*4882a593Smuzhiyun }
173