This repository was archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
192 lines (176 loc) · 7.05 KB
/
Copy pathmainwindow.cpp
File metadata and controls
192 lines (176 loc) · 7.05 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap background("icone/background.png");
QPalette palette;
palette.setBrush(QPalette::Background, background);
this->setPalette(palette);
checkLanguage();
QObject::connect(this->ui->pushButton_2, SIGNAL(clicked()), this, SLOT(translate()));
QObject::connect(this->ui->pushButton, SIGNAL(clicked()), this, SLOT(change()));
QObject::connect(this->ui->pushButton_3, SIGNAL(clicked()), this, SLOT(reset()));
QObject::connect(this->ui->pushButton_4, SIGNAL(clicked()), this, SLOT(voice()));
QAction::connect(this->ui->actionHistorique, SIGNAL(triggered()), this, SLOT(afficherHistorique()));
}
MainWindow::~MainWindow()
{
delete ui;
}
//traduit
void MainWindow::translate(){
if(this->ui->source->currentText() == this->ui->destination->currentText()){
QMessageBox mes;
mes.setText("La langue de destination est la meme que la langue source!");
mes.exec();
}else{
QString text = ui->textEdit->toPlainText();
QString translate;
QString key = ui->source->currentText()+"-"+ui->destination->currentText();
QRegExp del("\\.");
QRegExp del2("\\ ");
QStringList phrases = text.split(del);
QStringList phrase;
QMap<QString, word> dicoTrad = dico.value(key);
QString typePrecedent = "";
//QList keys = dicoTrad.keys();
for(int i = 0; i < phrases.length(); i++){
phrase = phrases[i].split(del2);
for(int q = 0; q < phrase.length();q++){
int j = 0;
int start = q;
word w;
typePrecedent = "";
while(q+j < phrase.length()&& (w = dicoTrad.value(phrase[q+j]))!= ""){
if(phrase[start+j]!= "" && w != ""){
if(typePrecedent != "n" && w.getType() == "adj" && ui->source->currentText()=="fra"){
qDebug() << translate;
QStringList changeGrammaire = translate.split(del2);
QString last = changeGrammaire[changeGrammaire.length()-2];
changeGrammaire[changeGrammaire.length()-2] = w.getTranslate();
qDebug() << last;
//changeGrammaire.append(last);
translate = "";
for(int z = 0; z < changeGrammaire.length(); z++){
qDebug() << translate;
translate += changeGrammaire[z] + " ";
}
translate += last + " ";
}else{
translate += w.getTranslate() + " ";
}
typePrecedent = w.getType();
q = start+(j);
}
j++;
}
if(w==""){
translate += phrase[q] + " ";
}
}
}
this->ui->textEdit_2->setText(translate);
h.writeHistorique("historique/"+key+".json", text, translate);
}
}
//échange les 2 valeurs du spinner
void MainWindow::change(){
int source = this->ui->source->currentIndex();
int destination = this->ui->destination->currentIndex();
int newSource = 0;
int newDestination = 0;
for(int i = 0; i < this->ui->source->count(); i++){
if(this->ui->source->itemText(i) == this->ui->destination->itemText(destination)){
newSource = i;
}
}
for(int i = 0; i < this->ui->destination->count(); i++){
if(this->ui->destination->itemText(i) == this->ui->source->itemText(source)){
newDestination = i;
}
}
this->ui->source->setCurrentIndex(newSource);
this->ui->destination->setCurrentIndex(newDestination);
}
//reset les champs text
void MainWindow::reset(){
ui->textEdit->clear();
ui->textEdit_2->clear();
}
//verifie les dicos disponnibles et les initialises
void MainWindow::checkLanguage(){
QDir dico("dico/");
QRegExp del("\\-|\\.");
if(dico.exists()){
QStringList files = dico.entryList();
for(int i = 0; i < files.size(); i++){
if(files[i]!="." && files[i]!=".."){
QString file = files[i];
QStringList countries = file.split(del);
if(isInVector(countries[0])){
for(int j = 0; j < this->lang.size(); j++){
if(this->lang[j]->getName()==countries[0]){
this->lang[j]->getTranslate().append(countries[1]);
bool isIn = false;
for(int q = 0; q < this->ui->destination->count(); q++){
if(this->ui->destination->itemText(q) == countries[1]){
isIn = true;
}
}
if(!isIn)
this->ui->destination->addItem(countries[1]);
QString key = countries[0]+"-"+countries[1];
XMLParseur xml("dico/"+files[i]);
this->dico.insert(key, xml.getDico());
}
}
}else{
QStringList trad;
trad << countries[1];
language* l = new language(countries[0], trad);
this->lang.append(l);
this->ui->source->addItem(countries[0]);
bool isIn = false;
for(int q = 0; q < this->ui->destination->count(); q++){
if(this->ui->destination->itemText(q) == countries[1]){
isIn = true;
}
}
if(!isIn)
this->ui->destination->addItem(countries[1]);
QString key = countries[0]+"-"+countries[1];
XMLParseur xml("dico/"+files[i]);
this->dico.insert(key, xml.getDico());
}
}
}
}else{
qDebug() << "the directory doesn'existe!";
}
}
void MainWindow::voice(){
QString string = "tts\\tts.exe \"" + ui->textEdit_2->toPlainText() + "\"";
QByteArray ba = string.toLatin1();
const char* text = ba.data();
system(text);
}
//verfie si une langue à déjà été enregister
bool MainWindow::isInVector(QString country){
if(!this->lang.isEmpty()){
for(int i = 0; i < this->lang.size(); i++){
if(this->lang[i]->getName() == country)
return true;
}
return false;
}else{
return false;
}
}
void MainWindow::afficherHistorique(){
QString filename = ui->source->currentText()+"-"+ui->destination->currentText()+".json";
HistoriqueFenetre hf("historique/"+filename);
hf.exec();
}