Merge remote-tracking branch 'graffy76/RefIdTypeDelegate'

This commit is contained in:
Marc Zinnschlag 2013-07-18 13:51:24 +02:00
commit 365ea11e86
10 changed files with 132 additions and 35 deletions

View file

@ -34,7 +34,7 @@ namespace CSMSettings
bool updateItem (const QString &value); bool updateItem (const QString &value);
bool updateItem (int valueListIndex); bool updateItem (int valueListIndex);
/// retroeve list of valid values for setting /// retrieve list of valid values for setting
inline QStringList *getValueList() { return mValueList; } inline QStringList *getValueList() { return mValueList; }
/// write list of valid values for setting /// write list of valid values for setting

View file

@ -14,7 +14,7 @@ namespace CSMSettings
typedef QList<SettingContainer *> SettingList; typedef QList<SettingContainer *> SettingList;
typedef QMap<QString, SettingContainer *> SettingMap; typedef QMap<QString, SettingContainer *> SettingMap;
typedef QMap<QString, SettingMap *> SectionMap; typedef QMap<QString, SettingMap *> SectionMap;
struct QStringPair struct QStringPair
{ {

View file

@ -42,6 +42,33 @@ CSMSettings::UserSettings::UserSettings()
mReadOnlyMessage = QObject::tr("<br><b>Could not open file for reading</b><br><br> \ mReadOnlyMessage = QObject::tr("<br><b>Could not open file for reading</b><br><br> \
Please make sure you have the right permissions and try again.<br>"); Please make sure you have the right permissions and try again.<br>");
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() CSMSettings::UserSettings::~UserSettings()
@ -104,17 +131,22 @@ bool CSMSettings::UserSettings::writeSettings(QMap<QString, CSMSettings::Setting
} }
const CSMSettings::SectionMap &CSMSettings::UserSettings::getSettings() const const CSMSettings::SectionMap &CSMSettings::UserSettings::getSectionMap() const
{ {
return mSectionSettings; return mSectionSettings;
} }
const CSMSettings::SettingMap *CSMSettings::UserSettings::getSettings(const QString &sectionName) const
{
return getValidSettings(sectionName);
}
bool CSMSettings::UserSettings::loadFromFile(const QString &filePath) bool CSMSettings::UserSettings::loadFromFile(const QString &filePath)
{ {
if (filePath.isEmpty()) if (filePath.isEmpty())
return false; return false;
mSectionSettings.clear(); SectionMap loadedSettings;
QTextStream *stream = openFileStream (filePath, true); QTextStream *stream = openFileStream (filePath, true);
@ -150,7 +182,7 @@ bool CSMSettings::UserSettings::loadFromFile(const QString &filePath)
{ {
//add the previous section's settings to the member map //add the previous section's settings to the member map
if (settings) if (settings)
mSectionSettings.insert(section, settings); loadedSettings.insert(section, settings);
//save new section and create a new list //save new section and create a new list
section = sectionRe.cap(1); section = sectionRe.cap(1);
@ -167,18 +199,48 @@ bool CSMSettings::UserSettings::loadFromFile(const QString &filePath)
} }
mSectionSettings.insert(section, settings); loadedSettings.insert(section, settings);
stream->device()->close(); stream->device()->close();
delete stream; delete stream;
stream = 0; stream = 0;
} }
mergeMap (loadedSettings);
return success; return success;
} }
void CSMSettings::UserSettings::mergeMap (const CSMSettings::SectionMap &sectionSettings)
{
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) void CSMSettings::UserSettings::loadSettings (const QString &fileName)
{ {
mSectionSettings.clear();
//global //global
QString globalFilePath = QString::fromStdString(mCfgMgr.getGlobalPath().string()) + fileName; QString globalFilePath = QString::fromStdString(mCfgMgr.getGlobalPath().string()) + fileName;
bool globalOk = loadFromFile(globalFilePath); bool globalOk = loadFromFile(globalFilePath);
@ -207,10 +269,11 @@ void CSMSettings::UserSettings::loadSettings (const QString &fileName)
void CSMSettings::UserSettings::updateSettings (const QString &sectionName, const QString &settingName) void CSMSettings::UserSettings::updateSettings (const QString &sectionName, 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()) if (settingName.isEmpty())
{ {
@ -229,15 +292,12 @@ void CSMSettings::UserSettings::updateSettings (const QString &sectionName, cons
QString CSMSettings::UserSettings::getSetting (const QString &section, const QString &setting) const QString CSMSettings::UserSettings::getSetting (const QString &section, const QString &setting) const
{ {
SettingMap *settings = getValidSettings(section);
QString retVal = ""; QString retVal = "";
if (mSectionSettings.find(section) != mSectionSettings.end()) if (settings->find(setting) != settings->end())
{ retVal = settings->value(setting)->getValue();
CSMSettings::SettingMap *settings = mSectionSettings.value(section);
if (settings->find(setting) != settings->end())
retVal = settings->value(setting)->getValue();
}
return retVal; return retVal;
} }
@ -263,3 +323,33 @@ void CSMSettings::UserSettings::displayFileErrorMessage(const QString &message,
msgBox.exec(); msgBox.exec();
} }
CSMSettings::SettingMap *
CSMSettings::UserSettings::getValidSettings (const QString &sectionName) 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;
}

View file

@ -27,6 +27,8 @@ namespace CSMSettings {
Q_OBJECT Q_OBJECT
SectionMap mSectionSettings; SectionMap mSectionSettings;
SectionMap mEditorSettingDefaults;
static UserSettings *mUserSettingsInstance; static UserSettings *mUserSettingsInstance;
QString mUserFilePath; QString mUserFilePath;
Files::ConfigurationManager mCfgMgr; Files::ConfigurationManager mCfgMgr;
@ -58,7 +60,9 @@ namespace CSMSettings {
void loadSettings (const QString &fileName); void loadSettings (const QString &fileName);
/// Returns the entire map of settings across all sections /// Returns the entire map of settings across all sections
const SectionMap &getSettings () const; const SectionMap &getSectionMap () const;
const SettingMap *getSettings (const QString &sectionName) const;
/// Retrieves the value as a QString of the specified setting in the specified section /// Retrieves the value as a QString of the specified setting in the specified section
QString getSetting(const QString &section, const QString &setting) const; QString getSetting(const QString &section, const QString &setting) const;
@ -72,8 +76,15 @@ namespace CSMSettings {
/// Parses a setting file specified in filePath from the provided text stream. /// Parses a setting file specified in filePath from the provided text stream.
bool loadFromFile (const QString &filePath = ""); bool loadFromFile (const QString &filePath = "");
/// merge the passed map into mSectionSettings
void mergeMap (const SectionMap &);
void displayFileErrorMessage(const QString &message, bool isReadOnly); void displayFileErrorMessage(const QString &message, bool isReadOnly);
void buildEditorSettingDefaults();
SettingMap *getValidSettings (const QString &sectionName) const;
signals: signals:
void signalUpdateEditorSetting (const QString &settingName, const QString &settingValue); void signalUpdateEditorSetting (const QString &settingName, const QString &settingValue);

View file

@ -189,10 +189,7 @@ CSVDoc::View::View (ViewManager& viewManager, CSMDoc::Document *document, int to
QString width = CSMSettings::UserSettings::instance().getSetting(QString("Window Size"), QString("Width")); QString width = CSMSettings::UserSettings::instance().getSetting(QString("Window Size"), QString("Width"));
QString height = CSMSettings::UserSettings::instance().getSetting(QString("Window Size"), QString("Height")); QString height = CSMSettings::UserSettings::instance().getSetting(QString("Window Size"), QString("Height"));
if(width==QString() || height==QString()) resize (width.toInt(), height.toInt());
resize(800, 600);
else
resize (width.toInt(), height.toInt());
mSubViewWindow.setDockOptions (QMainWindow::AllowNestedDocks); mSubViewWindow.setDockOptions (QMainWindow::AllowNestedDocks);

View file

@ -12,7 +12,7 @@ CSVSettings::GroupBlockDef *CSVSettings::DataDisplayFormatPage::setupDataDisplay
{ {
GroupBlockDef *statusBlock = new GroupBlockDef(QString(title)); 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"); *(statusItem->valueList) << QString("Icon and Text") << QString("Icon Only") << QString("Text Only");
WidgetDef statusWidget (Widget_RadioButton); WidgetDef statusWidget (Widget_RadioButton);

View file

@ -15,7 +15,7 @@ namespace CSVSettings
ItemBlock (QWidget* parent = 0); ItemBlock (QWidget* parent = 0);
/// pure virtual function not implemneted /// pure virtual function not implemented
bool updateSettings (const CSMSettings::SettingMap &settings) { return false; } bool updateSettings (const CSMSettings::SettingMap &settings) { return false; }
CSMSettings::SettingList *getSettings (); CSMSettings::SettingList *getSettings ();

View file

@ -44,7 +44,6 @@ void CSVSettings::UserSettingsDialog::closeEvent (QCloseEvent *event)
void CSVSettings::UserSettingsDialog::setWidgetStates () void CSVSettings::UserSettingsDialog::setWidgetStates ()
{ {
CSMSettings::UserSettings::instance().loadSettings("opencs.cfg"); CSMSettings::UserSettings::instance().loadSettings("opencs.cfg");
const CSMSettings::SectionMap &sectionSettings = CSMSettings::UserSettings::instance().getSettings();
//iterate the tabWidget's pages (sections) //iterate the tabWidget's pages (sections)
for (int i = 0; i < mStackedWidget->count(); i++) for (int i = 0; i < mStackedWidget->count(); i++)
@ -53,12 +52,9 @@ void CSVSettings::UserSettingsDialog::setWidgetStates ()
//and update widget //and update widget
QString pageName = mStackedWidget->widget(i)->objectName(); QString pageName = mStackedWidget->widget(i)->objectName();
if (sectionSettings.find(pageName) != sectionSettings.end()) const CSMSettings::SettingMap *settings = CSMSettings::UserSettings::instance().getSettings(pageName);
{ AbstractPage &page = getAbstractPage (i);
CSMSettings::SettingMap *settings = sectionSettings.value(pageName); page.initializeWidgets(*settings);
AbstractPage &page = getAbstractPage (i);
page.initializeWidgets(*settings);
}
} }
} }

View file

@ -19,7 +19,7 @@
#include "../../view/settings/abstractblock.hpp" #include "../../view/settings/abstractblock.hpp"
CSVSettings::WindowPage::WindowPage(QWidget *parent): CSVSettings::WindowPage::WindowPage(QWidget *parent):
AbstractPage("Window", parent) AbstractPage("Window Size", parent)
{ {
// Hacks to get the stylesheet look properly // Hacks to get the stylesheet look properly
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
@ -82,7 +82,7 @@ CSVSettings::GroupBlockDef *CSVSettings::WindowPage::buildCustomWindowSize()
CSVSettings::GroupBlockDef *CSVSettings::WindowPage::buildWindowSizeToggle() CSVSettings::GroupBlockDef *CSVSettings::WindowPage::buildWindowSizeToggle()
{ {
GroupBlockDef *block = new GroupBlockDef ("Window Size"); GroupBlockDef *block = new GroupBlockDef (objectName());
// window size toggle // window size toggle
block->captions << "Pre-Defined" << "Custom"; block->captions << "Pre-Defined" << "Custom";

View file

@ -217,9 +217,12 @@ void CSVWorld::Table::updateEditorSetting (const QString &settingName, const QSt
if (settingName == "Referenceable ID Type Display") if (settingName == "Referenceable ID Type Display")
{ {
RefIdTypeDelegate &refidDelegate = dynamic_cast <CSVWorld::RefIdTypeDelegate &> (*itemDelegateForColumn(2)); RefIdTypeDelegate *refidDelegate = dynamic_cast <CSVWorld::RefIdTypeDelegate *> (itemDelegateForColumn(2));
refidDelegate.updateEditorSetting (settingName, settingValue); if (refidDelegate)
emit dataChanged(mModel->index(0,1), mModel->index(mModel->rowCount()-1, 1)); {
refidDelegate->updateEditorSetting (settingName, settingValue);
emit dataChanged(mModel->index(0,1), mModel->index(mModel->rowCount()-1, 1));
}
} }
} }