-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfenprincipale.cpp
300 lines (253 loc) · 11.2 KB
/
fenprincipale.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
#include <fenprincipale.h>
#include <fencodegenere.h>
FenPrincipale::FenPrincipale()
// Constructeur de la fenetre principale
{
// Création des layouts et des widgets
// Groupe : Définition de la classe
// initialisation des attributs
nom = new QLineEdit;
classeMere = new QLineEdit;
attributs = new QListWidget;
// contrôle de l'input de l'utilisateur
// max 20 characters for the input
// and ASCII alphanumeric character required
QString inputMaskClass(20, *QString("N").data());
nom->setInputMask(inputMaskClass);
classeMere->setInputMask(inputMaskClass);
attributs->setInputMask(inputMaskClass);
// suite de l'initialisation des attributs
QLabel *labelAttributs = new QLabel("Attributs");
ajouterAttributBtn = new QPushButton("&Ajouter");
supprimerAttributBtn = new QPushButton("&Supprimer");
QHBoxLayout *attributsLayout = new QHBoxLayout;
attributsLayout->addWidget(labelAttributs);
attributsLayout->addWidget(attributs);
QHBoxLayout *boutonsAttributsLayout = new QHBoxLayout;
boutonsAttributsLayout->addWidget(ajouterAttributBtn);
boutonsAttributsLayout->addWidget(supprimerAttributBtn);
QFormLayout *formLayout = new QFormLayout;
formLayout->addRow("&Nom", nom);
formLayout->addRow("Classe &mère", classeMere);
formLayout->addItem(attributsLayout);
formLayout->addItem(boutonsAttributsLayout);
QGroupBox *groupDefinition = new QGroupBox("Définition de la classe");
groupDefinition->setLayout(formLayout);
// Groupe : options
protections = new QCheckBox("Protéger le header contre les inclusions multiples");
protections->setChecked(true);
headerGuard = new QLineEdit; // headerGuard sert à afficher en temps réel le header guard
genererConstructeur = new QCheckBox("Générer un &constructeur par défaut.");
genererDestructeur = new QCheckBox("Générer un &destructeur.");
genererAccesseurs = new QCheckBox("Générer les &accesseurs.");
QHBoxLayout *headerLayout = new QHBoxLayout; // layout contenant les widgets relatifs au header guard
headerLayout->addWidget(protections);
headerLayout->addWidget(headerGuard);
QVBoxLayout *optionsLayout = new QVBoxLayout;
optionsLayout->addLayout(headerLayout);
optionsLayout->addWidget(genererConstructeur);
optionsLayout->addWidget(genererDestructeur);
optionsLayout->addWidget(genererAccesseurs);
QGroupBox *groupOptions = new QGroupBox;
groupOptions->setLayout(optionsLayout);
// Groupe : commentaires
auteur = new QLineEdit;
date = new QDateEdit;
date->setDate(QDate::currentDate());
role = new QTextEdit;
QFormLayout *commentairesLayout = new QFormLayout;
commentairesLayout->addRow("&Auteur", auteur);
commentairesLayout->addRow("&Date de création", date);
commentairesLayout->addRow("&Role de la classe", role);
groupCommentaires = new QGroupBox("Ajouter des commentaires");
groupCommentaires->setCheckable(true);
groupCommentaires->setChecked(false);
groupCommentaires->setLayout(commentairesLayout);
// Groupe : boutons du bas (générer, quitter)
generer = new QPushButton("&Générer !");
quitter = new QPushButton("&Quitter");
QHBoxLayout *boutonsLayout = new QHBoxLayout;
boutonsLayout->setAlignment(Qt::AlignRight);
boutonsLayout->addWidget(generer);
boutonsLayout->addWidget(quitter);
// Définition du layout principal, du titre de la fenêtre, etc.
QVBoxLayout *layoutPrincipal = new QVBoxLayout;
layoutPrincipal->addWidget(groupDefinition);
layoutPrincipal->addWidget(groupOptions);
layoutPrincipal->addWidget(groupCommentaires);
layoutPrincipal->addLayout(boutonsLayout);
setLayout(layoutPrincipal);
setWindowTitle("Zero Class Generator");
setWindowIcon(QIcon("icone.png"));
resize(600, 600);
// Connexions des signaux et des slots
connect(quitter, SIGNAL(clicked()), qApp, SLOT(quit())); // bouton quitter de la fenêtre principale
connect(protections, SIGNAL(toggled(bool)), headerGuard, SLOT(setEnabled(bool))); // connexion entre la QCheckBox de protections et le QLineEdit headerGuard
connect(nom, SIGNAL(textChanged(QString)), this, SLOT(ecrireHeaderGuard(QString))); // slot personnalisé pour afficher le bon header guard
connect(generer, SIGNAL(clicked()), this, SLOT(genererCode())); // bouton générer pour afficher le code généré
connect(ajouterAttributBtn, SIGNAL(clicked()), this, SLOT(addAttributInList())); // ajouter un attribut
connect(supprimerAttributBtn, SIGNAL(clicked()), this, SLOT(deleteAttributInList())); // supprimer un attribut
}
void FenPrincipale::ecrireHeaderGuard(QString nom)
{
headerGuard->setText(QString("HEADER_" + nom.toUpper()));
}
void FenPrincipale::addAttributInList()
// slot personnalisé pour que l'utilisateur ajoute un attribut à la liste d'attributs
{
QString attributText = QInputDialog::getText(this, tr("Nouvel attribut"),
tr("Entrez ici votre nouvel attribut:"));
QString attributType = QInputDialog::getText(this, tr("Type de l'attribut"),
tr("Entrez ici le type du nouvel attribut:"));
attributs->addItem(attributType + " " + attributText);
}
void FenPrincipale::deleteAttributInList()
{
attributs->takeItem(attributs->currentRow());
}
QString FenPrincipale::genererCodeHeader()
// génère une QString contenant le code du .h
{
QString const indentation(" ");
// On vérifie que le nom de la classe n'est pas vide, sinon on arrête
if (nom->text().isEmpty())
{
QMessageBox::critical(this, "Erreur", "Veuillez entrer au moins un nom de classe");
return "";
}
// Si tout va bien, on génère le code
QString code;
// Génération du code à l'aide des informations rentrées par l'utilisateur
// bloc des commentaires
if (groupCommentaires->isChecked())
{
code.append("/*\n");
code.append("Auteur : " + auteur->text()+"\n");
code.append("Date de création : " + date->date().toString() + "\n");
code.append("Rôle :\n" + role->toPlainText() + "\n");
code.append("*/\n\n");
}
// bloc des protections contre les inclusions multiples
if (protections->isChecked())
{
code.append("#ifndef" + headerGuard->text() +"\n");
code.append("#define" + headerGuard->text() +"\n");
code.append("\n");
}
// bloc relatif à la classe
code.append("class " + nom->text());
if (!classeMere->text().isEmpty())
{
code.append(" : public " + classeMere->text() + "\n");
}
QString texteConstructeur = QString("");
QString texteDestructeur = QString("");
QString texteAccesseurs = QString("");
if (genererConstructeur->isChecked())
{
texteConstructeur = QString(nom->text()+"();");
}
if (genererDestructeur->isChecked())
{
texteDestructeur = QString("~"+nom->text()+"();");
}
if (genererAccesseurs->isChecked())
{
// si la liste des attributs est vide on affiche une erreur
if (attributs->count()==0)
{
QMessageBox::critical(this, "Erreur", "Veuillez entrer au moins un attribut.");
return "";
}
// sinon
for (int compteur(0) ; compteur < attributs->count() ; compteur++)
{
QString typeAttribut(attributs->item(compteur)->text().split(" ").at(0));
QString nomAttribut(attributs->item(compteur)->text().split(" ").at(1));
QStringRef nomAttribut2(&nomAttribut, 1, nomAttribut.count()-1);
texteAccesseurs.append(indentation + typeAttribut + " " + "get"
+ nomAttribut.at(0).toUpper() + nomAttribut2 + "();\n");
}
}
code.append("{\n");
code.append("public:\n");
code.append(indentation + texteConstructeur + "\n");
code.append(indentation + texteDestructeur + "\n");
code.append(texteAccesseurs);
code.append("\n");
code.append("protected:\n");
code.append("\n");
code.append("private:\n");
// ajout des attributs de la liste d'attributs dans le code généré
for (int compteur(0) ; compteur < attributs->count() ; compteur++)
{
QString typeAttribut(attributs->item(compteur)->text().split(" ").at(0));
QString nomAttribut(attributs->item(compteur)->text().split(" ").at(1));
code.append(indentation + typeAttribut + " " + "*" + nomAttribut + ";\n");
}
code.append("\n");
code.append("};");
code.append("\n\n");
if (protections->isChecked())
{
code.append("#endif\n");
}
return code;
}
QString FenPrincipale::genererCodeCpp()
// génère une QString contenant le code du .cpp
{
QString const indentation(" ");
QString code;
// Génération du code à l'aide des informations rentrées par l'utilisateur
// bloc des commentaires
if (groupCommentaires->isChecked())
{
code.append("/*\n");
code.append("Auteur : " + auteur->text()+"\n");
code.append("Date de création : " + date->date().toString() + "\n");
code.append("Rôle :\n" + role->toPlainText() + "\n");
code.append("*/\n\n");
}
// bloc d'inclusion du header
code.append("#include<"+nom->text()+".h>\n\n");
// bloc du constructeur
code.append(nom->text() + "::" + nom->text() + " : public " + classeMere->text() + "\n{");
code.append("\n");
// ajout des attributs de la liste d'attributs dans le corps du constructeur
for (int compteur(0) ; compteur < attributs->count() ; compteur++)
{
QString typeAttribut(attributs->item(compteur)->text().split(" ").at(0));
QString nomAttribut(attributs->item(compteur)->text().split(" ").at(1));
code.append(indentation + nomAttribut + " = " + "new " + typeAttribut + ";\n");
}
code.append("};");
code.append("\n\n");
// bloc des getter functions
if (genererAccesseurs->isChecked())
{
QString texteAccesseursCpp(QString(""));
for (int compteur(0) ; compteur < attributs->count() ; compteur++)
{
QString typeAttribut(attributs->item(compteur)->text().split(" ").at(0));
QString nomAttribut(attributs->item(compteur)->text().split(" ").at(1));
QStringRef nomAttribut2(&nomAttribut, 1, nomAttribut.count()-1);
texteAccesseursCpp.append(typeAttribut + " " + nom->text() + "::" + "get"
+ nomAttribut.at(0).toUpper() + nomAttribut2 + "()\n");
texteAccesseursCpp.append("{\n");
texteAccesseursCpp.append(indentation + "return " + nomAttribut + ";");
texteAccesseursCpp.append("\n}\n");
}
code.append(texteAccesseursCpp);
}
return code;
}
void FenPrincipale::genererCode()
{
// On appelle la fonction génèrant le code du header
QString codeHeader = genererCodeHeader();
QString codeCpp = genererCodeCpp();
// On crée puis affiche la fenêtre qui affichera le code généré, qu'on lui envoie en paramètre
FenCodeGenere *fenetreCode = new FenCodeGenere(codeHeader, codeCpp, this);
fenetreCode->show();
}