-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookInput.cpp
117 lines (100 loc) · 3.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
#include "BookInput.h"
BookInput::BookInput (QWidget* parent)
: QMainWindow(parent){
setWindowTitle("Book editor");
file.setTitle("File");
openAct.setText("open");
saveAct.setText("save");
closeAct.setText("close");
consoleAct.setText("console");
file.addAction(&openAct);
file.addAction(&saveAct);
file.addAction(&closeAct);
file.addAction(&consoleAct);
menuBar.addMenu(&file);
this->setMenuBar(&menuBar);
authorIn.setPlaceholderText("Comma separated values");
titleLable.setText("Title");
authorLable.setText("Author");
isbnLable.setText("ISBN");
dateLable.setText("Published date");
lineEditLayout.addWidget(&titleLable);
lineEditLayout.addWidget(&titleIn);
lineEditLayout.addWidget(&authorLable);
lineEditLayout.addWidget(&authorIn);
lineEditLayout.addWidget(&isbnLable);
lineEditLayout.addWidget(&isbnIn);
lineEditLayout.addWidget(&dateLable);
lineEditLayout.addWidget(&dateIn);
editor->setDocument(&document);
editorLayout.addWidget(editor);
centralWidget.setLayout(&mainLayout);
mainLayout.addLayout(&lineEditLayout);
mainLayout.addLayout(&editorLayout);
setCentralWidget(¢ralWidget);
connect(&openAct, &QAction::triggered, this, &BookInput::openClicked);
connect(&saveAct, &QAction::triggered, this, &BookInput::saveClicked);
connect(&closeAct, &QAction::triggered, this, &BookInput::closeClicked);
connect(&consoleAct, &QAction::triggered, this, &BookInput::consoleClicked);
}
BookInput::~BookInput() {
delete editor;
delete tempEditor;
}
void BookInput::openClicked() {
QString filePath;
filePath = QFileDialog::getOpenFileName(0, "Select file", "", "All files (*)");
if(!filePath.isEmpty()) {
QFile file(filePath);
if(file.open(QFile::ReadOnly | QFile::Text)) {
tempEditor->setPlainText(file.readAll());
this->read(tempEditor);
}
} else {
qDebug() << "File selection cancelled by user";
}
}
void BookInput::saveClicked() {
this->refreshBook();
writer->write(book);
}
void BookInput::closeClicked() {
}
void BookInput::consoleClicked() {
book->obtainBookInfo();
}
void BookInput::read(QTextEdit *e) {
QString txt = e->toPlainText();
//qDebug() << "read txt";
book->read(&txt);
//qDebug() << "refresh UI";
this->refreshUi();
}
QString BookInput::write() {
this->refreshBook();
book->write();
return book->getBinding();
}
void BookInput::refreshBook() {
book->setTitle(titleIn.text());
QStringList aList = authorIn.text().split(',').toList();
book->setAuthors(aList);
book->setIsbn(isbnIn.text());
book->setPublicationDate(dateIn.date());
book->setBookAttributes();
}
void BookInput::refreshUi() {
titleIn.setText(book->getTitle());
QString a = "";
QStringList aList = book->getAuthors();
for(int i=0; i<aList.length(); i++) {
a.append(aList[i]);
if(!(i-1 == aList.length())) {
a.append(',');
}
}
authorIn.setText(a);
isbnIn.setText(book->getIsbn());
dateIn.setDate(book->getPublicationDate());
editor->setText(book->getContent());
}