Wed, 08 Mar 2017 22:48:43 +0200
Better encapsulated the BaseConfiguration class.
/* * LDForge: LDraw parts authoring CAD * Copyright (C) 2013 - 2017 Teemu Piippo * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <QApplication> #include <QSettings> #include <QVariant> #include "baseconfiguration.h" #include "mainwindow.h" BaseConfiguration::BaseConfiguration() : m_settings {qApp->applicationDirPath() + "/" UNIXNAME ".ini", QSettings::IniFormat, this} {} /* * Returns the default value for the provided configuration name. */ QVariant BaseConfiguration::defaultValueByName(const QString& name) { if (not m_isInitialized) initDefaults(); return m_defaults.value(name); } /* * Returns whether or not a configuration entry exists for the provided value. */ bool BaseConfiguration::existsEntry(const QString& name) { if (not m_isInitialized) initDefaults(); return m_defaults.find(name) != m_defaults.end(); } /* * Initializes the default value map in the configuration. */ void BaseConfiguration::initDefaults() { m_isInitialized = true; } /* * Returns a configuration value by name. */ QVariant BaseConfiguration::value(const QString& name) { return m_settings.value(name, defaultValueByName(name)); } /* * Returns a configuration value by name. This particular overload allows overriding the default value. */ QVariant BaseConfiguration::value(const QString& name, const QVariant& defaultValue) { return m_settings.value(name, defaultValue); } /* * Sets a configuration value by name. */ void BaseConfiguration::setValue(const QString& name, const QVariant& value) { if (value != defaultValueByName(name)) m_settings.setValue(name, value); else m_settings.remove(name); } /* * Sets a configuration value by name and overridden default value. Please only use this if you indeed are in charge of the default value. */ void BaseConfiguration::setValue(const QString& name, const QVariant& value, const QVariant& defaultValue) { if (value != defaultValue) m_settings.setValue(name, value); else m_settings.remove(name); } /* * Syncs settings now. */ void BaseConfiguration::sync() { m_settings.sync(); } /* * Called by subclasses to register configuration entries. */ void BaseConfiguration::registerConfigurationEntry(const QString& name, const QVariant& value) { m_defaults[name] = value; }