Commit 32b13873 authored by Yann Boucher's avatar Yann Boucher
Browse files

Added support for saving and loading the alphabet associated with a model

parent fcfda1bb
Pipeline #78943 passed with stages
in 17 seconds
......@@ -175,6 +175,7 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event)
if (QGuiApplication::keyboardModifiers() == Qt::NoModifier && event->button() == Qt::LeftButton)
{
m_gridview.push_history();
m_gridview.click_on(coord);
m_gridview.update_gridview();
}
......@@ -191,7 +192,6 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event)
void GridGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
QPointF item_pos = mapToScene(event->pos());
if (!sceneRect().contains(item_pos) || !sceneRect().contains(mapToScene(m_last_mouse_pos)))
return;
......@@ -330,6 +330,8 @@ void GridView::set_alphabet(const Alphabet &alph)
m_undo_history.popGrid();
m_alph = alph;
// Default to using the first non-null state as the current pen
set_current_pen(1);
}
const Alphabet &GridView::alphabet() const
......@@ -339,6 +341,7 @@ const Alphabet &GridView::alphabet() const
void GridView::set_current_pen(unsigned state)
{
state %= m_alph.taille();
m_pen = state;
}
......@@ -423,6 +426,7 @@ void GridView::undo()
{
load_grid(m_undo_history.popGrid());
}
update_gridview();
}
void GridView::load_grid(const Grid &grid)
......
......@@ -327,10 +327,39 @@ void MainWindow::disable_rule_customization()
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 +382,12 @@ void MainWindow::load_model(const QJsonObject &obj)
prop->accept(visit);
}
ui->grid_view->set_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);
}
void MainWindow::save_model()
......@@ -387,8 +418,19 @@ 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;
......@@ -448,12 +490,20 @@ void MainWindow::load_from_image()
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()
......
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