diff options
Diffstat (limited to 'examples/sql/books/bookwindow.cpp')
-rw-r--r-- | examples/sql/books/bookwindow.cpp | 258 |
1 files changed, 156 insertions, 102 deletions
diff --git a/examples/sql/books/bookwindow.cpp b/examples/sql/books/bookwindow.cpp index 76f3c9da8f..6de2d5e80a 100644 --- a/examples/sql/books/bookwindow.cpp +++ b/examples/sql/books/bookwindow.cpp @@ -1,69 +1,31 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "bookwindow.h" #include "bookdelegate.h" #include "initdb.h" -#include <QtSql> +#include <QApplication> +#include <QComboBox> +#include <QDataWidgetMapper> +#include <QGridLayout> +#include <QHeaderView> +#include <QLabel> +#include <QLineEdit> +#include <QMenu> +#include <QMenuBar> +#include <QMessageBox> +#include <QPainter> +#include <QScrollBar> +#include <QSpinBox> +#include <QSqlDatabase> +#include <QTableView> BookWindow::BookWindow() { - ui.setupUi(this); - if (!QSqlDatabase::drivers().contains("QSQLITE")) - QMessageBox::critical( - this, - "Unable to load database", - "This demo needs the SQLITE driver" - ); + QMessageBox::critical(this, tr("Unable to load database"), + tr("This demo needs the SQLITE driver")); // Initialize the database: QSqlError err = initDb(); @@ -72,20 +34,87 @@ BookWindow::BookWindow() return; } - // Create the data model: - model = new QSqlRelationalTableModel(ui.bookTable); + // create the central widget for the window + window = new QWidget(this); + setCentralWidget(window); + + createLayout(); + + createModel(); + + // Populate the model + if (!model->select()) { + showError(model->lastError()); + return; + } + + configureWidgets(); + + // create the mappings between the UI elements and the SQL model + createMappings(); + + tableView->setCurrentIndex(model->index(0, 0)); + tableView->selectRow(0); + + createMenuBar(); +} + +void BookWindow::showError(const QSqlError &err) +{ + QMessageBox::critical(this, tr("Unable to initialize Database"), + tr("Error initializing database: %1").arg(err.text())); +} + +void BookWindow::createLayout() +{ + tableView = new QTableView(window); + + gridLayout = new QGridLayout(window); + + titleLabel = new QLabel(tr("Title:"), window); + titleLineEdit = new QLineEdit(window); + authorLabel = new QLabel(tr("Author:"), window); + authorComboBox = new QComboBox(window); + genreLabel = new QLabel(tr("Genre:"), window); + genreComboBox = new QComboBox(window); + yearLabel = new QLabel(tr("Year:"), window); + yearSpinBox = new QSpinBox(window); + ratingLabel = new QLabel(tr("Rating:"), window); + ratingComboBox = new QComboBox(window); + + gridLayout->addWidget(titleLabel, 0, 0, Qt::AlignRight); + gridLayout->addWidget(titleLineEdit, 0, 1, 1, 3); + gridLayout->addWidget(authorLabel, 1, 0, Qt::AlignRight); + gridLayout->addWidget(authorComboBox, 1, 1); + gridLayout->addWidget(yearLabel, 1, 2, Qt::AlignRight); + gridLayout->addWidget(yearSpinBox, 1, 3); + gridLayout->addWidget(genreLabel, 2, 0, Qt::AlignRight); + gridLayout->addWidget(genreComboBox, 2, 1); + gridLayout->addWidget(ratingLabel, 2, 2, Qt::AlignRight); + gridLayout->addWidget(ratingComboBox, 2, 3); + gridLayout->addWidget(tableView, 3, 0, 1, 4, Qt::AlignCenter); + gridLayout->setColumnStretch(1, 1000); + gridLayout->setColumnStretch(3, 1000); + + gridLayout->setContentsMargins(18, 18, 18, 18); + gridLayout->setSpacing(18); + gridLayout->setAlignment(Qt::AlignHCenter); +} + +void BookWindow::createModel() +{ + model = new QSqlRelationalTableModel(tableView); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->setTable("books"); - // Remember the indexes of the columns: authorIdx = model->fieldIndex("author"); genreIdx = model->fieldIndex("genre"); - // Set the relations to the other database tables: + // Set the relations to the other database tables model->setRelation(authorIdx, QSqlRelation("authors", "id", "name")); model->setRelation(genreIdx, QSqlRelation("genres", "id", "name")); - // Set the localized header captions: + // Set the localised header captions model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name")); model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre")); model->setHeaderData(model->fieldIndex("title"), @@ -93,56 +122,81 @@ BookWindow::BookWindow() model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year")); model->setHeaderData(model->fieldIndex("rating"), Qt::Horizontal, tr("Rating")); +} - // Populate the model: - if (!model->select()) { - showError(model->lastError()); - return; +void BookWindow::configureWidgets() +{ + tableView->setModel(model); + tableView->setItemDelegate(new BookDelegate(tableView)); + tableView->setColumnHidden(model->fieldIndex("id"), true); + tableView->verticalHeader()->setVisible(false); + tableView->setSelectionMode(QAbstractItemView::ExtendedSelection); + tableView->setSelectionBehavior(QAbstractItemView::SelectRows); + + // Lock and prohibit resizing of the width of the columns + tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + tableView->horizontalHeader()->setFixedHeight(tableView->rowHeight(0)); + + // increment by two to consider the frame + tableView->setFixedWidth(tableView->horizontalHeader()->length() + + tableView->verticalScrollBar()->sizeHint().width() + 2); + tableView->setMaximumHeight(tableView->verticalHeader()->length() + + tableView->horizontalHeader()->height() + 2); + + authorComboBox->setModel(model->relationModel(authorIdx)); + authorComboBox->setModelColumn(model->relationModel(authorIdx)->fieldIndex("name")); + + genreComboBox->setModel(model->relationModel(genreIdx)); + genreComboBox->setModelColumn(model->relationModel(genreIdx)->fieldIndex("name")); + + yearSpinBox->setMaximum(9999); + + const int width = 16; + const int height = width; + const int y = 2; + const int padding = 2; + + QSize iconSize = QSize(width * 5 + padding * 2, width + padding * 2); + QIcon starIcon(QStringLiteral(":images/star.svg")); + QIcon starFilledIcon(QStringLiteral(":images/star-filled.svg")); + + for (int row = 0; row < 6; ++row) { + QPixmap icon(iconSize); + icon.fill(Qt::transparent); + QPainter painter(&icon); + int x = 2; + + for (int col = 0; col < 5; ++col) { + if (col < row) { + starFilledIcon.paint(&painter, QRect(x, y, width, height)); + } else { + starIcon.paint(&painter, QRect(x, y, width, height)); + } + x += width; + } + ratingComboBox->addItem(icon, ""); + ratingComboBox->setItemData(row, QString::number(row + 1)); } - // Set the model and hide the ID column: - ui.bookTable->setModel(model); - ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable)); - ui.bookTable->setColumnHidden(model->fieldIndex("id"), true); - ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection); - - // Initialize the Author combo box: - ui.authorEdit->setModel(model->relationModel(authorIdx)); - ui.authorEdit->setModelColumn( - model->relationModel(authorIdx)->fieldIndex("name")); - - ui.genreEdit->setModel(model->relationModel(genreIdx)); - ui.genreEdit->setModelColumn( - model->relationModel(genreIdx)->fieldIndex("name")); - - // Lock and prohibit resizing of the width of the rating column: - ui.bookTable->horizontalHeader()->setSectionResizeMode( - model->fieldIndex("rating"), - QHeaderView::ResizeToContents); + ratingComboBox->setIconSize(iconSize); +} +void BookWindow::createMappings() +{ QDataWidgetMapper *mapper = new QDataWidgetMapper(this); mapper->setModel(model); mapper->setItemDelegate(new BookDelegate(this)); - mapper->addMapping(ui.titleEdit, model->fieldIndex("title")); - mapper->addMapping(ui.yearEdit, model->fieldIndex("year")); - mapper->addMapping(ui.authorEdit, authorIdx); - mapper->addMapping(ui.genreEdit, genreIdx); - mapper->addMapping(ui.ratingEdit, model->fieldIndex("rating")); - - connect(ui.bookTable->selectionModel(), + mapper->addMapping(titleLineEdit, model->fieldIndex("title")); + mapper->addMapping(yearSpinBox, model->fieldIndex("year")); + mapper->addMapping(authorComboBox, authorIdx); + mapper->addMapping(genreComboBox, genreIdx); + mapper->addMapping(ratingComboBox, model->fieldIndex("rating"), "currentIndex"); + connect(tableView->selectionModel(), &QItemSelectionModel::currentRowChanged, mapper, &QDataWidgetMapper::setCurrentModelIndex ); - - ui.bookTable->setCurrentIndex(model->index(0, 0)); - createMenuBar(); -} - -void BookWindow::showError(const QSqlError &err) -{ - QMessageBox::critical(this, "Unable to initialize Database", - "Error initializing database: " + err.text()); } void BookWindow::createMenuBar() @@ -158,7 +212,7 @@ void BookWindow::createMenuBar() helpMenu->addAction(aboutAction); helpMenu->addAction(aboutQtAction); - connect(quitAction, &QAction::triggered, this, &BookWindow::close); + connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); connect(aboutAction, &QAction::triggered, this, &BookWindow::about); connect(aboutQtAction, &QAction::triggered, qApp, &QApplication::aboutQt); } |