From 99a21ccf2938e12096da2a5da1fd606e68168fb2 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Fri, 21 Dec 2018 09:25:28 -0600 Subject: [PATCH] Add porymap config file --- include/config.h | 33 ++++++++++ include/mainwindow.h | 7 +-- porymap.pro | 2 + src/config.cpp | 142 +++++++++++++++++++++++++++++++++++++++++++ src/mainwindow.cpp | 79 +++++++----------------- 5 files changed, 200 insertions(+), 63 deletions(-) create mode 100644 include/config.h create mode 100644 src/config.cpp diff --git a/include/config.h b/include/config.h new file mode 100644 index 00000000..df75d60e --- /dev/null +++ b/include/config.h @@ -0,0 +1,33 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include + +enum MapSortOrder { + Group = 0, + Area = 1, + Layout = 2, +}; + +class Config +{ +public: + static void save(); + static void load(); + static void setRecentProject(QString project); + static void setRecentMap(QString map); + static void setMapSortOrder(MapSortOrder order); + static void setPrettyCursors(bool enabled); + static QString getRecentProject(); + static QString getRecentMap(); + static MapSortOrder getMapSortOrder(); + static bool getPrettyCursors(); +private: + static void parseConfigKeyValue(QString key, QString value); + static QString recentProject; + static QString recentMap; + static MapSortOrder mapSortOrder; + static bool prettyCursors; +}; + +#endif // CONFIG_H diff --git a/include/mainwindow.h b/include/mainwindow.h index 9a6dceae..24b22b5d 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -10,6 +10,7 @@ #include #include #include "project.h" +#include "config.h" #include "map.h" #include "editor.h" #include "tileseteditor.h" @@ -164,11 +165,7 @@ private: bool isProgrammaticEventTabChange; - enum MapSortOrder { - Group = 0, - Name = 1, - Layout = 2, - } mapSortOrder; + MapSortOrder mapSortOrder; bool setMap(QString, bool scrollTreeView = false); void redrawMapScene(); diff --git a/porymap.pro b/porymap.pro index 6605d981..f9876228 100644 --- a/porymap.pro +++ b/porymap.pro @@ -46,6 +46,7 @@ SOURCES += src/core/block.cpp \ src/ui/tileseteditor.cpp \ src/ui/tileseteditormetatileselector.cpp \ src/ui/tileseteditortileselector.cpp \ + src/config.cpp \ src/editor.cpp \ src/main.cpp \ src/mainwindow.cpp \ @@ -87,6 +88,7 @@ HEADERS += include/core/block.h \ include/ui/tileseteditor.h \ include/ui/tileseteditormetatileselector.h \ include/ui/tileseteditortileselector.h \ + include/config.h \ include/editor.h \ include/mainwindow.h \ include/project.h \ diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 00000000..07616dd6 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,142 @@ +#include "config.h" +#include "log.h" +#include +#include +#include +#include +#include + +const QString configFilename = "porymap.cfg"; + +QString Config::recentProject = ""; +QString Config::recentMap = ""; +MapSortOrder Config::mapSortOrder = MapSortOrder::Group; +bool Config::prettyCursors = true; + +const QMap mapSortOrderMap = { + {MapSortOrder::Group, "group"}, + {MapSortOrder::Layout, "layout"}, + {MapSortOrder::Area, "area"}, +}; + +const QMap mapSortOrderReverseMap = { + {"group", MapSortOrder::Group}, + {"layout", MapSortOrder::Layout}, + {"area", MapSortOrder::Area}, +}; + +void Config::load() { + QFile file(configFilename); + if (!file.exists()) { + if (!file.open(QIODevice::WriteOnly)) { + logError(QString("Could not create config file '%1'").arg(configFilename)); + } else { + file.close(); + } + } + + if (!file.open(QIODevice::ReadOnly)) { + logError(QString("Could not open config file '%1': ").arg(configFilename) + file.errorString()); + } + + QTextStream in(&file); + QList configLines; + QRegularExpression re("^(?.+)=(?.+)$"); + while (!in.atEnd()) { + QString line = in.readLine().trimmed(); + int commentIndex = line.indexOf("#"); + if (commentIndex >= 0) { + line = line.left(commentIndex).trimmed(); + } + + if (line.length() == 0) { + continue; + } + + QRegularExpressionMatch match = re.match(line); + if (!match.hasMatch()) { + logWarn(QString("Invalid config line in %1: '%2'").arg(configFilename).arg(line)); + continue; + } + + parseConfigKeyValue(match.captured("key").toLower(), match.captured("value")); + } + + file.close(); +} + +void Config::parseConfigKeyValue(QString key, QString value) { + if (key == "recent_project") { + Config::recentProject = value; + } else if (key == "recent_map") { + Config::recentMap = value; + } else if (key == "pretty_cursors") { + bool ok; + Config::prettyCursors = value.toInt(&ok); + if (!ok) { + logWarn(QString("Invalid config value for pretty_cursors: '%1'. Must be 0 or 1.").arg(value)); + } + } else if (key == "map_sort_order") { + QString sortOrder = value.toLower(); + if (mapSortOrderReverseMap.contains(sortOrder)) { + Config::mapSortOrder = mapSortOrderReverseMap.value(sortOrder); + } else { + Config::mapSortOrder = MapSortOrder::Group; + logWarn(QString("Invalid config value for map_sort_order: '%1'. Must be 'group', 'area', or 'layout'.")); + } + } else { + logWarn(QString("Invalid config key found in config file %1: '%2'").arg(configFilename).arg(key)); + } +} + +void Config::save() { + QString text = ""; + text += QString("recent_project=%1\n").arg(Config::recentProject); + text += QString("recent_map=%1\n").arg(Config::recentMap); + text += QString("pretty_cursors=%1\n").arg(Config::prettyCursors ? "1" : "0"); + text += QString("map_sort_order=%1\n").arg(mapSortOrderMap.value(Config::mapSortOrder)); + + QFile file(configFilename); + if (file.open(QIODevice::WriteOnly)) { + file.write(text.toUtf8()); + file.close(); + } else { + logError(QString("Could not open config file '%1' for writing: ").arg(configFilename) + file.errorString()); + } +} + +void Config::setRecentProject(QString project) { + Config::recentProject = project; + Config::save(); +} + +void Config::setRecentMap(QString map) { + Config::recentMap = map; + Config::save(); +} + +void Config::setMapSortOrder(MapSortOrder order) { + Config::mapSortOrder = order; + Config::save(); +} + +void Config::setPrettyCursors(bool enabled) { + Config::prettyCursors = enabled; + Config::save(); +} + +QString Config::getRecentProject() { + return Config::recentProject; +} + +QString Config::getRecentMap() { + return Config::recentMap; +} + +MapSortOrder Config::getMapSortOrder() { + return Config::mapSortOrder; +} + +bool Config::getPrettyCursors() { + return Config::prettyCursors; +} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3968a35a..3a2e0d54 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -57,6 +56,7 @@ MainWindow::~MainWindow() } void MainWindow::initWindow() { + Config::load(); this->initCustomUI(); this->initExtraSignals(); this->initExtraShortcuts(); @@ -143,8 +143,6 @@ void MainWindow::initMapSortOrder() { void MainWindow::mapSortOrder_changed(QAction *action) { - QSettings settings; - QList items = ui->toolButton_MapSortOrder->menu()->actions(); int i = 0; for (; i < items.count(); i++) @@ -159,8 +157,7 @@ void MainWindow::mapSortOrder_changed(QAction *action) { ui->toolButton_MapSortOrder->setIcon(action->icon()); mapSortOrder = static_cast(i); - settings.setValue("map_sort_order", i); - + Config::setMapSortOrder(mapSortOrder); if (isProjectOpen()) { sortMapList(); @@ -176,28 +173,16 @@ void MainWindow::on_lineEdit_filterBox_textChanged(const QString &arg1) } void MainWindow::loadUserSettings() { - QSettings settings; - - bool betterCursors = settings.contains("cursor_mode") && settings.value("cursor_mode") != "0"; - ui->actionBetter_Cursors->setChecked(betterCursors); - this->editor->settings->betterCursors = betterCursors; - - if (!settings.contains("map_sort_order")) - { - settings.setValue("map_sort_order", 0); - } - mapSortOrder = static_cast(settings.value("map_sort_order").toInt()); + ui->actionBetter_Cursors->setChecked(Config::getPrettyCursors()); + this->editor->settings->betterCursors = Config::getPrettyCursors(); + mapSortOrder = Config::getMapSortOrder(); } bool MainWindow::openRecentProject() { - QSettings settings; - QString key = "recent_projects"; - if (settings.contains(key)) { - QString default_dir = settings.value(key).toStringList().last(); - if (!default_dir.isNull()) { - logInfo(QString("Opening recent project: '%1'").arg(default_dir)); - return openProject(default_dir); - } + QString default_dir = Config::getRecentProject(); + if (!default_dir.isNull() && default_dir.length() > 0) { + logInfo(QString("Opening recent project: '%1'").arg(default_dir)); + return openProject(default_dir); } return true; @@ -243,16 +228,11 @@ QString MainWindow::getDefaultMap() { if (editor && editor->project) { QList names = editor->project->groupedMapNames; if (!names.isEmpty()) { - QSettings settings; - QString key = "project:" + editor->project->root; - if (settings.contains(key)) { - QMap qmap = settings.value(key).toMap(); - if (qmap.contains("recent_map")) { - QString map_name = qmap.value("recent_map").toString(); - for (int i = 0; i < names.length(); i++) { - if (names.value(i).contains(map_name)) { - return map_name; - } + QString recentMap = Config::getRecentMap(); + if (!recentMap.isNull() && recentMap.length() > 0) { + for (int i = 0; i < names.length(); i++) { + if (names.value(i).contains(recentMap)) { + return recentMap; } } } @@ -274,22 +254,13 @@ QString MainWindow::getExistingDirectory(QString dir) { void MainWindow::on_action_Open_Project_triggered() { - QSettings settings; - QString key = "recent_projects"; QString recent = "."; - if (settings.contains(key)) { - recent = settings.value(key).toStringList().last(); + if (!Config::getRecentMap().isNull() && Config::getRecentMap().length() > 0) { + recent = Config::getRecentMap(); } QString dir = getExistingDirectory(recent); if (!dir.isEmpty()) { - QStringList recents; - if (settings.contains(key)) { - recents = settings.value(key).toStringList(); - } - recents.removeAll(dir); - recents.append(dir); - settings.setValue(key, recents); - + Config::setRecentProject(dir); openProject(dir); } } @@ -407,15 +378,8 @@ void MainWindow::openWarpMap(QString map_name, QString warp_num) { } } -void MainWindow::setRecentMap(QString map_name) { - QSettings settings; - QString key = "project:" + editor->project->root; - QMap qmap; - if (settings.contains(key)) { - qmap = settings.value(key).toMap(); - } - qmap.insert("recent_map", map_name); - settings.setValue(key, qmap); +void MainWindow::setRecentMap(QString mapName) { + Config::setRecentMap(mapName); } void MainWindow::displayMapProperties() { @@ -591,7 +555,7 @@ void MainWindow::sortMapList() { } } break; - case MapSortOrder::Name: + case MapSortOrder::Area: { QMap mapsecToGroupNum; for (int i = 0; i < project->regionMapSections->length(); i++) { @@ -843,8 +807,7 @@ void MainWindow::on_actionZoom_Out_triggered() { } void MainWindow::on_actionBetter_Cursors_triggered() { - QSettings settings; - settings.setValue("cursor_mode", QString::number(ui->actionBetter_Cursors->isChecked())); + Config::setPrettyCursors(ui->actionBetter_Cursors->isChecked()); this->editor->settings->betterCursors = ui->actionBetter_Cursors->isChecked(); }