autocell.cpp 12.3 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()));
Boris Cazic's avatar
Boris Cazic committed
79

Victor Blanchet's avatar
Victor Blanchet committed
80
81
82

    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
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
    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);
136
137
138

    grid_run_control->setColumnMinimumWidth(1,100);
    grid_run_control->setColumnMinimumWidth(2,100);
Boris Cazic's avatar
Boris Cazic committed
139
140
141
142
    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);
143
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);
    edit_time_step->setText("1"); //valeur par défaut pour le pas de temps
Boris Cazic's avatar
Boris Cazic committed
149
150
151
152
    button_valider_delai = new QPushButton("valider");
    button_valider_delai->setStyleSheet("background-color: rgb(255,255,255)");
    button_valider_delai->setFixedWidth(50);
    //connect(button_valider_delai, SIGNAL(clicked()), Automate::getInstance(), SLOT(Automate::getInstance().setDelai()));
153
154
155
156
157
158
    button_prev = new QPushButton("<<");
    button_prev->setStyleSheet("background-color: rgb(255,255,255)");
    button_prev->setFixedSize(40,40);
    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
159
    connect(button_run, SIGNAL(clicked()), this, SLOT(gererSimulation()));
160
161
162
    button_next = new QPushButton(">>");
    button_next->setFixedSize(40,40);
    button_next->setStyleSheet("background-color: rgb(255,255,255)");
Boris Cazic's avatar
Boris Cazic committed
163
164
165
    button_reinitialiser = new QPushButton("Réinitialiser la simulation");
    button_reinitialiser->setStyleSheet("background-color: rgb(255,255,255)");
    button_reinitialiser->setFixedWidth(200);
166
167
168

    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
169
    grid_run_control->addWidget(button_valider_delai, 1, 2);
170
171
172
    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
173
    grid_run_control->addWidget(button_reinitialiser, 3, 0, Qt::AlignCenter);
174

175
176
177
178
179
180
181
182
183
    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);
184
    connect(button_save_grid,SIGNAL(clicked()),this,SLOT(sauvergarderGrille()));
185

186
187
188
    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);
189

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

Victor Blanchet's avatar
Victor Blanchet committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    //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
216
217
    delete grid;
    grid = new QTableWidget(h,l,win_grid);
Victor Blanchet's avatar
Victor Blanchet committed
218

Boris Cazic's avatar
Boris Cazic committed
219
220
    int dim_pixels_h=40; //Taille en pixel.
    int dim_pixels_v=40;
Victor Blanchet's avatar
Victor Blanchet committed
221

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

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

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

Victor Blanchet's avatar
Victor Blanchet committed
231
232
233
234
235
236
    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
237
            QString indice; indice.setNum(Grille->getReseau()[i][j].getIndEtat());
Victor Blanchet's avatar
Victor Blanchet committed
238

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

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

Victor Blanchet's avatar
Victor Blanchet committed
244
            //afficherCellule
Victor Blanchet's avatar
Victor Blanchet committed
245

246
            QColor color; color = Automate::getInstance().getEnsemble().getEtat(indice.toInt()).getColor();
Victor Blanchet's avatar
Victor Blanchet committed
247
248
249
250

            QBrush brush_color;
            brush_color.setColor(color);

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


void AutoCell::initialiserGrille(){
Victor Blanchet's avatar
Victor Blanchet committed
264
    bool ok;
Victor Blanchet's avatar
Victor Blanchet committed
265
266
267
268
269

    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
270
271
    QString str_h; str_h.setNum(h);

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

    delete Grille;

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

Victor Blanchet's avatar
Victor Blanchet committed
284
    if (check_aleatoire->isChecked()) Grille->setAleatoire();
Victor Blanchet's avatar
Victor Blanchet committed
285

Boris Cazic's avatar
Boris Cazic committed
286
    QString nom_grille = list_grids->currentText();
Leon Do Castelo's avatar
Leon Do Castelo committed
287
288

    if (check_load_grid->isChecked()) {
Boris Cazic's avatar
Boris Cazic committed
289
      delete Grille;
Leon Do Castelo's avatar
Leon Do Castelo committed
290
      Grille = new Reseau(Database::getInstance().getReseau(listeGrille[list_grids->currentIndex()*2].toInt())); /// Méthode à implémenter
Boris Cazic's avatar
Boris Cazic committed
291
      QString str_largeur;
Leon Do Castelo's avatar
Leon Do Castelo committed
292
      //str_largeur.setNum(listeGrille[list_grids->currentIndex()*2].toInt());
Boris Cazic's avatar
Boris Cazic committed
293
      QString str_hauteur;
Leon Do Castelo's avatar
Leon Do Castelo committed
294
      //edit_largeur->setText(str_largeur);
Leon Do Castelo's avatar
Leon Do Castelo committed
295
296
297
      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
298

Boris Cazic's avatar
Boris Cazic committed
299
    //réinitialiser l'automate
Victor Blanchet's avatar
Victor Blanchet committed
300

Leon Do Castelo's avatar
Leon Do Castelo committed
301
    //this->afficherGrille(this->Grille);
Victor Blanchet's avatar
Victor Blanchet committed
302

Boris Cazic's avatar
Boris Cazic committed
303
};
Victor Blanchet's avatar
Victor Blanchet committed
304
305
306
void AutoCell::RAZ(){
    delete grid;
    grid = new QTableWidget(0,0,win_grid);
Victor Blanchet's avatar
Victor Blanchet committed
307
308
    edit_largeur->setText("");
    edit_hauteur->setText("");
309
    edit_time_step->setText("1");
Boris Cazic's avatar
Boris Cazic committed
310
311
    check_aleatoire->setCheckState(Qt::Unchecked);
    check_load_grid->setCheckState(Qt::Unchecked);
312
;}
Victor Blanchet's avatar
Victor Blanchet committed
313

Boris Cazic's avatar
Boris Cazic committed
314
315
316
317
318
void AutoCell::defNouveauModele() {
    NouveauModele* nouveaumodele = new NouveauModele;
    nouveaumodele->show();
}

Victor Blanchet's avatar
Victor Blanchet committed
319
320
321
322
323
324
325
326
327
328
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);

329
    QColor color; color = Automate::getInstance().getEnsemble().getEtat(indice).getColor();
Victor Blanchet's avatar
Victor Blanchet committed
330
331
332
333
334
335
336

    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
337
338
    //réitnitialiser l'automate ou modif du buffer d
}
Victor Blanchet's avatar
Victor Blanchet committed
339

340
void AutoCell::sauvegarderGrille(){
341
342
343
344
345
346
347
348
349
350
     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);
351
352
};

Leon Do Castelo's avatar
Leon Do Castelo committed
353
void AutoCell::chargerGrilles(){
Leon Do Castelo's avatar
Leon Do Castelo committed
354
    listeGrille.clear();
Leon Do Castelo's avatar
Leon Do Castelo committed
355
356
357
358
    QString text;
    QString nb;
    text = liste->currentText();

Boris Cazic's avatar
Boris Cazic committed
359
    list_grids->clear();
Leon Do Castelo's avatar
Leon Do Castelo committed
360
361
362
    vector<QString> noms = Database::getInstance().getListeReseaux(text);
    nb.setNum(noms.size());

Leon Do Castelo's avatar
Leon Do Castelo committed
363
    for(size_t i=1 ; i<noms.size() ; i = i + 2)
Leon Do Castelo's avatar
Leon Do Castelo committed
364
        list_grids->addItem(noms[i]);
Leon Do Castelo's avatar
Leon Do Castelo committed
365
    listeGrille = noms;
Boris Cazic's avatar
Boris Cazic committed
366
367
368
369
370
371
372
373
374
375
376
}

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
377
}
378

379
380
381
382
383
void AutoCell::afficherErreur(QString& msg){
    QMessageBox messageBox;
    messageBox.critical(0,"Error",msg);
    messageBox.setFixedSize(500,200);
}