autocell.cpp 13.1 KB
Newer Older
1
2
3
4
5
#include <autocell.h>
#include <reseau_cellule_etats.h>
#include <autosql.h>
#include <Automate.h>
#include <parametragemodele.h>
Victor Blanchet's avatar
Victor Blanchet committed
6
7
8

#include <QApplication>
#include <QPushButton>
Victor Blanchet's avatar
Victor Blanchet committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QComboBox>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QHeaderView>
#include <QLayoutItem>
#include <QFont>
#include <QPushButton>
#include <QCheckBox>
#include <QFormLayout>
#include <QObject>
#include <string>
#include <QBrush>
26
#include <QWindow>
27
#include <QMessageBox>
Victor Blanchet's avatar
Victor Blanchet committed
28
29
30
31
32
33
34

#include <array>
#include <iostream>
#include <iomanip>

using namespace std;

35
36
std::unique_ptr<AutoCell> AutoCell::instance = nullptr;

Victor Blanchet's avatar
Victor Blanchet committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
AutoCell::AutoCell(QWidget* parent):QWidget(parent)
{
    this->setWindowTitle("Automate cellulaire");
    this->setMinimumSize(1150, 650);

    //division en frame de la fenêtre de l'application

    general = new QGridLayout;

    this->setLayout(general);

    //définition de la frame choix du modèle

    win_model_choice = new QWidget;
    win_model_choice->setStyleSheet("background-color: rgb(204, 209, 209)");
    win_model_choice->setFixedSize(300, 100);

    lab_model_choice = new QLabel("Choisir un modèle dans la liste ou \nen paramétrer un nouveau :", win_model_choice);

    button_add_model = new QPushButton("Ajouter un modèle");
Boris Cazic's avatar
Boris Cazic committed
57
    button_add_model->setToolTip("Configurer un nouveau modèle");
Victor Blanchet's avatar
Victor Blanchet committed
58
    button_add_model->setStyleSheet("background-color : rgb(251, 252, 252 )");
Boris Cazic's avatar
Boris Cazic committed
59
    button_add_model->setFixedWidth(140);
Boris Cazic's avatar
Boris Cazic committed
60

61
    connect(button_add_model, SIGNAL(clicked()), this, SLOT(defNouveauModele()));
Boris Cazic's avatar
Boris Cazic committed
62

Boris Cazic's avatar
Boris Cazic committed
63
64
    liste = new QComboBox(win_model_choice);
    liste->setPlaceholderText("--- select ---");
65

Boris Cazic's avatar
Boris Cazic committed
66
    // récupération des automates en mémoire
Victor Blanchet's avatar
Victor Blanchet committed
67

Leon Do Castelo's avatar
Leon Do Castelo committed
68
69
70
71
72
    std::vector<QString> noms_modeles = Database::getInstance().getAutomates();
    for(size_t i = 0 ; i<noms_modeles.size() ; i++){
        std::cout<<"modele :"<<noms_modeles[i].toStdString()<<std::endl;
        liste->addItem(noms_modeles[i]);
    }
Boris Cazic's avatar
Boris Cazic committed
73

Victor Blanchet's avatar
Victor Blanchet committed
74
    liste->setStyleSheet("background-color : rgb(251, 252, 252 )");
75
    liste->setFixedWidth(140);
Victor Blanchet's avatar
Victor Blanchet committed
76
77

    connect(liste,SIGNAL(currentIndexChanged(int)),this,SLOT(RAZ()));
Leon Do Castelo's avatar
Leon Do Castelo committed
78
    connect(liste,SIGNAL(currentIndexChanged(int)),this,SLOT(chargerGrilles()));
79
    connect(liste,SIGNAL(currentTextChanged(const QString&)),this,SLOT(initAutomate(const QString&)));
Boris Cazic's avatar
Boris Cazic committed
80

Victor Blanchet's avatar
Victor Blanchet committed
81
82
83

    grid_model_choice = new QGridLayout(win_model_choice);
    grid_model_choice->addWidget(lab_model_choice, 0, 0, 1, 2);
Victor Blanchet's avatar
Victor Blanchet committed
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
    grid_model_choice->addWidget(liste, 1,0,1,1);
    grid_model_choice->addWidget(button_add_model,1,1);

    general->addWidget(win_model_choice,0,0);

    //définition de la frame initialisation

    win_init = new QWidget;
    win_init->setFixedWidth(400);
    lab_init = new QLabel("Configuration du modèle :");

    form_init = new QGridLayout(win_init);

    form_saved_grids = new QFormLayout;
    form_config = new QFormLayout;

    form_init->addLayout(form_saved_grids, 1,1);
    form_init->addLayout(form_config, 1,0);

    list_grids = new QComboBox;
    check_load_grid = new QCheckBox;

    edit_largeur = new QLineEdit;
    edit_largeur->setFixedWidth(30);
    edit_hauteur= new QLineEdit;
    edit_hauteur->setFixedWidth(30);
    check_aleatoire = new QCheckBox;
    button_valide_init = new QPushButton("Valider");
    button_valide_init->setFixedWidth(50);

    connect(button_valide_init,SIGNAL(clicked()),this,SLOT(initialiserGrille()));

    form_saved_grids->addRow(tr("Importer une grille"), check_load_grid);
    form_saved_grids->addRow(tr("Grilles enregistrées"), list_grids);
    form_saved_grids->addWidget(button_valide_init);

    form_config->addRow(tr("Largeur :"), edit_largeur);
    form_config->addRow(tr("Hauteur :"), edit_hauteur);
    form_config->addRow(tr("Remplissage aléatoire"), check_aleatoire);

    form_init->addWidget(lab_init,0,0,1,3);

    general->addWidget(win_init,0,1,1,2);

    //définition de la frame "panneau de contrôle de l'exécution"

    win_run_ctrl = new QWidget;
    win_run_ctrl->setFixedWidth(300);
    lab_run_crtl = new QLabel("Outils de contrôle :");
    win_run_ctrl->setStyleSheet("background-color: rgb(174, 182, 191)");


    grid_run_control = new QGridLayout(win_run_ctrl);
137
138
139

    grid_run_control->setColumnMinimumWidth(1,100);
    grid_run_control->setColumnMinimumWidth(2,100);
Boris Cazic's avatar
Boris Cazic committed
140
141
142
143
    grid_run_control->setRowMinimumHeight(3,80);
    grid_run_control->setRowMinimumHeight(4,400);

    grid_run_control->addWidget(lab_run_crtl, 0,0,1,3,Qt::AlignTop);
144
145
146
147
148

    lab_time_step = new QLabel("Pas de temps : ");
    edit_time_step = new QLineEdit;
    edit_time_step->setStyleSheet("background-color: rgb(255,255,255)");
    edit_time_step->setFixedWidth(30);
Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
149
    edit_time_step->setText("1000"); //valeur par défaut pour le pas de temps
Boris Cazic's avatar
Boris Cazic committed
150
151
152
    button_valider_delai = new QPushButton("valider");
    button_valider_delai->setStyleSheet("background-color: rgb(255,255,255)");
    button_valider_delai->setFixedWidth(50);
Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
153
    connect(button_valider_delai, SIGNAL(clicked()), this, SLOT(changeDelai()));
154
155
156
    button_prev = new QPushButton("<<");
    button_prev->setStyleSheet("background-color: rgb(255,255,255)");
    button_prev->setFixedSize(40,40);
Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
157
    connect(button_prev, SIGNAL(clicked()), this, SLOT(previous()));
158
159
160
    button_run = new QPushButton("RUN");
    button_run->setFixedSize(40,40);
    button_run->setStyleSheet("background-color: rgb(255,255,255)");
Boris Cazic's avatar
Boris Cazic committed
161
    connect(button_run, SIGNAL(clicked()), this, SLOT(gererSimulation()));
162
163
164
    button_next = new QPushButton(">>");
    button_next->setFixedSize(40,40);
    button_next->setStyleSheet("background-color: rgb(255,255,255)");
Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
165
    connect(button_next, SIGNAL(clicked()), this, SLOT(next()));
Boris Cazic's avatar
Boris Cazic committed
166
167
168
    button_reinitialiser = new QPushButton("Réinitialiser la simulation");
    button_reinitialiser->setStyleSheet("background-color: rgb(255,255,255)");
    button_reinitialiser->setFixedWidth(200);
169
170
171

    grid_run_control->addWidget(lab_time_step, 1, 0);
    grid_run_control->addWidget(edit_time_step, 1, 1);
Boris Cazic's avatar
Boris Cazic committed
172
    grid_run_control->addWidget(button_valider_delai, 1, 2);
173
174
175
    grid_run_control->addWidget(button_prev, 2, 0);
    grid_run_control->addWidget(button_run, 2, 1);
    grid_run_control->addWidget(button_next, 2, 2);
Boris Cazic's avatar
Boris Cazic committed
176
    grid_run_control->addWidget(button_reinitialiser, 3, 0, Qt::AlignCenter);
177

178
179
180
181
182
183
184
185
186
    lab_sauv_grille = new QLabel("Sauvegarder la grille courante");
    edit_nom_grille = new QLineEdit;
    edit_nom_grille->setPlaceholderText("Entrer un nom");
    edit_nom_grille->setStyleSheet("background-color: rgb(255,255,255)");

    edit_nom_grille->setFixedWidth(180);

    button_save_grid = new QPushButton("sauvegarder");
    button_save_grid->setFixedWidth(90);
Leon Do Castelo's avatar
Leon Do Castelo committed
187
    connect(button_save_grid,SIGNAL(clicked()),this,SLOT(sauvegarderGrille()));
188

189
190
191
    grid_run_control->addWidget(lab_sauv_grille,4,0,1,3, Qt::AlignBottom);
    grid_run_control->addWidget(edit_nom_grille,5,0, Qt::AlignBottom);
    grid_run_control->addWidget(button_save_grid,5,2,Qt::AlignBottom);
192

Victor Blanchet's avatar
Victor Blanchet committed
193
194
    general->addWidget(win_run_ctrl,1,0,2,1);

Victor Blanchet's avatar
Victor Blanchet committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    //définition de la frame affichage de la grille

    win_grid = new QWidget;

    grid = new QTableWidget(0,0,win_grid);

    //notice

    win_notice = new QWidget;
    win_notice->setStyleSheet("background-color: rgb(204, 209, 209)");
    win_notice->setMaximumWidth(300);
    win_notice->setMinimumWidth(200);
    lab_notice = new QLabel("Notice :", win_notice);

    general->addWidget(win_notice,0,2,3,1);
};

void AutoCell::afficherGrille(Reseau* Grille)
{
    win_grid = new QWidget;

    unsigned int l = Grille->getLargeur();
    unsigned int h = Grille->getHauteur();

Victor Blanchet's avatar
Victor Blanchet committed
219
220
    delete grid;
    grid = new QTableWidget(h,l,win_grid);
Victor Blanchet's avatar
Victor Blanchet committed
221

Boris Cazic's avatar
Boris Cazic committed
222
223
    int dim_pixels_h=40; //Taille en pixel.
    int dim_pixels_v=40;
Victor Blanchet's avatar
Victor Blanchet committed
224

Victor Blanchet's avatar
Victor Blanchet committed
225
    grid->setFixedSize((l)*dim_pixels_h, h*dim_pixels_v);
Victor Blanchet's avatar
Victor Blanchet committed
226

Victor Blanchet's avatar
Victor Blanchet committed
227
228
    grid->horizontalHeader()->setVisible(false); //Pas de nom pour les colonnes.
    grid->verticalHeader()->setVisible(false);
Victor Blanchet's avatar
Victor Blanchet committed
229
230
231
    grid->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //Pas de barre pour scroller.
    grid->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

Boris Cazic's avatar
Boris Cazic committed
232
233
    grid->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);

Victor Blanchet's avatar
Victor Blanchet committed
234
235
236
237
238
239
    QFont serifFont("Times", 8, QFont::DemiBold);

    for(unsigned int i=0; i<h; i++){
            for(unsigned int j=0; j<l; j++)
        {
            //vérifier si les cellules ont ou non été générés (!= nullptr)
Victor Blanchet's avatar
Victor Blanchet committed
240
            QString indice; indice.setNum(Grille->getReseau()[i][j].getIndEtat());
Victor Blanchet's avatar
Victor Blanchet committed
241

242
            QString label; label = QString::fromStdString(Automate::getInstance().getEnsemble().getEtat(indice.toInt()).getLabel()); //label de la cellule
Victor Blanchet's avatar
Victor Blanchet committed
243

Victor Blanchet's avatar
Victor Blanchet committed
244
245
            grid->setItem(i, j, new QTableWidgetItem(indice));
            grid->item(i,j)->setFlags(Qt::ItemIsEnabled);
Victor Blanchet's avatar
Victor Blanchet committed
246

Victor Blanchet's avatar
Victor Blanchet committed
247
            //afficherCellule
Victor Blanchet's avatar
Victor Blanchet committed
248

249
            QColor color; color = Automate::getInstance().getEnsemble().getEtat(indice.toInt()).getColor();
Victor Blanchet's avatar
Victor Blanchet committed
250
251
252
253

            QBrush brush_color;
            brush_color.setColor(color);

Victor Blanchet's avatar
Victor Blanchet committed
254
            grid->item(i,j)->setBackground(brush_color.color());
Victor Blanchet's avatar
Victor Blanchet committed
255
256
            grid->item(i,j)->setForeground(Qt::black);
            grid->item(i,j)->setFont(serifFont);
Boris Cazic's avatar
Boris Cazic committed
257
            grid->setColumnWidth(j,40);
Victor Blanchet's avatar
Victor Blanchet committed
258
        }
Boris Cazic's avatar
Boris Cazic committed
259
    grid->setRowHeight(i,40);
Victor Blanchet's avatar
Victor Blanchet committed
260
    }
Victor Blanchet's avatar
Victor Blanchet committed
261
    connect(grid,SIGNAL(clicked(const QModelIndex&)),this,SLOT(modifierCellule(const QModelIndex&)));
Victor Blanchet's avatar
Victor Blanchet committed
262
    general->addWidget(win_grid,1,1,2,2);
Victor Blanchet's avatar
Victor Blanchet committed
263
264
265
266
};


void AutoCell::initialiserGrille(){
Victor Blanchet's avatar
Victor Blanchet committed
267
    bool ok;
Victor Blanchet's avatar
Victor Blanchet committed
268
269
270
271
272

    int l = (edit_largeur->text()).toInt(&ok, 10);
    int h = (edit_hauteur->text()).toInt(&ok, 10);

    QString str_l; str_l.setNum(l);
Victor Blanchet's avatar
Victor Blanchet committed
273
274
    QString str_h; str_h.setNum(h);

Leon Do Castelo's avatar
Leon Do Castelo committed
275
    if((0>=h || h>21 || 0>=l || l>41) && !check_load_grid->isChecked()){
276
277
278
279
280
281
282
283
284
        QString msg("erreur dimensions");
        afficherErreur(msg);
        edit_largeur->setText("");
        edit_hauteur->setText("");
        return;
    }

    delete Grille;

Victor Blanchet's avatar
Victor Blanchet committed
285
    this->Grille = new Reseau(h,l);
Victor Blanchet's avatar
Victor Blanchet committed
286

Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
287
288
    if (check_aleatoire->isChecked())
	    Grille->setAleatoire();
Victor Blanchet's avatar
Victor Blanchet committed
289

Boris Cazic's avatar
Boris Cazic committed
290
    QString nom_grille = list_grids->currentText();
Leon Do Castelo's avatar
Leon Do Castelo committed
291
292

    if (check_load_grid->isChecked()) {
Boris Cazic's avatar
Boris Cazic committed
293
      delete Grille;
Leon Do Castelo's avatar
Leon Do Castelo committed
294
      Grille = new Reseau(Database::getInstance().getReseau(listeGrille[list_grids->currentIndex()*2].toInt())); /// Méthode à implémenter
Boris Cazic's avatar
Boris Cazic committed
295
      QString str_largeur;
Leon Do Castelo's avatar
Leon Do Castelo committed
296
      //str_largeur.setNum(listeGrille[list_grids->currentIndex()*2].toInt());
Boris Cazic's avatar
Boris Cazic committed
297
      QString str_hauteur;
Leon Do Castelo's avatar
Leon Do Castelo committed
298
      //edit_largeur->setText(str_largeur);
Leon Do Castelo's avatar
Leon Do Castelo committed
299
300
301
      edit_largeur->setText(str_largeur.setNum(Grille->getLargeur(),10));
      edit_hauteur->setText(str_hauteur.setNum(Grille->getHauteur(),10));
      }
Victor Blanchet's avatar
Victor Blanchet committed
302

Boris Cazic's avatar
Boris Cazic committed
303
    //réinitialiser l'automate
Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
304
    Automate::getInstance().reset();
Victor Blanchet's avatar
Victor Blanchet committed
305

Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
306
    this->afficherGrille(this->Grille);
Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
307
308
    Automate::getInstance().setReseauInit(*Grille);
    Automate::getInstance().initialiserBuffer();
Victor Blanchet's avatar
Victor Blanchet committed
309

Boris Cazic's avatar
Boris Cazic committed
310
};
Victor Blanchet's avatar
Victor Blanchet committed
311
312
313
void AutoCell::RAZ(){
    delete grid;
    grid = new QTableWidget(0,0,win_grid);
Victor Blanchet's avatar
Victor Blanchet committed
314
315
    edit_largeur->setText("");
    edit_hauteur->setText("");
Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
316
    edit_time_step->setText("1000");
Boris Cazic's avatar
Boris Cazic committed
317
318
    check_aleatoire->setCheckState(Qt::Unchecked);
    check_load_grid->setCheckState(Qt::Unchecked);
319
;}
Victor Blanchet's avatar
Victor Blanchet committed
320

Boris Cazic's avatar
Boris Cazic committed
321
322
323
324
325
void AutoCell::defNouveauModele() {
    NouveauModele* nouveaumodele = new NouveauModele;
    nouveaumodele->show();
}

Victor Blanchet's avatar
Victor Blanchet committed
326
327
328
329
330
331
332
333
334
335
void AutoCell::modifierCellule(const QModelIndex& index) {
    unsigned int i = index.row();
    unsigned int j = index.column();

    Grille->getReseau()[i][j].incrementerEtat();

    unsigned int indice = (Grille->getReseau()[i][j].getIndEtat());

    QString str_indice ; str_indice.setNum(indice, 10);

336
    QColor color; color = Automate::getInstance().getEnsemble().getEtat(indice).getColor();
Victor Blanchet's avatar
Victor Blanchet committed
337
338
339
340
341
342
343

    QBrush brush_color;
    brush_color.setColor(color);

    grid->item(i,j)->setBackground(brush_color.color());
    grid->item(i,j)->setText(str_indice);

Boris Cazic's avatar
Boris Cazic committed
344
345
    //réitnitialiser l'automate ou modif du buffer d
}
Victor Blanchet's avatar
Victor Blanchet committed
346

347
void AutoCell::sauvegarderGrille(){
348
349
350
351
352
353
354
355
356
357
     if(edit_nom_grille->text()==""){
        QString msg("Entrer un nom");
        afficherErreur(msg);
        return;
    }
    QString nom_grille;
    nom_grille = edit_nom_grille->text();
    QString nom_automate;
    nom_automate = liste->currentText();
    Database::getInstance().stockerReseau(*Grille, nom_grille, nom_automate);
358
359
};

Leon Do Castelo's avatar
Leon Do Castelo committed
360
void AutoCell::chargerGrilles(){
Leon Do Castelo's avatar
Leon Do Castelo committed
361
    listeGrille.clear();
Leon Do Castelo's avatar
Leon Do Castelo committed
362
363
364
365
    QString text;
    QString nb;
    text = liste->currentText();

Boris Cazic's avatar
Boris Cazic committed
366
    list_grids->clear();
Leon Do Castelo's avatar
Leon Do Castelo committed
367
368
369
    vector<QString> noms = Database::getInstance().getListeReseaux(text);
    nb.setNum(noms.size());

Leon Do Castelo's avatar
Leon Do Castelo committed
370
    for(size_t i=1 ; i<noms.size() ; i = i + 2)
Leon Do Castelo's avatar
Leon Do Castelo committed
371
        list_grids->addItem(noms[i]);
Leon Do Castelo's avatar
Leon Do Castelo committed
372
    listeGrille = noms;
Boris Cazic's avatar
Boris Cazic committed
373
374
375
376
377
378
379
380
381
382
383
}

void AutoCell::gererSimulation(){
    if(button_run->text() == "RUN"){
        button_run->setText("STOP");
        //Automate::getInstance().start();
    }
    else {
        button_run->setText("RUN");
        //Automate::getInstance().pause();
    }
Victor Blanchet's avatar
Victor Blanchet committed
384
}
385

386
387
388
389
390
void AutoCell::afficherErreur(QString& msg){
    QMessageBox messageBox;
    messageBox.critical(0,"Error",msg);
    messageBox.setFixedSize(500,200);
}
391
392
393
394
395

void AutoCell::initAutomate(const QString& name) {
	std::cout << name.toStdString() << std::endl;
	Database::getInstance().initSingletonAutomate(name);
}
Romain De Laage De Bellefaye's avatar
Romain De Laage De Bellefaye committed
396
397
398
399
400
401
402
403
404
405
406
407
408

void AutoCell::changeDelai() {
	// @todo : implémenter un message d'erreur
	Automate::getInstance().setDelai(static_cast<unsigned int>(edit_time_step->text().toInt()));
}

void AutoCell::next() {
	Automate::getInstance().step();
}

void AutoCell::previous() {
	Automate::getInstance().previous();
}