SQL examples: code cleanup
Misc code cleanup for the sql examples: - don't include global Qt headers but only needed ones - use proper tr() where possible - pass parameters by const ref - style fixes Change-Id: I4fd4293948918b9d7b373b6d1e8eeecf6f25a622 Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
This commit is contained in:
parent
27dd178900
commit
8bfebaa22f
@ -3,13 +3,12 @@
|
|||||||
|
|
||||||
#include "bookdelegate.h"
|
#include "bookdelegate.h"
|
||||||
|
|
||||||
#include <QtWidgets>
|
#include <QMouseEvent>
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QSpinBox>
|
||||||
|
|
||||||
BookDelegate::BookDelegate(QObject *parent)
|
void BookDelegate::paint(QPainter *painter,
|
||||||
: QSqlRelationalDelegate(parent)
|
const QStyleOptionViewItem &option,
|
||||||
{}
|
|
||||||
|
|
||||||
void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|
||||||
const QModelIndex &index) const
|
const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
if (index.column() != 5) {
|
if (index.column() != 5) {
|
||||||
@ -38,11 +37,10 @@ void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|||||||
QIcon starFilledIcon(QStringLiteral(":images/star-filled.svg"));
|
QIcon starFilledIcon(QStringLiteral(":images/star-filled.svg"));
|
||||||
|
|
||||||
for (int i = 0; i < 5; ++i) {
|
for (int i = 0; i < 5; ++i) {
|
||||||
if (i < rating) {
|
if (i < rating)
|
||||||
starFilledIcon.paint(painter, QRect(x, y, width, height));
|
starFilledIcon.paint(painter, QRect(x, y, width, height));
|
||||||
} else {
|
else
|
||||||
starIcon.paint(painter, QRect(x, y, width, height));
|
starIcon.paint(painter, QRect(x, y, width, height));
|
||||||
}
|
|
||||||
x += width;
|
x += width;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,7 +64,7 @@ bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
|
|||||||
|
|
||||||
if (event->type() == QEvent::MouseButtonPress) {
|
if (event->type() == QEvent::MouseButtonPress) {
|
||||||
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
|
||||||
int stars = qBound(0, int(0.7 + qreal(mouseEvent->position().toPoint().x()
|
int stars = qBound(0, int(0.7 + qreal(mouseEvent->position().x()
|
||||||
- option.rect.x()) / iconDimension), 5);
|
- option.rect.x()) / iconDimension), 5);
|
||||||
model->setData(index, QVariant(stars));
|
model->setData(index, QVariant(stars));
|
||||||
// So that the selection can change:
|
// So that the selection can change:
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
#define BOOKDELEGATE_H
|
#define BOOKDELEGATE_H
|
||||||
|
|
||||||
#include <QModelIndex>
|
#include <QModelIndex>
|
||||||
#include <QPixmap>
|
|
||||||
#include <QSize>
|
#include <QSize>
|
||||||
#include <QSqlRelationalDelegate>
|
#include <QSqlRelationalDelegate>
|
||||||
|
|
||||||
@ -14,7 +13,7 @@ QT_FORWARD_DECLARE_CLASS(QPainter)
|
|||||||
class BookDelegate : public QSqlRelationalDelegate
|
class BookDelegate : public QSqlRelationalDelegate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BookDelegate(QObject *parent);
|
using QSqlRelationalDelegate::QSqlRelationalDelegate;
|
||||||
|
|
||||||
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||||
const QModelIndex &index) const override;
|
const QModelIndex &index) const override;
|
||||||
|
@ -5,16 +5,27 @@
|
|||||||
#include "bookdelegate.h"
|
#include "bookdelegate.h"
|
||||||
#include "initdb.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()
|
BookWindow::BookWindow()
|
||||||
{
|
{
|
||||||
if (!QSqlDatabase::drivers().contains("QSQLITE"))
|
if (!QSqlDatabase::drivers().contains("QSQLITE"))
|
||||||
QMessageBox::critical(
|
QMessageBox::critical(this, tr("Unable to load database"),
|
||||||
this,
|
tr("This demo needs the SQLITE driver"));
|
||||||
"Unable to load database",
|
|
||||||
"This demo needs the SQLITE driver"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Initialize the database:
|
// Initialize the database:
|
||||||
QSqlError err = initDb();
|
QSqlError err = initDb();
|
||||||
@ -50,8 +61,8 @@ BookWindow::BookWindow()
|
|||||||
|
|
||||||
void BookWindow::showError(const QSqlError &err)
|
void BookWindow::showError(const QSqlError &err)
|
||||||
{
|
{
|
||||||
QMessageBox::critical(this, "Unable to initialize Database",
|
QMessageBox::critical(this, tr("Unable to initialize Database"),
|
||||||
"Error initializing database: " + err.text());
|
tr("Error initializing database: %1").arg(err.text()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BookWindow::createLayout()
|
void BookWindow::createLayout()
|
||||||
|
@ -4,8 +4,15 @@
|
|||||||
#ifndef BOOKWINDOW_H
|
#ifndef BOOKWINDOW_H
|
||||||
#define BOOKWINDOW_H
|
#define BOOKWINDOW_H
|
||||||
|
|
||||||
#include <QtWidgets>
|
#include <QMainWindow>
|
||||||
#include <QtSql>
|
QT_FORWARD_DECLARE_CLASS(QComboBox)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QGridLayout)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QLabel)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QLineEdit)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QSpinBox)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QSqlError)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QTableView)
|
||||||
|
|
||||||
class BookWindow: public QMainWindow
|
class BookWindow: public QMainWindow
|
||||||
{
|
{
|
||||||
|
@ -4,7 +4,9 @@
|
|||||||
#ifndef INITDB_H
|
#ifndef INITDB_H
|
||||||
#define INITDB_H
|
#define INITDB_H
|
||||||
|
|
||||||
#include <QtSql>
|
#include <QDate>
|
||||||
|
#include <QSqlError>
|
||||||
|
#include <QSqlQuery>
|
||||||
|
|
||||||
void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
|
void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
|
||||||
const QVariant &genreId, int rating)
|
const QVariant &genreId, int rating)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "bookwindow.h"
|
#include "bookwindow.h"
|
||||||
|
|
||||||
#include <QtWidgets>
|
#include <QApplication>
|
||||||
|
|
||||||
int main(int argc, char * argv[])
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
// Copyright (C) 2016 The Qt Company Ltd.
|
// Copyright (C) 2016 The Qt Company Ltd.
|
||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||||
|
|
||||||
#include <QtWidgets>
|
|
||||||
#include <QtSql>
|
|
||||||
|
|
||||||
#include "tableeditor.h"
|
#include "tableeditor.h"
|
||||||
|
|
||||||
|
#include <QDialogButtonBox>
|
||||||
|
#include <QHBoxLayout>
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QPushButton>
|
||||||
|
#include <QSqlError>
|
||||||
|
#include <QSqlTableModel>
|
||||||
|
#include <QTableView>
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
TableEditor::TableEditor(const QString &tableName, QWidget *parent)
|
TableEditor::TableEditor(const QString &tableName, QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
|
@ -6,11 +6,9 @@
|
|||||||
|
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_FORWARD_DECLARE_CLASS(QDialogButtonBox)
|
||||||
class QDialogButtonBox;
|
QT_FORWARD_DECLARE_CLASS(QPushButton)
|
||||||
class QPushButton;
|
QT_FORWARD_DECLARE_CLASS(QSqlTableModel)
|
||||||
class QSqlTableModel;
|
|
||||||
QT_END_NAMESPACE
|
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
class TableEditor : public QWidget
|
class TableEditor : public QWidget
|
||||||
|
@ -3,10 +3,23 @@
|
|||||||
|
|
||||||
#include "dialog.h"
|
#include "dialog.h"
|
||||||
|
|
||||||
int uniqueAlbumId;
|
#include <QDialogButtonBox>
|
||||||
int uniqueArtistId;
|
#include <QDate>
|
||||||
|
#include <QDomDocument>
|
||||||
|
#include <QGroupBox>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QPushButton>
|
||||||
|
#include <QSpinBox>
|
||||||
|
#include <QSqlField>
|
||||||
|
#include <QSqlRelationalTableModel>
|
||||||
|
#include <QSqlRecord>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
|
||||||
Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
|
int Dialog::s_artistId = 0;
|
||||||
|
int Dialog::s_albumId = 0;
|
||||||
|
Dialog::Dialog(QSqlRelationalTableModel *albums, const QDomDocument &details,
|
||||||
QFile *output, QWidget *parent)
|
QFile *output, QWidget *parent)
|
||||||
: QDialog(parent)
|
: QDialog(parent)
|
||||||
{
|
{
|
||||||
@ -25,6 +38,12 @@ Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
|
|||||||
setWindowTitle(tr("Add Album"));
|
setWindowTitle(tr("Add Album"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Dialog::setInitialAlbumAndArtistId(int albumId, int artistId)
|
||||||
|
{
|
||||||
|
s_albumId = albumId;
|
||||||
|
s_artistId = artistId;
|
||||||
|
}
|
||||||
|
|
||||||
void Dialog::submit()
|
void Dialog::submit()
|
||||||
{
|
{
|
||||||
QString artist = artistEditor->text();
|
QString artist = artistEditor->text();
|
||||||
@ -145,7 +164,7 @@ void Dialog::addTracks(int albumId, const QStringList &tracks)
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dialog::increaseAlbumCount(QModelIndex artistIndex)
|
void Dialog::increaseAlbumCount(const QModelIndex &artistIndex)
|
||||||
{
|
{
|
||||||
QSqlTableModel *artistModel = model->relationModel(2);
|
QSqlTableModel *artistModel = model->relationModel(2);
|
||||||
|
|
||||||
@ -219,7 +238,7 @@ QDialogButtonBox *Dialog::createButtons()
|
|||||||
return buttonBox;
|
return buttonBox;
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex Dialog::indexOfArtist(const QString &artist)
|
QModelIndex Dialog::indexOfArtist(const QString &artist) const
|
||||||
{
|
{
|
||||||
QSqlTableModel *artistModel = model->relationModel(2);
|
QSqlTableModel *artistModel = model->relationModel(2);
|
||||||
|
|
||||||
@ -234,12 +253,12 @@ QModelIndex Dialog::indexOfArtist(const QString &artist)
|
|||||||
|
|
||||||
int Dialog::generateArtistId()
|
int Dialog::generateArtistId()
|
||||||
{
|
{
|
||||||
uniqueArtistId += 1;
|
s_artistId += 1;
|
||||||
return uniqueArtistId;
|
return s_artistId;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Dialog::generateAlbumId()
|
int Dialog::generateAlbumId()
|
||||||
{
|
{
|
||||||
uniqueAlbumId += 1;
|
s_albumId += 1;
|
||||||
return uniqueAlbumId;
|
return s_albumId;
|
||||||
}
|
}
|
||||||
|
@ -4,17 +4,25 @@
|
|||||||
#ifndef DIALOG_H
|
#ifndef DIALOG_H
|
||||||
#define DIALOG_H
|
#define DIALOG_H
|
||||||
|
|
||||||
#include <QtWidgets>
|
#include <QDialog>
|
||||||
#include <QtSql>
|
#include <QDomDocument>
|
||||||
#include <QtXml>
|
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QDialogButtonBox)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QFile)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QGroupBox)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QLineEdit)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QModelIndex)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QSpinBox)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel)
|
||||||
|
|
||||||
class Dialog : public QDialog
|
class Dialog : public QDialog
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
|
Dialog(QSqlRelationalTableModel *albums, const QDomDocument &details,
|
||||||
QFile *output, QWidget *parent = nullptr);
|
QFile *output, QWidget *parent = nullptr);
|
||||||
|
static void setInitialAlbumAndArtistId(int albumId, int artistId);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void revert();
|
void revert();
|
||||||
@ -27,10 +35,8 @@ private:
|
|||||||
QDialogButtonBox *createButtons();
|
QDialogButtonBox *createButtons();
|
||||||
QGroupBox *createInputWidgets();
|
QGroupBox *createInputWidgets();
|
||||||
int findArtistId(const QString &artist);
|
int findArtistId(const QString &artist);
|
||||||
static int generateAlbumId();
|
void increaseAlbumCount(const QModelIndex &artistIndex);
|
||||||
static int generateArtistId();
|
QModelIndex indexOfArtist(const QString &artist) const;
|
||||||
void increaseAlbumCount(QModelIndex artistIndex);
|
|
||||||
QModelIndex indexOfArtist(const QString &artist);
|
|
||||||
|
|
||||||
QSqlRelationalTableModel *model;
|
QSqlRelationalTableModel *model;
|
||||||
QDomDocument albumDetails;
|
QDomDocument albumDetails;
|
||||||
@ -40,6 +46,11 @@ private:
|
|||||||
QLineEdit *titleEditor;
|
QLineEdit *titleEditor;
|
||||||
QSpinBox *yearEditor;
|
QSpinBox *yearEditor;
|
||||||
QLineEdit *tracksEditor;
|
QLineEdit *tracksEditor;
|
||||||
|
|
||||||
|
static int generateAlbumId();
|
||||||
|
static int generateArtistId();
|
||||||
|
static int s_artistId;
|
||||||
|
static int s_albumId;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
@ -4,12 +4,20 @@
|
|||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "dialog.h"
|
#include "dialog.h"
|
||||||
|
|
||||||
#include <QtWidgets>
|
#include <QApplication>
|
||||||
#include <QtSql>
|
#include <QComboBox>
|
||||||
#include <QtXml>
|
#include <QHeaderView>
|
||||||
|
#include <QFile>
|
||||||
extern int uniqueAlbumId;
|
#include <QGridLayout>
|
||||||
extern int uniqueArtistId;
|
#include <QGroupBox>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QListWidget>
|
||||||
|
#include <QMenu>
|
||||||
|
#include <QMenuBar>
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QSqlRecord>
|
||||||
|
#include <QSqlRelationalTableModel>
|
||||||
|
#include <QTableView>
|
||||||
|
|
||||||
MainWindow::MainWindow(const QString &artistTable, const QString &albumTable,
|
MainWindow::MainWindow(const QString &artistTable, const QString &albumTable,
|
||||||
QFile *albumDetails, QWidget *parent)
|
QFile *albumDetails, QWidget *parent)
|
||||||
@ -28,13 +36,12 @@ MainWindow::MainWindow(const QString &artistTable, const QString &albumTable,
|
|||||||
QGroupBox *details = createDetailsGroupBox();
|
QGroupBox *details = createDetailsGroupBox();
|
||||||
|
|
||||||
artistView->setCurrentIndex(0);
|
artistView->setCurrentIndex(0);
|
||||||
uniqueAlbumId = model->rowCount();
|
Dialog::setInitialAlbumAndArtistId(model->rowCount(), artistView->count());
|
||||||
uniqueArtistId = artistView->count();
|
|
||||||
|
|
||||||
connect(model, &QSqlRelationalTableModel::rowsInserted,
|
connect(model, &QSqlRelationalTableModel::rowsInserted,
|
||||||
this, &MainWindow::updateHeader);
|
this, &MainWindow::adjustHeader);
|
||||||
connect(model, &QSqlRelationalTableModel::rowsRemoved,
|
connect(model, &QSqlRelationalTableModel::rowsRemoved,
|
||||||
this, &MainWindow::updateHeader);
|
this, &MainWindow::adjustHeader);
|
||||||
|
|
||||||
QGridLayout *layout = new QGridLayout;
|
QGridLayout *layout = new QGridLayout;
|
||||||
layout->addWidget(artists, 0, 0);
|
layout->addWidget(artists, 0, 0);
|
||||||
@ -67,7 +74,7 @@ void MainWindow::changeArtist(int row)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::showArtistProfile(QModelIndex index)
|
void MainWindow::showArtistProfile(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
QSqlRecord record = model->relationModel(2)->record(index.row());
|
QSqlRecord record = model->relationModel(2)->record(index.row());
|
||||||
|
|
||||||
@ -84,7 +91,7 @@ void MainWindow::showArtistProfile(QModelIndex index)
|
|||||||
imageLabel->hide();
|
imageLabel->hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::showAlbumDetails(QModelIndex index)
|
void MainWindow::showAlbumDetails(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
QSqlRecord record = model->record(index.row());
|
QSqlRecord record = model->record(index.row());
|
||||||
|
|
||||||
@ -113,14 +120,11 @@ void MainWindow::getTrackList(QDomNode album)
|
|||||||
{
|
{
|
||||||
trackList->clear();
|
trackList->clear();
|
||||||
|
|
||||||
QDomNodeList tracks = album.childNodes();
|
const QDomNodeList tracks = album.childNodes();
|
||||||
QDomNode track;
|
|
||||||
QString trackNumber;
|
|
||||||
|
|
||||||
for (int i = 0; i < tracks.count(); ++i) {
|
for (int i = 0; i < tracks.count(); ++i) {
|
||||||
|
|
||||||
track = tracks.item(i);
|
const QDomNode track = tracks.item(i);
|
||||||
trackNumber = track.toElement().attribute("number");
|
const QString trackNumber = track.toElement().attribute("number");
|
||||||
|
|
||||||
QListWidgetItem *item = new QListWidgetItem(trackList);
|
QListWidgetItem *item = new QListWidgetItem(trackList);
|
||||||
item->setText(trackNumber + ": " + track.toElement().text());
|
item->setText(trackNumber + ": " + track.toElement().text());
|
||||||
@ -132,7 +136,7 @@ void MainWindow::addAlbum()
|
|||||||
Dialog *dialog = new Dialog(model, albumData, file, this);
|
Dialog *dialog = new Dialog(model, albumData, file, this);
|
||||||
int accepted = dialog->exec();
|
int accepted = dialog->exec();
|
||||||
|
|
||||||
if (accepted == 1) {
|
if (accepted == QDialog::Accepted) {
|
||||||
int lastRow = model->rowCount() - 1;
|
int lastRow = model->rowCount() - 1;
|
||||||
albumView->selectRow(lastRow);
|
albumView->selectRow(lastRow);
|
||||||
albumView->scrollToBottom();
|
albumView->scrollToBottom();
|
||||||
@ -142,10 +146,10 @@ void MainWindow::addAlbum()
|
|||||||
|
|
||||||
void MainWindow::deleteAlbum()
|
void MainWindow::deleteAlbum()
|
||||||
{
|
{
|
||||||
QModelIndexList selection = albumView->selectionModel()->selectedRows(0);
|
const QModelIndexList selection = albumView->selectionModel()->selectedRows(0);
|
||||||
|
|
||||||
if (!selection.empty()) {
|
if (!selection.empty()) {
|
||||||
QModelIndex idIndex = selection.at(0);
|
const QModelIndex &idIndex = selection.at(0);
|
||||||
int id = idIndex.data().toInt();
|
int id = idIndex.data().toInt();
|
||||||
QString title = idIndex.sibling(idIndex.row(), 1).data().toString();
|
QString title = idIndex.sibling(idIndex.row(), 1).data().toString();
|
||||||
QString artist = idIndex.sibling(idIndex.row(), 2).data().toString();
|
QString artist = idIndex.sibling(idIndex.row(), 2).data().toString();
|
||||||
@ -172,9 +176,7 @@ void MainWindow::deleteAlbum()
|
|||||||
|
|
||||||
void MainWindow::removeAlbumFromFile(int id)
|
void MainWindow::removeAlbumFromFile(int id)
|
||||||
{
|
{
|
||||||
|
|
||||||
QDomNodeList albums = albumData.elementsByTagName("album");
|
QDomNodeList albums = albumData.elementsByTagName("album");
|
||||||
|
|
||||||
for (int i = 0; i < albums.count(); ++i) {
|
for (int i = 0; i < albums.count(); ++i) {
|
||||||
QDomNode node = albums.item(i);
|
QDomNode node = albums.item(i);
|
||||||
if (node.toElement().attribute("id").toInt() == id) {
|
if (node.toElement().attribute("id").toInt() == id) {
|
||||||
@ -197,12 +199,12 @@ void MainWindow::removeAlbumFromFile(int id)
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::removeAlbumFromDatabase(QModelIndex index)
|
void MainWindow::removeAlbumFromDatabase(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
model->removeRow(index.row());
|
model->removeRow(index.row());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::decreaseAlbumCount(QModelIndex artistIndex)
|
void MainWindow::decreaseAlbumCount(const QModelIndex &artistIndex)
|
||||||
{
|
{
|
||||||
int row = artistIndex.row();
|
int row = artistIndex.row();
|
||||||
QModelIndex albumCountIndex = artistIndex.sibling(row, 2);
|
QModelIndex albumCountIndex = artistIndex.sibling(row, 2);
|
||||||
@ -358,7 +360,7 @@ void MainWindow::showImageLabel()
|
|||||||
imageLabel->show();
|
imageLabel->show();
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex MainWindow::indexOfArtist(const QString &artist)
|
QModelIndex MainWindow::indexOfArtist(const QString &artist) const
|
||||||
{
|
{
|
||||||
QSqlTableModel *artistModel = model->relationModel(2);
|
QSqlTableModel *artistModel = model->relationModel(2);
|
||||||
|
|
||||||
@ -370,11 +372,6 @@ QModelIndex MainWindow::indexOfArtist(const QString &artist)
|
|||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::updateHeader(QModelIndex, int, int)
|
|
||||||
{
|
|
||||||
adjustHeader();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::adjustHeader()
|
void MainWindow::adjustHeader()
|
||||||
{
|
{
|
||||||
albumView->hideColumn(0);
|
albumView->hideColumn(0);
|
||||||
|
@ -8,15 +8,13 @@
|
|||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
#include <QModelIndex>
|
#include <QModelIndex>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_FORWARD_DECLARE_CLASS(QComboBox)
|
||||||
class QComboBox;
|
QT_FORWARD_DECLARE_CLASS(QFile)
|
||||||
class QFile;
|
QT_FORWARD_DECLARE_CLASS(QGroupBox)
|
||||||
class QGroupBox;
|
QT_FORWARD_DECLARE_CLASS(QLabel)
|
||||||
class QLabel;
|
QT_FORWARD_DECLARE_CLASS(QListWidget)
|
||||||
class QListWidget;
|
QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel)
|
||||||
class QSqlRelationalTableModel;
|
QT_FORWARD_DECLARE_CLASS(QTableView)
|
||||||
class QTableView;
|
|
||||||
QT_END_NAMESPACE
|
|
||||||
|
|
||||||
class MainWindow : public QMainWindow
|
class MainWindow : public QMainWindow
|
||||||
{
|
{
|
||||||
@ -31,21 +29,20 @@ private slots:
|
|||||||
void addAlbum();
|
void addAlbum();
|
||||||
void changeArtist(int row);
|
void changeArtist(int row);
|
||||||
void deleteAlbum();
|
void deleteAlbum();
|
||||||
void showAlbumDetails(QModelIndex index);
|
void showAlbumDetails(const QModelIndex &index);
|
||||||
void showArtistProfile(QModelIndex index);
|
void showArtistProfile(const QModelIndex &index);
|
||||||
void updateHeader(QModelIndex, int, int);
|
void adjustHeader();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void adjustHeader();
|
|
||||||
QGroupBox *createAlbumGroupBox();
|
QGroupBox *createAlbumGroupBox();
|
||||||
QGroupBox *createArtistGroupBox();
|
QGroupBox *createArtistGroupBox();
|
||||||
QGroupBox *createDetailsGroupBox();
|
QGroupBox *createDetailsGroupBox();
|
||||||
void createMenuBar();
|
void createMenuBar();
|
||||||
void decreaseAlbumCount(QModelIndex artistIndex);
|
void decreaseAlbumCount(const QModelIndex &artistIndex);
|
||||||
void getTrackList(QDomNode album);
|
void getTrackList(QDomNode album);
|
||||||
QModelIndex indexOfArtist(const QString &artist);
|
QModelIndex indexOfArtist(const QString &artist) const;
|
||||||
void readAlbumData();
|
void readAlbumData();
|
||||||
void removeAlbumFromDatabase(QModelIndex album);
|
void removeAlbumFromDatabase(const QModelIndex &album);
|
||||||
void removeAlbumFromFile(int id);
|
void removeAlbumFromFile(int id);
|
||||||
void showImageLabel();
|
void showImageLabel();
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
// Copyright (C) 2016 The Qt Company Ltd.
|
// Copyright (C) 2016 The Qt Company Ltd.
|
||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||||
|
|
||||||
#include <QtWidgets>
|
|
||||||
|
|
||||||
#include "customsqlmodel.h"
|
#include "customsqlmodel.h"
|
||||||
|
|
||||||
|
#include <QColor>
|
||||||
|
|
||||||
CustomSqlModel::CustomSqlModel(QObject *parent)
|
CustomSqlModel::CustomSqlModel(QObject *parent)
|
||||||
: QSqlQueryModel(parent)
|
: QSqlQueryModel(parent)
|
||||||
{
|
{
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
// Copyright (C) 2016 The Qt Company Ltd.
|
// Copyright (C) 2016 The Qt Company Ltd.
|
||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||||
|
|
||||||
#include <QtSql>
|
|
||||||
|
|
||||||
#include "editablesqlmodel.h"
|
#include "editablesqlmodel.h"
|
||||||
|
|
||||||
|
#include <QSqlQuery>
|
||||||
|
|
||||||
EditableSqlModel::EditableSqlModel(QObject *parent)
|
EditableSqlModel::EditableSqlModel(QObject *parent)
|
||||||
: QSqlQueryModel(parent)
|
: QSqlQueryModel(parent)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
Qt::ItemFlags EditableSqlModel::flags(
|
Qt::ItemFlags EditableSqlModel::flags(const QModelIndex &index) const
|
||||||
const QModelIndex &index) const
|
|
||||||
{
|
{
|
||||||
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
|
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
|
||||||
if (index.column() == 1 || index.column() == 2)
|
if (index.column() == 1 || index.column() == 2)
|
||||||
@ -33,11 +32,10 @@ bool EditableSqlModel::setData(const QModelIndex &index, const QVariant &value,
|
|||||||
clear();
|
clear();
|
||||||
|
|
||||||
bool ok;
|
bool ok;
|
||||||
if (index.column() == 1) {
|
if (index.column() == 1)
|
||||||
ok = setFirstName(id, value.toString());
|
ok = setFirstName(id, value.toString());
|
||||||
} else {
|
else
|
||||||
ok = setLastName(id, value.toString());
|
ok = setLastName(id, value.toString());
|
||||||
}
|
|
||||||
refresh();
|
refresh();
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,6 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QTableView>
|
#include <QTableView>
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void initializeModel(QSqlQueryModel *model)
|
void initializeModel(QSqlQueryModel *model)
|
||||||
{
|
{
|
||||||
model->setQuery("select * from person");
|
model->setQuery("select * from person");
|
||||||
|
@ -1,11 +1,20 @@
|
|||||||
// Copyright (C) 2016 The Qt Company Ltd.
|
// Copyright (C) 2016 The Qt Company Ltd.
|
||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||||
|
|
||||||
#include <QtWidgets>
|
|
||||||
#include <QtSql>
|
|
||||||
|
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
|
||||||
|
#include <QComboBox>
|
||||||
|
#include <QDataWidgetMapper>
|
||||||
|
#include <QGridLayout>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QPushButton>
|
||||||
|
#include <QSqlQuery>
|
||||||
|
#include <QSqlRelationalDelegate>
|
||||||
|
#include <QSqlTableModel>
|
||||||
|
#include <QTextEdit>
|
||||||
|
|
||||||
//! [Set up widgets]
|
//! [Set up widgets]
|
||||||
Window::Window(QWidget *parent)
|
Window::Window(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
|
@ -6,18 +6,16 @@
|
|||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_FORWARD_DECLARE_CLASS(QComboBox)
|
||||||
class QComboBox;
|
QT_FORWARD_DECLARE_CLASS(QDataWidgetMapper)
|
||||||
class QDataWidgetMapper;
|
QT_FORWARD_DECLARE_CLASS(QItemSelectionModel)
|
||||||
class QItemSelectionModel;
|
QT_FORWARD_DECLARE_CLASS(QLabel)
|
||||||
class QLabel;
|
QT_FORWARD_DECLARE_CLASS(QLineEdit)
|
||||||
class QLineEdit;
|
QT_FORWARD_DECLARE_CLASS(QPushButton)
|
||||||
class QPushButton;
|
QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel)
|
||||||
class QSqlRelationalTableModel;
|
QT_FORWARD_DECLARE_CLASS(QStandardItemModel)
|
||||||
class QStandardItemModel;
|
QT_FORWARD_DECLARE_CLASS(QStringListModel)
|
||||||
class QStringListModel;
|
QT_FORWARD_DECLARE_CLASS(QTextEdit)
|
||||||
class QTextEdit;
|
|
||||||
QT_END_NAMESPACE
|
|
||||||
|
|
||||||
//! [Window definition]
|
//! [Window definition]
|
||||||
class Window : public QWidget
|
class Window : public QWidget
|
||||||
|
@ -6,8 +6,6 @@
|
|||||||
#include <QSqlTableModel>
|
#include <QSqlTableModel>
|
||||||
#include <QTableView>
|
#include <QTableView>
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void initializeModel(QSqlTableModel *model)
|
void initializeModel(QSqlTableModel *model)
|
||||||
{
|
{
|
||||||
model->setTable("person");
|
model->setTable("person");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user