diff --git a/path.cpp b/path.cpp index ab0fdfb..4f4fd97 100644 --- a/path.cpp +++ b/path.cpp @@ -1,13 +1,44 @@ #include "path.h" +#include "step.h" +#include +#include +#include +#include +#include + +#include + Path::Path(QFile *file){ - QTextStream in(&file); - QString text = in.readAll(); - ui->textEdit->setText(text); - file.close(); -} -void Path::addStep(){ + if (!file->open(QIODevice::ReadOnly)) { + qWarning() << "Could not open file:" << file->errorString(); + return; + } + QByteArray data = file->readAll(); + file->close(); + QJsonDocument doc = QJsonDocument::fromJson(data); + if (doc.isNull()) { + qWarning() << "Failed to create JSON document"; + return; + } + QJsonObject json = doc.object(); + + name = json["name"].toString(); + city = json["city"].toString(); + departement = json["departement"].toInt(); + difficulty = json["difficulty"].toInt(); + duration = json["duration"].toDouble(); + length = json["length"].toDouble(); + image = json["image"].toString(); + + QJsonArray stepsArray = json["steps"].toArray(); + for (const QJsonValue &stepValue : stepsArray) { + QJsonObject stepObj = stepValue.toObject(); + step.append(Step(stepObj)); + } } + +void Path::addStep(int indice){step.insert(indice, Step());} diff --git a/path.h b/path.h index 52ec154..025aafa 100644 --- a/path.h +++ b/path.h @@ -2,6 +2,9 @@ #define PATH_H #include +#include +#include "step.h" + class Path { @@ -17,7 +20,7 @@ private: public: Path(); Path(QFile *file); - void addStep(); + void addStep(int indice); }; #endif // PATH_H