-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddbook.cpp
97 lines (93 loc) · 3.1 KB
/
addbook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "addbook.h"
#include "ui_addbook.h"
#include "bookdelegate.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include <QByteArray>
#include <QBuffer>
#include <QRegExp>
#include <QRegExpValidator>
#include <QIntValidator>
AddBook::AddBook(QWidget *parent, QSqlDatabase database) :
QDialog(parent),
ui(new Ui::AddBook)
{
ui->setupUi(this);
// Thiết lập cơ sở dữ liệu
db = database;
// Thiết lập query
QRegExp isbnReg("(^[A-Z0-9]{10}$)|(^[A-Z0-9]{13}$)");
QRegExp titleReg(".+");
QRegExp authorReg("^([^0-9]+)$");
QRegExp yearReg("^[1-9][0-9]{1,3}$");
QRegExp pagesReg("^[1-9][0-9]+$");
ui->ISBN->setValidator(new QRegExpValidator(isbnReg));
ui->title->setValidator(new QRegExpValidator(titleReg));
ui->author->setValidator(new QRegExpValidator(authorReg));
ui->year->setValidator(new QRegExpValidator(yearReg));
ui->pages->setValidator(new QRegExpValidator(pagesReg));
}
AddBook::~AddBook()
{
delete ui;
}
void AddBook::on_submitButton_clicked()
{
QString errorMessage = "";
if (!ui->ISBN->hasAcceptableInput()) {
errorMessage += "Mã ISBN không hợp lệ\n";
}
if (!ui->title->hasAcceptableInput()) {
errorMessage += "Tiêu đề không hợp lệ\n";
}
if (!ui->author->hasAcceptableInput()) {
errorMessage += "Tác giả không hợp lệ\n";
}
if (!ui->year->hasAcceptableInput()) {
errorMessage += "Năm sảm xuất không hợp lệ\n";
}
if (!ui->pages->hasAcceptableInput()) {
errorMessage += "Số trang không hợp lệ";
}
if (!errorMessage.isEmpty()) {
QMessageBox::warning(this, "Không hợp lệ", errorMessage);
return;
}
QSqlQuery query(0, db);
query.prepare("INSERT INTO book(book_id, cover, title, author, pages, year, description, status) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
query.addBindValue(ui->ISBN->text());
QByteArray imageByteArray;
QBuffer inBuffer(&imageByteArray);
inBuffer.open(QIODevice::WriteOnly);
ui->cover->pixmap()->save(&inBuffer, "PNG");
query.addBindValue(imageByteArray);
query.addBindValue(ui->title->text());
query.addBindValue(ui->author->text());
query.addBindValue(ui->pages->text().toInt());
query.addBindValue(ui->year->text());
query.addBindValue(ui->description->toPlainText());
query.addBindValue(ui->status->isChecked());
if (query.exec()) {
QMessageBox::information(this, "Thành công", "Bạn đã thêm sách thành công!");
emit accepted();
} else {
QMessageBox::information(this, "Thất bại", "Đã có lỗi xảy ra!");
qDebug() << query.lastError();
}
}
void AddBook::on_cancelButton_clicked()
{
this->close();
}
void AddBook::on_changeCoverButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), QString(), tr("Image Files (*.png *.jpg *.bmp)"));
if (!fileName.isEmpty()) {
QPixmap pixmap(fileName);
pixmap = pixmap.scaled(128, 128);
ui->cover->setPixmap(pixmap);
}
}