-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooklist_admin.cpp
180 lines (163 loc) · 5.36 KB
/
booklist_admin.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
#include "booklist_admin.h"
#include "ui_booklist_admin.h"
BookList_Admin::BookList_Admin(Ui::MainWindow *ui_dash, QWidget *dash, QWidget *parent) :
QWidget(parent),
ui(new Ui::BookList_Admin)
{
this->admin_dash = dash;
this->ui_admindash = ui_dash;
this->change = false;
ui->setupUi(this);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->loadData();
}
void BookList_Admin::mousePressEvent(QMouseEvent *event)
{
oldPos = event->globalPosition();
}
void BookList_Admin::mouseMoveEvent(QMouseEvent *event)
{
const QPointF delta = event->globalPosition() - oldPos;
move(x()+delta.x(), y()+delta.y());
oldPos = event->globalPosition();
}
int BookList_Admin::loadData()
{
ui->lineEdit_search->clear();
ui->tableWidget->setRowCount(0);
int j = 0;
this->booksdb = Book::loadBooks();
// Set Completer
{
for (int i = 0; i < completer.size(); i++)
delete completer.at(i);
completer.clear();
this->completer.append(new QCompleter(booksdb.keys(), this));
for (int i = 0; i < 6; i++)
{
QStringList tmp;
for (auto it = booksdb.constBegin(); it != booksdb.constEnd(); ++it)
tmp << it.value().at(i);
this->completer.append(new QCompleter(tmp, this));
tmp.clear();
}
for (int i = 0; i < 7; ++i)
completer.at(i)->setCaseSensitivity(Qt::CaseInsensitive);
}
// Add to table
{
for (auto i = booksdb.constBegin(); i != booksdb.constEnd(); ++i)
{
ui->tableWidget->insertRow(ui->tableWidget->rowCount());
ui->tableWidget->setItem(j, 0, new QTableWidgetItem(i.key()));
for (int k = 0; k < i.value().size(); ++k)
ui->tableWidget->setItem(j, k + 1, new QTableWidgetItem(i.value().at(k)));
++j;
}
}
ui->label->setText("Books List | " + QString::number(j) + " Records Loaded");
return j;
}
BookList_Admin::~BookList_Admin()
{
delete ui;
}
void BookList_Admin::on_pushButton_backtodash_clicked()
{
this->hide();
if (change)
{
this->ui_admindash->pushButton_totalb->setText("Total Books: " + QString::number(this->booksdb.size()));
this->change = false;
}
admin_dash->show();
}
void BookList_Admin::on_pushButton_refresh_clicked()
{
this->loadData();
}
void BookList_Admin::on_pushButton_add_clicked()
{
addBook * ab = new addBook(&this->booksdb);
ab->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
ab->show();
this->change = true;
}
void BookList_Admin::on_pushButton_delete_clicked()
{
if (!ui->tableWidget->selectedItems().size())
{
QMessageBox::critical(nullptr, "No Item Selected", "Please select an item to delete");
return;
}
QString key = ui->tableWidget->selectedItems().at(0)->text(); //ISBN
int ret = QMessageBox::warning(nullptr, "Confirm Delete Book", "Are you sure you want to delete <" + booksdb.value(key).at(0) + ">?", QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes)
{
booksdb.remove(key);
book_item::deleteBooks(key);
group_item::deleteBook(key);
Book::saveChanges(&booksdb);
loadData();
this->change = true;
}
}
void BookList_Admin::on_lineEdit_search_textChanged(const QString &arg1)
{
if (arg1 == "")
{
for (int i = 0; i < ui->tableWidget->rowCount(); ++i)
ui->tableWidget->showRow(i);
ui->label->setText("Books List | " + QString::number(booksdb.size()) + " Records Loaded");
}
else
{
int cnt = 0;
if (ui->comboBox_search->currentIndex())
{
for (int i = 0; i < ui->tableWidget->rowCount(); ++i)
{
if (ui->tableWidget->item(i, ui->comboBox_search->currentIndex() - 1)->text().startsWith(arg1, Qt::CaseInsensitive))
{
cnt++;
ui->tableWidget->showRow(i);
}
else
ui->tableWidget->hideRow(i);
}
}
else // for "All"
{
for (int i = 0; i < ui->tableWidget->rowCount(); ++i)
ui->tableWidget->hideRow(i);
QList<QTableWidgetItem *> filterd_items = ui->tableWidget->findItems(arg1, Qt::MatchStartsWith);
QSet<int> rows;
for (int i = 0; i < filterd_items.size(); ++i)
rows.insert(filterd_items.at(i)->row());
for (int r : rows)
ui->tableWidget->showRow(r);
cnt = rows.size();
}
ui->label->setText("Books List | " + QString::number(cnt) + " Records Loaded");
}
}
void BookList_Admin::on_comboBox_search_currentIndexChanged(int index)
{
ui->lineEdit_search->clear();
if (index)
ui->lineEdit_search->setCompleter(completer.at(index - 1));
else
ui->lineEdit_search->setCompleter(0); // for "All"
}
void BookList_Admin::on_pushButton_edit_clicked()
{
if (!ui->tableWidget->selectedItems().size())
{
QMessageBox::critical(nullptr, "No Item Selected", "Please select an item to edit");
return;
}
QString key = ui->tableWidget->selectedItems().at(0)->text(); //ISBN
editBook * eb = new editBook(&this->booksdb, key);
eb->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
eb->show();
}