Files
SAE-terra-aventura/mainwindow.cpp
T
2025-06-20 10:58:40 +02:00

253 lines
5.6 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "path.h"
#include <QFileDialog>
#include <QMessageBox>
#include <fstream>
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());
loadImage(p->getImage());
}
void MainWindow::addNewPath()
{
}
void MainWindow::addNewStep()
{
}
void MainWindow::exportHTMLMap()
{
std::ofstream file("parcours.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>
#map {
height: 600px;
width: 70%;
border-radius:8%;
}
</style>
</head>
<body>
<h1>Carte du parcours</h1>
<div id="map"></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;
// c un test
// qDebug() << "Nombre d'étapes dans le path :" << p->getStep().size();
for (const Step& s : p->getStep()) {
qDebug() << s.getTitle() << s.getLatitude() << s.getLongitude();
}
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>";
// ajout des perso
if (!personnages.isEmpty()) {
popupHtml += "<b>Personnages :</b><ul>";
for (const QString& pers : personnages) {
popupHtml += "<li>" + pers + "</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";
// Ajouter la polyline en pointillés
file << R"(
var polyline = L.polyline(latlngs, {
color: 'purple',
weight: 2,
dashArray: '10, 10', // ligne en pointillés
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 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);
}
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;
}