move fileWatcher signals to project class, block when saving
This commit is contained in:
parent
96a870552e
commit
df8dac965f
3 changed files with 57 additions and 27 deletions
|
@ -20,10 +20,11 @@
|
||||||
static QString NONE_MAP_CONSTANT = "MAP_NONE";
|
static QString NONE_MAP_CONSTANT = "MAP_NONE";
|
||||||
static QString NONE_MAP_NAME = "None";
|
static QString NONE_MAP_NAME = "None";
|
||||||
|
|
||||||
class Project
|
class Project : public QObject
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
Project();
|
Project(QWidget *parent = nullptr);
|
||||||
~Project();
|
~Project();
|
||||||
|
|
||||||
Project(const Project &) = delete;
|
Project(const Project &) = delete;
|
||||||
|
@ -66,6 +67,8 @@ public:
|
||||||
|
|
||||||
void set_root(QString);
|
void set_root(QString);
|
||||||
|
|
||||||
|
void initSignals();
|
||||||
|
|
||||||
void clearMapCache();
|
void clearMapCache();
|
||||||
void clearTilesetCache();
|
void clearTilesetCache();
|
||||||
|
|
||||||
|
@ -196,6 +199,11 @@ private:
|
||||||
static int num_metatiles_total;
|
static int num_metatiles_total;
|
||||||
static int num_pals_primary;
|
static int num_pals_primary;
|
||||||
static int num_pals_total;
|
static int num_pals_total;
|
||||||
|
|
||||||
|
QWidget *parent;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void reloadProject();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PROJECT_H
|
#endif // PROJECT_H
|
||||||
|
|
|
@ -300,28 +300,8 @@ bool MainWindow::openProject(QString dir) {
|
||||||
bool already_open = isProjectOpen() && (editor->project->root == dir);
|
bool already_open = isProjectOpen() && (editor->project->root == dir);
|
||||||
if (!already_open) {
|
if (!already_open) {
|
||||||
editor->closeProject();
|
editor->closeProject();
|
||||||
editor->project = new Project;
|
editor->project = new Project(this);
|
||||||
QObject::connect(&editor->project->fileWatcher, &QFileSystemWatcher::fileChanged, [this](QString changed){
|
QObject::connect(editor->project, SIGNAL(reloadProject), this, SLOT(on_action_Reload_Project_triggered));
|
||||||
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(changed.remove(editor->project->root + "/")));
|
|
||||||
notice.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
|
|
||||||
notice.setIcon(QMessageBox::Question);
|
|
||||||
|
|
||||||
QCheckBox showAgainCheck("Do not ask again.");
|
|
||||||
notice.setCheckBox(&showAgainCheck);
|
|
||||||
|
|
||||||
int choice = notice.exec();
|
|
||||||
if (choice == QMessageBox::Yes) {
|
|
||||||
on_action_Reload_Project_triggered();
|
|
||||||
} else if (choice == QMessageBox::No) {
|
|
||||||
if (showAgainCheck.isChecked()) {
|
|
||||||
porymapConfig.setMonitorFiles(false);
|
|
||||||
this->editor->project->fileWatcher.blockSignals(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
editor->project->set_root(dir);
|
editor->project->set_root(dir);
|
||||||
success = loadDataStructures()
|
success = loadDataStructures()
|
||||||
&& populateMapList()
|
&& populateMapList()
|
||||||
|
|
|
@ -34,7 +34,7 @@ int Project::num_metatiles_total = 1024;
|
||||||
int Project::num_pals_primary = 6;
|
int Project::num_pals_primary = 6;
|
||||||
int Project::num_pals_total = 13;
|
int Project::num_pals_total = 13;
|
||||||
|
|
||||||
Project::Project()
|
Project::Project(QWidget *parent) : parent(parent)
|
||||||
{
|
{
|
||||||
groupNames = new QStringList;
|
groupNames = new QStringList;
|
||||||
mapGroups = new QMap<QString, int>;
|
mapGroups = new QMap<QString, int>;
|
||||||
|
@ -54,6 +54,8 @@ Project::Project()
|
||||||
mapConstantsToMapNames = new QMap<QString, QString>;
|
mapConstantsToMapNames = new QMap<QString, QString>;
|
||||||
mapNamesToMapConstants = new QMap<QString, QString>;
|
mapNamesToMapConstants = new QMap<QString, QString>;
|
||||||
tilesetCache = new QMap<QString, Tileset*>;
|
tilesetCache = new QMap<QString, Tileset*>;
|
||||||
|
|
||||||
|
initSignals();
|
||||||
}
|
}
|
||||||
|
|
||||||
Project::~Project()
|
Project::~Project()
|
||||||
|
@ -83,6 +85,31 @@ Project::~Project()
|
||||||
delete this->tilesetCache;
|
delete this->tilesetCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Project::initSignals() {
|
||||||
|
// detect changes to specific filepaths being monitored
|
||||||
|
QObject::connect(&fileWatcher, &QFileSystemWatcher::fileChanged, [this](QString changed){
|
||||||
|
QMessageBox notice(this->parent);
|
||||||
|
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.setIcon(QMessageBox::Question);
|
||||||
|
|
||||||
|
QCheckBox showAgainCheck("Do not ask again.");
|
||||||
|
notice.setCheckBox(&showAgainCheck);
|
||||||
|
|
||||||
|
int choice = notice.exec();
|
||||||
|
if (choice == QMessageBox::Yes) {
|
||||||
|
emit reloadProject();
|
||||||
|
} else if (choice == QMessageBox::No) {
|
||||||
|
if (showAgainCheck.isChecked()) {
|
||||||
|
porymapConfig.setMonitorFiles(false);
|
||||||
|
this->fileWatcher.blockSignals(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void Project::set_root(QString dir) {
|
void Project::set_root(QString dir) {
|
||||||
this->root = dir;
|
this->root = dir;
|
||||||
this->parser.set_root(dir);
|
this->parser.set_root(dir);
|
||||||
|
@ -633,6 +660,7 @@ bool Project::readMapLayouts() {
|
||||||
|
|
||||||
void Project::saveMapLayouts() {
|
void Project::saveMapLayouts() {
|
||||||
QString layoutsFilepath = QString("%1/data/layouts/layouts.json").arg(root);
|
QString layoutsFilepath = QString("%1/data/layouts/layouts.json").arg(root);
|
||||||
|
fileWatcher.removePath(layoutsFilepath);
|
||||||
QFile layoutsFile(layoutsFilepath);
|
QFile layoutsFile(layoutsFilepath);
|
||||||
if (!layoutsFile.open(QIODevice::WriteOnly)) {
|
if (!layoutsFile.open(QIODevice::WriteOnly)) {
|
||||||
logError(QString("Error: Could not open %1 for writing").arg(layoutsFilepath));
|
logError(QString("Error: Could not open %1 for writing").arg(layoutsFilepath));
|
||||||
|
@ -667,6 +695,7 @@ void Project::saveMapLayouts() {
|
||||||
OrderedJsonDoc jsonDoc(&layoutJson);
|
OrderedJsonDoc jsonDoc(&layoutJson);
|
||||||
jsonDoc.dump(&layoutsFile);
|
jsonDoc.dump(&layoutsFile);
|
||||||
layoutsFile.close();
|
layoutsFile.close();
|
||||||
|
fileWatcher.addPath(layoutsFilepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::setNewMapLayout(Map* map) {
|
void Project::setNewMapLayout(Map* map) {
|
||||||
|
@ -691,6 +720,7 @@ void Project::setNewMapLayout(Map* map) {
|
||||||
|
|
||||||
void Project::saveMapGroups() {
|
void Project::saveMapGroups() {
|
||||||
QString mapGroupsFilepath = QString("%1/data/maps/map_groups.json").arg(root);
|
QString mapGroupsFilepath = QString("%1/data/maps/map_groups.json").arg(root);
|
||||||
|
fileWatcher.removePath(mapGroupsFilepath);
|
||||||
QFile mapGroupsFile(mapGroupsFilepath);
|
QFile mapGroupsFile(mapGroupsFilepath);
|
||||||
if (!mapGroupsFile.open(QIODevice::WriteOnly)) {
|
if (!mapGroupsFile.open(QIODevice::WriteOnly)) {
|
||||||
logError(QString("Error: Could not open %1 for writing").arg(mapGroupsFilepath));
|
logError(QString("Error: Could not open %1 for writing").arg(mapGroupsFilepath));
|
||||||
|
@ -720,12 +750,14 @@ void Project::saveMapGroups() {
|
||||||
OrderedJsonDoc jsonDoc(&mapGroupJson);
|
OrderedJsonDoc jsonDoc(&mapGroupJson);
|
||||||
jsonDoc.dump(&mapGroupsFile);
|
jsonDoc.dump(&mapGroupsFile);
|
||||||
mapGroupsFile.close();
|
mapGroupsFile.close();
|
||||||
|
fileWatcher.addPath(mapGroupsFilepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::saveWildMonData() {
|
void Project::saveWildMonData() {
|
||||||
if (!projectConfig.getEncounterJsonActive()) return;
|
if (!projectConfig.getEncounterJsonActive()) return;
|
||||||
|
|
||||||
QString wildEncountersJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root);
|
QString wildEncountersJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root);
|
||||||
|
fileWatcher.removePath(wildEncountersJsonFilepath);
|
||||||
QFile wildEncountersFile(wildEncountersJsonFilepath);
|
QFile wildEncountersFile(wildEncountersJsonFilepath);
|
||||||
if (!wildEncountersFile.open(QIODevice::WriteOnly)) {
|
if (!wildEncountersFile.open(QIODevice::WriteOnly)) {
|
||||||
logError(QString("Error: Could not open %1 for writing").arg(wildEncountersJsonFilepath));
|
logError(QString("Error: Could not open %1 for writing").arg(wildEncountersJsonFilepath));
|
||||||
|
@ -806,6 +838,7 @@ void Project::saveWildMonData() {
|
||||||
OrderedJsonDoc jsonDoc(&encounterJson);
|
OrderedJsonDoc jsonDoc(&encounterJson);
|
||||||
jsonDoc.dump(&wildEncountersFile);
|
jsonDoc.dump(&wildEncountersFile);
|
||||||
wildEncountersFile.close();
|
wildEncountersFile.close();
|
||||||
|
fileWatcher.addPath(wildEncountersJsonFilepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::saveMapConstantsHeader() {
|
void Project::saveMapConstantsHeader() {
|
||||||
|
@ -939,8 +972,15 @@ void Project::saveHealLocationStruct(Map *map) {
|
||||||
data_text += QString("};\n");
|
data_text += QString("};\n");
|
||||||
constants_text += QString("\n#endif // GUARD_CONSTANTS_HEAL_LOCATIONS_H\n");
|
constants_text += QString("\n#endif // GUARD_CONSTANTS_HEAL_LOCATIONS_H\n");
|
||||||
|
|
||||||
saveTextFile(root + "/src/data/heal_locations.h", data_text);
|
QString healLocationFilepath = root + "/src/data/heal_locations.h";
|
||||||
saveTextFile(root + "/include/constants/heal_locations.h", constants_text);
|
fileWatcher.removePath(healLocationFilepath);
|
||||||
|
saveTextFile(healLocationFilepath, data_text);
|
||||||
|
fileWatcher.addPath(healLocationFilepath);
|
||||||
|
|
||||||
|
QString healLocationConstantsFilepath = root + "/include/constants/heal_locations.h";
|
||||||
|
fileWatcher.removePath(healLocationConstantsFilepath);
|
||||||
|
saveTextFile(healLocationConstantsFilepath, constants_text);
|
||||||
|
fileWatcher.addPath(healLocationConstantsFilepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::saveTilesets(Tileset *primaryTileset, Tileset *secondaryTileset) {
|
void Project::saveTilesets(Tileset *primaryTileset, Tileset *secondaryTileset) {
|
||||||
|
@ -1032,6 +1072,8 @@ void Project::saveTilesetMetatileLabels(Tileset *primaryTileset, Tileset *second
|
||||||
}
|
}
|
||||||
|
|
||||||
outputText += "\n#endif // GUARD_METATILE_LABELS_H\n";
|
outputText += "\n#endif // GUARD_METATILE_LABELS_H\n";
|
||||||
|
|
||||||
|
|
||||||
saveTextFile(root + "/include/constants/metatile_labels.h", outputText);
|
saveTextFile(root + "/include/constants/metatile_labels.h", outputText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue