From 525ef8c67c386aa72cba9050e954c4e02f07eef7 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Tue, 25 Dec 2018 14:41:06 -0600 Subject: [PATCH] Refactor config into generic key-value base class and settings-specific child class --- include/config.h | 55 ++++++++++++------ src/config.cpp | 136 ++++++++++++++++++++++++--------------------- src/mainwindow.cpp | 24 ++++---- 3 files changed, 122 insertions(+), 93 deletions(-) diff --git a/include/config.h b/include/config.h index df75d60e..ab625498 100644 --- a/include/config.h +++ b/include/config.h @@ -9,25 +9,46 @@ enum MapSortOrder { Layout = 2, }; -class Config +class KeyValueConfigBase { 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; + void save(); + void load(); + virtual ~KeyValueConfigBase(); +protected: + QString configFilename; + virtual void parseConfigKeyValue(QString key, QString value) = 0; + virtual QMap getKeyValueMap() = 0; }; +class PorymapConfig: public KeyValueConfigBase +{ +public: + PorymapConfig() { + this->configFilename = "porymap.cfg"; + this->recentProject = ""; + this->recentMap = ""; + this->mapSortOrder = MapSortOrder::Group; + this->prettyCursors = true; + } + void setRecentProject(QString project); + void setRecentMap(QString map); + void setMapSortOrder(MapSortOrder order); + void setPrettyCursors(bool enabled); + QString getRecentProject(); + QString getRecentMap(); + MapSortOrder getMapSortOrder(); + bool getPrettyCursors(); +protected: + void parseConfigKeyValue(QString key, QString value); + QMap getKeyValueMap(); +private: + QString recentProject; + QString recentMap; + MapSortOrder mapSortOrder; + bool prettyCursors; +}; + +extern PorymapConfig porymapConfig; + #endif // CONFIG_H diff --git a/src/config.cpp b/src/config.cpp index 07616dd6..cf8c76d1 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -6,37 +6,22 @@ #include #include -const QString configFilename = "porymap.cfg"; +KeyValueConfigBase::~KeyValueConfigBase() { -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); +void KeyValueConfigBase::load() { + QFile file(this->configFilename); if (!file.exists()) { if (!file.open(QIODevice::WriteOnly)) { - logError(QString("Could not create config file '%1'").arg(configFilename)); + logError(QString("Could not create config file '%1'").arg(this->configFilename)); } else { file.close(); } } if (!file.open(QIODevice::ReadOnly)) { - logError(QString("Could not open config file '%1': ").arg(configFilename) + file.errorString()); + logError(QString("Could not open config file '%1': ").arg(this->configFilename) + file.errorString()); } QTextStream in(&file); @@ -55,88 +40,111 @@ void Config::load() { QRegularExpressionMatch match = re.match(line); if (!match.hasMatch()) { - logWarn(QString("Invalid config line in %1: '%2'").arg(configFilename).arg(line)); + logWarn(QString("Invalid config line in %1: '%2'").arg(this->configFilename).arg(line)); continue; } - parseConfigKeyValue(match.captured("key").toLower(), match.captured("value")); + this->parseConfigKeyValue(match.captured("key").toLower(), match.captured("value")); } file.close(); } -void Config::parseConfigKeyValue(QString key, QString value) { +void KeyValueConfigBase::save() { + QString text = ""; + QMap map = this->getKeyValueMap(); + for (QMap::iterator it = map.begin(); it != map.end(); it++) { + text += QString("%1=%2\n").arg(it.key()).arg(it.value()); + } + + QFile file(this->configFilename); + if (file.open(QIODevice::WriteOnly)) { + file.write(text.toUtf8()); + file.close(); + } else { + logError(QString("Could not open config file '%1' for writing: ").arg(this->configFilename) + file.errorString()); + } +} + +const QMap mapSortOrderMap = { + {MapSortOrder::Group, "group"}, + {MapSortOrder::Layout, "layout"}, + {MapSortOrder::Area, "area"}, +}; + +const QMap mapSortOrderReverseMap = { + {"group", MapSortOrder::Group}, + {"layout", MapSortOrder::Layout}, + {"area", MapSortOrder::Area}, +}; + +PorymapConfig porymapConfig; + +void PorymapConfig::parseConfigKeyValue(QString key, QString value) { if (key == "recent_project") { - Config::recentProject = value; + this->recentProject = value; } else if (key == "recent_map") { - Config::recentMap = value; + this->recentMap = value; } else if (key == "pretty_cursors") { bool ok; - Config::prettyCursors = value.toInt(&ok); + this->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); + this->mapSortOrder = mapSortOrderReverseMap.value(sortOrder); } else { - Config::mapSortOrder = MapSortOrder::Group; + this->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)); + logWarn(QString("Invalid config key found in config file %1: '%2'").arg(this->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()); - } +QMap PorymapConfig::getKeyValueMap() { + QMap map; + map.insert("recent_project", this->recentProject); + map.insert("recent_map", this->recentMap); + map.insert("pretty_cursors", this->prettyCursors ? "1" : "0"); + map.insert("map_sort_order", mapSortOrderMap.value(this->mapSortOrder)); + return map; } -void Config::setRecentProject(QString project) { - Config::recentProject = project; - Config::save(); +void PorymapConfig::setRecentProject(QString project) { + this->recentProject = project; + this->save(); } -void Config::setRecentMap(QString map) { - Config::recentMap = map; - Config::save(); +void PorymapConfig::setRecentMap(QString map) { + this->recentMap = map; + this->save(); } -void Config::setMapSortOrder(MapSortOrder order) { - Config::mapSortOrder = order; - Config::save(); +void PorymapConfig::setMapSortOrder(MapSortOrder order) { + this->mapSortOrder = order; + this->save(); } -void Config::setPrettyCursors(bool enabled) { - Config::prettyCursors = enabled; - Config::save(); +void PorymapConfig::setPrettyCursors(bool enabled) { + this->prettyCursors = enabled; + this->save(); } -QString Config::getRecentProject() { - return Config::recentProject; +QString PorymapConfig::getRecentProject() { + return this->recentProject; } -QString Config::getRecentMap() { - return Config::recentMap; +QString PorymapConfig::getRecentMap() { + return this->recentMap; } -MapSortOrder Config::getMapSortOrder() { - return Config::mapSortOrder; +MapSortOrder PorymapConfig::getMapSortOrder() { + return this->mapSortOrder; } -bool Config::getPrettyCursors() { - return Config::prettyCursors; +bool PorymapConfig::getPrettyCursors() { + return this->prettyCursors; } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3a2e0d54..c9903d9f 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -56,7 +56,7 @@ MainWindow::~MainWindow() } void MainWindow::initWindow() { - Config::load(); + porymapConfig.load(); this->initCustomUI(); this->initExtraSignals(); this->initExtraShortcuts(); @@ -157,7 +157,7 @@ void MainWindow::mapSortOrder_changed(QAction *action) { ui->toolButton_MapSortOrder->setIcon(action->icon()); mapSortOrder = static_cast(i); - Config::setMapSortOrder(mapSortOrder); + porymapConfig.setMapSortOrder(mapSortOrder); if (isProjectOpen()) { sortMapList(); @@ -173,13 +173,13 @@ void MainWindow::on_lineEdit_filterBox_textChanged(const QString &arg1) } void MainWindow::loadUserSettings() { - ui->actionBetter_Cursors->setChecked(Config::getPrettyCursors()); - this->editor->settings->betterCursors = Config::getPrettyCursors(); - mapSortOrder = Config::getMapSortOrder(); + ui->actionBetter_Cursors->setChecked(porymapConfig.getPrettyCursors()); + this->editor->settings->betterCursors = porymapConfig.getPrettyCursors(); + mapSortOrder = porymapConfig.getMapSortOrder(); } bool MainWindow::openRecentProject() { - QString default_dir = Config::getRecentProject(); + QString default_dir = porymapConfig.getRecentProject(); if (!default_dir.isNull() && default_dir.length() > 0) { logInfo(QString("Opening recent project: '%1'").arg(default_dir)); return openProject(default_dir); @@ -228,7 +228,7 @@ QString MainWindow::getDefaultMap() { if (editor && editor->project) { QList names = editor->project->groupedMapNames; if (!names.isEmpty()) { - QString recentMap = Config::getRecentMap(); + QString recentMap = porymapConfig.getRecentMap(); if (!recentMap.isNull() && recentMap.length() > 0) { for (int i = 0; i < names.length(); i++) { if (names.value(i).contains(recentMap)) { @@ -255,12 +255,12 @@ QString MainWindow::getExistingDirectory(QString dir) { void MainWindow::on_action_Open_Project_triggered() { QString recent = "."; - if (!Config::getRecentMap().isNull() && Config::getRecentMap().length() > 0) { - recent = Config::getRecentMap(); + if (!porymapConfig.getRecentMap().isNull() && porymapConfig.getRecentMap().length() > 0) { + recent = porymapConfig.getRecentMap(); } QString dir = getExistingDirectory(recent); if (!dir.isEmpty()) { - Config::setRecentProject(dir); + porymapConfig.setRecentProject(dir); openProject(dir); } } @@ -379,7 +379,7 @@ void MainWindow::openWarpMap(QString map_name, QString warp_num) { } void MainWindow::setRecentMap(QString mapName) { - Config::setRecentMap(mapName); + porymapConfig.setRecentMap(mapName); } void MainWindow::displayMapProperties() { @@ -807,7 +807,7 @@ void MainWindow::on_actionZoom_Out_triggered() { } void MainWindow::on_actionBetter_Cursors_triggered() { - Config::setPrettyCursors(ui->actionBetter_Cursors->isChecked()); + porymapConfig.setPrettyCursors(ui->actionBetter_Cursors->isChecked()); this->editor->settings->betterCursors = ui->actionBetter_Cursors->isChecked(); }