#include "mainwindow.h" #include "ui_mainwindow.h" #include "path.h" #include "step.h" #include "Undo.h" #include #include #include #include #include #include #include #include #include int MainWindow::indexPath = 0; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , undoStack(new QUndoStack(this)) , Clipboard(QGuiApplication::clipboard()) { ui->setupUi(this); indexPath++; //currentPath = new Path(); connect(ui->titleEdit, &QLineEdit::editingFinished, this, [this]() { static QString previousText; QString currentText = ui->titleEdit->text(); if(previousText != currentText) { undoStack->push(new LineEditCommand(ui->titleEdit, previousText, currentText)); previousText = currentText; } }); connect(ui->locEdit, &QLineEdit::editingFinished, this, [this]() { static QString previousText; QString currentText = ui->locEdit->text(); if(previousText != currentText) { undoStack->push(new LineEditCommand(ui->locEdit, previousText, currentText)); previousText = currentText; } }); connect(ui->imagePath, &QLineEdit::editingFinished, this, [this]() { static QString previousText; QString currentText = ui->imagePath->text(); if(previousText != currentText) { undoStack->push(new LineEditCommand(ui->imagePath, previousText, currentText)); previousText = currentText; } }); connect(ui->actionEditUndo, &QAction::triggered, undoStack, &QUndoStack::undo); connect(ui->actionEditRedo, &QAction::triggered, undoStack, &QUndoStack::redo); } 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::loadNewPath() { 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); loadPath(p); currentFile = fileName; int pathCount = path.length(); ui->pathNumber->setMaximum(pathCount); ui->pathNumber->setSuffix("/" + QString::number(pathCount)); ui->pathNumber->setValue(path.indexOf(currentPath)+1); } void MainWindow::loadPath(Path* p) { QList steps = p->getStep(); 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()); ui->dialogEdit->clear(); if(!steps.isEmpty()) { Step firstStep = p->getStep().first(); ui->stepTitle->setText(firstStep.getTitle()); ui->LatitudeSpin->setValue(firstStep.getLatitude()); ui->longitudeSpin->setValue(firstStep.getLongitude()); for(int i = 0; i < firstStep.getTexte().length(); i++) { QString q = firstStep.getPersonnage().at(i) + ": " + firstStep.getTexte().at(i); ui->dialogEdit->appendPlainText(q); } ui->stepNumber->setValue(1); ui->stepNumber->setSuffix("/" + QString::number(steps.length())); ui->stepNumber->setMaximum(steps.length()); } } void MainWindow::loadStep(Step s) { ui->stepTitle->setText(s.getTitle()); ui->LatitudeSpin->setValue(s.getLatitude()); ui->longitudeSpin->setValue(s.getLongitude()); ui->responseSpin->setValue(s.getResponse()); for(int i = 0; i < s.getTexte().length(); i++) { QString q = s.getPersonnage().at(i) + ": " + s.getTexte().at(i); ui->dialogEdit->appendPlainText(q); } } void MainWindow::addNewPath() { path.append(currentPath); } void MainWindow::addNewStep() { } void MainWindow::exportHTMLMap(int index) { std::ofstream file("./pages/parcours" + std::to_string(index) + ".html"); if (!file.is_open()) { QMessageBox::warning(this, "Erreur", "Impossible d'ouvrir le fichier."); return; } file << R"( Carte du parcours

Fiche du parcours

)"; if (currentPath) { Path* p = currentPath; file << "

" << p->getName().toStdString() << "

\n"; file << "

Ville : " << p->getCity().toStdString() << "

\n"; file << "

Département : " << p->getDepartement() << "

\n"; file << "

Difficulté : " << p->getDifficulty() << "

\n"; file << "

Durée (heures) : " << p->getDuration() << "

\n"; file << "

Longueur (km) : " << p->getLength() << "

\n"; if (!p->getImage().isEmpty()) { file << "getImage().toStdString() << "\">\n"; } int stepNum = 1; for (const Step& s : p->getStep()) { file << "

Étape " << stepNum << "

\n"; const QList persos = s.getPersonnage(); const QList textes = s.getTexte(); if (!persos.isEmpty()) { file << "

Personnages :

\n
    "; for (const QString& pers : persos) { file << "
  • " << pers.toStdString() << "
  • \n"; } file << "
\n"; } if (!textes.isEmpty()) { file << "

Dialogues :

\n
    "; for (const QString& txt : textes) { file << "
  • " << txt.toStdString() << "
  • \n"; } file << "
\n"; } stepNum++; } } file << R"(
)"; file.close(); } 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 image: " + file.errorString()); }else{ 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->loadNewPath(); } void MainWindow::on_toolButton_clicked() { QString fileName = ui->imagePath->text(); if(fileName.isEmpty()) return; loadImage(fileName); } <<<<<<< mainwindow.cpp int MainWindow::getIndexPath() const { return indexPath; } void MainWindow::setIndexPath(int newIndexPath) { indexPath = newIndexPath; } QString MainWindow::getCurrentFile() const { return currentFile; } void MainWindow::setCurrentFile(const QString &newCurrentFile) { currentFile = newCurrentFile; } QList MainWindow::getPath() const { return path; } void MainWindow::setPath(const QList &newPath) { path = newPath; } Path *MainWindow::getCurrentPath() const { return currentPath; } void MainWindow::setCurrentPath(Path *newCurrentPath) { currentPath = newCurrentPath; ======= 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; isaveFile(); } void MainWindow::on_actionopenFile_triggered() { this->loadNewPath(); } void MainWindow::on_actionEditCopy_triggered() { QWidget *focused = QApplication::focusWidget(); QLineEdit* lineEdit = qobject_cast(focused); if(lineEdit) { Clipboard->setText(lineEdit->selectedText()); } } void MainWindow::on_actionEditPaste_triggered() { QWidget *focused = QApplication::focusWidget(); QLineEdit* lineEdit = qobject_cast(focused); if(lineEdit) { QString text = lineEdit->text(); int pos = lineEdit->cursorPosition(); text.insert(pos, Clipboard->text()); lineEdit->setText(text); } } void MainWindow::on_actionEditCut_triggered() { QWidget *focused = QApplication::focusWidget(); QLineEdit* lineEdit = qobject_cast(focused); if(lineEdit) { QString text = lineEdit->text(); QString selectedText = lineEdit->selectedText(); int pos = lineEdit->selectionStart(); text.remove(pos, selectedText.length()); Clipboard->setText(selectedText); lineEdit->setText(text); } } void MainWindow::on_pathNumber_valueChanged(int arg1) { this->loadPath(path.at(ui->pathNumber->value()-1)); currentPath = path.at(arg1-1); } void MainWindow::on_stepNumber_valueChanged(int arg1) { this->loadStep(currentPath->getStep().at(arg1-1)); } void MainWindow::on_validateBtn_clicked() { currentPath->setName(ui->titleEdit->text()); currentPath->setCity(ui->locEdit->text()); currentPath->setImage(ui->imagePath->text()); currentPath->setLength(ui->lengthSpin->value()); currentPath->setDifficulty(ui->diffSpin->value()); currentPath->setDuration(ui->durationSpin->value()); currentPath->getStep()[ui->stepNumber->value()-1].setTitle(ui->stepTitle->text()); currentPath->getStep()[ui->stepNumber->value()-1].setResponse(ui->responseSpin->value()); }