363 lines
9.0 KiB
C++
363 lines
9.0 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
#include "path.h"
|
|
#include "Undo.h"
|
|
#include <QFileDialog>
|
|
#include <QMessageBox>
|
|
#include <QLineEdit>
|
|
#include <QTimer>
|
|
#include <QTextEdit>
|
|
#include <QColorDialog>
|
|
#include <QPlainTextEdit>
|
|
#include <QInputDialog>
|
|
#include <QFontDialog>
|
|
|
|
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);
|
|
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()
|
|
{
|
|
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
|