From 2e7924a07d45c1d1468552f951dcd2f2dcc948fa Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Mon, 26 Sep 2022 22:14:18 -0400 Subject: Add a few basic settings items. Yes I stole qmp's settings design and silently relicensed the (largely identical) code. But who in the world cares, plus I'm the author of qmp anyway. --- qdeduper/settings.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 qdeduper/settings.cpp (limited to 'qdeduper/settings.cpp') diff --git a/qdeduper/settings.cpp b/qdeduper/settings.cpp new file mode 100644 index 0000000..75dfbfd --- /dev/null +++ b/qdeduper/settings.cpp @@ -0,0 +1,129 @@ +#include + +#include "settings.hpp" + +SettingsRegistry::SettingsRegistry(QString path) +{ + s = new QSettings(path, QSettings::Format::IniFormat); +} +SettingsRegistry::~SettingsRegistry() +{ + delete s; +} +int SettingsRegistry::register_tab(QString tab_name) +{ + tabs.push_back(tab_name); + return tabs.size() - 1; +} + +void SettingsRegistry::register_int_option(int tab, std::string key, QString desc, int min, int max, int defaultval) +{ + klist.push_back(key); + smap[key] = { + SettingsItem::ParameterType::_int, + tab, + key, + desc, + QVariant::fromValue(min), + QVariant::fromValue(max), + QVariant::fromValue(defaultval), + nullptr}; +} + +int SettingsRegistry::get_option_int(std::string key) +{ + if (smap.find(key) == smap.end() || smap[key].type != SettingsItem::ParameterType::_int) + return -1; + return s->value(QString::fromStdString(key), smap[key].defaultv).value(); +} + +void SettingsRegistry::set_option_int(std::string key, int val) +{ + if (smap.find(key) == smap.end() || smap[key].type != SettingsItem::ParameterType::_int) + return; + s->setValue(QString::fromStdString(key), QVariant::fromValue(val)); +} + +void SettingsRegistry::register_bool_option(int tab, std::string key, QString desc, bool defaultval) +{ + klist.push_back(key); + smap[key] = { + SettingsItem::ParameterType::_bool, + tab, + key, + desc, + QVariant(), + QVariant(), + QVariant::fromValue(defaultval), + nullptr}; +} + +bool SettingsRegistry::get_option_bool(std::string key) +{ + if (smap.find(key) == smap.end() || smap[key].type != SettingsItem::ParameterType::_bool) + return false; + return s->value(QString::fromStdString(key), smap[key].defaultv).value(); +} + +void SettingsRegistry::set_option_bool(std::string key, bool val) +{ + if (smap.find(key) == smap.end() || smap[key].type != SettingsItem::ParameterType::_bool) + return; + s->setValue(QString::fromStdString(key), QVariant::fromValue(val)); +} + +void SettingsRegistry::register_double_option(int tab, std::string key, QString desc, double min, double max, double defaultval) +{ + klist.push_back(key); + smap[key] = { + SettingsItem::ParameterType::_double, + tab, + key, + desc, + QVariant::fromValue(min), + QVariant::fromValue(max), + QVariant::fromValue(defaultval), + nullptr}; +} + +double SettingsRegistry::get_option_double(std::string key) +{ + if (smap.find(key) == smap.end() || smap[key].type != SettingsItem::ParameterType::_double) + return std::numeric_limits::quiet_NaN(); + return s->value(QString::fromStdString(key), smap[key].defaultv).value(); +} + +void SettingsRegistry::set_option_double(std::string key, double val) +{ + if (smap.find(key) == smap.end() || smap[key].type != SettingsItem::ParameterType::_double) + return; + s->setValue(QString::fromStdString(key), QVariant::fromValue(val)); +} + +void SettingsRegistry::register_keyseq_option(int tab, std::string key, QString desc, QKeySequence defaultval) +{ + klist.push_back(key); + smap[key] = { + SettingsItem::ParameterType::_keyseq, + tab, + key, + desc, + QVariant(), + QVariant(), + QVariant::fromValue(defaultval), + nullptr}; +} + +QKeySequence SettingsRegistry::get_option_keyseq(std::string key) +{ + if (smap.find(key) == smap.end() || smap[key].type != SettingsItem::ParameterType::_keyseq) + return QKeySequence(); + return s->value(QString::fromStdString(key), smap[key].defaultv).value(); +} + +void SettingsRegistry::set_option_keyseq(std::string key, QKeySequence ks) +{ + if (smap.find(key) == smap.end() || smap[key].type != SettingsItem::ParameterType::_keyseq) + return; + s->setValue(QString::fromStdString(key), QVariant::fromValue(ks)); +} -- cgit v1.2.3