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 <QGraphicsSceneMouseEvent>
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include "project.h" #include "project.h"
#include "config.h"
#include "map.h" #include "map.h"
#include "editor.h" #include "editor.h"
#include "tileseteditor.h" #include "tileseteditor.h"
@ -164,11 +165,7 @@ private:
bool isProgrammaticEventTabChange; bool isProgrammaticEventTabChange;
enum MapSortOrder { MapSortOrder mapSortOrder;
Group = 0,
Name = 1,
Layout = 2,
} mapSortOrder;
bool setMap(QString, bool scrollTreeView = false); bool setMap(QString, bool scrollTreeView = false);
void redrawMapScene(); void redrawMapScene();

View file

@ -46,6 +46,7 @@ SOURCES += src/core/block.cpp \
src/ui/tileseteditor.cpp \ src/ui/tileseteditor.cpp \
src/ui/tileseteditormetatileselector.cpp \ src/ui/tileseteditormetatileselector.cpp \
src/ui/tileseteditortileselector.cpp \ src/ui/tileseteditortileselector.cpp \
src/config.cpp \
src/editor.cpp \ src/editor.cpp \
src/main.cpp \ src/main.cpp \
src/mainwindow.cpp \ src/mainwindow.cpp \
@ -87,6 +88,7 @@ HEADERS += include/core/block.h \
include/ui/tileseteditor.h \ include/ui/tileseteditor.h \
include/ui/tileseteditormetatileselector.h \ include/ui/tileseteditormetatileselector.h \
include/ui/tileseteditortileselector.h \ include/ui/tileseteditortileselector.h \
include/config.h \
include/editor.h \ include/editor.h \
include/mainwindow.h \ include/mainwindow.h \
include/project.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 <QFileDialog>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QShortcut> #include <QShortcut>
#include <QSettings>
#include <QSpinBox> #include <QSpinBox>
#include <QTextEdit> #include <QTextEdit>
#include <QSpacerItem> #include <QSpacerItem>
@ -57,6 +56,7 @@ MainWindow::~MainWindow()
} }
void MainWindow::initWindow() { void MainWindow::initWindow() {
Config::load();
this->initCustomUI(); this->initCustomUI();
this->initExtraSignals(); this->initExtraSignals();
this->initExtraShortcuts(); this->initExtraShortcuts();
@ -143,8 +143,6 @@ void MainWindow::initMapSortOrder() {
void MainWindow::mapSortOrder_changed(QAction *action) void MainWindow::mapSortOrder_changed(QAction *action)
{ {
QSettings settings;
QList<QAction*> items = ui->toolButton_MapSortOrder->menu()->actions(); QList<QAction*> items = ui->toolButton_MapSortOrder->menu()->actions();
int i = 0; int i = 0;
for (; i < items.count(); i++) for (; i < items.count(); i++)
@ -159,8 +157,7 @@ void MainWindow::mapSortOrder_changed(QAction *action)
{ {
ui->toolButton_MapSortOrder->setIcon(action->icon()); ui->toolButton_MapSortOrder->setIcon(action->icon());
mapSortOrder = static_cast<MapSortOrder>(i); mapSortOrder = static_cast<MapSortOrder>(i);
settings.setValue("map_sort_order", i); Config::setMapSortOrder(mapSortOrder);
if (isProjectOpen()) if (isProjectOpen())
{ {
sortMapList(); sortMapList();
@ -176,28 +173,16 @@ void MainWindow::on_lineEdit_filterBox_textChanged(const QString &arg1)
} }
void MainWindow::loadUserSettings() { void MainWindow::loadUserSettings() {
QSettings settings; ui->actionBetter_Cursors->setChecked(Config::getPrettyCursors());
this->editor->settings->betterCursors = Config::getPrettyCursors();
bool betterCursors = settings.contains("cursor_mode") && settings.value("cursor_mode") != "0"; mapSortOrder = Config::getMapSortOrder();
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());
} }
bool MainWindow::openRecentProject() { bool MainWindow::openRecentProject() {
QSettings settings; QString default_dir = Config::getRecentProject();
QString key = "recent_projects"; if (!default_dir.isNull() && default_dir.length() > 0) {
if (settings.contains(key)) { logInfo(QString("Opening recent project: '%1'").arg(default_dir));
QString default_dir = settings.value(key).toStringList().last(); return openProject(default_dir);
if (!default_dir.isNull()) {
logInfo(QString("Opening recent project: '%1'").arg(default_dir));
return openProject(default_dir);
}
} }
return true; return true;
@ -243,16 +228,11 @@ QString MainWindow::getDefaultMap() {
if (editor && editor->project) { if (editor && editor->project) {
QList<QStringList> names = editor->project->groupedMapNames; QList<QStringList> names = editor->project->groupedMapNames;
if (!names.isEmpty()) { if (!names.isEmpty()) {
QSettings settings; QString recentMap = Config::getRecentMap();
QString key = "project:" + editor->project->root; if (!recentMap.isNull() && recentMap.length() > 0) {
if (settings.contains(key)) { for (int i = 0; i < names.length(); i++) {
QMap<QString, QVariant> qmap = settings.value(key).toMap(); if (names.value(i).contains(recentMap)) {
if (qmap.contains("recent_map")) { return recentMap;
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;
}
} }
} }
} }
@ -274,22 +254,13 @@ QString MainWindow::getExistingDirectory(QString dir) {
void MainWindow::on_action_Open_Project_triggered() void MainWindow::on_action_Open_Project_triggered()
{ {
QSettings settings;
QString key = "recent_projects";
QString recent = "."; QString recent = ".";
if (settings.contains(key)) { if (!Config::getRecentMap().isNull() && Config::getRecentMap().length() > 0) {
recent = settings.value(key).toStringList().last(); recent = Config::getRecentMap();
} }
QString dir = getExistingDirectory(recent); QString dir = getExistingDirectory(recent);
if (!dir.isEmpty()) { if (!dir.isEmpty()) {
QStringList recents; Config::setRecentProject(dir);
if (settings.contains(key)) {
recents = settings.value(key).toStringList();
}
recents.removeAll(dir);
recents.append(dir);
settings.setValue(key, recents);
openProject(dir); openProject(dir);
} }
} }
@ -407,15 +378,8 @@ void MainWindow::openWarpMap(QString map_name, QString warp_num) {
} }
} }
void MainWindow::setRecentMap(QString map_name) { void MainWindow::setRecentMap(QString mapName) {
QSettings settings; Config::setRecentMap(mapName);
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::displayMapProperties() { void MainWindow::displayMapProperties() {
@ -591,7 +555,7 @@ void MainWindow::sortMapList() {
} }
} }
break; break;
case MapSortOrder::Name: case MapSortOrder::Area:
{ {
QMap<QString, int> mapsecToGroupNum; QMap<QString, int> mapsecToGroupNum;
for (int i = 0; i < project->regionMapSections->length(); i++) { 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() { void MainWindow::on_actionBetter_Cursors_triggered() {
QSettings settings; Config::setPrettyCursors(ui->actionBetter_Cursors->isChecked());
settings.setValue("cursor_mode", QString::number(ui->actionBetter_Cursors->isChecked()));
this->editor->settings->betterCursors = ui->actionBetter_Cursors->isChecked(); this->editor->settings->betterCursors = ui->actionBetter_Cursors->isChecked();
} }