Fix crash when file watcher message triggers

This commit is contained in:
GriffinR 2024-10-29 20:09:01 -04:00
parent 2ce5c3fcc5
commit 785ac958a5
4 changed files with 44 additions and 50 deletions

View file

@ -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();

View file

@ -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);
};

View file

@ -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);
}

View file

@ -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);