SAE-terra-aventura/mainwindow.cpp
T'JAMPENS QUENTIN p2406187 4f508adbb9 Display path number
2025-06-19 23:22:19 +02:00

225 lines
5.2 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "path.h"
#include "step.h"
#include "Undo.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QLineEdit>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, undoStack(new QUndoStack(this))
, Clipboard(QGuiApplication::clipboard())
{
ui->setupUi(this);
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::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);
QList<Step> steps = p->getStep();
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());
if(!steps.isEmpty()) {
ui->stepTitle->setText(p->getStep().first().getTitle());
}
int pathCount = path.length();
ui->pathNumber->setMaximum(pathCount);
ui->pathNumber->setSuffix("/" + QString::number(pathCount));
ui->pathNumber->setValue(path.indexOf(currentPath)+1);
}
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::on_actionopenFile_triggered()
{
this->loadPath();
}
void MainWindow::on_actionEditCopy_triggered()
{
QWidget *focused = QApplication::focusWidget();
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(focused);
if(lineEdit) {
Clipboard->setText(lineEdit->selectedText());
}
}
void MainWindow::on_actionEditPaste_triggered()
{
QWidget *focused = QApplication::focusWidget();
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(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<QLineEdit*>(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);
}
}