SAE-terra-aventura/mainwindow.cpp
2025-06-21 13:14:54 +02:00

934 lines
24 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "path.h"
#include "step.h"
#include "Undo.h"
#include <QFileDialog>
#include <QMessageBox>
#include <fstream>
#include <QLineEdit>
#include <QTimer>
#include <QTextEdit>
#include <QColorDialog>
#include <QPlainTextEdit>
#include <QInputDialog>
#include <QFontDialog>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include "web.h"
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++;
Path* p = new Path();
currentPath = p;
path.append(p);
loadPath(p);
int pathCount = path.length();
ui->pathNumber->setMaximum(pathCount);
ui->pathNumber->setSuffix("/" + QString::number(pathCount));
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);
currentPath->getStep().append(Step());
}
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<Step> 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()
{
this->on_validateBtn_clicked();
currentPath->getStep().append(Step());
ui->stepNumber->setMaximum(currentPath->getStep().length());
ui->stepNumber->setSuffix("/" + QString::number(currentPath->getStep().length()));
}
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"(<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Carte du parcours</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: whitesmoke;
}
#container {
display: flex;
gap: 20px;
}
#map {
height: 600px;
width: 60%;
border-radius: 5%;
border: 1px solid #aaa;
}
#fiche {
padding-right:20px;
width: 40%;
max-height: 600px;
overflow-y: auto;
border: 1px solid #aaa;
padding: 10px;
box-sizing: border-box;
background-color:#095228;
border-radius: 5%;
}
#fiche ul {
padding-left: 20px;
list-style-type: disc;
overflow-wrap: break-word;
word-wrap: break-word;
word-break: break-word;
white-space: normal;
max-width: 90%;
}
#fiche li {
white-space: normal;
overflow-wrap: break-word;
}
body h1 {
display:flex;
align-items:center;
justify-content:center;
text-align: center;
color: black;
font-style: bold;
margin-bottom: 20px;
background-color:brown;
border-radius:12px;
height: 75px;
}
#fiche h2, #fiche h3, #fiche p, #fiche li {
color: white;
}
#fiche img {
max-width: 100%;
height: auto;
margin-top: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>Fiche du parcours</h1>
<div id="container">
<div id="map"></div>
<div id="fiche">
)";
if (currentPath) {
Path* p = currentPath;
file << "<h2>" << p->getName().toStdString() << "</h2>\n";
file << "<p><strong>Ville :</strong> " << p->getCity().toStdString() << "</p>\n";
file << "<p><strong>Département :</strong> " << p->getDepartement() << "</p>\n";
file << "<p><strong>Difficulté :</strong> " << p->getDifficulty() << "</p>\n";
file << "<p><strong>Durée (heures) :</strong> " << p->getDuration() << "</p>\n";
file << "<p><strong>Longueur (km) :</strong> " << p->getLength() << "</p>\n";
if (!p->getImage().isEmpty()) {
file << "<img src=\"" << p->getImage().toStdString() << "\">\n";
}
int stepNum = 1;
for (const Step& s : p->getStep()) {
file << "<h3>Étape " << stepNum << "</h3>\n";
const QList<QString> persos = s.getPersonnage();
const QList<QString> textes = s.getTexte();
if (!persos.isEmpty()) {
file << "<p><strong>Personnages :</strong></p>\n<ul>";
for (const QString& pers : persos) {
file << "<li>" << pers.toStdString() << "</li>\n";
}
file << "</ul>\n";
}
if (!textes.isEmpty()) {
file << "<p><strong>Dialogues :</strong></p>\n<ul>";
for (const QString& txt : textes) {
file << "<li>" << txt.toStdString() << "</li>\n";
}
file << "</ul>\n";
}
stepNum++;
}
}
file << R"(
</div>
</div>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script>
var map = L.map('map').setView([45.5, 1.5], 10); // Vue centrée
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
)";
if (!path.isEmpty() && path[0]) {
Path* p = path[0];
int stepNum = 1;
for (const Step& s : p->getStep()) {
float lat = s.getLatitude();
float lon = s.getLongitude();
QList<QString> personnages = s.getPersonnage();
QList<QString> textes = s.getTexte();
QString popupHtml = "<b>Étape " + QString::number(stepNum) + "</b><br>";
if (!personnages.isEmpty()) {
popupHtml += "<b>Personnages :</b><ul>";
for (const QString& pers : personnages) {
popupHtml += "<li>" + pers + "</li>";
}
popupHtml += "</ul>";
}
if (!textes.isEmpty()) {
popupHtml += "<b>Textes :</b><ul>";
for (const QString& txt : textes) {
popupHtml += "<li>" + txt + "</li>";
}
popupHtml += "</ul>";
}
file << "L.marker([" << lat << ", " << lon << "]).addTo(map)"
<< ".bindPopup(\"" << popupHtml.toStdString() << "\");\n";
++stepNum;
}
file << "var latlngs = [\n";
for (const Step& s : p->getStep()) {
float lat = s.getLatitude();
float lon = s.getLongitude();
file << " [" << lat << ", " << lon << "],\n";
}
file << "];\n";
file << R"(
var polyline = L.polyline(latlngs, {
color: 'purple',
weight: 2,
dashArray: '10, 10',
opacity: 0.7
}).addTo(map);
map.fitBounds(polyline.getBounds());
)";
}
file << R"(
</script>
</body>
</html>
)";
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);
}
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<Path *> MainWindow::getPath() const
{
return path;
}
void MainWindow::setPath(const QList<Path *> &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; i<step.getTexte().size(); i++){
QJsonObject dialogueObject;
dialogueObject["personnage"] = step.getPersonnage()[i];
dialogueObject["texte"] = step.getTexte()[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();
}
void MainWindow::on_actionopenFile_triggered()
{
this->loadNewPath();
}
void MainWindow::copyText(){
QWidget *focused = QApplication::focusWidget();
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(focused);
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (lineEdit && !lineEdit->selectedText().isEmpty()) {
Clipboard->setText(lineEdit->selectedText());
} else if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
if (cursor.hasSelection()) {
Clipboard->setText(cursor.selectedText());
}
}
}
void MainWindow::on_actionEditCopy_triggered()
{
this->copyText();
}
void MainWindow::pastText(){
QWidget *focused = QApplication::focusWidget();
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(focused);
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (lineEdit) {
lineEdit->insert(Clipboard->text());
} else if (plainTextEdit) {
plainTextEdit->insertPlainText(Clipboard->text());
}
}
void MainWindow::on_actionEditPaste_triggered()
{
this->pastText();
}
void MainWindow::cutText(){
QWidget *focused = QApplication::focusWidget();
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(focused);
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (lineEdit) {
QString selectedText = lineEdit->selectedText();
if (!selectedText.isEmpty()) {
Clipboard->setText(selectedText);
lineEdit->del();
}
} else if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
if (cursor.hasSelection()) {
Clipboard->setText(cursor.selectedText());
cursor.removeSelectedText();
}
}
}
void MainWindow::on_actionEditCut_triggered()
{
this->cutText();
}
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());
}
void MainWindow::loadAndExportPaths(QStringList fichiers) {
for (const QString& nomFichier : fichiers) {
QFile* f = new QFile(nomFichier);
Path* p = new Path(f);
path.append(p);
}
int exportIndex = 1;
for (Path* p : path) {
currentPath = p;
exportHTMLMap(exportIndex++);
}
}
void MainWindow::setBold(){
QWidget *focused = QApplication::focusWidget();
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setBold(!font.bold());
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
void MainWindow::on_actionBold_triggered()
{
this->setBold();
}
void MainWindow::setItalic(){
QWidget *focused = QApplication::focusWidget();
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setItalic(!font.italic());
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
void MainWindow::on_actionItalic_triggered()
{
this->setItalic();
}
void MainWindow::setUnderline(){
QWidget *focused = QApplication::focusWidget();
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setUnderline(!font.underline());
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
void MainWindow::on_actionUnderline_triggered()
{
this->setUnderline();
}
void MainWindow::setColor(){
QWidget *focused = QApplication::focusWidget();
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (plainTextEdit) {
QColor color = QColorDialog::getColor(Qt::black, this, "Choisir une couleur");
QTextCursor cursor = plainTextEdit->textCursor();
if (color.isValid()) {
QTextCharFormat format;
format.setForeground(color);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
}
void MainWindow::on_actionColor_triggered()
{
this->setColor();
}
void MainWindow::setOverline(){
QWidget *focused = QApplication::focusWidget();
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setOverline(!font.overline());
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
void MainWindow::on_actionOverline_triggered()
{
this->setOverline();
}
void MainWindow::setSize(){
QWidget *focused = QApplication::focusWidget();
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
bool ok;
int size = QInputDialog::getInt(this, "Taille de la police", "Entrez la taille de la police:", cursor.charFormat().font().pointSize(), 1, 100, 1, &ok);
if (ok) {
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setPointSize(size);
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
}
void MainWindow::on_actionSize_triggered()
{
this->setSize();
}
void MainWindow::setFont(){
QWidget *focused = QApplication::focusWidget();
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
bool ok;
QFont font = QFontDialog::getFont(&ok, cursor.charFormat().font(), this, "Choisir une police");
if (ok) {
QTextCharFormat format;
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
}
void MainWindow::on_actionFont_triggered()
{
this->setFont();
}
void MainWindow::on_actionFont_2_triggered()
{
this->setFont();
}
void MainWindow::on_actionBold_2_triggered()
{
this->setBold();
}
void MainWindow::on_actionItalic_2_triggered()
{
this->setItalic();
}
void MainWindow::on_actionUnderline_2_triggered()
{
this->setUnderline();
}
void MainWindow::on_actionOverline_2_triggered()
{
this->setOverline();
}
void MainWindow::on_actionFont_size_triggered()
{
this->setSize();
}
void MainWindow::saveAsFile(){
QString fileName = QFileDialog::getSaveFileName(this, "Save as");
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
return;
}
setWindowTitle(fileName);
currentFile = 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.getTexte().size(); i++){
QJsonObject dialogueObject;
dialogueObject["personnage"] = step.getPersonnage()[i];
dialogueObject["texte"] = step.getTexte()[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_as_triggered()
{
this->saveAsFile();
}
void MainWindow::on_exportHTMLBtn_clicked()
{
for(int i = 0; i < path.length(); i++) {
this->exportHTMLMap(i);
}
Web w(path);
w.siteHtml();
}
void MainWindow::on_actionSaveFile_triggered()
{
this->saveFile();
}
void MainWindow::on_actionSaveAsFile_triggered()
{
this->saveAsFile();
}
void MainWindow::on_actionCopy_triggered()
{
this->copyText();
}
void MainWindow::on_actionPast_triggered()
{
this->pastText();
}
void MainWindow::on_actionCut_triggered()
{
this->cutText();
}
void MainWindow::newPath(){
Path* p = new Path();
currentPath = p;
path.append(p);
loadPath(p);
int pathCount = path.length();
ui->pathNumber->setMaximum(pathCount);
ui->pathNumber->setSuffix("/" + QString::number(pathCount));
ui->pathNumber->setValue(path.indexOf(currentPath)+1);
}
void MainWindow::on_actionNew_triggered()
{
this->newPath();
}
void MainWindow::on_actionNewFile_triggered()
{
this->newPath();
}
void MainWindow::on_actionFont_color_triggered()
{
this->setColor();
}
void MainWindow::on_addStep_clicked()
{
this->addNewStep();
}