Wed, 08 Mar 2017 22:48:43 +0200
Better encapsulated the BaseConfiguration class.
--- a/src/baseconfiguration.cpp Wed Mar 08 22:09:48 2017 +0200 +++ b/src/baseconfiguration.cpp Wed Mar 08 22:48:43 2017 +0200 @@ -16,34 +16,96 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <QSet> +#include <QApplication> #include <QSettings> #include <QVariant> #include "baseconfiguration.h" #include "mainwindow.h" BaseConfiguration::BaseConfiguration() : - m_settings {MainWindow::makeSettings(this)} {} + m_settings {qApp->applicationDirPath() + "/" UNIXNAME ".ini", QSettings::IniFormat, this} {} -BaseConfiguration::~BaseConfiguration() -{ - delete m_settings; -} - +/* + * Returns the default value for the provided configuration name. + */ QVariant BaseConfiguration::defaultValueByName(const QString& name) { - if (m_isInitialized) + 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; +}
--- a/src/baseconfiguration.h Wed Mar 08 22:09:48 2017 +0200 +++ b/src/baseconfiguration.h Wed Mar 08 22:48:43 2017 +0200 @@ -21,25 +21,34 @@ #include <QSettings> #include "basics.h" +/* + * This class encapsulates a simple interface around QSettings. It is derived by a subclass written by a code generator that implements + * static, type-safe configuration entries. + */ class BaseConfiguration : public QObject { Q_OBJECT public: BaseConfiguration(); - ~BaseConfiguration(); bool existsEntry(const QString& name); QVariant defaultValueByName(const QString& name); virtual void initDefaults(); + void setValue(const QString& name, const QVariant& value); + void setValue(const QString& name, const QVariant& value, const QVariant& defaultValue); + void sync(); + QVariant value(const QString& name); + QVariant value(const QString& name, const QVariant& defaultValue); signals: void configurationChanged(QString, QVariant, QVariant); protected: - QMap<QString, QVariant> m_defaults; - class QSettings* m_settings; + void registerConfigurationEntry(const QString& name, const QVariant& value); private: + QMap<QString, QVariant> m_defaults; + QSettings m_settings; bool m_isInitialized = false; };
--- a/src/dialogs/configdialog.cpp Wed Mar 08 22:09:48 2017 +0200 +++ b/src/dialogs/configdialog.cpp Wed Mar 08 22:48:43 2017 +0200 @@ -73,15 +73,14 @@ ConfigDialog::ConfigDialog (QWidget* parent, ConfigDialog::Tab defaulttab, Qt::WindowFlags f) : QDialog (parent, f), HierarchyElement (parent), - ui (*new Ui_ConfigDialog), - m_settings (MainWindow::makeSettings (this)) + ui (*new Ui_ConfigDialog) { ui.setupUi (this); // Set defaults applyToWidgetOptions([&](QWidget* widget, QString confname) { - QVariant value = m_settings->value (confname, configuration().defaultValueByName (confname)); + QVariant value = configuration().value(confname); QLineEdit* lineedit; QSpinBox* spinbox; QDoubleSpinBox* doublespinbox; @@ -271,7 +270,7 @@ return; } - m_settings->setValue (confname, value); + configuration().setValue(confname, value); }); // Rebuild the quick color toolbar @@ -297,7 +296,7 @@ item->action()->setShortcut (item->sequence()); } - m_window->syncSettings(); + configuration().sync(); currentDocument()->reloadAllSubfiles(); m_documents->loadLogoedStuds(); m_window->renderer()->setBackground();
--- a/src/dialogs/configdialog.h Wed Mar 08 22:09:48 2017 +0200 +++ b/src/dialogs/configdialog.h Wed Mar 08 22:48:43 2017 +0200 @@ -72,7 +72,6 @@ QList<QListWidgetItem*> quickColorItems; QMap<QPushButton*, QColor> m_buttonColors; ExternalProgramWidgets m_externalProgramWidgets[NumExternalPrograms]; - class QSettings* m_settings; QVector<ColorToolbarItem> quickColors; void applySettings();
--- a/src/documentmanager.cpp Wed Mar 08 22:09:48 2017 +0200 +++ b/src/documentmanager.cpp Wed Mar 08 22:48:43 2017 +0200 @@ -380,7 +380,7 @@ // Add the file recentFiles << path; configuration().setRecentFiles (recentFiles); - m_window->syncSettings(); + configuration().sync(); m_window->updateRecentFilesMenu(); }
--- a/src/mainwindow.cpp Wed Mar 08 22:09:48 2017 +0200 +++ b/src/mainwindow.cpp Wed Mar 08 22:48:43 2017 +0200 @@ -53,8 +53,7 @@ QMainWindow (parent, flags), m_primitives(new PrimitiveManager(this)), ui (*new Ui_MainWindow), - m_externalPrograms (nullptr), - m_settings (makeSettings (this)), + m_externalPrograms (nullptr), m_documents (new DocumentManager (this)), m_currentDocument (nullptr), m_isSelectionLocked (false) @@ -167,7 +166,6 @@ { delete m_primitives; delete &ui; - delete m_settings; for (Toolset* toolset : m_toolsets) delete toolset; @@ -585,7 +583,7 @@ // Save the configuration before leaving. configuration().setHiddenToolbars (hiddenToolbars); - syncSettings(); + configuration().sync(); ev->accept(); } @@ -944,7 +942,7 @@ { for (QAction* act : findChildren<QAction*>()) { - QKeySequence seq = m_settings->value ("shortcut_" + act->objectName(), act->shortcut()).value<QKeySequence>(); + QKeySequence seq = configuration().value("shortcut_" + act->objectName(), act->shortcut()).value<QKeySequence>(); act->setShortcut (seq); } } @@ -955,12 +953,7 @@ { applyToActions ([&](QAction* act) { - QString const key = "shortcut_" + act->objectName(); - - if (m_defaultShortcuts[act] != act->shortcut()) - m_settings->setValue (key, act->shortcut()); - else - m_settings->remove (key); + configuration().setValue("shortcut_" + act->objectName(), act->shortcut(), m_defaultShortcuts[act]); }); } @@ -1029,30 +1022,6 @@ ui.ringToolSegmentsLabel->setText (format ("%1 / %2", numerator, denominator)); } -/* - * Returns a settings object that interfaces the ini file. - */ -QSettings* MainWindow::makeSettings(QObject* parent) -{ - QString path = qApp->applicationDirPath() + "/" UNIXNAME ".ini"; - return new QSettings {path, QSettings::IniFormat, parent}; -} - -// --------------------------------------------------------------------------------------------------------------------- -// -void MainWindow::syncSettings() -{ - m_settings->sync(); -} - -// --------------------------------------------------------------------------------------------------------------------- -// -QVariant MainWindow::getConfigValue (QString name) -{ - QVariant value = m_settings->value (name, configuration().defaultValueByName (name)); - return value; -} - // --------------------------------------------------------------------------------------------------------------------- // void MainWindow::createBlankDocument()
--- a/src/mainwindow.h Wed Mar 08 22:09:48 2017 +0200 +++ b/src/mainwindow.h Wed Mar 08 22:48:43 2017 +0200 @@ -79,9 +79,7 @@ void doFullRefresh(); void endAction(); class ExtProgramToolset* externalPrograms(); - QVariant getConfigValue (QString name); QTreeWidget* getPrimitivesTree() const; - class QSettings* getSettings() { return m_settings; } LDColor getUniformSelectedColor(); Canvas* getRendererForDocument(LDDocument* document); void loadShortcuts(); @@ -101,7 +99,6 @@ void setQuickColors (const QVector<ColorToolbarItem> &colors); void spawnContextMenu (const QPoint& position); int suggestInsertPoint(); - void syncSettings(); Q_SLOT void updateActions(); void updateColorToolbar(); void updateDocumentList(); @@ -113,7 +110,6 @@ void updateTitle(); static QPixmap getIcon(QString iconName); - static class QSettings* makeSettings(QObject* parent = nullptr); template<typename... Args> void print(QString formatString, Args... args) @@ -154,7 +150,6 @@ QVector<Toolset*> m_toolsets; QMap<QAction*, ToolInfo> m_toolmap; class ExtProgramToolset* m_externalPrograms; - class QSettings* m_settings; DocumentManager* m_documents; LDDocument* m_currentDocument; DoubleMap<LDObject*, QListWidgetItem*> m_objectsInList;
--- a/src/toolsets/extprogramtoolset.cpp Wed Mar 08 22:09:48 2017 +0200 +++ b/src/toolsets/extprogramtoolset.cpp Wed Mar 08 22:48:43 2017 +0200 @@ -76,22 +76,22 @@ bool ExtProgramToolset::getWineSetting (ExtProgramType program) { - return m_window->getConfigValue (externalProgramName (program) + "UsesWine").toBool(); + return configuration().value(externalProgramName (program) + "UsesWine").toBool(); } QString ExtProgramToolset::getPathSetting (ExtProgramType program) { - return m_window->getConfigValue (externalProgramName (program) + "Path").toString(); + return configuration().value(externalProgramName (program) + "Path").toString(); } void ExtProgramToolset::setPathSetting (ExtProgramType program, QString value) { - m_window->getSettings()->setValue (externalProgramName (program) + "Path", QVariant::fromValue (value)); + configuration().setValue (externalProgramName (program) + "Path", QVariant::fromValue (value)); } void ExtProgramToolset::setWineSetting (ExtProgramType program, bool value) { - m_window->getSettings()->setValue (externalProgramName (program) + "UsesWine", QVariant::fromValue (value)); + configuration().setValue (externalProgramName (program) + "UsesWine", QVariant::fromValue (value)); } QString ExtProgramToolset::externalProgramName (ExtProgramType program)
--- a/tools/configcollector.py Wed Mar 08 22:09:48 2017 +0200 +++ b/tools/configcollector.py Wed Mar 08 22:48:43 2017 +0200 @@ -136,7 +136,7 @@ write('public:') write('\tvoid initDefaults() override;') for declaration in self.declarations.values(): - write('\t{type} {readgate}() const;'.format(**declaration)) + write('\t{type} {readgate}();'.format(**declaration)) for declaration in self.declarations.values(): write('\tvoid {writegate}({typereference} value);'.format(**declaration)) for declaration in filter(lambda declaration: declaration['type'] == 'bool', self.declarations.values()): @@ -145,34 +145,25 @@ write('') def writeSource(self, device, headername): - for qttype in self.qtTypes: + for qttype in sorted(self.qtTypes): device.write('#include <%s>\n' % qttype) device.write('#include "configuration.h"\n') device.write('void Configuration::initDefaults()\n') device.write('{\n') device.write('\tBaseConfiguration::initDefaults();\n') for declaration in self.declarations.values(): - device.write('\tm_defaults["{name}"] = QVariant::fromValue<{type}>({default});\n'.format(**declaration)) + device.write('\tregisterConfigurationEntry("{name}", QVariant::fromValue<{type}>({default}));\n'.format(**declaration)) device.write('}\n') for declaration in self.declarations.values(): - device.write('{type} Configuration::{readgate}() const\n'.format(**declaration)) + device.write('{type} Configuration::{readgate}()\n'.format(**declaration)) device.write('{\n') - device.write('\tstatic const QVariant defaultvalue = QVariant::fromValue<{type}>({default});\n'.format(**declaration)) - device.write('\treturn m_settings->value("{name}", defaultvalue).value<{type}>();\n'.format(**declaration)) + device.write('\treturn value("{name}").value<{type}>();\n'.format(**declaration)) device.write('}\n') device.write('\n') for declaration in self.declarations.values(): device.write('void Configuration::{writegate}({typereference} value)\n'.format(**declaration)) device.write('{\n') - device.write('\tif (value != {readgate}())\n'.format(**declaration)) - device.write('\t{\n') - device.write('\t\tQVariant oldValue = {readgate}();\n'.format(**declaration)) - device.write('\t\tif(value != {default})\n'.format(**declaration)) - device.write('\t\t\tm_settings->setValue("{name}", QVariant::fromValue<{type}>(value));\n'.format(**declaration)) - device.write('\t\telse\n') - device.write('\t\t\tm_settings->remove("{name}");\n'.format(**declaration)) - device.write('\t\temit configurationChanged("{name}", oldValue, value);\n'.format(**declaration)) - device.write('\t}\n') + device.write('\tsetValue("{name}", QVariant::fromValue(value));\n'.format(**declaration)) device.write('}\n') device.write('\n') for declaration in filter(lambda declaration: declaration['type'] == 'bool', self.declarations.values()):