Decoupled setting definitions from the Setting class. QSettings

implementation is now fully responsible for managing user settings.
openmw-30
graffy76 11 years ago
parent e47e3de3d2
commit cd7983adb5

@ -81,6 +81,7 @@ void CSMSettings::Connector::slotUpdateMaster() const
} }
QString masterValue = mMasterView->value (masterColumn); QString masterValue = mMasterView->value (masterColumn);
mMasterView->setSelectedValue (masterValue); mMasterView->setSelectedValue (masterValue);
} }

@ -77,16 +77,6 @@ QStringList CSMSettings::Setting::declaredValues() const
return property (Property_DeclaredValues); 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 QStringList CSMSettings::Setting::property (SettingProperty prop) const
{ {
if (prop >= mProperties.size()) if (prop >= mProperties.size())

@ -44,9 +44,6 @@ namespace CSMSettings
void setDeclaredValues (QStringList list); void setDeclaredValues (QStringList list);
QStringList declaredValues() const; QStringList declaredValues() const;
void setDefinedValues (QStringList list);
QStringList definedValues() const;
void setDefaultValue (int value); void setDefaultValue (int value);
void setDefaultValue (double value); void setDefaultValue (double value);
void setDefaultValue (const QString &value); void setDefaultValue (const QString &value);

@ -38,31 +38,6 @@ CSMSettings::UserSettings::UserSettings()
buildSettingModelDefaults(); 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() void CSMSettings::UserSettings::buildSettingModelDefaults()
{ {
QString section = "Window Size"; QString section = "Window Size";
@ -139,13 +114,14 @@ void CSMSettings::UserSettings::buildSettingModelDefaults()
* Defined values * Defined values
* *
* Values which represent the actual, current value of * Values which represent the actual, current value of
* a setting. For settings with declared values, this must be one or * a setting. For settings with declared values, this must be one
* several declared values, as appropriate. * or several declared values, as appropriate.
* *
* Proxy values - values the proxy master updates the proxy slave when * Proxy values
* it's own definition is set / changed. These are definitions for * Values the proxy master updates the proxy slave when
* proxy slave settings, but must match any declared values the proxy * it's own definition is set / changed. These are definitions for
* slave has, if any. * proxy slave settings, but must match any declared values the
* proxy slave has, if any.
*******************************************************************/ *******************************************************************/
/* /*
//create setting objects, specifying the basic widget type, //create setting objects, specifying the basic widget type,
@ -328,31 +304,36 @@ void CSMSettings::UserSettings::loadSettings (const QString &fileName)
mSettingDefinitions = new QSettings mSettingDefinitions = new QSettings
(QSettings::IniFormat, QSettings::UserScope, "opencs", QString(), this); (QSettings::IniFormat, QSettings::UserScope, "opencs", QString(), this);
}
addDefinitions(); bool CSMSettings::UserSettings::hasSettingDefinitions
(const QString &viewKey) const
{
return (mSettingDefinitions->contains (viewKey));
} }
void CSMSettings::UserSettings::saveSettings void CSMSettings::UserSettings::setDefinitions
(const QMap <QString, QStringList> &settingMap) (const QString &key, const QStringList &list)
{ {
foreach (const QString &key, settingMap.keys()) mSettingDefinitions->setValue (key, list);
mSettingDefinitions->setValue (key, settingMap.value (key)); }
void CSMSettings::UserSettings::saveDefinitions() const
{
mSettingDefinitions->sync(); mSettingDefinitions->sync();
} }
QString CSMSettings::UserSettings::settingValue (const QString &settingKey) 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 (defs.isEmpty())
{ return QString();
if (!setting->definedValues().isEmpty())
return setting->definedValues().at(0); return defs.at(0);
}
return "";
} }
CSMSettings::UserSettings& CSMSettings::UserSettings::instance() CSMSettings::UserSettings& CSMSettings::UserSettings::instance()
@ -364,11 +345,7 @@ CSMSettings::UserSettings& CSMSettings::UserSettings::instance()
void CSMSettings::UserSettings::updateUserSetting(const QString &settingKey, void CSMSettings::UserSettings::updateUserSetting(const QString &settingKey,
const QStringList &list) const QStringList &list)
{ {
QStringList names = settingKey.split('/'); mSettingDefinitions->setValue (settingKey ,list);
Setting *setting = findSetting (names.at(0), names.at(1));
setting->setDefinedValues (list);
emit userSettingUpdated (settingKey, list); emit userSettingUpdated (settingKey, list);
} }
@ -439,3 +416,11 @@ CSMSettings::Setting *CSMSettings::UserSettings::createSetting
return setting; return setting;
} }
QStringList CSMSettings::UserSettings::definitions (const QString &viewKey) const
{
if (mSettingDefinitions->contains (viewKey))
return mSettingDefinitions->value (viewKey).toStringList();
return QStringList();
}

@ -49,8 +49,8 @@ namespace CSMSettings {
/// Retrieves the settings file at all three levels (global, local and user). /// Retrieves the settings file at all three levels (global, local and user).
void loadSettings (const QString &fileName); void loadSettings (const QString &fileName);
/// Writes settings to the user's config file path /// Updates QSettings and syncs with the ini file
void saveSettings (const QMap <QString, QStringList > &settingMap); void setDefinitions (const QString &key, const QStringList &defs);
QString settingValue (const QString &settingKey); QString settingValue (const QString &settingKey);
@ -65,10 +65,17 @@ namespace CSMSettings {
///Retreive a map of the settings, keyed by page name ///Retreive a map of the settings, keyed by page name
SettingPageMap settingPageMap() const; 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 ///Save any unsaved changes in the QSettings object
void addDefinitions(); void saveDefinitions() const;
private:
void buildSettingModelDefaults(); void buildSettingModelDefaults();

@ -109,7 +109,10 @@ void CSVSettings::Dialog::closeEvent (QCloseEvent *event)
void CSVSettings::Dialog::show() void CSVSettings::Dialog::show()
{ {
if (pages().isEmpty()) if (pages().isEmpty())
{
buildPages(); buildPages();
setViewValues();
}
QPoint screenCenter = QApplication::desktop()->screenGeometry().center(); QPoint screenCenter = QApplication::desktop()->screenGeometry().center();

@ -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 CSVSettings::View *CSVSettings::SettingWindow::findView
(const QString &pageName, const QString &setting) (const QString &pageName, const QString &setting)
{ {
@ -95,17 +114,19 @@ CSVSettings::View *CSVSettings::SettingWindow::findView
void CSVSettings::SettingWindow::saveSettings() void CSVSettings::SettingWindow::saveSettings()
{ {
QMap <QString, QStringList> settingMap; //setting the definition in the model automatically syncs with the file
foreach (const Page *page, mPages) foreach (const Page *page, mPages)
{ {
foreach (const View *view, page->views()) foreach (const View *view, page->views())
{ {
if (view->serializable()) if (!view->serializable())
settingMap[view->viewKey()] = view->selectedValues(); continue;
mModel->setDefinitions (view->viewKey(), view->selectedValues());
} }
} }
CSMSettings::UserSettings::instance().saveSettings (settingMap);
mModel->saveDefinitions();
} }
void CSVSettings::SettingWindow::closeEvent (QCloseEvent *event) void CSVSettings::SettingWindow::closeEvent (QCloseEvent *event)

@ -47,6 +47,9 @@ namespace CSVSettings {
///save settings from the GUI to file ///save settings from the GUI to file
void saveSettings(); void saveSettings();
///sets the defined values for the views that have been created
void setViewValues();
private: private:
///create connections between settings (used for proxy settings) ///create connections between settings (used for proxy settings)

@ -26,10 +26,7 @@ CSVSettings::View::View(CSMSettings::Setting *setting,
void CSVSettings::View::buildModel (const CSMSettings::Setting *setting) void CSVSettings::View::buildModel (const CSMSettings::Setting *setting)
{ {
QStringList values = setting->definedValues(); QStringList values = setting->defaultValues();
if (values.isEmpty())
values.append (setting->defaultValues());
if (mHasFixedValues) if (mHasFixedValues)
buildFixedValueModel (setting->declaredValues()); buildFixedValueModel (setting->declaredValues());
@ -116,7 +113,7 @@ void CSVSettings::View::setSelectedValue (const QString &value,
} }
void CSVSettings::View::setSelectedValues (const QStringList &list, void CSVSettings::View::setSelectedValues (const QStringList &list,
bool doViewUpdate, bool signalUpdate) bool doViewUpdate, bool signalUpdate) const
{ {
QItemSelection selection; QItemSelection selection;

@ -71,7 +71,7 @@ namespace CSVSettings
///or signaling the view was updatedto avoid viscious cylcing. ///or signaling the view was updatedto avoid viscious cylcing.
void setSelectedValues (const QStringList &values, void setSelectedValues (const QStringList &values,
bool updateView = true, bool updateView = true,
bool signalUpdate = true); bool signalUpdate = true) const;
void setSelectedValue (const QString &value, void setSelectedValue (const QString &value,
bool updateView = true, bool updateView = true,

Loading…
Cancel
Save