-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
224 lines (173 loc) · 5.89 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <qfile.h>
#include <QTextStream>
#include <QFileDialog>
#include <QDirIterator>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// on_Load();
on_import_2_clicked();
}
MainWindow::~MainWindow()
{
delete ui;
}
// Convert input string to list of tokens
// Each token is either a word (sequence of letters)
// or a non-word (numeric, symbol, etc)
QStringList tokenizeInput(QString input) {
QStringList tokenList;
QString currentToken;
for(int i = 0; i < input.length(); i++) {
if (input[i].isLetter()){
// keep accumulating letters that form a word
currentToken += input[i];
} else {
// if there is an accumulated word, flush to the list
if (currentToken.length() != 0) {
tokenList.append(currentToken);
currentToken.clear();
}
// add the non-word token into token list (e.g. space/symbol/digit)
tokenList.append(QString(input[i]));
}
}
// the input can end with a letter, flush if there is an accumulated word
if (currentToken.length() != 0) {
tokenList.append(currentToken);
}
// return produced token list
return tokenList;
}
void MainWindow::on_translatebtn_clicked()
{
// Grab the input string to translate/substitute
QString input = ui->input->toPlainText().toLatin1();
// Build rule map
QMap<QString, QString> ruleMap;
int ruleCount = ui->sublist->count();
qDebug() << ruleCount;
for (int i = 0; i < ruleCount; i++) {
// A wild item has appeared
QString ruleStr = ui->sublist->item(i)->text();
QStringList ruleTokens = ruleStr.split(",");
ruleMap.insert(ruleTokens.at(0), ruleTokens.at(1));
ruleMap.insert(ruleTokens.at(1), ruleTokens.at(0));
}
// Convert input to list of tokens (words and non-words)
QStringList inputTokens = tokenizeInput(input);
// Produce output using list of input tokens and rule map
QString output;
for (int i=0; i < inputTokens.length(); i++){
QString inputToken = inputTokens.at(i);
if (ruleMap.contains(inputToken)) {
output.append(ruleMap[inputToken]);
} else {
if (inputToken.length() > 1){
output.append("un"+inputToken);}else{ output.append(inputToken);}
}
}
if (ui->reversed->isChecked()){
// std::reverse(output.begin(), output.end());
};
// Update UI with produced output
ui->translated->setText(output.toLatin1());
}
void MainWindow::on_sublist_itemSelectionChanged()
{
QString test = ui->sublist->currentItem()->text();
QStringList test2 = test.split(",");
ui->in1->setText(test2.at(0));
ui->in2->setText(test2.at(1));
//todo still needs to save and add new row
}
void MainWindow::on_import_2_clicked()
{
//do this on startup to import any settings
// QFile file(ui->loadlistcmb->currentText());
QFile file("default.txt");
if (file.exists()){
ui->sublist->clear();
QStringList stringList;
// QFile textFile;
file.open(QIODevice::ReadWrite | QFile::Text);
QTextStream textStream(&file);
while (true)
{
QString line = textStream.readLine();
if (line.isNull())
break;
else
stringList.append(line);
QStringList test2 = line.split(",");
qDebug() << test2.at(0) << test2.at(1);
// ui->in1->setText(test2.at(0));// << "," << test2.at(1)
// ui->in1->setText(test2.at(1));//
QString itemtxt = test2.at(0).toLatin1();
itemtxt += ",";
itemtxt += test2.at(1).toLatin1();
ui->sublist->addItem(itemtxt.toLatin1());
}
file.close();
}
}
void MainWindow::on_editbtn_clicked()
{
//if empty in1 and 2 then make new or unselected sublist
if ((ui->in1->text().toLatin1()=="") && (ui->in2->text().toLatin1()=="")){
ui->sublist->insertItem((ui->sublist->count()+1),"new1,new1");
}else{
QString test = ui->sublist->currentItem()->text();
QStringList test2 = test.split(",");
if ((test2.at(0).toLatin1()=="new1") && (test2.at(1).toLatin1()=="new1")){
QString itemtxt = ui->in1->text().toLatin1();
itemtxt += ",";
itemtxt += ui->in2->text().toLatin1();
ui->sublist->currentItem()->setText(itemtxt.toLatin1());
}else{
QString itemtxt = ui->in1->text().toLatin1();
itemtxt += ",";
itemtxt += ui->in2->text().toLatin1();
ui->sublist->currentItem()->setText(itemtxt.toLatin1());
}
}
on_export_2_clicked();
}
void MainWindow::on_export_2_clicked()
{
//QString Filename = QFileDialog::getSaveFileName(this, "Save File",".txt",".txt");
QString Filename = "./default.txt";
QFile file(Filename);
file.open(QIODevice::ReadWrite | QFile::Text);
QStringList exported;
int listcount = ui->sublist->count();
qDebug() << listcount;
for (int i=0;i < listcount;i++){
QTextStream out(&file);
QString test = ui->sublist->item(i)->text(); //ui->sublist->currentItem()->text();
QStringList test2 = test.split(",");
// // exported << test2.at(0) << test2.at(1);
out << test2.at(0).toLatin1() << "," << test2.at(1).toLatin1() << endl;
// qDebug() << "testing2";
// qDebug() << test2.at(0).toLatin1();
}
file.close();
//dump exported to file
on_Load();
}
void MainWindow::on_Load()
{
QDirIterator it("./", QStringList() << "*.txt", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()){
// QFileInfo fileInfo(f.fileName());
ui->loadlistcmb->addItem(it.next().toLatin1());
}
if (ui->loadlistcmb->currentText() == "default"){
qDebug() << "testing";
}
}