From 6486f342b5081dae717bcad49b1cd0775ab12309 Mon Sep 17 00:00:00 2001 From: graffy76 Date: Wed, 17 Jul 2013 20:19:01 -0500 Subject: [PATCH 1/2] Fixed setting synchronization issue Moved default settings to CSMSettings::UserSettings. Other minor code changes / cleanup --- apps/opencs/model/settings/settingsitem.hpp | 2 +- apps/opencs/model/settings/support.hpp | 2 +- apps/opencs/model/settings/usersettings.cpp | 118 +++++++++++++++--- apps/opencs/model/settings/usersettings.hpp | 13 +- apps/opencs/view/doc/view.cpp | 5 +- .../view/settings/datadisplayformatpage.cpp | 2 +- apps/opencs/view/settings/itemblock.hpp | 2 +- .../view/settings/usersettingsdialog.cpp | 10 +- apps/opencs/view/settings/windowpage.cpp | 4 +- 9 files changed, 126 insertions(+), 32 deletions(-) diff --git a/apps/opencs/model/settings/settingsitem.hpp b/apps/opencs/model/settings/settingsitem.hpp index c2587e892f..a1daee4ac9 100644 --- a/apps/opencs/model/settings/settingsitem.hpp +++ b/apps/opencs/model/settings/settingsitem.hpp @@ -34,7 +34,7 @@ namespace CSMSettings bool updateItem (const QString &value); bool updateItem (int valueListIndex); - /// retroeve list of valid values for setting + /// retrieve list of valid values for setting inline QStringList *getValueList() { return mValueList; } /// write list of valid values for setting diff --git a/apps/opencs/model/settings/support.hpp b/apps/opencs/model/settings/support.hpp index 1155d2a298..4ffd01b73c 100644 --- a/apps/opencs/model/settings/support.hpp +++ b/apps/opencs/model/settings/support.hpp @@ -14,7 +14,7 @@ namespace CSMSettings typedef QList SettingList; typedef QMap SettingMap; - typedef QMap SectionMap; + typedef QMap SectionMap; struct QStringPair { diff --git a/apps/opencs/model/settings/usersettings.cpp b/apps/opencs/model/settings/usersettings.cpp index d106110d0d..1ce28ed75c 100644 --- a/apps/opencs/model/settings/usersettings.cpp +++ b/apps/opencs/model/settings/usersettings.cpp @@ -42,6 +42,33 @@ CSMSettings::UserSettings::UserSettings() mReadOnlyMessage = QObject::tr("
Could not open file for reading

\ Please make sure you have the right permissions and try again.
"); + + buildEditorSettingDefaults(); +} + +void CSMSettings::UserSettings::buildEditorSettingDefaults() +{ + SettingContainer *windowHeight = new SettingContainer("768", this); + SettingContainer *windowWidth = new SettingContainer("1024", this); + SettingContainer *rsDelegate = new SettingContainer("Icon and Text", this); + SettingContainer *refIdTypeDelegate = new SettingContainer("Icon and Text", this); + + windowHeight->setObjectName ("Height"); + windowWidth->setObjectName ("Width"); + rsDelegate->setObjectName ("Record Status Display"); + refIdTypeDelegate->setObjectName ("Referenceable ID Type Display"); + + SettingMap *displayFormatMap = new SettingMap; + SettingMap *windowSizeMap = new SettingMap; + + displayFormatMap->insert (rsDelegate->objectName(), rsDelegate ); + displayFormatMap->insert (refIdTypeDelegate->objectName(), refIdTypeDelegate); + + windowSizeMap->insert (windowWidth->objectName(), windowWidth ); + windowSizeMap->insert (windowHeight->objectName(), windowHeight ); + + mEditorSettingDefaults.insert ("Display Format", displayFormatMap); + mEditorSettingDefaults.insert ("Window Size", windowSizeMap); } CSMSettings::UserSettings::~UserSettings() @@ -104,17 +131,22 @@ bool CSMSettings::UserSettings::writeSettings(QMapdevice()->close(); delete stream; stream = 0; } + mergeMap (loadedSettings); + return success; } +void CSMSettings::UserSettings::mergeMap (const CSMSettings::SectionMap §ionSettings) +{ + foreach (QString key, sectionSettings.uniqueKeys()) + { + // insert entire section if it does not already exist in the loaded files + if (mSectionSettings.find(key) == mSectionSettings.end()) + mSectionSettings.insert(key, sectionSettings.value(key)); + else + { + SettingMap *passedSettings = sectionSettings.value(key); + SettingMap *settings = mSectionSettings.value(key); + + foreach (QString key2, passedSettings->uniqueKeys()) + { + //insert section settings individially if they do not already exist + if (settings->find(key2) == settings->end()) + settings->insert(key2, passedSettings->value(key2)); + else + { + settings->value(key2)->update(passedSettings->value(key2)->getValue()); + } + } + } + } +} + void CSMSettings::UserSettings::loadSettings (const QString &fileName) { + mSectionSettings.clear(); + //global QString globalFilePath = QString::fromStdString(mCfgMgr.getGlobalPath().string()) + fileName; bool globalOk = loadFromFile(globalFilePath); @@ -207,10 +269,11 @@ void CSMSettings::UserSettings::loadSettings (const QString &fileName) void CSMSettings::UserSettings::updateSettings (const QString §ionName, const QString &settingName) { - if (mSectionSettings.find(sectionName) == mSectionSettings.end()) - return; - SettingMap *settings = mSectionSettings.value(sectionName); + SettingMap *settings = getValidSettings(sectionName); + + if (!settings) + return; if (settingName.isEmpty()) { @@ -229,15 +292,12 @@ void CSMSettings::UserSettings::updateSettings (const QString §ionName, cons QString CSMSettings::UserSettings::getSetting (const QString §ion, const QString &setting) const { + SettingMap *settings = getValidSettings(section); + QString retVal = ""; - if (mSectionSettings.find(section) != mSectionSettings.end()) - { - CSMSettings::SettingMap *settings = mSectionSettings.value(section); - - if (settings->find(setting) != settings->end()) - retVal = settings->value(setting)->getValue(); - } + if (settings->find(setting) != settings->end()) + retVal = settings->value(setting)->getValue(); return retVal; } @@ -263,3 +323,33 @@ void CSMSettings::UserSettings::displayFileErrorMessage(const QString &message, msgBox.exec(); } + +CSMSettings::SettingMap * +CSMSettings::UserSettings::getValidSettings (const QString §ionName) const +{ + SettingMap *settings = 0; + + //copy the default values for the entire section if it's not found + if (mSectionSettings.find(sectionName) == mSectionSettings.end()) + { + if (mEditorSettingDefaults.find(sectionName) != mEditorSettingDefaults.end()) + settings = mEditorSettingDefaults.value (sectionName); + } + //otherwise, iterate the section's settings, looking for missing values and replacing them with defaults. + else + { + SettingMap *loadedSettings = mSectionSettings[sectionName]; + SettingMap *defaultSettings = mEditorSettingDefaults[sectionName]; + + foreach (QString key, defaultSettings->uniqueKeys()) + { + //write the default value to the loaded settings + if (loadedSettings->find((key))==loadedSettings->end()) + loadedSettings->insert(key, defaultSettings->value(key)); + } + + settings = mSectionSettings.value (sectionName); + } + + return settings; +} diff --git a/apps/opencs/model/settings/usersettings.hpp b/apps/opencs/model/settings/usersettings.hpp index 49b226df90..63e78bd612 100644 --- a/apps/opencs/model/settings/usersettings.hpp +++ b/apps/opencs/model/settings/usersettings.hpp @@ -27,6 +27,8 @@ namespace CSMSettings { Q_OBJECT SectionMap mSectionSettings; + SectionMap mEditorSettingDefaults; + static UserSettings *mUserSettingsInstance; QString mUserFilePath; Files::ConfigurationManager mCfgMgr; @@ -58,7 +60,9 @@ namespace CSMSettings { void loadSettings (const QString &fileName); /// Returns the entire map of settings across all sections - const SectionMap &getSettings () const; + const SectionMap &getSectionMap () const; + + const SettingMap *getSettings (const QString §ionName) const; /// Retrieves the value as a QString of the specified setting in the specified section QString getSetting(const QString §ion, const QString &setting) const; @@ -72,8 +76,15 @@ namespace CSMSettings { /// Parses a setting file specified in filePath from the provided text stream. bool loadFromFile (const QString &filePath = ""); + /// merge the passed map into mSectionSettings + void mergeMap (const SectionMap &); + void displayFileErrorMessage(const QString &message, bool isReadOnly); + void buildEditorSettingDefaults(); + + SettingMap *getValidSettings (const QString §ionName) const; + signals: void signalUpdateEditorSetting (const QString &settingName, const QString &settingValue); diff --git a/apps/opencs/view/doc/view.cpp b/apps/opencs/view/doc/view.cpp index 3c061ca8a6..31e5a00a40 100644 --- a/apps/opencs/view/doc/view.cpp +++ b/apps/opencs/view/doc/view.cpp @@ -182,10 +182,7 @@ CSVDoc::View::View (ViewManager& viewManager, CSMDoc::Document *document, int to QString width = CSMSettings::UserSettings::instance().getSetting(QString("Window Size"), QString("Width")); QString height = CSMSettings::UserSettings::instance().getSetting(QString("Window Size"), QString("Height")); - if(width==QString() || height==QString()) - resize(800, 600); - else - resize (width.toInt(), height.toInt()); + resize (width.toInt(), height.toInt()); mSubViewWindow.setDockOptions (QMainWindow::AllowNestedDocks); diff --git a/apps/opencs/view/settings/datadisplayformatpage.cpp b/apps/opencs/view/settings/datadisplayformatpage.cpp index cf25237f94..332b68f5c4 100755 --- a/apps/opencs/view/settings/datadisplayformatpage.cpp +++ b/apps/opencs/view/settings/datadisplayformatpage.cpp @@ -12,7 +12,7 @@ CSVSettings::GroupBlockDef *CSVSettings::DataDisplayFormatPage::setupDataDisplay { GroupBlockDef *statusBlock = new GroupBlockDef(QString(title)); - SettingsItemDef *statusItem = new SettingsItemDef (statusBlock->title, "Icon and Text"); + SettingsItemDef *statusItem = new SettingsItemDef (statusBlock->title, "Icon Only"); *(statusItem->valueList) << QString("Icon and Text") << QString("Icon Only") << QString("Text Only"); WidgetDef statusWidget (Widget_RadioButton); diff --git a/apps/opencs/view/settings/itemblock.hpp b/apps/opencs/view/settings/itemblock.hpp index 1a5447e312..2d1d45d418 100644 --- a/apps/opencs/view/settings/itemblock.hpp +++ b/apps/opencs/view/settings/itemblock.hpp @@ -15,7 +15,7 @@ namespace CSVSettings ItemBlock (QWidget* parent = 0); - /// pure virtual function not implemneted + /// pure virtual function not implemented bool updateSettings (const CSMSettings::SettingMap &settings) { return false; } CSMSettings::SettingList *getSettings (); diff --git a/apps/opencs/view/settings/usersettingsdialog.cpp b/apps/opencs/view/settings/usersettingsdialog.cpp index 947e8cd1b0..21311c2dab 100644 --- a/apps/opencs/view/settings/usersettingsdialog.cpp +++ b/apps/opencs/view/settings/usersettingsdialog.cpp @@ -44,7 +44,6 @@ void CSVSettings::UserSettingsDialog::closeEvent (QCloseEvent *event) void CSVSettings::UserSettingsDialog::setWidgetStates () { CSMSettings::UserSettings::instance().loadSettings("opencs.cfg"); - const CSMSettings::SectionMap §ionSettings = CSMSettings::UserSettings::instance().getSettings(); //iterate the tabWidget's pages (sections) for (int i = 0; i < mStackedWidget->count(); i++) @@ -53,12 +52,9 @@ void CSVSettings::UserSettingsDialog::setWidgetStates () //and update widget QString pageName = mStackedWidget->widget(i)->objectName(); - if (sectionSettings.find(pageName) != sectionSettings.end()) - { - CSMSettings::SettingMap *settings = sectionSettings.value(pageName); - AbstractPage &page = getAbstractPage (i); - page.initializeWidgets(*settings); - } + const CSMSettings::SettingMap *settings = CSMSettings::UserSettings::instance().getSettings(pageName); + AbstractPage &page = getAbstractPage (i); + page.initializeWidgets(*settings); } } diff --git a/apps/opencs/view/settings/windowpage.cpp b/apps/opencs/view/settings/windowpage.cpp index 42d72cf756..ae42623b78 100644 --- a/apps/opencs/view/settings/windowpage.cpp +++ b/apps/opencs/view/settings/windowpage.cpp @@ -19,7 +19,7 @@ #include "../../view/settings/abstractblock.hpp" CSVSettings::WindowPage::WindowPage(QWidget *parent): - AbstractPage("Window", parent) + AbstractPage("Window Size", parent) { // Hacks to get the stylesheet look properly #ifdef Q_OS_MAC @@ -82,7 +82,7 @@ CSVSettings::GroupBlockDef *CSVSettings::WindowPage::buildCustomWindowSize() CSVSettings::GroupBlockDef *CSVSettings::WindowPage::buildWindowSizeToggle() { - GroupBlockDef *block = new GroupBlockDef ("Window Size"); + GroupBlockDef *block = new GroupBlockDef (objectName()); // window size toggle block->captions << "Pre-Defined" << "Custom"; From 0ae254f6a8d9b03e4497ca3c731a6ecabd65cc73 Mon Sep 17 00:00:00 2001 From: graffy76 Date: Thu, 18 Jul 2013 06:31:47 -0500 Subject: [PATCH 2/2] Fixed segfault opening tables --- apps/opencs/view/world/table.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/opencs/view/world/table.cpp b/apps/opencs/view/world/table.cpp index f188adfaf1..315f4c742a 100644 --- a/apps/opencs/view/world/table.cpp +++ b/apps/opencs/view/world/table.cpp @@ -217,9 +217,12 @@ void CSVWorld::Table::updateEditorSetting (const QString &settingName, const QSt if (settingName == "Referenceable ID Type Display") { - RefIdTypeDelegate &refidDelegate = dynamic_cast (*itemDelegateForColumn(2)); + RefIdTypeDelegate *refidDelegate = dynamic_cast (itemDelegateForColumn(2)); - refidDelegate.updateEditorSetting (settingName, settingValue); - emit dataChanged(mModel->index(0,1), mModel->index(mModel->rowCount()-1, 1)); + if (refidDelegate) + { + refidDelegate->updateEditorSetting (settingName, settingValue); + emit dataChanged(mModel->index(0,1), mModel->index(mModel->rowCount()-1, 1)); + } } }