Commit f4f159f7 authored by Yann Boucher's avatar Yann Boucher
Browse files

Restore discarded changes regarding loading/saving alphabet data; added...

Restore discarded changes regarding loading/saving alphabet data; added support for cancelling customization
parent ce449e49
Pipeline #78994 passed with stages
in 17 seconds
......@@ -95,6 +95,8 @@ private:
//! \brief Charge la grille depuis une image.
void load_from_image();
void ui_update_alphabet(const Alphabet& alph);
//! \brief Retourne le modèle par défaut à charger
QJsonObject default_model() const;
private:
......@@ -102,5 +104,7 @@ private:
Simulation simulation;
TransitionRule* m_transition_rule;
NeighborhoodRule* m_neighborhood_rule;
QJsonObject m_loaded_model;
bool m_customizing;
};
#endif // MAINWINDOW_HPP
......@@ -17,7 +17,8 @@
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, simulation()
, simulation(),
m_customizing(false)
{
ui->setupUi(this);
......@@ -313,8 +314,9 @@ void MainWindow::update_neighborhood_settings()
void MainWindow::enable_rule_customization()
{
ui->simulation_tab->setEnabled(false);
ui->customize_button->setEnabled(false);
ui->customize_button->setText("Cancel customization");
ui->rule_settings->setEnabled(true);
m_customizing = true;
}
void MainWindow::disable_rule_customization()
......@@ -322,15 +324,47 @@ void MainWindow::disable_rule_customization()
ui->simulation_tab->setEnabled(true);
ui->customize_button->setEnabled(true);
ui->rule_settings->setEnabled(false);
ui->customize_button->setText("Customize...");
m_customizing = false;
}
void MainWindow::load_model(const QJsonObject &obj)
{
ui->rule_name->setText(obj.value("title").toString());
// TODO : load alphabet
ui->transition_list->setCurrentText(obj.value("transition_name").toString());
ui->neighborhood_list->setCurrentText(obj.value("neighborhood_name").toString());
bool alpha_initialise = false;
Alphabet alpha;
if (obj.value("alphabet").isArray())
{
for (auto entry : obj.value("alphabet").toArray())
{
if (!entry.isObject())
continue;
auto entry_obj = entry.toObject();
std::string name = entry_obj.value("name").toString().toStdString();
if (!entry_obj.value("color").isArray())
continue;
auto color_array = entry_obj.value("color").toArray();
stateColor color(color_array[0].toInt(), color_array[1].toInt(), color_array[2].toInt());
// Initialisation avec le premier élement
if (!alpha_initialise)
{
alpha = Alphabet(state(color, name));
alpha_initialise = true;
}
else
alpha.newEtat(state(color, name));
}
}
update_transition_settings();
update_neighborhood_settings();
disable_rule_customization();
......@@ -353,10 +387,14 @@ void MainWindow::load_model(const QJsonObject &obj)
prop->accept(visit);
}
ui_update_alphabet(alpha);
// On transfère la propriété de ces pointeurs vers Simulation, qui en est désormais propriétaire pour l'exécution de l'automate
simulation.setNeighborhoodRule(m_neighborhood_rule);
simulation.setTransitionRule(m_transition_rule);
simulation.setAlphabet(ui->grid_view->alphabet());
simulation.setAlphabet(alpha);
m_loaded_model = obj;
}
void MainWindow::save_model()
......@@ -387,9 +425,21 @@ void MainWindow::save_model()
root["neighborhood_name"] = ui->neighborhood_list->currentText();
root["neighborhood_data"] = neighborhood_saver.save();
// TODO : sauvegarder l'alphabet quand on pourra y accéder avec Simulation
QJsonArray alphabet;
for (unsigned i = 0; i < simulation.getAlphabet().taille(); ++i)
{
auto state = simulation.getAlphabet().getState(i);
QJsonArray color;
QJsonObject entry;
entry["name"] = QString::fromStdString(state.getStateLabel());
color.push_back(state.getColor().getRed());
color.push_back(state.getColor().getGreen());
color.push_back(state.getColor().getBlue());
entry["color"] = color;
alphabet.push_back(entry);
}
root["alphabet"] = alphabet;
QJsonDocument doc(root);
......@@ -446,14 +496,32 @@ void MainWindow::load_from_image()
ui->grid_view->copy_grid(grid);
}
void MainWindow::ui_update_alphabet(const Alphabet &alpha)
{
ui->grid_view->set_alphabet(alpha);
ui->nbrStateComboBox->clear();
for (unsigned i = 0; i < alpha.taille(); ++i)
ui->nbrStateComboBox->addItem(QString::number(i));
ui->nbrStatesComboBox->clear();
ui->nbrStatesComboBox->addItem(QString::number(alpha.taille()));
}
QJsonObject MainWindow::default_model() const
{
QJsonObject obj;
obj["title"] = "Game of Life";
obj["transition_name"] = "Game of Life";
obj["transition_data"] = QJsonObject();
const char* json = R"(
{
"title" : "Game of Life",
"transition_name" : "Game of Life",
"transition_data" : {},
"alphabet" : [
{"name" : "Dead", "color" : [255, 255, 255]},
{"name" : "Alive", "color" : [0, 0, 255]}]
}
)";
QJsonDocument doc = QJsonDocument::fromJson(QByteArray(json));
return obj;
return doc.object();
}
void MainWindow::on_saveRuleButton_clicked()
......@@ -463,6 +531,14 @@ void MainWindow::on_saveRuleButton_clicked()
void MainWindow::on_customize_button_clicked()
{
enable_rule_customization();
if (!m_customizing)
{
enable_rule_customization();
}
else
{
disable_rule_customization();
load_model(m_loaded_model);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment