From f485ebdd3e236b10925f7f9e81d340bfbf47bc91 Mon Sep 17 00:00:00 2001 From: garak Date: Wed, 22 Feb 2023 15:41:29 -0500 Subject: [PATCH] preserve layout in config --- include/config.h | 4 ++++ include/mainwindow.h | 8 ++++++-- src/config.cpp | 12 +++++++++++ src/editor.cpp | 2 ++ src/mainwindow.cpp | 49 ++++++++++++++++++++++++++++++++++++-------- 5 files changed, 64 insertions(+), 11 deletions(-) diff --git a/include/config.h b/include/config.h index 923abae2..d306fb5d 100644 --- a/include/config.h +++ b/include/config.h @@ -344,12 +344,15 @@ public: } virtual void reset() override { this->recentMap = QString(); + this->recentLayout = QString(); this->useEncounterJson = true; this->customScripts.clear(); this->readKeys.clear(); } void setRecentMap(const QString &map); QString getRecentMap(); + void setRecentLayout(const QString &map); + QString getRecentLayout(); void setEncounterJsonActive(bool active); bool getEncounterJsonActive(); void setProjectDir(QString projectDir); @@ -371,6 +374,7 @@ protected: private: QString projectDir; QString recentMap; + QString recentLayout; bool useEncounterJson; QMap customScripts; QStringList readKeys; diff --git a/include/mainwindow.h b/include/mainwindow.h index 522bf0f2..d09fb917 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -357,10 +357,12 @@ private: bool tilesetNeedsRedraw = false; + bool setDefaultView(); + bool setRecentView(); bool setLayout(QString layoutId); - bool setMap(QString, bool scroll = false); void unsetMap(); + void redrawMapScene(); void redrawLayoutScene(); void refreshMapScene(); @@ -373,7 +375,9 @@ private: QString getExistingDirectory(QString); bool openProject(QString dir); QString getDefaultMap(); - void setRecentMap(QString map_name); + QString getDefaultLayout(); + void setRecentMapConfig(QString map_name); + void setRecentLayoutConfig(QString layoutId); void updateMapList(); diff --git a/src/config.cpp b/src/config.cpp index 9dddcd42..39c8e807 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1102,6 +1102,8 @@ QString UserConfig::getConfigFilepath() { void UserConfig::parseConfigKeyValue(QString key, QString value) { if (key == "recent_map") { this->recentMap = value; + } else if (key == "recent_layout") { + this->recentLayout = value; } else if (key == "use_encounter_json") { this->useEncounterJson = getConfigBool(key, value); } else if (key == "custom_scripts") { @@ -1118,6 +1120,7 @@ void UserConfig::setUnreadKeys() { QMap UserConfig::getKeyValueMap() { QMap map; map.insert("recent_map", this->recentMap); + map.insert("recent_layout", this->recentLayout); map.insert("use_encounter_json", QString::number(this->useEncounterJson)); map.insert("custom_scripts", this->outputCustomScripts()); return map; @@ -1146,6 +1149,15 @@ QString UserConfig::getRecentMap() { return this->recentMap; } +void UserConfig::setRecentLayout(const QString &layout) { + this->recentLayout = layout; + this->save(); +} + +QString UserConfig::getRecentLayout() { + return this->recentLayout; +} + void UserConfig::setEncounterJsonActive(bool active) { this->useEncounterJson = active; this->save(); diff --git a/src/editor.cpp b/src/editor.cpp index 48b8ce4e..d5d48e6a 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -1164,6 +1164,8 @@ bool Editor::setMap(QString map_name) { bool Editor::setLayout(QString layoutId) { // + if (layoutId.isEmpty()) return false; + this->layout = this->project->loadLayout(layoutId); if (!displayLayout()) { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index e9e1e912..02ae9ace 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -342,8 +342,8 @@ void MainWindow::initMiscHeapObjects() { // !TODO: scroll view on first showing void MainWindow::initMapSortOrder() { mapSortOrder = porymapConfig.getMapSortOrder(); - if (mapSortOrder == MapSortOrder::SortByLayout) - mapSortOrder = MapSortOrder::SortByGroup; + // if (mapSortOrder == MapSortOrder::SortByLayout) + // mapSortOrder = MapSortOrder::SortByGroup; this->ui->mapListContainer->setCurrentIndex(static_cast(this->mapSortOrder)); } @@ -541,16 +541,13 @@ bool MainWindow::openProject(QString dir) { this->preferenceEditor->updateFields(); }); editor->project->set_root(dir); - success = loadDataStructures() - && populateMapList() - && setMap(getDefaultMap(), true); + success = loadDataStructures() && populateMapList() && setDefaultView(); } else { - QString open_map = editor->map->name; editor->project->fileWatcher.removePaths(editor->project->fileWatcher.files()); editor->project->clearLayoutsTable(); editor->project->clearMapCache(); editor->project->clearTilesetCache(); - success = loadDataStructures() && populateMapList() && setMap(open_map, true); + success = loadDataStructures() && populateMapList() && setRecentView(); } projectOpenFailure = !success; @@ -581,6 +578,22 @@ bool MainWindow::isProjectOpen() { return !projectOpenFailure && editor && editor->project; } +bool MainWindow::setDefaultView() { + if (this->mapSortOrder == MapSortOrder::SortByLayout) { + return setLayout(getDefaultLayout()); + } else { + return setMap(getDefaultMap(), true); + } +} + +bool MainWindow::setRecentView() { + if (this->mapSortOrder == MapSortOrder::SortByLayout) { + return setLayout(userConfig.getRecentLayout()); + } else { + return setMap(userConfig.getRecentMap(), true); + } +} + QString MainWindow::getDefaultMap() { if (editor && editor->project) { QList names = editor->project->groupedMapNames; @@ -618,6 +631,18 @@ void MainWindow::openSubWindow(QWidget * window) { } } +QString MainWindow::getDefaultLayout() { + if (editor && editor->project) { + QString recentLayout = userConfig.getRecentLayout(); + if (!recentLayout.isEmpty() && editor->project->mapLayoutsTable.contains(recentLayout)) { + return recentLayout; + } else if (!editor->project->mapLayoutsTable.isEmpty()) { + return editor->project->mapLayoutsTable.first(); + } + } + return QString(); +} + QString MainWindow::getExistingDirectory(QString dir) { return QFileDialog::getExistingDirectory(this, "Open Directory", dir, QFileDialog::ShowDirsOnly); } @@ -707,7 +732,7 @@ bool MainWindow::setMap(QString map_name, bool scroll) { connect(editor->layout, &Layout::layoutChanged, [this]() { onMapChanged(nullptr); }); connect(editor->layout, &Layout::needsRedrawing, this, &MainWindow::onLayoutNeedsRedrawing); - setRecentMap(map_name); + setRecentMapConfig(map_name); updateMapList(); Scripting::cb_MapOpened(map_name); @@ -741,6 +766,8 @@ bool MainWindow::setLayout(QString layoutId) { updateTilesetEditor(); + setRecentLayoutConfig(layoutId); + return true; } @@ -825,10 +852,14 @@ void MainWindow::openWarpMap(QString map_name, int event_id, Event::Group event_ } } -void MainWindow::setRecentMap(QString mapName) { +void MainWindow::setRecentMapConfig(QString mapName) { userConfig.setRecentMap(mapName); } +void MainWindow::setRecentLayoutConfig(QString layoutId) { + userConfig.setRecentLayout(layoutId); +} + void MainWindow::displayMapProperties() { // Block signals to the comboboxes while they are being modified const QSignalBlocker blocker1(ui->comboBox_Song);