197 lines
4.2 KiB
C++
197 lines
4.2 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
#include "path.h"
|
|
|
|
#include <QFileDialog>
|
|
#include <QMessageBox>
|
|
|
|
|
|
#include <QFile>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::MainWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete ui;
|
|
delete currentPath;
|
|
|
|
for(Path* p : path) {
|
|
delete p;
|
|
}
|
|
}
|
|
|
|
void MainWindow::updatePathView()
|
|
{
|
|
|
|
}
|
|
|
|
void MainWindow::updateStepView(size_t num)
|
|
{
|
|
|
|
}
|
|
|
|
void MainWindow::onTextChanged()
|
|
{
|
|
textChanged = true;
|
|
}
|
|
|
|
void MainWindow::loadPath()
|
|
{
|
|
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
|
|
if(fileName.isEmpty()) return;
|
|
QFile file(fileName);
|
|
if(!file.open(QIODevice::ReadOnly | QFile::Text)) {
|
|
QMessageBox::warning(this, "Warning", "Fichier non valide" + file.errorString());
|
|
return;
|
|
}
|
|
|
|
file.close();
|
|
Path* p = new Path(&file);
|
|
currentPath = p;
|
|
path.append(p);
|
|
|
|
ui->titleEdit->setText(p->getName());
|
|
ui->locEdit->setText(p->getCity());
|
|
ui->diffSpin->setValue(p->getDifficulty());
|
|
ui->lengthSpin->setValue(p->getLength());
|
|
ui->durationSpin->setValue(p->getDuration());
|
|
ui->imagePath->setText(p->getImage());
|
|
ui->depSpin->setValue(p->getDepartement());
|
|
|
|
loadImage(p->getImage());
|
|
}
|
|
|
|
void MainWindow::addNewPath()
|
|
{
|
|
|
|
}
|
|
|
|
void MainWindow::addNewStep()
|
|
{
|
|
|
|
}
|
|
|
|
void MainWindow::exportHTMLMap()
|
|
{
|
|
|
|
}
|
|
|
|
void MainWindow::loadImage(QString fileName) {
|
|
|
|
QString ext[] = {"png", "jpeg", "jpg"};
|
|
|
|
QFile file(fileName);
|
|
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
|
|
QMessageBox::warning(this, "Warning", "Cannot open file: " +
|
|
file.errorString());
|
|
return;
|
|
}
|
|
QString text = file.fileName();
|
|
|
|
bool acceptedExt = false;
|
|
for(QString e : ext) {
|
|
if(text.endsWith(e)) acceptedExt = true;
|
|
}
|
|
|
|
if(!acceptedExt) {
|
|
QMessageBox::warning(this, "Warning", "Format de fichier incorrect");
|
|
return;
|
|
}
|
|
|
|
|
|
ui->imagePath->setText(text);
|
|
|
|
|
|
QPixmap px(fileName);
|
|
|
|
ui->imageLbl->setPixmap(px);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
void MainWindow::on_pushButton_clicked()
|
|
{
|
|
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
|
|
if (fileName.isEmpty()) return;
|
|
this->loadImage(fileName);
|
|
}
|
|
|
|
|
|
void MainWindow::on_actionOpen_triggered()
|
|
{
|
|
this->loadPath();
|
|
}
|
|
|
|
void MainWindow::on_toolButton_clicked()
|
|
{
|
|
QString fileName = ui->imagePath->text();
|
|
if(fileName.isEmpty()) return;
|
|
loadImage(fileName);
|
|
}
|
|
|
|
void MainWindow::saveFile(){
|
|
QString fileName;
|
|
if (currentFile.isEmpty()) {
|
|
fileName = QFileDialog::getSaveFileName(this, "Save");
|
|
currentFile = fileName;
|
|
} else {
|
|
fileName = currentFile;
|
|
}
|
|
QFile file(fileName);
|
|
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
|
|
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
|
|
return;
|
|
}
|
|
setWindowTitle(fileName);
|
|
QJsonObject json;
|
|
json["name"] = ui->titleEdit->text();
|
|
json["city"] =ui->locEdit->text();
|
|
json["departement"] = ui->depSpin->text();
|
|
json["difficulty"] = ui->diffSpin->value();
|
|
json["duration"] = ui->durationSpin->value();
|
|
json["length"] = ui->lengthSpin->value();
|
|
json["image"] = ui->imagePath->text();
|
|
|
|
QJsonArray steps;
|
|
int cpt=0;
|
|
for(Step step: currentPath->getStep()){
|
|
cpt++;
|
|
QJsonObject stepObject;
|
|
stepObject["numero"] = cpt;
|
|
stepObject["title"] = step.getTitle();
|
|
stepObject["GPS"] = step.toGPSFormat();
|
|
stepObject["reponse"] = step.getResponse();
|
|
QJsonArray dialogues;
|
|
for(int i=0; i<step.getListeDialogue().size(); i++){
|
|
QJsonObject dialogueObject;
|
|
dialogueObject["personnage"] = step.getListePersonnage()[i];
|
|
dialogueObject["texte"] = step.getListeDialogue()[i];
|
|
dialogues.append(dialogueObject);
|
|
}
|
|
stepObject["dialogue"] = dialogues;
|
|
steps.append(stepObject);
|
|
}
|
|
json["steps"] = steps;
|
|
|
|
QJsonDocument doc(json);
|
|
file.write(doc.toJson());
|
|
|
|
file.close();
|
|
}
|
|
|
|
void MainWindow::on_actionSave_triggered()
|
|
{
|
|
this->saveFile();
|
|
}
|
|
|