Reopen Porymap to last-opened map/layout

This commit is contained in:
GriffinR 2024-10-17 11:46:47 -04:00
parent b5c50e7660
commit 5e9ab4c7c7
5 changed files with 38 additions and 128 deletions

View file

@ -406,8 +406,7 @@ public:
reset(); reset();
} }
virtual void reset() override { virtual void reset() override {
this->recentMap = QString(); this->recentMapOrLayout = QString();
this->recentLayout = QString();
this->useEncounterJson = true; this->useEncounterJson = true;
this->customScripts.clear(); this->customScripts.clear();
this->readKeys.clear(); this->readKeys.clear();
@ -419,8 +418,7 @@ public:
QList<bool> getCustomScriptsEnabled(); QList<bool> getCustomScriptsEnabled();
QString projectDir; QString projectDir;
QString recentMap; QString recentMapOrLayout;
QString recentLayout;
bool useEncounterJson; bool useEncounterJson;
protected: protected:

View file

@ -373,8 +373,6 @@ private:
bool tilesetNeedsRedraw = false; bool tilesetNeedsRedraw = false;
bool setDefaultView();
bool setRecentView();
bool setLayout(QString layoutId); bool setLayout(QString layoutId);
bool setMap(QString, bool scroll = false); bool setMap(QString, bool scroll = false);
void unsetMap(); void unsetMap();
@ -398,12 +396,6 @@ private:
QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum); QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum);
bool setInitialMap(); bool setInitialMap();
bool setInitialLayout();
QString getDefaultMap();
QString getDefaultLayout();
void setRecentMapConfig(QString map_name);
void setRecentLayoutConfig(QString layoutId);
void saveGlobalConfigs(); void saveGlobalConfigs();
void refreshRecentProjectsMenu(); void refreshRecentProjectsMenu();

View file

@ -278,13 +278,7 @@ uint32_t KeyValueConfigBase::getConfigUint32(QString key, QString value, uint32_
return qMin(max, qMax(min, result)); return qMin(max, qMax(min, result));
} }
const QMap<MapSortOrder, QString> mapSortOrderMap = { const QMap<QString, MapSortOrder> mapSortOrderMap = {
{MapSortOrder::SortByGroup, "group"},
{MapSortOrder::SortByLayout, "layout"},
{MapSortOrder::SortByArea, "area"},
};
const QMap<QString, MapSortOrder> mapSortOrderReverseMap = {
{"group", MapSortOrder::SortByGroup}, {"group", MapSortOrder::SortByGroup},
{"layout", MapSortOrder::SortByLayout}, {"layout", MapSortOrder::SortByLayout},
{"area", MapSortOrder::SortByArea}, {"area", MapSortOrder::SortByArea},
@ -316,8 +310,8 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) {
this->prettyCursors = getConfigBool(key, value); this->prettyCursors = getConfigBool(key, value);
} else if (key == "map_sort_order") { } else if (key == "map_sort_order") {
QString sortOrder = value.toLower(); QString sortOrder = value.toLower();
if (mapSortOrderReverseMap.contains(sortOrder)) { if (mapSortOrderMap.contains(sortOrder)) {
this->mapSortOrder = mapSortOrderReverseMap.value(sortOrder); this->mapSortOrder = mapSortOrderMap.value(sortOrder);
} else { } else {
this->mapSortOrder = MapSortOrder::SortByGroup; this->mapSortOrder = MapSortOrder::SortByGroup;
logWarn(QString("Invalid config value for map_sort_order: '%1'. Must be 'group', 'area', or 'layout'.").arg(value)); logWarn(QString("Invalid config value for map_sort_order: '%1'. Must be 'group', 'area', or 'layout'.").arg(value));
@ -438,7 +432,7 @@ QMap<QString, QString> PorymapConfig::getKeyValueMap() {
map.insert("project_manually_closed", this->projectManuallyClosed ? "1" : "0"); map.insert("project_manually_closed", this->projectManuallyClosed ? "1" : "0");
map.insert("reopen_on_launch", this->reopenOnLaunch ? "1" : "0"); map.insert("reopen_on_launch", this->reopenOnLaunch ? "1" : "0");
map.insert("pretty_cursors", this->prettyCursors ? "1" : "0"); map.insert("pretty_cursors", this->prettyCursors ? "1" : "0");
map.insert("map_sort_order", mapSortOrderMap.value(this->mapSortOrder)); map.insert("map_sort_order", mapSortOrderMap.key(this->mapSortOrder));
map.insert("main_window_geometry", stringFromByteArray(this->mainWindowGeometry)); map.insert("main_window_geometry", stringFromByteArray(this->mainWindowGeometry));
map.insert("main_window_state", stringFromByteArray(this->mainWindowState)); map.insert("main_window_state", stringFromByteArray(this->mainWindowState));
map.insert("map_splitter_state", stringFromByteArray(this->mapSplitterState)); map.insert("map_splitter_state", stringFromByteArray(this->mapSplitterState));
@ -740,8 +734,8 @@ void ProjectConfig::parseConfigKeyValue(QString key, QString value) {
} else if (key == "enable_map_allow_flags") { } else if (key == "enable_map_allow_flags") {
this->mapAllowFlagsEnabled = getConfigBool(key, value); this->mapAllowFlagsEnabled = getConfigBool(key, value);
#ifdef CONFIG_BACKWARDS_COMPATABILITY #ifdef CONFIG_BACKWARDS_COMPATABILITY
} else if (key == "recent_map") { } else if (key == "recent_map_or_layout") {
userConfig.recentMap = value; userConfig.recentMapOrLayout = value;
} else if (key == "use_encounter_json") { } else if (key == "use_encounter_json") {
userConfig.useEncounterJson = getConfigBool(key, value); userConfig.useEncounterJson = getConfigBool(key, value);
} else if (key == "custom_scripts") { } else if (key == "custom_scripts") {
@ -1035,10 +1029,8 @@ QString UserConfig::getConfigFilepath() {
} }
void UserConfig::parseConfigKeyValue(QString key, QString value) { void UserConfig::parseConfigKeyValue(QString key, QString value) {
if (key == "recent_map") { if (key == "recent_map_or_layout") {
this->recentMap = value; this->recentMapOrLayout = value;
} else if (key == "recent_layout") {
this->recentLayout = value;
} else if (key == "use_encounter_json") { } else if (key == "use_encounter_json") {
this->useEncounterJson = getConfigBool(key, value); this->useEncounterJson = getConfigBool(key, value);
} else if (key == "custom_scripts") { } else if (key == "custom_scripts") {
@ -1054,8 +1046,7 @@ void UserConfig::setUnreadKeys() {
QMap<QString, QString> UserConfig::getKeyValueMap() { QMap<QString, QString> UserConfig::getKeyValueMap() {
QMap<QString, QString> map; QMap<QString, QString> map;
map.insert("recent_map", this->recentMap); map.insert("recent_map_or_layout", this->recentMapOrLayout);
map.insert("recent_layout", this->recentLayout);
map.insert("use_encounter_json", QString::number(this->useEncounterJson)); map.insert("use_encounter_json", QString::number(this->useEncounterJson));
map.insert("custom_scripts", this->outputCustomScripts()); map.insert("custom_scripts", this->outputCustomScripts());
return map; return map;

View file

@ -1319,7 +1319,7 @@ bool Editor::setLayout(QString layoutId) {
return true; return true;
} }
void Editor::onMapStartPaint(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *item) { void Editor::onMapStartPaint(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *) {
if (!this->getEditingLayout()) { if (!this->getEditingLayout()) {
return; return;
} }
@ -1332,7 +1332,7 @@ void Editor::onMapStartPaint(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *
} }
} }
void Editor::onMapEndPaint(QGraphicsSceneMouseEvent *, LayoutPixmapItem *item) { void Editor::onMapEndPaint(QGraphicsSceneMouseEvent *, LayoutPixmapItem *) {
if (!this->getEditingLayout()) { if (!this->getEditingLayout()) {
return; return;
} }

View file

@ -696,83 +696,29 @@ bool MainWindow::isProjectOpen() {
return editor && editor->project; return editor && editor->project;
} }
bool MainWindow::setDefaultView() {
if (porymapConfig.mapSortOrder == MapSortOrder::SortByLayout) {
return setLayout(getDefaultLayout());
} else {
return setMap(getDefaultMap(), true);
}
}
bool MainWindow::setRecentView() {
if (porymapConfig.mapSortOrder == MapSortOrder::SortByLayout) {
return setLayout(userConfig.recentLayout);
} else {
return setMap(userConfig.recentMap, true);
}
}
QString MainWindow::getDefaultMap() {
if (editor && editor->project) {
QList<QStringList> names = editor->project->groupedMapNames;
if (!names.isEmpty()) {
QString recentMap = userConfig.recentMap;
if (!recentMap.isNull() && recentMap.length() > 0) {
for (int i = 0; i < names.length(); i++) {
if (names.value(i).contains(recentMap)) {
return recentMap;
}
}
}
// Failing that, just get the first map in the list.
for (int i = 0; i < names.length(); i++) {
QStringList list = names.value(i);
if (list.length()) {
return list.value(0);
}
}
}
}
return QString();
}
bool MainWindow::setInitialMap() { bool MainWindow::setInitialMap() {
QStringList names; const QString recent = userConfig.recentMapOrLayout;
if (editor && editor->project) if (editor->project->mapNames.contains(recent)) {
names = editor->project->mapNames; // User recently had a map open that still exists.
if (setMap(recent, true))
// Try to set most recently-opened map, if it's still in the list. return true;
QString recentMap = userConfig.recentMap; } else if (editor->project->mapLayoutsTable.contains(recent)) {
if (!recentMap.isEmpty() && names.contains(recentMap) && setMap(recentMap, true)) // User recently had a layout open that still exists.
return true; if (setLayout(recent))
// Failing that, try loading maps in the map list sequentially.
for (auto name : names) {
if (name != recentMap && setMap(name, true))
return true; return true;
} }
logError("Failed to load any maps."); // Failed to open recent map/layout, or no recent map/layout. Try opening maps then layouts sequentially.
return false; for (const auto &name : editor->project->mapNames) {
} if (name != recent && setMap(name, true))
return true;
bool MainWindow::setInitialLayout() { }
QStringList names; for (const auto &id : editor->project->mapLayoutsTable) {
if (editor && editor->project) if (id != recent && setLayout(id))
names = editor->project->mapLayoutsTable;
// Try to set most recently-opened layout, if it's still in the list.
QString recentLayout = userConfig.recentLayout;
if (!recentLayout.isEmpty() && names.contains(recentLayout) && setLayout(recentLayout))
return true;
// Failing that, try loading maps in the map list sequentially.
for (auto name : names) {
if (name != recentLayout && setLayout(name))
return true; return true;
} }
logError("Failed to load any layouts."); logError("Failed to load any maps or layouts.");
return false; return false;
} }
@ -824,25 +770,13 @@ void MainWindow::openSubWindow(QWidget * window) {
} }
} }
QString MainWindow::getDefaultLayout() {
if (editor && editor->project) {
QString recentLayout = userConfig.recentLayout;
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) { QString MainWindow::getExistingDirectory(QString dir) {
return FileDialog::getExistingDirectory(this, "Open Directory", dir, QFileDialog::ShowDirsOnly); return FileDialog::getExistingDirectory(this, "Open Directory", dir, QFileDialog::ShowDirsOnly);
} }
void MainWindow::on_action_Open_Project_triggered() void MainWindow::on_action_Open_Project_triggered()
{ {
QString dir = getExistingDirectory(!userConfig.recentMap.isEmpty() ? userConfig.recentMap : "."); QString dir = getExistingDirectory(!projectConfig.projectDir.isEmpty() ? userConfig.projectDir : ".");
if (!dir.isEmpty()) if (!dir.isEmpty())
openProject(dir); openProject(dir);
} }
@ -904,11 +838,16 @@ bool MainWindow::userSetMap(QString map_name, bool scrollTreeView) {
bool MainWindow::setMap(QString map_name, bool scroll) { bool MainWindow::setMap(QString map_name, bool scroll) {
// if map name is empty, clear & disable map ui // if map name is empty, clear & disable map ui
if (map_name.isEmpty() || map_name == DYNAMIC_MAP_NAME) { if (map_name.isEmpty()) {
unsetMap(); unsetMap();
return false; return false;
} }
if (map_name == DYNAMIC_MAP_NAME) {
logInfo(QString("Cannot set map to '%1'").arg(DYNAMIC_MAP_NAME));
return false;
}
logInfo(QString("Setting map to '%1'").arg(map_name)); logInfo(QString("Setting map to '%1'").arg(map_name));
if (!editor || !editor->setMap(map_name)) { if (!editor || !editor->setMap(map_name)) {
@ -944,7 +883,7 @@ bool MainWindow::setMap(QString map_name, bool scroll) {
connect(editor->layout, &Layout::layoutChanged, this, &MainWindow::onLayoutChanged, Qt::UniqueConnection); connect(editor->layout, &Layout::layoutChanged, this, &MainWindow::onLayoutChanged, Qt::UniqueConnection);
connect(editor->layout, &Layout::needsRedrawing, this, &MainWindow::onLayoutNeedsRedrawing, Qt::UniqueConnection); connect(editor->layout, &Layout::needsRedrawing, this, &MainWindow::onLayoutNeedsRedrawing, Qt::UniqueConnection);
userConfig.recentMap = map_name; userConfig.recentMapOrLayout = map_name;
Scripting::cb_MapOpened(map_name); Scripting::cb_MapOpened(map_name);
prefab.updatePrefabUi(editor->layout); prefab.updatePrefabUi(editor->layout);
@ -974,7 +913,7 @@ bool MainWindow::setLayout(QString layoutId) {
updateTilesetEditor(); updateTilesetEditor();
setRecentLayoutConfig(layoutId); userConfig.recentMapOrLayout = layoutId;
return true; return true;
} }
@ -1050,16 +989,6 @@ void MainWindow::openWarpMap(QString map_name, int event_id, Event::Group event_
} }
} }
void MainWindow::setRecentMapConfig(QString mapName) {
userConfig.recentMap = mapName;
userConfig.recentLayout = "";
}
void MainWindow::setRecentLayoutConfig(QString layoutId) {
userConfig.recentLayout = layoutId;
userConfig.recentMap = "";
}
void MainWindow::displayMapProperties() { void MainWindow::displayMapProperties() {
// Block signals to the comboboxes while they are being modified // Block signals to the comboboxes while they are being modified
const QSignalBlocker blocker1(ui->comboBox_Song); const QSignalBlocker blocker1(ui->comboBox_Song);