Add porymap config file

This commit is contained in:
Marcus Huderle 2018-12-21 09:25:28 -06:00
parent dcf6b5af6b
commit 99a21ccf29
5 changed files with 200 additions and 63 deletions

33
include/config.h Normal file
View file

@ -0,0 +1,33 @@
#ifndef CONFIG_H
#define CONFIG_H
#include <QString>
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

View file

@ -10,6 +10,7 @@
#include <QGraphicsSceneMouseEvent>
#include <QAbstractItemModel>
#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();

View file

@ -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 \

142
src/config.cpp Normal file
View file

@ -0,0 +1,142 @@
#include "config.h"
#include "log.h"
#include <QDir>
#include <QFile>
#include <QList>
#include <QTextStream>
#include <QRegularExpression>
const QString configFilename = "porymap.cfg";
QString Config::recentProject = "";
QString Config::recentMap = "";
MapSortOrder Config::mapSortOrder = MapSortOrder::Group;
bool Config::prettyCursors = true;
const QMap<MapSortOrder, QString> mapSortOrderMap = {
{MapSortOrder::Group, "group"},
{MapSortOrder::Layout, "layout"},
{MapSortOrder::Area, "area"},
};
const QMap<QString, MapSortOrder> 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<QString> configLines;
QRegularExpression re("^(?<key>.+)=(?<value>.+)$");
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;
}

View file

@ -11,7 +11,6 @@
#include <QFileDialog>
#include <QStandardItemModel>
#include <QShortcut>
#include <QSettings>
#include <QSpinBox>
#include <QTextEdit>
#include <QSpacerItem>
@ -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<QAction*> 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<MapSortOrder>(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<MapSortOrder>(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<QStringList> names = editor->project->groupedMapNames;
if (!names.isEmpty()) {
QSettings settings;
QString key = "project:" + editor->project->root;
if (settings.contains(key)) {
QMap<QString, QVariant> 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<QString, QVariant> 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<QString, int> 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();
}