-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
271 lines (217 loc) · 10.2 KB
/
mainwindow.cpp
File metadata and controls
271 lines (217 loc) · 10.2 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "mainwindow.h"
#include "addlangwindow.h"
#include "./ui_mainwindow.h"
#include <QApplication>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QTreeView>
#include <QDir>
#include <QFileInfo>
// #include <string>
namespace {
QString resolveProjectFilePath(const QString &fileName)
{
const QDir appDir(QCoreApplication::applicationDirPath());
const QStringList candidates = {
appDir.filePath("../../../../../" + fileName),
appDir.filePath("../../" + fileName),
QDir::current().filePath(fileName)
};
for (const QString &candidate : candidates) {
if (QFileInfo::exists(candidate)) {
return QFileInfo(candidate).absoluteFilePath();
}
}
return {};
}
void resetLanguageView(Ui::MainWindow *ui)
{
if (QAbstractItemModel *existingModel = ui->langDetailTree->model()) {
ui->langDetailTree->setModel(nullptr);
delete existingModel;
}
ui->vocabInfoOutput->clear();
}
YAML::Node loadLanguageData(YAML::Node languages, const std::string &languageName)
{
// const YAML::Node languages = language_db["languages"];
if (!languages || !languages.IsMap()) {
return {};
}
else{
qInfo("Successfully loaded language: %s", languageName.c_str());
}
return languages[languageName];
}
} // namespace
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// read in the language spec YAML and populate the list of available languages
YAML::Node lang_spec = YAML::LoadFile(resolveProjectFilePath("spec.yaml").toStdString());
YAML::Node lang_db = YAML::LoadFile(resolveProjectFilePath("language.yaml").toStdString());
// TODO: this needs to be able to be derived for each language, it should be possible to be derived
// from the spec.yaml for each language
verb_types = {"Verbs-en-inf", "Verbs-en-simple-past", "Verbs-present-1ppl",
"Verbs-present-1psing", "Verbs-present_cont-1ppl", "Verbs-present_cont-1psing"};
supported_langs = lang_spec["specs"];
lang_database = lang_db["languages"];
// iterate over the supported langs and add to the list
switch (supported_langs.Type()) {
case YAML::NodeType::Null: qInfo("supported_langs is a Null type"); break;
case YAML::NodeType::Scalar: qInfo("supported_langs is a Scalar type"); break;
case YAML::NodeType::Sequence: qInfo("supported_langs is a Sequence type"); break;
case YAML::NodeType::Map: qInfo("supported_langs is a Map type"); break;
case YAML::NodeType::Undefined: qInfo("supported_langs is a Undefined type"); break;
}
qInfo("size: %zu", supported_langs.size());
switch (lang_database.Type()) {
case YAML::NodeType::Null: qInfo("lang_database is a Null type"); break;
case YAML::NodeType::Scalar: qInfo("lang_database is a Scalar type"); break;
case YAML::NodeType::Sequence: qInfo("lang_database is a Sequence type"); break;
case YAML::NodeType::Map: qInfo("lang_database is a Map type"); break;
case YAML::NodeType::Undefined: qInfo("lang_database is a Undefined type"); break;
}
qInfo("size: %zu", lang_database.size());
// With for-range loop
for (auto child : supported_langs) {
qInfo( "found language in spec.YAML: %s", child.first.as<std::string>().c_str() );
ui->langList->addItem(child.first.as<std::string>().c_str());
}
ui->vocabInfoOutput->setReadOnly(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_addLangButton_clicked()
{
addlangwindow *addlangwin = new addlangwindow(this);
addlangwin->setModal(true);
addlangwin->exec();
}
void MainWindow::receive_new_lang(const QString &lang)
{
}
void MainWindow::on_langList_itemClicked(QListWidgetItem *item)
{
std::string selected_lang_name = item->text().toStdString();
qInfo("User selected lang: %s", selected_lang_name.c_str());
// TODO: possibly redundant function? Doesn't seem to do much, once complete see if we can't get rid of it!
resetLanguageView(ui);
// lookup the selected language and grab the vocab from the lang_database
// selected_lang_handle = lang_database[selected_lang_name];
// lookup the selected language and grab the vocab from the language database file
const YAML::Node selected_language = loadLanguageData(lang_database, selected_lang_name);
if (!selected_language || !selected_language.IsMap()) {
ui->vocabInfoOutput->setText(
QString("No vocabulary is loaded for %1 yet.").arg(item->text()));
return;
}
// add vocab relevant to selected language to the ui elem langDetailTree
// using model based approach rather than item based
auto *model = new QStandardItemModel();
model->setHorizontalHeaderLabels({"Vocab"});
auto *verbs_item = new QStandardItem("Verbs");
auto *nouns_item = new QStandardItem("Nouns");
auto *adjs_item = new QStandardItem("Adjectives");
auto *preps_item = new QStandardItem("Prepositions");
for(auto verb : selected_language["Verbs"]){
verbs_item->appendRow(new QStandardItem(verb.as<std::string>().c_str()));
qInfo("User selected lang has verb: %s", verb.as<std::string>().c_str());
}
for(auto noun : selected_language["Nouns"]){
nouns_item->appendRow(new QStandardItem(noun.as<std::string>().c_str()));
qInfo("User selected lang has noun: %s", noun.as<std::string>().c_str());
}
for(auto adj : selected_language["Adjectives"]){
adjs_item->appendRow(new QStandardItem(adj.as<std::string>().c_str()));
qInfo("User selected lang has adj: %s", adj.as<std::string>().c_str());
}
for(auto prep : selected_language["Prepositions"]){
preps_item->appendRow(new QStandardItem(prep.as<std::string>().c_str()));
qInfo("User selected lang has prep: %s", prep.as<std::string>().c_str());
}
model->appendRow(verbs_item);
model->appendRow(nouns_item);
model->appendRow(adjs_item);
model->appendRow(preps_item);
// QTreeView view;
ui->langDetailTree->setModel(model);
ui->langDetailTree->expandAll();
QItemSelectionModel *langDetailTreeSelModel = ui->langDetailTree->selectionModel();
connect(langDetailTreeSelModel,
SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
this,
SLOT(vocabSelected(const QModelIndex &, const QModelIndex &)));
// TODO: tree
// - give collapse all button and retract all button
}
void MainWindow::vocabSelected(const QModelIndex &curr, const QModelIndex &prev){
auto parent = curr.parent();
// const char *vocab =
bool parentPresent = true;
YAML::Node vocab_sample;
std::stringstream vocab_str;
if(parent == QModelIndex()){
parentPresent = false;
}
qInfo("TEST SLOT TRIGGERED: %d %d %d", parentPresent, curr.row(), curr.column());
qInfo(" Selected word: " + ui->langDetailTree->model()->data(curr).toString().toLatin1());
if(parentPresent){
qInfo(" belongs to word class: " + ui->langDetailTree->model()->data(curr.parent()).toString().toLatin1());
}
// we have to perform the YAML Map lookup with std::string as index
vocab_str << ui->langDetailTree->model()->data(curr).toString().toLatin1().data();
std::string vocab_std_str = vocab_str.str();
// clear the output display
ui->vocabInfoOutput->setText("");
qInfo("SELECTED WORD IN LANG'S VOCAB LIST IS: <%s>", vocab_std_str.c_str());
// determine what kind of vocab is currently selected
if(ui->langDetailTree->model()->data(curr.parent()).toString().toLatin1() == "Verbs"){
// fetch the conjugated versions of the selected vocab
for(std::string verb_type : verb_types){
// vocab_sample = selected_language[verb_type][vocab_std_str];
vocab_sample = selected_language[verb_type];
switch (vocab_sample.Type()) {
case YAML::NodeType::Null: qInfo("vocab_sample is a Null type"); break;
case YAML::NodeType::Scalar: qInfo("vocab_sample is a Scalar type"); break;
case YAML::NodeType::Sequence: qInfo("vocab_sample is a Sequence type"); break;
case YAML::NodeType::Map: qInfo("vocab_sample is a Map type"); break;
case YAML::NodeType::Undefined: qInfo("vocab_sample is a Undefined type"); break;
}
qInfo("size: %zu", vocab_sample.size());
vocab_sample = vocab_sample[vocab_std_str];
switch (vocab_sample.Type()) {
case YAML::NodeType::Null: qInfo("vocab_sample is a Null type"); break;
case YAML::NodeType::Scalar: qInfo("vocab_sample is a Scalar type"); break;
case YAML::NodeType::Sequence: qInfo("vocab_sample is a Sequence type"); break;
case YAML::NodeType::Map: qInfo("vocab_sample is a Map type"); break;
case YAML::NodeType::Undefined: qInfo("vocab_sample is a Undefined type"); break;
}
qInfo("size: %zu", vocab_sample.size());
qInfo("Doing lookup for params: <%s> & <%s>", verb_type.c_str(), vocab_std_str.c_str());
qInfo("UP TO HERE 0");
ui->vocabInfoOutput->append(verb_type.c_str());
qInfo("UP TO HERE 1");
ui->vocabInfoOutput->append(vocab_sample.as<std::string>().c_str());
qInfo("UP TO HERE 2");
}
}
else if(ui->langDetailTree->model()->data(curr.parent()).toString().toLatin1() == "Nouns"){
// fetch the noun genders
vocab_sample = selected_language["Nouns-gender"][vocab_std_str];
ui->vocabInfoOutput->append(vocab_sample.as<std::string>().c_str());
}
else if(ui->langDetailTree->model()->data(curr.parent()).toString().toLatin1() == "Adjectives"){
ui->vocabInfoOutput->append("No extra info to display");
}
else if(ui->langDetailTree->model()->data(curr.parent()).toString().toLatin1() == "Prepositions"){
ui->vocabInfoOutput->append("No extra info to display");
}
else{
// do nothing, a parent must've been selected -> no action
}
}