Commit 10e4c433 authored by Yann Boucher's avatar Yann Boucher
Browse files

Implémentation de l'action de sauvegarde d'une structure, aux formats JSON et RLE

parent 6e275ba2
Pipeline #77955 passed with stages
in 17 seconds
......@@ -38,7 +38,7 @@
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="GridView" name="frame">
<widget class="GridView" name="grid_view">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
......
......@@ -36,15 +36,15 @@ public:
void set_cell(Coord pos, unsigned int state)
{
int i = pos.x;
int j = pos.y;
int i = pos.y;
int j = pos.x;
matrix[((i%nb_rows + nb_rows)%nb_rows)*nb_col
+((j%nb_col+nb_col)%nb_col)]
=state;
}
unsigned int get_state(Coord pos)const{
int i = pos.x;
int j = pos.y;
int i = pos.y;
int j = pos.x;
return matrix[((i%nb_rows + nb_rows)%nb_rows)*nb_col
+((j%nb_col+nb_col)%nb_col)];
}
......
......@@ -20,6 +20,9 @@ public:
QString desc() const;
QDate date() const;
protected:
void done(int r);
private:
Ui::StructureSavingDialog *ui;
};
......
......@@ -3,6 +3,8 @@
#include "structuresavingdialog.hpp"
#include "structurewriter.hpp"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
......@@ -75,7 +77,42 @@ void MainWindow::on_heightSpinBox_valueChanged(int arg1)
void MainWindow::afficher_interface_sauvegarde_structure(bool)
{
StructureSavingDialog dialog(this);
dialog.exec();
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());
// 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();
s.author = dialog.auteur().toStdString();
s.title = dialog.titre().toStdString();
s.desc = dialog.desc().toStdString();
s.date = dialog.date().toString().toStdString();
std::string save_data;
if (info.suffix() == "json")
{
JSONStructureWriter writer;
save_data = writer.save_structure(s);
}
else if (info.suffix() == "rle")
{
RLEStructureWriter writer;
save_data = writer.save_structure(s);
}
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, "Impossible de créer le fichier",
file.errorString());
return;
}
QTextStream stream(&file);
stream << QString::fromStdString(save_data);
}
......
......@@ -213,11 +213,18 @@ Structure JSONStructureReader::read_structure()
QJsonObject root = jsonDoc.object();
Structure s;
if (!root.contains("cells"))
throw StructureReaderException("Pas de champ 'cells' !");
if (!root["cells"].isArray())
throw StructureReaderException("'cells' doit être un array.");
s.author = root.value("author").toString().toStdString();
s.title = root.value("title").toString().toStdString();
s.date = root.value("date").toString().toStdString();
s.desc = root.value("desc").toString().toStdString();
for (const auto& entry : root["cells"].toArray())
{
const char* msg = "Chaque entrée de 'cells' doit être un objet contenant des entiers x, y, et state.";
......@@ -234,5 +241,6 @@ Structure JSONStructureReader::read_structure()
data.push_back({{val["x"].toInt(), val["y"].toInt()}, val["state"].toInt()});
}
return Structure(data.begin(), data.end());
s.load(data.begin(), data.end());
return s;
}
......@@ -2,6 +2,7 @@
#include "ui_structuresavingdialog.h"
#include <QDate>
#include <QMessageBox>
StructureSavingDialog::StructureSavingDialog(QWidget *parent) :
QDialog(parent),
......@@ -24,6 +25,28 @@ StructureSavingDialog::~StructureSavingDialog()
delete ui;
}
void StructureSavingDialog::done(int r)
{
if(QDialog::Accepted == r) // ok was pressed
{
if(ui->nom->text().isEmpty()) // validate the data somehow
{
QMessageBox::information(this, "Erreur", "Le champ 'Titre' doit être rempli.");
return;
}
else
{
QDialog::done(r);
return;
}
}
else // cancel, close or exc was pressed
{
QDialog::done(r);
return;
}
}
QString StructureSavingDialog::auteur() const
{
return ui->auteur->text();
......
......@@ -30,6 +30,10 @@ std::string JSONStructureWriter::save_structure(const Structure& s) const
cells.append(entry);
}
root.insert("author", QString::fromStdString(s.author));
root.insert("title", QString::fromStdString(s.title));
root.insert("desc", QString::fromStdString(s.desc));
root.insert("date", QString::fromStdString(s.date));
root.insert("cells", cells);
QJsonDocument doc(root);
......@@ -39,6 +43,13 @@ std::string JSONStructureWriter::save_structure(const Structure& s) const
std::string RLEStructureWriter::save_structure(const Structure& s) const
{
std::string result;
if (!s.title.empty())
result += "#N " + s.title + "\n";
if (!s.author.empty() || !s.date.empty())
result += "#O " + s.author + " " + s.date + "\n";
if (!s.desc.empty())
result += "#C " + s.desc;
result += "x = " + std::to_string(s.width()) + ", ";
result += "y = " + std::to_string(s.height()) + "\n";
......
......@@ -10,11 +10,11 @@ void CellulutTests::test_grid()
Coord pos1 = {4,4};
grid.set_cell(pos1,2);
Coord pos2 = {1,1};
grid.set_cell(pos1,10);
grid.set_cell(pos2,10);
Coord pos3 = {1,2};
grid.set_cell(pos3,1);
unsigned int state = grid.get_state(pos1);
QVERIFY(state == 2);
QCOMPARE(state, 2);
QVERIFY(grid.get_state(pos2) == 10);
QVERIFY(grid.get_state(pos3) == 1);
......
......@@ -65,8 +65,14 @@ void CellulutTests::test_json_structurewriter()
try
{
Structure s{coords.begin(), coords.end()};
s.author = "Bob";
s.title = "Foo";
s.date = "28/07/2001";
s.desc = "Lorem ipsum";
JSONStructureWriter writer;
auto out = writer.save_structure(Structure{coords.begin(), coords.end()});
auto out = writer.save_structure(s);
//printf("%s\n", out.c_str());
......@@ -74,6 +80,10 @@ void CellulutTests::test_json_structurewriter()
Structure struct_out = reader.read_structure();
QVERIFY(std::is_permutation(struct_out.begin(), struct_out.end(), coords.begin()));
QCOMPARE(struct_out.author, s.author);
QCOMPARE(struct_out.date, s.date);
QCOMPARE(struct_out.desc, s.desc);
QCOMPARE(struct_out.title, s.title);
}
catch (const std::exception& ex)
{
......
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