Commit 0cb352ba authored by Yann Boucher's avatar Yann Boucher
Browse files

Ajout d'une ébauche de sauvegarde de modèle d'automate

parent accff21e
Pipeline #78348 passed with stages
in 17 seconds
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>StructureSavingDialog</class>
<widget class="QDialog" name="StructureSavingDialog">
<class>SavingDialog</class>
<widget class="QDialog" name="SavingDialog">
<property name="geometry">
<rect>
<x>0</x>
......@@ -17,7 +17,7 @@
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Sauvegarde d'une structure</string>
<string>Sauvegarde</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
......@@ -96,7 +96,7 @@
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>StructureSavingDialog</receiver>
<receiver>SavingDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
......@@ -112,7 +112,7 @@
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>StructureSavingDialog</receiver>
<receiver>SavingDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
......
......@@ -64,6 +64,8 @@ private slots:
//! \brief Copie la structure choisie dans la bibliothèque dans le presse-papier du GridView
void copy_structure_clicked(const Structure& s);
void on_saveRuleButton_clicked();
private:
//! \brief Initialiser la liste des transitions et voisinages disponibles
void init_transition_neighborhood_list();
......
#ifndef STRUCTURESAVINGDIALOG_HPP
#define STRUCTURESAVINGDIALOG_HPP
#ifndef SAVINGDIALOG_H
#define SAVINGDIALOG_H
#include <QDialog>
namespace Ui {
class StructureSavingDialog;
class SavingDialog;
}
class StructureSavingDialog : public QDialog
class SavingDialog : public QDialog
{
Q_OBJECT
public:
explicit StructureSavingDialog(QWidget *parent = nullptr);
~StructureSavingDialog();
explicit SavingDialog(QWidget *parent = nullptr);
~SavingDialog();
QString auteur() const;
QString titre() const;
......@@ -24,7 +24,7 @@ protected:
void done(int r);
private:
Ui::StructureSavingDialog *ui;
Ui::SavingDialog *ui;
};
#endif // STRUCTURESAVINGDIALOG_HPP
#endif // SAVINGDIALOG_H
#include "interface.hpp"
#include "ui_interface.h"
#include "structuresavingdialog.hpp"
#include "savingdialog.hpp"
#include "structurewriter.hpp"
#include "factory.hpp"
#include "transitionrule.hpp"
#include "neighborhoodrule.hpp"
#include "propertyvisitors.hpp"
#include <QJsonArray>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
......@@ -84,13 +86,13 @@ void MainWindow::on_heightSpinBox_valueChanged(int)
void MainWindow::afficher_interface_sauvegarde_structure(bool)
{
StructureSavingDialog dialog(this);
SavingDialog dialog(this);
if (!dialog.exec())
return;
QString filename = QFileDialog::getSaveFileName(this, "Choisir un nom de fichier", QString(), "Format Cellulut (*.json);;Format Golly RLE (*.rle)");
QFileInfo info(filename);
printf("%s\n", info.suffix().toStdString().c_str());
//printf("%s\n", info.suffix().toStdString().c_str());
// on pourrait utiliser un Factory ici, mais c'est possiblement overkill, les possibilités d'évolution de format de fichier sont faibles et ne justifient pas l'effort
Structure s = ui->grid_view->selected_cells();
......@@ -246,6 +248,8 @@ void MainWindow::init_transition_neighborhood_list()
}
}
// TODO FIXME quand on aura une classe simulation commune
static std::unique_ptr<TransitionRule> transition_rule;
void MainWindow::update_transition_settings()
{
std::string selected = ui->transition_list->currentText().toStdString();
......@@ -263,4 +267,41 @@ void MainWindow::update_transition_settings()
for (auto& prop : rule->get_properties())
prop->accept(visit);
}
transition_rule = std::move(rule);
}
void MainWindow::on_saveRuleButton_clicked()
{
SavingDialog dialog(this);
if (!dialog.exec())
return;
QString filename = QFileDialog::getSaveFileName(this, "Choisir un nom de fichier", QString(), "Modèle d'automate (*.json)");
// TODO : permettre de choisir auteur/titre/année/description
PropertySaverVisitor saver;
for (auto& prop : transition_rule->get_properties())
prop->accept(saver);
QJsonObject root;
root["title"] = dialog.titre();
root["author"] = dialog.auteur();
root["desc"] = dialog.desc();
root["date"] = dialog.date().toString();
root["transition_name"] = ui->transition_list->currentText();
root["transition_data"] = saver.save();
// TODO : sauvegarder l'alphabet quand on pourra y accéder avec Simulation
QJsonArray alphabet;
root["alphabet"] = alphabet;
QJsonDocument doc(root);
QFile file(filename);
file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
file.write(doc.toJson());
file.close();
}
#include "structuresavingdialog.hpp"
#include "ui_structuresavingdialog.h"
#include "savingdialog.hpp"
#include "ui_savingdialog.h"
#include <QDate>
#include <QMessageBox>
StructureSavingDialog::StructureSavingDialog(QWidget *parent) :
SavingDialog::SavingDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::StructureSavingDialog)
ui(new Ui::SavingDialog)
{
ui->setupUi(this);
......@@ -20,12 +20,12 @@ StructureSavingDialog::StructureSavingDialog(QWidget *parent) :
ui->date->setDate(QDate::currentDate());
}
StructureSavingDialog::~StructureSavingDialog()
SavingDialog::~SavingDialog()
{
delete ui;
}
void StructureSavingDialog::done(int r)
void SavingDialog::done(int r)
{
if(QDialog::Accepted == r) // ok was pressed
{
......@@ -47,22 +47,22 @@ void StructureSavingDialog::done(int r)
}
}
QString StructureSavingDialog::auteur() const
QString SavingDialog::auteur() const
{
return ui->auteur->text();
}
QString StructureSavingDialog::titre() const
QString SavingDialog::titre() const
{
return ui->nom->text();
}
QString StructureSavingDialog::desc() const
QString SavingDialog::desc() const
{
return ui->desc->toPlainText();
}
QDate StructureSavingDialog::date() const
QDate SavingDialog::date() const
{
return ui->date->date();
}
......@@ -16,6 +16,7 @@ SOURCES += \
neighborhood_rules/arbitraryneighborhoodrule.cpp \
automaton.cpp \
gridview.cpp \
savingdialog.cpp \
transition_rules/circulartransition.cpp \
transition_rules/lifegametransition.cpp \
main.cpp \
......@@ -29,13 +30,13 @@ SOURCES += \
history.cpp \
neighborhood_rules/vonNeumannNeighborhoodRule.cpp \
structurewriter.cpp \
structuresavingdialog.cpp \
structurelibraryview.cpp
HEADERS += \
../include/automaton.hpp \
../include/coord.hpp \
../include/savingdialog.hpp \
../include/transition_rules/lifegametransition.h \
../include/neighborhoodrule.hpp \
../include/neighborhood_rules/arbitraryneighborhoodrule.hpp \
......@@ -55,14 +56,13 @@ HEADERS += \
../include/stateColor.hpp \
../include/state.hpp \
../include/history.h \
../include/structuresavingdialog.hpp \
../include/structurelibraryview.hpp \
../include/transitionrule.hpp \
../include/transition_rules/circulartransition.hpp \
FORMS += \
../forms/interface.ui \
../forms/structuresavingdialog.ui \
../forms/savingdialog.ui \
../forms/structurelibraryview.ui
......
Markdown is supported
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