From cd7983adb5f04140e72ad157b03b1eb931293259 Mon Sep 17 00:00:00 2001 From: graffy76 Date: Wed, 7 May 2014 21:59:26 -0500 Subject: [PATCH] Decoupled setting definitions from the Setting class. QSettings implementation is now fully responsible for managing user settings. --- apps/opencs/model/settings/connector.cpp | 1 + apps/opencs/model/settings/setting.cpp | 10 --- apps/opencs/model/settings/setting.hpp | 3 - apps/opencs/model/settings/usersettings.cpp | 83 +++++++++------------ apps/opencs/model/settings/usersettings.hpp | 17 +++-- apps/opencs/view/settings/dialog.cpp | 3 + apps/opencs/view/settings/settingwindow.cpp | 31 ++++++-- apps/opencs/view/settings/settingwindow.hpp | 3 + apps/opencs/view/settings/view.cpp | 7 +- apps/opencs/view/settings/view.hpp | 2 +- 10 files changed, 82 insertions(+), 78 deletions(-) diff --git a/apps/opencs/model/settings/connector.cpp b/apps/opencs/model/settings/connector.cpp index 05a9ba8f9..5e1d64544 100644 --- a/apps/opencs/model/settings/connector.cpp +++ b/apps/opencs/model/settings/connector.cpp @@ -81,6 +81,7 @@ void CSMSettings::Connector::slotUpdateMaster() const } QString masterValue = mMasterView->value (masterColumn); + mMasterView->setSelectedValue (masterValue); } diff --git a/apps/opencs/model/settings/setting.cpp b/apps/opencs/model/settings/setting.cpp index 2ef829f86..2f86d4ff8 100644 --- a/apps/opencs/model/settings/setting.cpp +++ b/apps/opencs/model/settings/setting.cpp @@ -77,16 +77,6 @@ QStringList CSMSettings::Setting::declaredValues() const return property (Property_DeclaredValues); } -void CSMSettings::Setting::setDefinedValues (QStringList list) -{ - setProperty (Property_DefinedValues, list); -} - -QStringList CSMSettings::Setting::definedValues() const -{ - return property (Property_DefinedValues); -} - QStringList CSMSettings::Setting::property (SettingProperty prop) const { if (prop >= mProperties.size()) diff --git a/apps/opencs/model/settings/setting.hpp b/apps/opencs/model/settings/setting.hpp index 89d6cc2c4..e40302f00 100644 --- a/apps/opencs/model/settings/setting.hpp +++ b/apps/opencs/model/settings/setting.hpp @@ -44,9 +44,6 @@ namespace CSMSettings void setDeclaredValues (QStringList list); QStringList declaredValues() const; - void setDefinedValues (QStringList list); - QStringList definedValues() const; - void setDefaultValue (int value); void setDefaultValue (double value); void setDefaultValue (const QString &value); diff --git a/apps/opencs/model/settings/usersettings.cpp b/apps/opencs/model/settings/usersettings.cpp index 8d71dbe82..1a9125439 100644 --- a/apps/opencs/model/settings/usersettings.cpp +++ b/apps/opencs/model/settings/usersettings.cpp @@ -38,31 +38,6 @@ CSMSettings::UserSettings::UserSettings() buildSettingModelDefaults(); } -void CSMSettings::UserSettings::addDefinitions () -{ - foreach (const QString &key, mSettingDefinitions->allKeys()) - { - QStringList names = key.split('/'); - - Setting *setting = findSetting (names.at(0), names.at(1)); - - if (!setting) - { - qWarning() << "Found definitions for undeclared setting " - << names.at(0) << "/" << names.at(1); - removeSetting (names.at(0), names.at(1)); - continue; - } - - QStringList values = mSettingDefinitions->value (key).toStringList(); - - if (values.isEmpty()) - values.append (setting->defaultValues()); - - setting->setDefinedValues (values); - } -} - void CSMSettings::UserSettings::buildSettingModelDefaults() { QString section = "Window Size"; @@ -139,13 +114,14 @@ void CSMSettings::UserSettings::buildSettingModelDefaults() * Defined values * * Values which represent the actual, current value of - * a setting. For settings with declared values, this must be one or - * several declared values, as appropriate. + * a setting. For settings with declared values, this must be one + * or several declared values, as appropriate. * - * Proxy values - values the proxy master updates the proxy slave when - * it's own definition is set / changed. These are definitions for - * proxy slave settings, but must match any declared values the proxy - * slave has, if any. + * Proxy values + * Values the proxy master updates the proxy slave when + * it's own definition is set / changed. These are definitions for + * proxy slave settings, but must match any declared values the + * proxy slave has, if any. *******************************************************************/ /* //create setting objects, specifying the basic widget type, @@ -328,31 +304,36 @@ void CSMSettings::UserSettings::loadSettings (const QString &fileName) mSettingDefinitions = new QSettings (QSettings::IniFormat, QSettings::UserScope, "opencs", QString(), this); +} - addDefinitions(); +bool CSMSettings::UserSettings::hasSettingDefinitions + (const QString &viewKey) const +{ + return (mSettingDefinitions->contains (viewKey)); } -void CSMSettings::UserSettings::saveSettings - (const QMap &settingMap) +void CSMSettings::UserSettings::setDefinitions + (const QString &key, const QStringList &list) { - foreach (const QString &key, settingMap.keys()) - mSettingDefinitions->setValue (key, settingMap.value (key)); + mSettingDefinitions->setValue (key, list); +} +void CSMSettings::UserSettings::saveDefinitions() const +{ mSettingDefinitions->sync(); } QString CSMSettings::UserSettings::settingValue (const QString &settingKey) { - QStringList names = settingKey.split('/'); + if (!mSettingDefinitions->contains (settingKey)) + return QString(); - Setting *setting = findSetting(names.at(0), names.at(1)); + QStringList defs = mSettingDefinitions->value (settingKey).toStringList(); - if (setting) - { - if (!setting->definedValues().isEmpty()) - return setting->definedValues().at(0); - } - return ""; + if (defs.isEmpty()) + return QString(); + + return defs.at(0); } CSMSettings::UserSettings& CSMSettings::UserSettings::instance() @@ -364,11 +345,7 @@ CSMSettings::UserSettings& CSMSettings::UserSettings::instance() void CSMSettings::UserSettings::updateUserSetting(const QString &settingKey, const QStringList &list) { - QStringList names = settingKey.split('/'); - - Setting *setting = findSetting (names.at(0), names.at(1)); - - setting->setDefinedValues (list); + mSettingDefinitions->setValue (settingKey ,list); emit userSettingUpdated (settingKey, list); } @@ -439,3 +416,11 @@ CSMSettings::Setting *CSMSettings::UserSettings::createSetting return setting; } + +QStringList CSMSettings::UserSettings::definitions (const QString &viewKey) const +{ + if (mSettingDefinitions->contains (viewKey)) + return mSettingDefinitions->value (viewKey).toStringList(); + + return QStringList(); +} diff --git a/apps/opencs/model/settings/usersettings.hpp b/apps/opencs/model/settings/usersettings.hpp index 3ac348760..830cc8c69 100644 --- a/apps/opencs/model/settings/usersettings.hpp +++ b/apps/opencs/model/settings/usersettings.hpp @@ -49,8 +49,8 @@ namespace CSMSettings { /// Retrieves the settings file at all three levels (global, local and user). void loadSettings (const QString &fileName); - /// Writes settings to the user's config file path - void saveSettings (const QMap &settingMap); + /// Updates QSettings and syncs with the ini file + void setDefinitions (const QString &key, const QStringList &defs); QString settingValue (const QString &settingKey); @@ -65,10 +65,17 @@ namespace CSMSettings { ///Retreive a map of the settings, keyed by page name SettingPageMap settingPageMap() const; - private: + ///Returns a string list of defined vlaues for the specified setting + ///in "page/name" format. + QStringList definitions (const QString &viewKey) const; + + ///Test to indicate whether or not a setting has any definitions + bool hasSettingDefinitions (const QString &viewKey) const; - ///add definitions to the settings specified in the page map - void addDefinitions(); + ///Save any unsaved changes in the QSettings object + void saveDefinitions() const; + + private: void buildSettingModelDefaults(); diff --git a/apps/opencs/view/settings/dialog.cpp b/apps/opencs/view/settings/dialog.cpp index 4e7b08aff..56bc1fdfe 100644 --- a/apps/opencs/view/settings/dialog.cpp +++ b/apps/opencs/view/settings/dialog.cpp @@ -109,7 +109,10 @@ void CSVSettings::Dialog::closeEvent (QCloseEvent *event) void CSVSettings::Dialog::show() { if (pages().isEmpty()) + { buildPages(); + setViewValues(); + } QPoint screenCenter = QApplication::desktop()->screenGeometry().center(); diff --git a/apps/opencs/view/settings/settingwindow.cpp b/apps/opencs/view/settings/settingwindow.cpp index aeb652b3b..7cdf2bded 100644 --- a/apps/opencs/view/settings/settingwindow.cpp +++ b/apps/opencs/view/settings/settingwindow.cpp @@ -82,6 +82,25 @@ void CSVSettings::SettingWindow::createConnections } } +void CSVSettings::SettingWindow::setViewValues() +{ + //iterate each page and view, setting their definintions + //if they exist in the model + foreach (const Page *page, mPages) + { + foreach (const View *view, page->views()) + { + //testing beforehand prevents overwriting a proxy setting + if (!mModel->hasSettingDefinitions (view->viewKey())) + continue; + + QStringList defs = mModel->definitions (view->viewKey()); + + view->setSelectedValues(defs); + } + } +} + CSVSettings::View *CSVSettings::SettingWindow::findView (const QString &pageName, const QString &setting) { @@ -95,17 +114,19 @@ CSVSettings::View *CSVSettings::SettingWindow::findView void CSVSettings::SettingWindow::saveSettings() { - QMap settingMap; - + //setting the definition in the model automatically syncs with the file foreach (const Page *page, mPages) { foreach (const View *view, page->views()) { - if (view->serializable()) - settingMap[view->viewKey()] = view->selectedValues(); + if (!view->serializable()) + continue; + + mModel->setDefinitions (view->viewKey(), view->selectedValues()); } } - CSMSettings::UserSettings::instance().saveSettings (settingMap); + + mModel->saveDefinitions(); } void CSVSettings::SettingWindow::closeEvent (QCloseEvent *event) diff --git a/apps/opencs/view/settings/settingwindow.hpp b/apps/opencs/view/settings/settingwindow.hpp index 071f1b45e..2266f130d 100644 --- a/apps/opencs/view/settings/settingwindow.hpp +++ b/apps/opencs/view/settings/settingwindow.hpp @@ -47,6 +47,9 @@ namespace CSVSettings { ///save settings from the GUI to file void saveSettings(); + ///sets the defined values for the views that have been created + void setViewValues(); + private: ///create connections between settings (used for proxy settings) diff --git a/apps/opencs/view/settings/view.cpp b/apps/opencs/view/settings/view.cpp index c3b33cf84..69109e2b3 100644 --- a/apps/opencs/view/settings/view.cpp +++ b/apps/opencs/view/settings/view.cpp @@ -26,10 +26,7 @@ CSVSettings::View::View(CSMSettings::Setting *setting, void CSVSettings::View::buildModel (const CSMSettings::Setting *setting) { - QStringList values = setting->definedValues(); - - if (values.isEmpty()) - values.append (setting->defaultValues()); + QStringList values = setting->defaultValues(); if (mHasFixedValues) buildFixedValueModel (setting->declaredValues()); @@ -116,7 +113,7 @@ void CSVSettings::View::setSelectedValue (const QString &value, } void CSVSettings::View::setSelectedValues (const QStringList &list, - bool doViewUpdate, bool signalUpdate) + bool doViewUpdate, bool signalUpdate) const { QItemSelection selection; diff --git a/apps/opencs/view/settings/view.hpp b/apps/opencs/view/settings/view.hpp index 5106d8d90..84ad62759 100644 --- a/apps/opencs/view/settings/view.hpp +++ b/apps/opencs/view/settings/view.hpp @@ -71,7 +71,7 @@ namespace CSVSettings ///or signaling the view was updatedto avoid viscious cylcing. void setSelectedValues (const QStringList &values, bool updateView = true, - bool signalUpdate = true); + bool signalUpdate = true) const; void setSelectedValue (const QString &value, bool updateView = true,