-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookinput.cpp
233 lines (200 loc) · 7.16 KB
/
bookinput.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "book.h"
#include "bookinput.h"
#include "booktablemodel.h"
#include "bookfactory.h"
#include <QDateEdit>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QMessageBox>
#include <QPushButton>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
BookInput::BookInput(BookTableModel *model, QWidget *parent)
: QDialog{parent},
bookTableModel(model),
mainLayout(new QGridLayout(this)),
gridLayoutBook(new QGridLayout()),
gridLayoutAuthor(new QGridLayout()),
groupBoxBook(new QGroupBox("Book", this)),
groupBoxAuthor(new QGroupBox("Author", this)),
lineEditTitle(new QLineEdit(this)),
lineEditIsbn(new QLineEdit(this)),
dateEditPublicationDate(new QDateEdit(this)),
lineEditFirstName(new QLineEdit(this)),
lineEditLastName(new QLineEdit(this)),
pushButtonAddAuthor(new QPushButton("Add", this)),
listWidgetAuthors(new QListWidget(this)),
pushButtonRemoveAuthor(new QPushButton(QIcon(":/icons/removeAuthor"), "", this)),
pushButtonSave(new QPushButton("Save", this)),
pushButtonCancel(new QPushButton("Cancel", this))
{
setupUI();
// Connect signals and slots
connect(pushButtonAddAuthor, &QPushButton::clicked, this, &BookInput::addAuthor);
connect(pushButtonRemoveAuthor, &QPushButton::clicked, this, &BookInput::removeAuthor);
connect(pushButtonSave, &QPushButton::clicked, this, &BookInput::saveBook);
connect(pushButtonCancel, &QPushButton::clicked, this, &BookInput::close);
}
BookInput::~BookInput()
{
}
void BookInput::addAuthor()
{
if (lineEditFirstName->text().isEmpty())
{
QMessageBox::warning(this, "Input Error", "Please enter a first name for the author.");
lineEditFirstName->setFocus();
return;
}
if (lineEditLastName->text().isEmpty())
{
QMessageBox::warning(this, "Input Error", "Please enter a last name for the author.");
lineEditLastName->setFocus();
return;
}
QString author = lineEditLastName->text().toUpper() + ", " + lineEditFirstName->text().toUpper();
listWidgetAuthors->addItem(author);
lineEditFirstName->clear();
lineEditLastName->clear();
lineEditFirstName->setFocus();
}
void BookInput::removeAuthor()
{
listWidgetAuthors->takeItem(listWidgetAuthors->currentRow());
}
void BookInput::saveBook()
{
if (!isValidInput())
{
return;
}
// Add book and author to model
QString title = lineEditTitle->text();
QStringList authors;
for (int i = 0; i < listWidgetAuthors->count(); i++)
{
QListWidgetItem *item = listWidgetAuthors->item(i);
if (item)
{
authors.append(item->text());
}
}
QString isbn = lineEditIsbn->text();
QDate publicationDate = dateEditPublicationDate->date();
// Create the new book using the book factory
BookFactory &bookFactory = BookFactory::getInstance();
Book *newBook = bookFactory.createBook(title, authors, isbn, publicationDate);
// Add the new book to the model
bookTableModel->addBook(newBook);
// Close the dialog
accept();
}
void BookInput::cancel()
{
close();
}
void BookInput::setupUI()
{
// Window Attrbiutes
setWindowTitle("New Book");
setWindowModality(Qt::ApplicationModal);
resize(410, 472);
setupBookGroup();
setupAuthorGroup();
setupButtons();
}
void BookInput::setupBookGroup()
{
// Title
QLabel *labelTitle = new QLabel("Title", this);
lineEditTitle->setToolTip("The book's title");
gridLayoutBook->addWidget(labelTitle, 0, 0);
gridLayoutBook->addWidget(lineEditTitle, 0, 1, 1, 3);
// ISBN
QLabel *labelIsbn = new QLabel("ISBN", this);
lineEditIsbn->setInputMask("999-9-99-999999-9;_");
lineEditIsbn->setPlaceholderText("978-3-16-148410-0");
gridLayoutBook->addWidget(labelIsbn, 1, 0);
gridLayoutBook->addWidget(lineEditIsbn, 1, 1, 1, 3);
// Publication Date
QLabel *labelPublicationDate = new QLabel("Date Published", this);
dateEditPublicationDate->setDate(QDate::currentDate());
dateEditPublicationDate->setCalendarPopup(true);
dateEditPublicationDate->setMaximumDate(QDate::currentDate());
gridLayoutBook->addWidget(labelPublicationDate, 2, 0);
gridLayoutBook->addWidget(dateEditPublicationDate, 2, 1);
groupBoxBook->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
groupBoxBook->setLayout(gridLayoutBook);
mainLayout->addWidget(groupBoxBook, 0, 0, 1, 4);
}
void BookInput::setupAuthorGroup()
{
// First name
QLabel *labelFirstName = new QLabel("First Name");
lineEditFirstName->setToolTip("Author's first name");
gridLayoutAuthor->addWidget(labelFirstName, 0, 0);
gridLayoutAuthor->addWidget(lineEditFirstName,0 , 1, 1, 3);
// Last name
QLabel *labelLastName = new QLabel("Last Name");
lineEditLastName->setToolTip("Author's last name");
gridLayoutAuthor->addWidget(labelLastName, 1, 0);
gridLayoutAuthor->addWidget(lineEditLastName, 1, 1, 1, 3);
// Add author button
gridLayoutAuthor->addWidget(pushButtonAddAuthor, 2, 0, 1, 4);
// List widget
gridLayoutAuthor->addWidget(listWidgetAuthors, 3, 0, 1, 4);
// Remove author button
pushButtonRemoveAuthor->setFixedSize(30, 30);
gridLayoutAuthor->addWidget(pushButtonRemoveAuthor, 3, 4, 1, 1);
// pushButtonRemoveAuthor->setIcon(QIcon(":/icons/removeAuthor"));
pushButtonRemoveAuthor->setIconSize(pushButtonRemoveAuthor->size());
gridLayoutAuthor->addWidget(pushButtonRemoveAuthor, 3, 4, Qt::AlignRight, 1);
// Group box and layout
groupBoxAuthor->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
groupBoxAuthor->setLayout(gridLayoutAuthor);
mainLayout->addWidget(groupBoxAuthor, 1, 0, 1, 4);
}
void BookInput::setupButtons()
{
pushButtonSave->setToolTip("Save the book to the list");
mainLayout->addWidget(pushButtonSave, 3, 0, 1, 4);
mainLayout->addWidget(pushButtonCancel, 4, 0, 1, 4);
}
bool BookInput::isValidInput()
{
// Check if title is empty
if (lineEditTitle->text().isEmpty())
{
QMessageBox::warning(this, "Input Error", "Please enter a title for the book.");
lineEditTitle->setFocus();
return false;
}
// // Check if ISBN is empty or invalid
// static QRegularExpression isbnRegex("\\d{3}-\\d{1,5}-\\d{1,7}-\\d{1,7}-\\d{1}");
// QRegularExpressionMatch isbnMatch = isbnRegex.match(lineEditIsbn->text());
// if (lineEditIsbn->text().isEmpty() || !isbnMatch.hasMatch())
// {
// QMessageBox::warning(this, "Input Error", "Please enter a valid ISBN (format: 978-3-16-148410-0).");
// lineEditIsbn->setFocus();
// return false;
// }
// Check if ISBN is empty
if (lineEditIsbn->text().isEmpty())
{
QMessageBox::warning(this, "Input Error", "Please enter an ISBN.");
lineEditIsbn->setFocus();
return false;
}
// Check if at least one author is added
if (listWidgetAuthors->count() == 0)
{
QMessageBox::warning(this, "Input Error", "Please add at least one author.");
lineEditFirstName->setFocus();
return false;
}
// If all checks pass
return true;
}