From 785ac958a519706bbd373b0b98b0ad17b58e9a08 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Tue, 29 Oct 2024 20:09:01 -0400 Subject: [PATCH] Fix crash when file watcher message triggers --- include/mainwindow.h | 1 + include/project.h | 5 +---- src/mainwindow.cpp | 47 ++++++++++++++++++++++++++++++++++++++------ src/project.cpp | 41 +------------------------------------- 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/include/mainwindow.h b/include/mainwindow.h index 986036d6..160c138a 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -366,6 +366,7 @@ private: void scrollMapListToCurrentMap(MapTree *list); void scrollMapListToCurrentLayout(MapTree *list); void resetMapListFilters(); + void showFileWatcherWarning(QString filepath); QString getExistingDirectory(QString); bool openProject(QString dir, bool initial = false); bool closeProject(); diff --git a/include/project.h b/include/project.h index 6782769b..07d6dcab 100644 --- a/include/project.h +++ b/include/project.h @@ -88,8 +88,6 @@ public: void set_root(QString); - void initSignals(); - void clearMapCache(); void clearTilesetCache(); void clearMapLayouts(); @@ -267,8 +265,7 @@ private: static int max_object_events; signals: - void reloadProject(); - void uncheckMonitorFilesAction(); + void fileChanged(QString filepath); void mapLoaded(Map *map); }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index bd69d464..6e0f9185 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -602,13 +602,8 @@ bool MainWindow::openProject(QString dir, bool initial) { // Create the project auto project = new Project(editor); project->set_root(dir); - QObject::connect(project, &Project::reloadProject, this, &MainWindow::on_action_Reload_Project_triggered); + QObject::connect(project, &Project::fileChanged, this, &MainWindow::showFileWatcherWarning); QObject::connect(project, &Project::mapLoaded, this, &MainWindow::onMapLoaded); - QObject::connect(project, &Project::uncheckMonitorFilesAction, [this]() { - porymapConfig.monitorFiles = false; - if (this->preferenceEditor) - this->preferenceEditor->updateFields(); - }); this->editor->setProject(project); // Make sure project looks reasonable before attempting to load it @@ -761,6 +756,46 @@ void MainWindow::openSubWindow(QWidget * window) { } } +void MainWindow::showFileWatcherWarning(QString filepath) { + if (!porymapConfig.monitorFiles || !isProjectOpen()) + return; + + Project *project = this->editor->project; + if (project->modifiedFileTimestamps.contains(filepath)) { + if (QDateTime::currentMSecsSinceEpoch() < project->modifiedFileTimestamps[filepath]) { + return; + } + project->modifiedFileTimestamps.remove(filepath); + } + + static bool showing = false; + if (showing) return; + + QMessageBox notice(this); + notice.setText("File Changed"); + notice.setInformativeText(QString("The file %1 has changed on disk. Would you like to reload the project?") + .arg(filepath.remove(project->root + "/"))); + notice.setStandardButtons(QMessageBox::No | QMessageBox::Yes); + notice.setDefaultButton(QMessageBox::No); + notice.setIcon(QMessageBox::Question); + + QCheckBox showAgainCheck("Do not ask again."); + notice.setCheckBox(&showAgainCheck); + + showing = true; + int choice = notice.exec(); + if (choice == QMessageBox::Yes) { + on_action_Reload_Project_triggered(); + } else if (choice == QMessageBox::No) { + if (showAgainCheck.isChecked()) { + porymapConfig.monitorFiles = false; + if (this->preferenceEditor) + this->preferenceEditor->updateFields(); + } + } + showing = false; +} + QString MainWindow::getExistingDirectory(QString dir) { return FileDialog::getExistingDirectory(this, "Open Directory", dir, QFileDialog::ShowDirsOnly); } diff --git a/src/project.cpp b/src/project.cpp index f3c02ecb..314503f4 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -38,7 +38,7 @@ int Project::max_object_events = 64; Project::Project(QObject *parent) : QObject(parent) { - initSignals(); + QObject::connect(&this->fileWatcher, &QFileSystemWatcher::fileChanged, this, &Project::fileChanged); } Project::~Project() @@ -49,45 +49,6 @@ Project::~Project() clearEventGraphics(); } -void Project::initSignals() { - // detect changes to specific filepaths being monitored - QObject::connect(&fileWatcher, &QFileSystemWatcher::fileChanged, [this](QString changed){ - if (!porymapConfig.monitorFiles) return; - if (modifiedFileTimestamps.contains(changed)) { - if (QDateTime::currentMSecsSinceEpoch() < modifiedFileTimestamps[changed]) { - return; - } - modifiedFileTimestamps.remove(changed); - } - - static bool showing = false; - if (showing) return; - - QMessageBox notice(this->parentWidget()); - notice.setText("File Changed"); - notice.setInformativeText(QString("The file %1 has changed on disk. Would you like to reload the project?") - .arg(changed.remove(this->root + "/"))); - notice.setStandardButtons(QMessageBox::No | QMessageBox::Yes); - notice.setDefaultButton(QMessageBox::No); - notice.setIcon(QMessageBox::Question); - - QCheckBox showAgainCheck("Do not ask again."); - notice.setCheckBox(&showAgainCheck); - - showing = true; - int choice = notice.exec(); - if (choice == QMessageBox::Yes) { - emit reloadProject(); - } else if (choice == QMessageBox::No) { - if (showAgainCheck.isChecked()) { - porymapConfig.monitorFiles = false; - emit uncheckMonitorFilesAction(); - } - } - showing = false; - }); -} - void Project::set_root(QString dir) { this->root = dir; FileDialog::setDirectory(dir);