-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookdialog.cpp
83 lines (71 loc) · 2.27 KB
/
bookdialog.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
#include "bookdialog.h"
#include "./ui_bookdialog.h"
#include "confirmdialog.h"
#include <QDir>
#include <QUrl>
#include <QHeaderView>
#include <QDebug>
#include <QSortFilterProxyModel>
#include "models.h"
using namespace bookdata;
using json = nlohmann::json;
BookDialog::BookDialog(std::vector<bookdata::Book*> &books, QWidget *parent): QDialog(parent), ui(new Ui::BookDialog), sortModel(new QSortFilterProxyModel)
{
ui->setupUi(this);
setupModel(books);
connect(ui->treeView, &QTreeView::doubleClicked, this, &BookDialog::onDoubleClicked);
connect(ui->pushButtonDelete, &QPushButton::clicked, this, &BookDialog::onDelete);
connect(this, &BookDialog::loadBook, this, &BookDialog::accept);
}
BookDialog::~BookDialog()
{
delete ui;
delete sortModel;
}
void BookDialog::setupModel(std::vector<bookdata::Book*> &books)
{
BasicModel *model = new BasicModel(4);
connect(this, &BookDialog::reloadBooks, model, &BasicModel::reset);
model->setHeaderData({tr("Book ID"), tr("Last Edited"), tr("Box"), tr("Section")});
for(Book *book : books)
{
BookItem *item = new BookItem(*book);
model->addItem(item);
}
sortModel->setSourceModel(model);
sortModel->sort(1, Qt::AscendingOrder); //We sort first by ID...
ui->treeView->setModel(sortModel);
ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
}
void BookDialog::onDoubleClicked(const QModelIndex &index)
{
if(index.column() == 0)
{
int bookid = sortModel->data(index, Qt::DisplayRole).toInt();
emit loadBook(bookid);
}
}
void BookDialog::onDelete()
{
auto selected = ui->treeView->selectionModel()->selection().indexes();
if(selected.size() > 0)
{
QString str("Are you sure you want to delete those book(s)?\n");
QString bookstr{};
std::vector<int> bks{};
for(QModelIndex index : selected)
{
int id = sortModel->data(index, Qt::DisplayRole).toInt();
QString bk = QString("Book %1\n").arg(id);
bks.push_back(id);
bookstr.append(bk);
}
str.append(bookstr);
ConfirmDialog dialog(str, "Delete Book?");
if(dialog.exec())
{
emit deleteBooks(bks);
accept();
}
}
}