-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_window.cpp
400 lines (311 loc) · 14.7 KB
/
main_window.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "main_window.h"
#include "declarations.h"
#include "prog_window.h"
#include <Qt>
MainWindow::Handler MainWindow::handler=MainWindow::Handler();
MainWindow& MainWindow::getInstance(){
if (handler.instance==0) {
handler.instance=new MainWindow;
}
return *handler.instance;
}
void MainWindow::libererInstance(){
delete(handler.instance) ;
handler.instance=0;
}
MainWindow::MainWindow()
{
//CREATION DU MENU
setWindowTitle("CalcUTC");
this->size_min();
QMenu *menuOption = menuBar()->addMenu("&Option");
QAction *actionQuitter = new QAction("&Quitter", this); //AJOUT DE QUITTER
menuOption->addAction(actionQuitter);
QAction *actionProgEdit = new QAction("Nouveau programme", this); // Ajout d'une action créer nouveau programme qui ouvre un QEDITTEXT
menuOption->addAction(actionProgEdit);
QMenu *menuAffichage = menuBar()->addMenu("&Affichage");
QAction *actionClavier = new QAction("&Clavier", this); //OPTION D'AFFICHAGE DU CLAVIER ET DES FONCTIONS SCIENTIFIQUES
menuAffichage->addAction(actionClavier);
QAction *actionScientifique = new QAction("&Mode scientifique", this);
menuAffichage->addAction(actionScientifique);
QAction *actionProg = new QAction("&Gestion Programme",this);
menuAffichage->addAction(actionProg);
QAction *actionRedo = new QAction("&Redo", this); // AJOUT DU "Redo"
menuOption->addAction(actionRedo);
actionRedo->setIcon(QIcon("\\Users\\Adrien\\Documents\\Adrien\\UTC\\GI 02\\LO21\\Projet\\version finale\\projet_1_0\\images/arrow_redo.png"));
actionRedo->setShortcut(QKeySequence(tr("Ctrl+y")));
QAction *actionUndo = new QAction("&Undo", this); // AJOUT DU "Undo"
menuOption->addAction(actionUndo);
actionUndo->setIcon(QIcon("\\Users\\Adrien\\Documents\\Adrien\\UTC\\GI 02\\LO21\\Projet\\version finale\\projet_1_0\\images/arrow_undo.png"));
actionUndo->setShortcut(QKeySequence(tr("Ctrl+z")));
QMenu *menuAide = menuBar()->addMenu("&Aide");
QAction *actionAPropos = new QAction("&A propos", this); // AJOUT DU "A PROPOS"
menuAide->addAction(actionAPropos);
// GESTION DES SIGNAUX ET SLOTS DU MENU
connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
actionQuitter->setShortcut(QKeySequence("Escape"));
connect(actionScientifique, SIGNAL(triggered()), this, SLOT(afficher_scientique()));
connect(actionClavier, SIGNAL(triggered()), this, SLOT(afficher_clavier()));
actionClavier->setCheckable(true);
actionScientifique->setCheckable(true);
// CONNECT DU PROGEDIT
connect(actionProg,SIGNAL(triggered(bool)), this, SLOT(openProgWindow()));
connect(actionProgEdit,SIGNAL(triggered()), this, SLOT(openEditProg())); // OUVRE LE QEDITTEXT
connect(actionAPropos,SIGNAL(triggered(bool)), this, SLOT(afficher_APropos()));
//INITIALISATION DES OBJETS DU .h
mainArea = new QWidget;
mainSet = new QHBoxLayout;
scientificPad = new scientific_pad;
scientificPad->hide();
keyboard = new Keyboard();
keyboard->hide();
controlScreen = new QWidget();
controlScreen->setFixedSize(QSize(280, 300));
pile = new Pile ;
qDebug()<<pile;
controleur = Controleur::getInstance(LiteraleManager::getInstance() , *pile) ;
message = new QLineEdit() ;
commande = new QLineEdit() ;
vuePile = new QTableWidget (pile->getNbItemsToAffiche(),1) ;
pile_create();
topLayout = new QHBoxLayout();
middleLayout = new QHBoxLayout();
boutons_droits = new QVBoxLayout();
bottomLayout = new QHBoxLayout();
pile_plus = new QPushButton();
pile_moins = new QPushButton();
enter = new QPushButton();
leftLayout = new QVBoxLayout();
leftLayout->addWidget(vuePile);
vuePile->setStyleSheet("background : rgba(35,141,214,45)");
commande->setStyleSheet("background : rgba(35,141,214,45)");
//MIDDLELAYOUT SETTINGS
middleLayout->addLayout(leftLayout);
middleLayout->addLayout(boutons_droits);
boutons_droits->addWidget(pile_plus);
boutons_droits->addWidget(pile_moins);
//BOTTOM LAYOUT SETTINGS
bottomLayout->addWidget(commande);
bottomLayout->addWidget(enter);
enter->setIcon(QIcon("\\Users\\Adrien\\Documents\\Adrien\\UTC\\GI 02\\LO21\\Projet\\version finale\\projet_1_0\\images/enter-arrow.png"));
enter->setFlat(true);
enter->setFocusPolicy(Qt::NoFocus);
connect(enter,SIGNAL(clicked(bool)),this,SLOT(slot_getNextCommande()));
//BOUTON PILE SETTINGS + CONNECT
pile_plus->setIcon(QIcon("\\Users\\Adrien\\Documents\\Adrien\\UTC\\GI 02\\LO21\\Projet\\version finale\\projet_1_0\\images/add.png"));
pile_plus->setFlat(true);
pile_plus->setFocusPolicy(Qt::NoFocus);
pile_moins->setIcon(QIcon("\\Users\\Adrien\\Documents\\Adrien\\UTC\\GI 02\\LO21\\Projet\\version finale\\projet_1_0\\images/minus.png"));
pile_moins->setFlat(true);
pile_moins->setFocusPolicy(Qt::NoFocus);
connect(pile_plus,SIGNAL(clicked(bool)),pile,SLOT(aug_pile()));
connect(pile_plus,SIGNAL(clicked(bool)),this,SLOT(pile_create()));
connect(pile_moins,SIGNAL(clicked(bool)),pile,SLOT(dim_pile()));
connect(pile_moins,SIGNAL(clicked(bool)),this,SLOT(pile_create()));
// MESSAGE SETTINGS
message->setReadOnly(true); //empèche d'écrire dans la lineEdit
message->setStyleSheet("background: blue; color: yellow");
message->setAlignment(Qt::AlignHCenter);
message->setFixedHeight(45);
//VUE PLIE SETTINGS
vuePile->horizontalHeader()->setVisible(false); //enlève l'indice de col
vuePile->horizontalHeader()->setStretchLastSection(true); //ajuste la largeur à la fenetre
vuePile->setSelectionMode(QAbstractItemView::NoSelection);
vuePile->setEditTriggers(QAbstractItemView::NoEditTriggers) ; //empèche d'écrire dans les cellules
//SOUND_LOCK SETTINGS
sound_lock = new QPushButton();
sound_lock->setIcon(QIcon("\\Users\\Adrien\\Documents\\Adrien\\UTC\\GI 02\\LO21\\Projet\\version finale\\projet_1_0\\images/speaker.png"));
sound_lock->setFlat(true);
sound_lock->setFocusPolicy(Qt::NoFocus);
connect(sound_lock,SIGNAL(clicked(bool)),this,SLOT(sound_disable()));
//GESTION DES CONNECTS DU SCIENTIFIC_PAD
connect(scientificPad->cos, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->sin, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->tan, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->arccos, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->arcsin, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->arctan, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->sqrt, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->puis, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->exp, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->ln, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->div, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
connect(scientificPad->mod, SIGNAL(clicked(bool)), this, SLOT(keyboardButtonPressed()));
//GESTION DES CONNECTS DU CONTROLSCREEN
connect(commande, SIGNAL (returnPressed()) , this , SLOT(slot_getNextCommande())) ;
connect(pile , SIGNAL (modificationEtat()) , this, SLOT(refresh())) ;
//GESTION DES CONNECTS DU KEYBOARD
connect(keyboard->b1,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->b2,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->b3,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->b4,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->b5,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->b6,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->b7,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->b8,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->b9,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->b0,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->bplus,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->bmoins,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->bdiv,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->bmult,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->clear,SIGNAL(clicked(bool)),commande, SLOT(clear()));
connect(keyboard->space,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->bComp,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->bPo,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
connect(keyboard->bPf,SIGNAL(clicked(bool)),this, SLOT(keyboardButtonPressed()));
//CREATION DES LIENS ENTRE LES LAYOUTS
mainSet->addWidget(scientificPad);
scientificPad->setStyleSheet("background : rgba(35,141,214,45)");
mainSet->addWidget(controlScreen);
message->setStyleSheet("background : rgba(35,186,117,45)");
vuePile->setStyleSheet("background : rgba(35,141,214,25)");;
mainSet->addWidget(keyboard);
keyboard->setStyleSheet("background : rgba(35,141,214,45)");
topLayout->addWidget(message);
topLayout->addWidget(sound_lock);
LcontrolScreen = new QVBoxLayout;
controlScreen->setLayout(LcontrolScreen);
LcontrolScreen->addLayout(topLayout);
LcontrolScreen->addLayout(middleLayout);
LcontrolScreen->addLayout(bottomLayout);
mainArea->setLayout(mainSet);
setCentralWidget(mainArea);
//GESTION SAUVEGARDE CONTEXTE
connect(qApp,SIGNAL(aboutToQuit()),this,SLOT(save_context()));
open_context();
}
void MainWindow::afficher_APropos()
{
QMessageBox::information(this,"CalcUTC", "Ce logiciel a été créé par Adrien et Antonin dans le cadre d'un projet universitaire");
}
void MainWindow::refresh()
{ //affiche état pile et message éventuel
message->setText(pile->getMessage());
if(message->text()!=""){
this->bip();
}
//on efface tt ce qui est affiché
for(unsigned int i = 0 ; i<pile->getNbItemsToAffiche() ; i++)
vuePile->item(i,0)->setText("") ;
unsigned int nb = 0 ;
for(Item* it= (pile->stack.end() -1) ; it!= (pile->stack.begin()-1) and nb < pile->getNbItemsToAffiche() ; --it , ++nb)
vuePile->item(pile->getNbItemsToAffiche()-nb-1,0)->setText((it->getLiterale()).toString()) ;
}
void MainWindow::openEditProg(){
editWindow = new SecondWindow(); // Be sure to destroy you window somewhere
editWindow->show();
}
void MainWindow::openProgWindow(){
progWindow = new ProgWindow();
progWindow->refresh_content();
progWindow->show();
}
void MainWindow::getNextCommande(QString repet)
{
pile->setMessage("");
QString source ;
if (repet.isEmpty())
source =commande->text() ;
else
source = repet ;
QString c ;
QTextStream s (&source) ;
s>> c ;
while (c != "") {
if(c.startsWith('\'')) {//regarde s'il y a une quote
source = s.readAll() ; //on prend tout ce qu'il y a dans la textStream
source.push_front(c); //on remet c
c = getExp(source) ;
if (!c.isEmpty()){ //si c contient qqch
controleur->commande(c) ; //on la traite
commande->clear() ;
controleur->sauvegarde();
refresh() ;
source.remove(c) ; //on l'enlève de source
getNextCommande(source) ; //on rappelle la fct avec l'exp en moins
return ;
}
//sinon, alors l'expression est mal écrite, et on annule toute la suite.
message->setText("exp fausse");
commande->clear() ;
return ;
}
if(c.startsWith('[')) {//regarde s'il y a [
source = s.readAll() ; //on prend tout ce qu'il y a dans la textStream
source.push_front(c); //on remet c
c = getProg(source) ;
if (!c.isEmpty()){ //si c contient qqch
controleur->commande(c) ; //on la traite
commande->clear() ;
controleur->sauvegarde();
refresh() ;
source.remove(c) ; //on l'enlève de source
getNextCommande(source) ; //on rappelle la fct avec l'exp en moins
return ;
}
//sinon, c'est faux, on annule la suite
message->setText("prog faux");
commande->clear() ;
return ;
}
controleur->commande(c) ;
commande->clear() ;
s>>c ;
}
controleur->sauvegarde();
refresh() ;
controleur->erreur(false) ;
}
void MainWindow::pile_create(){
QStringList liste;
vuePile->setRowCount(pile->getNbItemsToAffiche()) ;
for(unsigned int i=pile->getNbItemsToAffiche() ; i>0 ; i--)
{
QString str = QString::number(i) ;
str+=" :";
liste<<str ;
}
vuePile->setVerticalHeaderLabels(liste) ;
for(unsigned int i=0 ; i<pile->getNbItemsToAffiche() ; i++)
{
vuePile->setItem(i , 0 , new QTableWidgetItem("")) ;
}
refresh();
}
void MainWindow::printText(){
getNextCommande(text_enter);
qDebug() << "printText()" << text_enter;
}
void MainWindow::open_context(){
QFile fichier("\\Users\\Adrien\\Documents\\Adrien\\UTC\\GI 02\\LO21\\Projet\\version finale\\projet_1_0\\save.txt");
if(!fichier.exists())
QMessageBox::critical(NULL,"Erreur","Le fichier de sauvegarde n'existe pas !");
else{
fichier.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream flux(&fichier);
QString ligne = "";
while(!flux.atEnd())
{
ligne = flux.readLine();
getNextCommande(ligne);
}
fichier.close();
}
}
void MainWindow::save_context(){
QFile fichier("C:\\Users\\Adrien\\Documents\\Adrien\\UTC\\GI 02\\LO21\\Projet\\version finale\\projet_1_0\\save.txt");
if(!fichier.exists())
QMessageBox::critical(NULL,"Erreur","Le fichier de sauvegarde n'existe pas !");
else{
fichier.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream flux(&fichier);
for(Item* it= (pile->stack.begin()) ; it!= (pile->stack.end()); ++it)
{
flux << it->getLiterale().toString();
flux << "\n";
}
fichier.close();
QMessageBox::critical(NULL,"Sauvegarde","La sauvegarde du contexte à bien été effectué !");
}
}