Skip to content

Commit

Permalink
preserve layout in config
Browse files Browse the repository at this point in the history
  • Loading branch information
garakmon committed Feb 22, 2023
1 parent f3e1a5d commit f256558
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
4 changes: 4 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,15 @@ class UserConfig: public KeyValueConfigBase
}
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);
Expand All @@ -351,6 +354,7 @@ class UserConfig: public KeyValueConfigBase
private:
QString projectDir;
QString recentMap;
QString recentLayout;
bool useEncounterJson;
QList<QString> customScripts;
QStringList readKeys;
Expand Down
8 changes: 6 additions & 2 deletions include/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,12 @@ private slots:

bool needsFullRedraw = false;

bool setDefaultView();
bool setRecentView();
bool setLayout(QString layoutId);

bool setMap(QString, bool scroll = false);
void unsetMap();

void redrawMapScene();
void redrawLayoutScene();
void refreshMapScene();
Expand All @@ -369,7 +371,9 @@ private slots:
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();

Expand Down
12 changes: 12 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,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") {
Expand All @@ -1003,6 +1005,7 @@ void UserConfig::setUnreadKeys() {
QMap<QString, QString> UserConfig::getKeyValueMap() {
QMap<QString, QString> 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->customScripts.join(","));
return map;
Expand Down Expand Up @@ -1031,6 +1034,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();
Expand Down
2 changes: 2 additions & 0 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,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()) {
Expand Down
49 changes: 40 additions & 9 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,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<int>(this->mapSortOrder));
}
Expand Down Expand Up @@ -535,16 +535,13 @@ bool MainWindow::openProject(QString dir) {
QObject::connect(editor->project, &Project::uncheckMonitorFilesAction, [this]() { ui->actionMonitor_Project_Files->setChecked(false); });
on_actionMonitor_Project_Files_triggered(porymapConfig.getMonitorFiles());
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;
Expand Down Expand Up @@ -575,6 +572,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<QStringList> names = editor->project->groupedMapNames;
Expand All @@ -599,6 +612,18 @@ QString MainWindow::getDefaultMap() {
return QString();
}

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);
}
Expand Down Expand Up @@ -688,7 +713,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);
Expand Down Expand Up @@ -722,6 +747,8 @@ bool MainWindow::setLayout(QString layoutId) {

updateTilesetEditor();

setRecentLayoutConfig(layoutId);

return true;
}

Expand Down Expand Up @@ -806,10 +833,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);
Expand Down

0 comments on commit f256558

Please sign in to comment.