-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControleur.hpp
executable file
·283 lines (239 loc) · 8.08 KB
/
Controleur.hpp
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
/* Controleur.hpp
*
* Gestion de la communication entre VueG et Modele
*
*/
////////////////////////////////////////////////////////////////////
// IMPORTATION bibliotheques / headers //
////////////////////////////////////////////////////////////////////
#pragma once
#include <string>
#include <regex>
#include "Modele.hpp"
#include "VueG.hpp"
#include "parametres.hpp"
////////////////////////////////////////////////////////////////////
// Classe CONTROLEUR //
////////////////////////////////////////////////////////////////////
class Controleur {
private:
Modele *m;
VueG *v;
public:
Controleur(Modele *mm, VueG *vv) : m(mm), v(vv) {
v->addPrecisionListener(this);
v->addResizeListener(this);
v->addModifListener(this);
v->addRangListener(this);
v->addTransListener(this);
v->addInvListener(this);
v->addPseudoInvListener(this);
v->addAleaListener(this);
v->addDetListener(this);
v->addVpListener(this);
v->addMoyListener(this);
v->addDiscoListener(this);
v->addResetListener(this);
v->addAddListener(this);
v->addDivListener(this);
v->addRacListener(this);
v->addSquareListener(this);
v->addSetListener(this);
v->addUndoListener(this);
v->addRedoListener(this);
v->addHelpListener(this);
v->addMultListener(this);
v->addSumDiagListener(this);
v->addSumListener(this);
v->addExitListener(this);
}
////////////////////////////////////////////////////////////////////
// Boutons - Actions //
////////////////////////////////////////////////////////////////////
void on_button_resize() {
std::string xtext = v->getX(); v->setTabx(xtext);
std::string ytext = v->getY(); v->setTaby(ytext);
if (verif_int(xtext) && verif_int(ytext)) {
int x = atoi(xtext.c_str());
int y = atoi(ytext.c_str());
if (x < TAILLE_MINI || TAILLE_MAX < x || y < TAILLE_MINI || TAILLE_MAX < y) {
v->fenetre_avertissement("Attention", "Les dimensions de la matrice doivent être comprises entre " + std::to_string(TAILLE_MINI) + " et " + std::to_string(TAILLE_MAX));
}
else {
m->resizeModele(x, y);
}
}
}
void on_button_precision() {
std::string xtext = v->getX(); v->setTabx(xtext);
if (verif_int(xtext)) {
int x = atoi(xtext.c_str());
if (x < PRECISION_MINI || PRECISION_MAX < x) {
v->fenetre_avertissement("Attention", "Le nombre de chiffres après la virgule doit être compris entre " + std::to_string(PRECISION_MINI) + " et " + std::to_string(PRECISION_MAX));
}
else {
m->precisionModele(x);
}
}
}
void on_button_modif() {
Matrice *mat = new Matrice(m->get_line(), m->get_col(), 0);
for (int i=0; i < m->get_line(); i++) {
for (int j=0; j < m->get_col(); j++) {
std::string vtext = v->getEntMat(i, j);
if (verif_double_mat(vtext, i, j)) {
double vd = atof(vtext.c_str());
mat->set(i, j, vd);
}
}
}
m->modifModele(mat);
}
void on_button_det() {
m->detModele();
}
void on_button_alea() {
std::string xtext = v->getX(); v->setTabx(xtext);
std::string ytext = v->getY(); v->setTaby(ytext);
if (verif_int(xtext) && verif_int(ytext)) {
int x = atoi(xtext.c_str());
int y = atoi(ytext.c_str());
if (x <= y) {
m->aleaModele(x, y);
}
else {
v->fenetre_avertissement("Attention", "Il faut que y soit plus grand que x pour pouvoir tirer un entier entre x et y");
}
}
}
void on_button_rang() {
m->rangModele();
}
void on_button_trans() {
m->transModele();
}
void on_button_inv() {
m->invModele();
}
void on_button_pseudoinv() {
m->pseudoinvModele();
}
void on_button_reset() {
m->resetModele();
}
void on_button_add() {
std::string vtext = v->getEntry(); v->setEntry(vtext);
if (verif_double(vtext)) {
double vd = atof(vtext.c_str());
m->addModele(vd);
}
}
void on_button_div() {
std::string vtext = v->getEntry(); v->setEntry(vtext);
if (verif_double(vtext)) {
double vd = atof(vtext.c_str());
if (vd != 0) {
m->divModele(vd);
}
else {
v->fenetre_avertissement("Attention", "Division par zéro impossible");
}
}
}
void on_button_square() {
m->squareModele();
}
void on_button_rac() {
m->racModele();
}
void on_button_set() {
std::string vtext = v->getEntry(); v->setEntry(vtext);
std::string xtext = v->getX(); v->setTabx(xtext);
std::string ytext = v->getY(); v->setTaby(ytext);
if (verif_double(vtext) && verif_int(xtext) && verif_int(ytext)) {
double vd = atof(vtext.c_str());
int x = atoi(xtext.c_str());
int y = atoi(ytext.c_str());
m->setModele(vd, x, y);
}
}
void on_button_undo() {
m->undoModele();
}
void on_button_redo() {
m->redoModele();
}
void on_button_help() {
v->on_button_help_fct();
}
void on_button_mult() {
std::string vtext = v->getEntry(); v->setEntry(vtext);
if (verif_double(vtext)) {
double vd = atof(vtext.c_str());
m->multModele(vd);
}
}
void on_button_sumdiag() {
m->sumdiagModele();
}
void on_button_sum() {
m->sumModele();
}
void on_button_moy() {
m->moyModele();
}
void on_button_disco() {
switch(m->getDisco()) {
case 0 : v->change_color_disco("#6CB7E0", "#2A8FC6"); m->incrDisco(); break;
case 1 : v->change_color_disco("#1A587A", "#2A8FC6"); m->incrDisco(); break;
case 2 : v->change_color_disco("#6CB7E0", "#1A587A"); m->incrDisco(); break;
case 3 : v->change_color_disco("#FFFFFF", "#FFFFFF"); m->rstDisco(); break;
}
}
void on_button_vp() {
m->vpModele();
}
void on_button_exit() {
v->on_button_close();
}
////////////////////////////////////////////////////////////////////
// Verification des entrées //
////////////////////////////////////////////////////////////////////
// fonctions de vérifications pour les nombres entiers et doubles sous forme de string
bool verif_int(std::string str) {
// test si entier
if (!(std::regex_match(str, std::regex{"[+-]?[0-9]+"}))) {
v->fenetre_avertissement("Attention", "Veuillez entrer un entier pour x et y");
return false;
}
return true;
}
bool verif_double(std::string str) {
// test si reel
if (!(std::regex_match(str, std::regex{"[+-]?\\d*\\.?\\,?\\d+"}))) {
v->fenetre_avertissement("Attention", "Veuillez entrer un réel pour v (séparateur autorisé : virgule ou point)");
return false;
}
// test si nb chiffres après la virgule cohérent
std::string s = "[+-]?\\d*\\.?\\,?\\d{0," + std::to_string(m->getPrecisionModele()) + "}";
if (!(std::regex_match(str, std::regex{s}))) {
v->fenetre_avertissement("Attention", "Veuillez entrer un réel avec un nombre de chiffres après la virgule cohérent pour v \nprécision actuelle : " + std::to_string(m->getPrecisionModele()) + " chiffres après la virgule");
return false;
}
return true;
}
bool verif_double_mat(std::string str, const int i, const int j) {
// test si reel
if (!(std::regex_match(str, std::regex{"[+-]?\\d*\\.?\\,?\\d+"}))) {
v->fenetre_avertissement("Attention", "Veuillez entrer un réel à l'élément (" + std::to_string(i) + "," + std::to_string(j) + ") (séparateur autorisé : virgule ou point)");
return false;
}
// test si nb chiffres après la virgule cohérent
std::string s = "[+-]?\\d*\\.?\\,?\\d{0," + std::to_string(m->getPrecisionModele()) + "}";
if (!(std::regex_match(str, std::regex{s}))) {
v->fenetre_avertissement("Attention", "Veuillez entrer un réel avec un nombre de chiffres après la virgule cohérent pour l'élément (" + std::to_string(i) + "," + std::to_string(j) + ") \nprécision actuelle : " + std::to_string(m->getPrecisionModele()) + " chiffres après la virgule");
return false;
}
return true;
}
};