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